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