cabinet: Clean up the documentation for Extract.
[wine/wine64.git] / tools / wmc / utils.c
blob14739bb0b8f16c614a6b896124cee78740b2615d
1 /*
2 * Utility routines
4 * Copyright 1998,2000 Bertho A. Stultiens
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.
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.
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
21 #include "config.h"
22 #include "wine/port.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>
31 #include "wmctypes.h"
32 #include "utils.h"
33 #include "wmc.h"
35 #define SUPPRESS_YACC_ERROR_MESSAGE
37 static void generic_msg(const char *s, const char *t, va_list ap)
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");
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.
52 int yyerror(const char *s, ...)
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;
63 int xyyerror(const char *s, ...)
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;
73 int yywarning(const char *s, ...)
75 va_list ap;
76 va_start(ap, s);
77 generic_msg(s, "Warning", ap);
78 va_end(ap);
79 return 0;
82 void internal_error(const char *file, int line, const char *s, ...)
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);
93 void error(const char *s, ...)
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);
104 void warning(const char *s, ...)
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);
114 char *dup_basename(const char *name, const char *ext)
116 int namelen;
117 int extlen = strlen(ext);
118 char *base;
119 char *slash;
121 if(!name)
122 name = "wmc.tab";
124 slash = strrchr(name, '/');
125 if (slash)
126 name = slash + 1;
128 namelen = strlen(name);
130 /* +4 for later extension and +1 for '\0' */
131 base = (char *)xmalloc(namelen +4 +1);
132 strcpy(base, name);
133 if(!strcasecmp(name + namelen-extlen, ext))
135 base[namelen - extlen] = '\0';
137 return base;
140 void *xmalloc(size_t size)
142 void *res;
144 assert(size > 0);
145 assert(size < 102400);
146 res = malloc(size);
147 if(res == NULL)
149 error("Virtual memory exhausted.\n");
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.
156 memset(res, 0, size);
157 return res;
161 void *xrealloc(void *p, size_t size)
163 void *res;
165 assert(size > 0);
166 assert(size < 102400);
167 res = realloc(p, size);
168 if(res == NULL)
170 error("Virtual memory exhausted.\n");
172 return res;
175 char *xstrdup(const char *str)
177 char *s;
179 assert(str != NULL);
180 s = (char *)xmalloc(strlen(str)+1);
181 return strcpy(s, str);
184 int unistrlen(const WCHAR *s)
186 int n;
187 for(n = 0; *s; n++, s++)
189 return n;
192 WCHAR *unistrcpy(WCHAR *dst, const WCHAR *src)
194 WCHAR *t = dst;
195 while(*src)
196 *t++ = *src++;
197 *t = 0;
198 return dst;
201 WCHAR *xunistrdup(const WCHAR * str)
203 WCHAR *s;
205 assert(str != NULL);
206 s = (WCHAR *)xmalloc((unistrlen(str)+1) * sizeof(WCHAR));
207 return unistrcpy(s, str);
210 int unistricmp(const WCHAR *s1, const WCHAR *s2)
212 int i;
213 int once = 0;
214 static const char warn[] = "Don't know the uppercase equivalent of non acsii characters;"
215 "comparison might yield wrong results";
216 while(*s1 && *s2)
218 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
220 if(!once)
222 once++;
223 yywarning(warn);
225 i = *s1++ - *s2++;
227 else
228 i = toupper(*s1++) - toupper(*s2++);
229 if(i)
230 return i;
233 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
235 if(!once)
236 yywarning(warn);
237 return *s1 - *s2;
239 else
240 return toupper(*s1) - toupper(*s2);
243 int unistrcmp(const WCHAR *s1, const WCHAR *s2)
245 int i;
246 while(*s1 && *s2)
248 i = *s1++ - *s2++;
249 if(i)
250 return i;
253 return *s1 - *s2;