Revert "winex11.drv: Optimise getting the bits of a DIB after calling SetDIBits."
[wine] / programs / winetest / util.c
1 /*
2  * Utility functions.
3  *
4  * Copyright 2003 Dimitrie O. Paun
5  * Copyright 2003 Ferenc Wagner
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25
26 #include "winetest.h"
27
28 HANDLE logfile = 0;
29
30 void *xmalloc (size_t len)
31 {
32     void *p = malloc (len);
33
34     if (!p) report (R_FATAL, "Out of memory.");
35     return p;
36 }
37
38 void *xrealloc (void *op, size_t len)
39 {
40     void *p = realloc (op, len);
41
42     if (len && !p) report (R_FATAL, "Out of memory.");
43     return p;
44 }
45
46 char *xstrdup( const char *str )
47 {
48     char *res = strdup( str );
49     if (!res) report (R_FATAL, "Out of memory.");
50     return res;
51 }
52
53 static char *vstrfmtmake (size_t *lenp, const char *fmt, va_list ap)
54 {
55     size_t size = 1000;
56     char *p, *q;
57     int n;
58
59     p = malloc (size);
60     if (!p) return NULL;
61     while (1) {
62         n = vsnprintf (p, size, fmt, ap);
63         if (n < 0) size *= 2;   /* Windows */
64         else if ((unsigned)n >= size) size = n+1; /* glibc */
65         else break;
66         q = realloc (p, size);
67         if (!q) {
68           free (p);
69           return NULL;
70        }
71        p = q;
72     }
73     if (lenp) *lenp = n;
74     return p;
75 }
76
77 char *vstrmake (size_t *lenp, va_list ap)
78 {
79     const char *fmt;
80
81     fmt = va_arg (ap, const char*);
82     return vstrfmtmake (lenp, fmt, ap);
83 }
84
85 char *strmake (size_t *lenp, ...)
86 {
87     va_list ap;
88     char *p;
89
90     va_start (ap, lenp);
91     p = vstrmake (lenp, ap);
92     if (!p) report (R_FATAL, "Out of memory.");
93     va_end (ap);
94     return p;
95 }
96
97 void xprintf (const char *fmt, ...)
98 {
99     va_list ap;
100     size_t size;
101     DWORD written;
102     char *buffer, *head;
103
104     va_start (ap, fmt);
105     buffer = vstrfmtmake (&size, fmt, ap);
106     head = buffer;
107     va_end (ap);
108     while (size) {
109         if (!WriteFile( logfile, head, size, &written, NULL ))
110             report (R_FATAL, "Can't write logs: %u", GetLastError());
111         head += written;
112         size -= written;
113     }
114     free (buffer);
115 }
116
117 int
118 goodtagchar (char c)
119 {
120     return (('a'<=c && c<='z') ||
121             ('A'<=c && c<='Z') ||
122             ('0'<=c && c<='9') ||
123             c=='-' || c=='.');
124 }
125
126 const char *
127 findbadtagchar (const char *tag)
128 {
129     while (*tag)
130         if (goodtagchar (*tag)) tag++;
131         else return tag;
132     return NULL;
133 }