- constify some strings
[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 <unistd.h>
22 #include <errno.h>
23
24 #include "winetest.h"
25
26 void *xmalloc (size_t len)
27 {
28     void *p = malloc (len);
29
30     if (!p) report (R_FATAL, "Out of memory.");
31     return p;
32 }
33
34 void *xrealloc (void *op, size_t len)
35 {
36     void *p = realloc (op, len);
37
38     if (!p) report (R_FATAL, "Out of memory.");
39     return p;
40 }
41
42 char *vstrfmtmake (size_t *lenp, const char *fmt, va_list ap)
43 {
44     size_t size = 1000;
45     char *p, *q;
46     int n;
47
48     p = malloc (size);
49     if (!p) return NULL;
50     while (1) {
51         n = vsnprintf (p, size, fmt, ap);
52         if (n < 0) size *= 2;   /* Windows */
53         else if ((unsigned)n >= size) size = n+1; /* glibc */
54         else break;
55         q = realloc (p, size);
56         if (!q) {
57           free (p);
58           return NULL;
59        }
60        p = q;
61     }
62     if (lenp) *lenp = n;
63     return p;
64 }
65
66 char *vstrmake (size_t *lenp, va_list ap)
67 {
68     const char *fmt;
69
70     fmt = va_arg (ap, const char*);
71     return vstrfmtmake (lenp, fmt, ap);
72 }
73
74 char *strmake (size_t *lenp, ...)
75 {
76     va_list ap;
77     char *p;
78
79     va_start (ap, lenp);
80     p = vstrmake (lenp, ap);
81     if (!p) report (R_FATAL, "Out of memory.");
82     va_end (ap);
83     return p;
84 }
85
86 void xprintf (const char *fmt, ...)
87 {
88     va_list ap;
89     size_t size;
90     ssize_t written;
91     char *buffer, *head;
92
93     va_start (ap, fmt);
94     buffer = vstrfmtmake (&size, fmt, ap);
95     head = buffer;
96     va_end (ap);
97     while ((written = write (1, head, size)) != size) {
98         if (written == -1)
99             report (R_FATAL, "Can't write logs: %d", errno);
100         head += written;
101         size -= written;
102     }
103     free (buffer);
104 }
105
106 const char *
107 badtagchar (const char *tag)
108 {
109     while (*tag)
110         if (('a'<=*tag && *tag<='z') ||
111             ('A'<=*tag && *tag<='Z') ||
112             ('0'<=*tag && *tag<='9') ||
113             *tag=='-' || *tag=='.')
114             tag++;
115         else return tag;
116     return NULL;
117 }