Release 1.2-rc6.
[wine/gsoc-2012-control.git] / tools / wmc / utils.c
blob2b29db9bb1f2848789584e393a8005b4f0bb302b
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
40 vfprintf(stderr, s, ap);
44 * The yyerror routine should not exit because we use the error-token
45 * to determine the syntactic error in the source. However, YACC
46 * uses the same routine to print an error just before the error
47 * token is reduced.
48 * The extra routine 'xyyerror' is used to exit after giving a real
49 * message.
51 int mcy_error(const char *s, ...)
53 #ifndef SUPPRESS_YACC_ERROR_MESSAGE
54 va_list ap;
55 va_start(ap, s);
56 generic_msg(s, "Yacc error", ap);
57 va_end(ap);
58 #endif
59 return 1;
62 int xyyerror(const char *s, ...)
64 va_list ap;
65 va_start(ap, s);
66 generic_msg(s, "Error", ap);
67 va_end(ap);
68 exit(1);
69 return 1;
72 int mcy_warning(const char *s, ...)
74 va_list ap;
75 va_start(ap, s);
76 generic_msg(s, "Warning", ap);
77 va_end(ap);
78 return 0;
81 void internal_error(const char *file, int line, const char *s, ...)
83 va_list ap;
84 va_start(ap, s);
85 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
86 vfprintf(stderr, s, ap);
87 va_end(ap);
88 exit(3);
91 void error(const char *s, ...)
93 va_list ap;
94 va_start(ap, s);
95 fprintf(stderr, "Error: ");
96 vfprintf(stderr, s, ap);
97 va_end(ap);
98 exit(2);
101 void warning(const char *s, ...)
103 va_list ap;
104 va_start(ap, s);
105 fprintf(stderr, "Warning: ");
106 vfprintf(stderr, s, ap);
107 va_end(ap);
110 char *dup_basename(const char *name, const char *ext)
112 int namelen;
113 int extlen = strlen(ext);
114 char *base;
115 char *slash;
117 if(!name)
118 name = "wmc.tab";
120 slash = strrchr(name, '/');
121 if (slash)
122 name = slash + 1;
124 namelen = strlen(name);
126 /* +4 for later extension and +1 for '\0' */
127 base = xmalloc(namelen +4 +1);
128 strcpy(base, name);
129 if(!strcasecmp(name + namelen-extlen, ext))
131 base[namelen - extlen] = '\0';
133 return base;
136 void *xmalloc(size_t size)
138 void *res;
140 assert(size > 0);
141 assert(size < 102400);
142 res = malloc(size);
143 if(res == NULL)
145 error("Virtual memory exhausted.\n");
147 memset(res, 0x55, size);
148 return res;
152 void *xrealloc(void *p, size_t size)
154 void *res;
156 assert(size > 0);
157 assert(size < 102400);
158 res = realloc(p, size);
159 if(res == NULL)
161 error("Virtual memory exhausted.\n");
163 return res;
166 char *xstrdup(const char *str)
168 char *s;
170 assert(str != NULL);
171 s = xmalloc(strlen(str)+1);
172 return strcpy(s, str);
175 int unistrlen(const WCHAR *s)
177 int n;
178 for(n = 0; *s; n++, s++)
180 return n;
183 WCHAR *unistrcpy(WCHAR *dst, const WCHAR *src)
185 WCHAR *t = dst;
186 while(*src)
187 *t++ = *src++;
188 *t = 0;
189 return dst;
192 WCHAR *xunistrdup(const WCHAR * str)
194 WCHAR *s;
196 assert(str != NULL);
197 s = xmalloc((unistrlen(str)+1) * sizeof(WCHAR));
198 return unistrcpy(s, str);
201 int unistricmp(const WCHAR *s1, const WCHAR *s2)
203 int i;
204 int once = 0;
205 static const char warn[] = "Don't know the uppercase equivalent of non acsii characters;"
206 "comparison might yield wrong results";
207 while(*s1 && *s2)
209 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
211 if(!once)
213 once++;
214 mcy_warning(warn);
216 i = *s1++ - *s2++;
218 else
219 i = toupper(*s1++) - toupper(*s2++);
220 if(i)
221 return i;
224 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
226 if(!once)
227 mcy_warning(warn);
228 return *s1 - *s2;
230 else
231 return toupper(*s1) - toupper(*s2);
234 int unistrcmp(const WCHAR *s1, const WCHAR *s2)
236 int i;
237 while(*s1 && *s2)
239 i = *s1++ - *s2++;
240 if(i)
241 return i;
244 return *s1 - *s2;