minor format modifications to reduce diffs with latest wine sources to simplify updating.
[reactos.git] / reactos / tools / wmc / utils.c
1 /*
2 * Utility routines
3 *
4 * Copyright 1998,2000 Bertho A. Stultiens
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "config.h"
22
23 #include <windows.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <ctype.h>
30
31 #include "wmctypes.h"
32 #include "utils.h"
33 #include "wmc.h"
34
35 #define SUPPRESS_YACC_ERROR_MESSAGE
36
37 static void generic_msg(const char *s, const char *t, va_list ap)
38 {
39 fprintf(stderr, "%s %s: %d, %d: ", t, input_name ? input_name : "stdin", line_number, char_number);
40 vfprintf(stderr, s, ap);
41 fprintf(stderr, "\n");
42 }
43
44 /*
45 * The yyerror routine should not exit because we use the error-token
46 * to determine the syntactic error in the source. However, YACC
47 * uses the same routine to print an error just before the error
48 * token is reduced.
49 * The extra routine 'xyyerror' is used to exit after giving a real
50 * message.
51 */
52 int yyerror(const char *s, ...)
53 {
54 #ifndef SUPPRESS_YACC_ERROR_MESSAGE
55 va_list ap;
56 va_start(ap, s);
57 generic_msg(s, "Yacc error", ap);
58 va_end(ap);
59 #endif
60 return 1;
61 }
62
63 int xyyerror(const char *s, ...)
64 {
65 va_list ap;
66 va_start(ap, s);
67 generic_msg(s, "Error", ap);
68 va_end(ap);
69 exit(1);
70 return 1;
71 }
72
73 int yywarning(const char *s, ...)
74 {
75 va_list ap;
76 va_start(ap, s);
77 generic_msg(s, "Warning", ap);
78 va_end(ap);
79 return 0;
80 }
81
82 void internal_error(const char *file, int line, const char *s, ...)
83 {
84 va_list ap;
85 va_start(ap, s);
86 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
87 vfprintf(stderr, s, ap);
88 fprintf(stderr, "\n");
89 va_end(ap);
90 exit(3);
91 }
92
93 void error(const char *s, ...)
94 {
95 va_list ap;
96 va_start(ap, s);
97 fprintf(stderr, "Error: ");
98 vfprintf(stderr, s, ap);
99 fprintf(stderr, "\n");
100 va_end(ap);
101 exit(2);
102 }
103
104 void warning(const char *s, ...)
105 {
106 va_list ap;
107 va_start(ap, s);
108 fprintf(stderr, "Warning: ");
109 vfprintf(stderr, s, ap);
110 fprintf(stderr, "\n");
111 va_end(ap);
112 }
113
114 char *dup_basename(const char *name, const char *ext)
115 {
116 int namelen;
117 int extlen = strlen(ext);
118 char *base;
119 char *slash;
120
121 if(!name)
122 name = "wmc.tab";
123
124 slash = strrchr(name, '/');
125 if (slash)
126 name = slash + 1;
127
128 namelen = strlen(name);
129
130 /* +4 for later extension and +1 for '\0' */
131 base = (char *)xmalloc(namelen +4 +1);
132 strcpy(base, name);
133 if(!stricmp(name + namelen-extlen, ext))
134 {
135 base[namelen - extlen] = '\0';
136 }
137 return base;
138 }
139
140 void *xmalloc(size_t size)
141 {
142 void *res;
143
144 assert(size > 0);
145 assert(size < 102400);
146 res = malloc(size);
147 if(res == NULL)
148 {
149 error("Virtual memory exhausted.\n");
150 }
151 /*
152 * We set it to 0.
153 * This is *paramount* because we depend on it
154 * just about everywhere in the rest of the code.
155 */
156 memset(res, 0, size);
157 return res;
158 }
159
160
161 void *xrealloc(void *p, size_t size)
162 {
163 void *res;
164
165 assert(size > 0);
166 assert(size < 102400);
167 res = realloc(p, size);
168 if(res == NULL)
169 {
170 error("Virtual memory exhausted.\n");
171 }
172 return res;
173 }
174
175 char *xstrdup(const char *str)
176 {
177 char *s;
178
179 assert(str != NULL);
180 s = (char *)xmalloc(strlen(str)+1);
181 return strcpy(s, str);
182 }
183
184 int unistrlen(const WCHAR *s)
185 {
186 int n;
187 for(n = 0; *s; n++, s++)
188 ;
189 return n;
190 }
191
192 WCHAR *unistrcpy(WCHAR *dst, const WCHAR *src)
193 {
194 WCHAR *t = dst;
195 while(*src)
196 *t++ = *src++;
197 *t = 0;
198 return dst;
199 }
200
201 WCHAR *xunistrdup(const WCHAR * str)
202 {
203 WCHAR *s;
204
205 assert(str != NULL);
206 s = (WCHAR *)xmalloc((unistrlen(str)+1) * sizeof(WCHAR));
207 return unistrcpy(s, str);
208 }
209
210 int unistricmp(const WCHAR *s1, const WCHAR *s2)
211 {
212 int i;
213 int once = 0;
214 static char warn[] = "Don't know the uppercase equivalent of non acsii characters;"
215 "comparison might yield wrong results";
216 while(*s1 && *s2)
217 {
218 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
219 {
220 if(!once)
221 {
222 once++;
223 yywarning(warn);
224 }
225 i = *s1++ - *s2++;
226 }
227 else
228 i = toupper(*s1++) - toupper(*s2++);
229 if(i)
230 return i;
231 }
232
233 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
234 {
235 if(!once)
236 yywarning(warn);
237 return *s1 - *s2;
238 }
239 else
240 return toupper(*s1) - toupper(*s2);
241 }
242
243 int unistrcmp(const WCHAR *s1, const WCHAR *s2)
244 {
245 int i;
246 while(*s1 && *s2)
247 {
248 i = *s1++ - *s2++;
249 if(i)
250 return i;
251 }
252
253 return *s1 - *s2;
254 }
255