Bugfix: Declare CALL32_CBClient[Ex] without WINAPI.
[wine/wine64.git] / tools / wrc / utils.c
blob3ff6656b2fba967f285ace718c8af5e2ba9e7d8e
1 /*
2 * Utility routines
4 * Copyright 1998 Bertho A. Stultiens
6 */
8 #include "config.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <string.h>
14 #include <assert.h>
15 #include <ctype.h>
17 #include "wrc.h"
18 #include "utils.h"
19 #include "parser.h"
21 #define WANT_NEAR_INDICATION
24 #ifdef WANT_NEAR_INDICATION
25 void make_print(char *str)
27 while(*str)
29 if(!isprint(*str))
30 *str = ' ';
31 str++;
34 #endif
36 int yyerror(const char *s, ...)
38 va_list ap;
39 va_start(ap, s);
40 fprintf(stderr, "Error %s: %d, %d: ", input_name ? input_name : "stdin", line_number, char_number);
41 vfprintf(stderr, s, ap);
42 #ifdef WANT_NEAR_INDICATION
44 char *cpy = xstrdup(yytext);
45 make_print(cpy);
46 fprintf(stderr, " near '%s'\n", cpy);
47 free(cpy);
49 #else
50 fprintf(stderr, "\n");
51 #endif
52 va_end(ap);
53 exit(1);
54 return 1;
57 int yywarning(const char *s, ...)
59 va_list ap;
60 va_start(ap, s);
61 fprintf(stderr, "Warning %s: %d, %d: ", input_name ? input_name : "stdin", line_number, char_number);
62 vfprintf(stderr, s, ap);
63 #ifdef WANT_NEAR_INDICATION
65 char *cpy = xstrdup(yytext);
66 make_print(cpy);
67 fprintf(stderr, " near '%s'\n", cpy);
68 free(cpy);
70 #else
71 fprintf(stderr, "\n");
72 #endif
73 va_end(ap);
74 return 0;
77 void internal_error(const char *file, int line, const char *s, ...)
79 va_list ap;
80 va_start(ap, s);
81 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
82 vfprintf(stderr, s, ap);
83 fprintf(stderr, "\n");
84 va_end(ap);
85 exit(3);
88 void error(const char *s, ...)
90 va_list ap;
91 va_start(ap, s);
92 fprintf(stderr, "Error: ");
93 vfprintf(stderr, s, ap);
94 fprintf(stderr, "\n");
95 va_end(ap);
96 exit(2);
99 void warning(const char *s, ...)
101 va_list ap;
102 va_start(ap, s);
103 fprintf(stderr, "Warning: ");
104 vfprintf(stderr, s, ap);
105 fprintf(stderr, "\n");
106 va_end(ap);
109 void chat(const char *s, ...)
111 if(debuglevel & DEBUGLEVEL_CHAT)
113 va_list ap;
114 va_start(ap, s);
115 fprintf(stderr, "FYI: ");
116 vfprintf(stderr, s, ap);
117 fprintf(stderr, "\n");
118 va_end(ap);
122 char *dup_basename(const char *name, const char *ext)
124 int namelen;
125 int extlen = strlen(ext);
126 char *base;
127 char *slash;
129 if(!name)
130 name = "wrc.tab";
132 slash = strrchr(name, '/');
133 if (slash)
134 name = slash + 1;
136 namelen = strlen(name);
138 /* +4 for later extension and +1 for '\0' */
139 base = (char *)xmalloc(namelen +4 +1);
140 strcpy(base, name);
141 if(!strcasecmp(name + namelen-extlen, ext))
143 base[namelen - extlen] = '\0';
145 return base;
148 void *xmalloc(size_t size)
150 void *res;
152 assert(size > 0);
153 assert(size < 102400);
154 res = malloc(size);
155 if(res == NULL)
157 error("Virtual memory exhausted.\n");
159 memset(res, 0, size);
160 return res;
164 void *xrealloc(void *p, size_t size)
166 void *res;
168 assert(size > 0);
169 assert(size < 102400);
170 res = realloc(p, size);
171 if(res == NULL)
173 error("Virtual memory exhausted.\n");
175 return res;
178 char *xstrdup(const char *str)
180 char *s = (char *)xmalloc(strlen(str)+1);
181 return strcpy(s, str);
184 int string_compare(const string_t *s1, const string_t *s2)
186 if(s1->type == str_char && s2->type == str_char)
188 return strcasecmp(s1->str.cstr, s2->str.cstr);
190 else
192 internal_error(__FILE__, __LINE__, "Cannot yet compare unicode strings");
194 return 0;
197 int wstrlen(const short *s)
199 int cnt = 0;
200 while(*s++)
201 cnt++;
202 return cnt;
205 short *wstrcpy(short *dst, const short *src)
207 short *d = dst;
208 while(*src)
209 *d++ = *src++;
210 return dst;
213 int wstricmp(const short *s1, const short *s2)
215 char *cs1 = dupwstr2cstr(s1);
216 char *cs2 = dupwstr2cstr(s2);
217 int retval = strcasecmp(cs1, cs2);
218 free(cs1);
219 free(cs2);
220 warning("Comparing unicode strings without case -> converting to ascii");
221 return retval;;
224 short *dupcstr2wstr(const char *str)
226 int len = strlen(str) + 1;
227 short *ws = (short *)xmalloc(len*2);
228 short *wptr;
230 wptr = ws;
231 /* FIXME: codepage translation */
232 while(*str)
233 *wptr++ = (short)(*str++ & 0xff);
234 *wptr = 0;
235 return ws;
238 char *dupwstr2cstr(const short *str)
240 int len = wstrlen(str) + 1;
241 char *cs = (char *)xmalloc(len);
242 char *cptr;
244 cptr = cs;
245 /* FIXME: codepage translation */
246 while(*str)
247 *cptr++ = (char)*str++;
248 *cptr = 0;
249 return cs;