- command line handling (GUI will follow)
[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  *
20  */
21 #include <windows.h>
22
23 #include "winetest.h"
24
25 void *xmalloc (size_t len)
26 {
27     void *p = malloc (len);
28
29     if (!p) report (R_FATAL, "Out of memory.");
30     return p;
31 }
32
33 void *xrealloc (void *op, size_t len)
34 {
35     void *p = realloc (op, len);
36
37     if (!p) report (R_FATAL, "Out of memory.");
38     return p;
39 }
40
41 void xprintf (const char *fmt, ...)
42 {
43     va_list ap;
44
45     va_start (ap, fmt);
46     if (vprintf (fmt, ap) < 0) report (R_FATAL, "Can't write logs.");
47     va_end (ap);
48 }
49
50 char *vstrmake (size_t *lenp, va_list ap)
51 {
52     char *fmt;
53     size_t size = 1000;
54     char *p, *q;
55     int n;
56
57     p = malloc (size);
58     if (!p) return NULL;
59     fmt = va_arg (ap, char*);
60     while (1) {
61         n = vsnprintf (p, size, fmt, ap);
62         if (n < 0) size *= 2;   /* Windows */
63         else if ((unsigned)n >= size) size = n+1; /* glibc */
64         else break;
65         q = realloc (p, size);
66         if (!q) {
67           free (p);
68           return NULL;
69        }
70        p = q;
71     }
72     if (lenp) *lenp = n;
73     return p;
74 }
75
76 char *strmake (size_t *lenp, ...)
77 {
78     va_list ap;
79     char *p;
80
81     va_start (ap, lenp);
82     p = vstrmake (lenp, ap);
83     if (!p) report (R_FATAL, "Out of memory.");
84     va_end (ap);
85     return p;
86 }
87
88 char *
89 badtagchar (char *tag)
90 {
91     while (*tag)
92         if (('a'<=*tag && *tag<='z') ||
93             ('A'<=*tag && *tag<='Z') ||
94             ('0'<=*tag && *tag<='9') ||
95             *tag=='-' || *tag=='.')
96             tag++;
97         else return tag;
98     return NULL;
99 }