winecfg: Added missing break (Coverity).
[wine.git] / tools / wmc / utils.c
blob6f26da3d0b6844366acd8e70bf53bc18f627eaff
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 res = malloc(size);
142 if(res == NULL)
144 error("Virtual memory exhausted.\n");
146 memset(res, 0x55, size);
147 return res;
151 void *xrealloc(void *p, size_t size)
153 void *res;
155 assert(size > 0);
156 res = realloc(p, size);
157 if(res == NULL)
159 error("Virtual memory exhausted.\n");
161 return res;
164 char *xstrdup(const char *str)
166 char *s;
168 assert(str != NULL);
169 s = xmalloc(strlen(str)+1);
170 return strcpy(s, str);
173 char *strmake( const char* fmt, ... )
175 int n;
176 size_t size = 100;
177 va_list ap;
179 for (;;)
181 char *p = xmalloc( size );
182 va_start( ap, fmt );
183 n = vsnprintf( p, size, fmt, ap );
184 va_end( ap );
185 if (n == -1) size *= 2;
186 else if ((size_t)n >= size) size = n + 1;
187 else return p;
188 free( p );
192 int unistrlen(const WCHAR *s)
194 int n;
195 for(n = 0; *s; n++, s++)
197 return n;
200 WCHAR *unistrcpy(WCHAR *dst, const WCHAR *src)
202 WCHAR *t = dst;
203 while(*src)
204 *t++ = *src++;
205 *t = 0;
206 return dst;
209 WCHAR *xunistrdup(const WCHAR * str)
211 WCHAR *s;
213 assert(str != NULL);
214 s = xmalloc((unistrlen(str)+1) * sizeof(WCHAR));
215 return unistrcpy(s, str);
218 int unistricmp(const WCHAR *s1, const WCHAR *s2)
220 int i;
221 int once = 0;
222 static const char warn[] = "Don't know the uppercase equivalent of non acsii characters;"
223 "comparison might yield wrong results";
224 while(*s1 && *s2)
226 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
228 if(!once)
230 once++;
231 mcy_warning(warn);
233 i = *s1++ - *s2++;
235 else
236 i = toupper(*s1++) - toupper(*s2++);
237 if(i)
238 return i;
241 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
243 if(!once)
244 mcy_warning(warn);
245 return *s1 - *s2;
247 else
248 return toupper(*s1) - toupper(*s2);
251 int unistrcmp(const WCHAR *s1, const WCHAR *s2)
253 int i;
254 while(*s1 && *s2)
256 i = *s1++ - *s2++;
257 if(i)
258 return i;
261 return *s1 - *s2;
264 /*******************************************************************
265 * buffer management
267 * Function for writing to a memory buffer.
270 int byte_swapped = 0;
271 unsigned char *output_buffer;
272 size_t output_buffer_pos;
273 size_t output_buffer_size;
275 static void check_output_buffer_space( size_t size )
277 if (output_buffer_pos + size >= output_buffer_size)
279 output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
280 output_buffer = xrealloc( output_buffer, output_buffer_size );
284 void init_output_buffer(void)
286 output_buffer_size = 1024;
287 output_buffer_pos = 0;
288 output_buffer = xmalloc( output_buffer_size );
291 void flush_output_buffer( const char *name )
293 int fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
294 if (fd == -1) error( "Error creating %s\n", name );
295 if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
296 error( "Error writing to %s\n", name );
297 close( fd );
298 free( output_buffer );
301 void put_data( const void *data, size_t size )
303 check_output_buffer_space( size );
304 memcpy( output_buffer + output_buffer_pos, data, size );
305 output_buffer_pos += size;
308 void put_byte( unsigned char val )
310 check_output_buffer_space( 1 );
311 output_buffer[output_buffer_pos++] = val;
314 void put_word( unsigned short val )
316 if (byte_swapped) val = (val << 8) | (val >> 8);
317 put_data( &val, sizeof(val) );
320 void put_dword( unsigned int val )
322 if (byte_swapped)
323 val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
324 put_data( &val, sizeof(val) );
327 void align_output( unsigned int align )
329 size_t size = align - (output_buffer_pos % align);
331 if (size == align) return;
332 check_output_buffer_space( size );
333 memset( output_buffer + output_buffer_pos, 0, size );
334 output_buffer_pos += size;