wined3d: Introduce wined3d_device_get_immediate_context().
[wine.git] / tools / widl / utils.c
blob634bd12a0bada78242578becbcb6019a7a720cc6
1 /*
2 * Utility routines
4 * Copyright 1998 Bertho A. Stultiens
5 * Copyright 2002 Ove Kaaven
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <ctype.h>
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
36 #define CURRENT_LOCATION { input_name ? input_name : "stdin", line_number, parser_text }
38 static const int want_near_indication = 0;
40 static void make_print(char *str)
42 while(*str)
44 if(!isprint(*str))
45 *str = ' ';
46 str++;
50 static void generic_msg(const loc_info_t *loc_info, const char *s, const char *t, va_list ap)
52 fprintf(stderr, "%s:%d: %s: ", loc_info->input_name, loc_info->line_number, t);
53 vfprintf(stderr, s, ap);
55 if (want_near_indication)
57 char *cpy;
58 if(loc_info->near_text)
60 cpy = xstrdup(loc_info->near_text);
61 make_print(cpy);
62 fprintf(stderr, " near '%s'", cpy);
63 free(cpy);
69 void error_loc(const char *s, ...)
71 loc_info_t cur_loc = CURRENT_LOCATION;
72 va_list ap;
73 va_start(ap, s);
74 generic_msg(&cur_loc, s, "error", ap);
75 va_end(ap);
76 exit(1);
79 /* yyerror: yacc assumes this is not newline terminated. */
80 void parser_error(const char *s)
82 error_loc("%s\n", s);
85 void error_loc_info(const loc_info_t *loc_info, const char *s, ...)
87 va_list ap;
88 va_start(ap, s);
89 generic_msg(loc_info, s, "error", ap);
90 va_end(ap);
91 exit(1);
94 int parser_warning(const char *s, ...)
96 loc_info_t cur_loc = CURRENT_LOCATION;
97 va_list ap;
98 va_start(ap, s);
99 generic_msg(&cur_loc, s, "warning", ap);
100 va_end(ap);
101 return 0;
104 void error(const char *s, ...)
106 va_list ap;
107 va_start(ap, s);
108 fprintf(stderr, "error: ");
109 vfprintf(stderr, s, ap);
110 va_end(ap);
111 exit(2);
114 void warning(const char *s, ...)
116 va_list ap;
117 va_start(ap, s);
118 fprintf(stderr, "warning: ");
119 vfprintf(stderr, s, ap);
120 va_end(ap);
123 void warning_loc_info(const loc_info_t *loc_info, const char *s, ...)
125 va_list ap;
126 va_start(ap, s);
127 generic_msg(loc_info, s, "warning", ap);
128 va_end(ap);
131 void chat(const char *s, ...)
133 if(debuglevel & DEBUGLEVEL_CHAT)
135 va_list ap;
136 va_start(ap, s);
137 fprintf(stderr, "chat: ");
138 vfprintf(stderr, s, ap);
139 va_end(ap);
143 char *dup_basename(const char *name, const char *ext)
145 int namelen;
146 int extlen = strlen(ext);
147 char *base;
148 char *slash;
150 if(!name)
151 name = "widl.tab";
153 slash = strrchr(name, '/');
154 if (!slash)
155 slash = strrchr(name, '\\');
157 if (slash)
158 name = slash + 1;
160 namelen = strlen(name);
162 /* +6 for later extension (strlen("_r.rgs")) and +1 for '\0' */
163 base = xmalloc(namelen +6 +1);
164 strcpy(base, name);
165 if(!strcasecmp(name + namelen-extlen, ext))
167 base[namelen - extlen] = '\0';
169 return base;
172 size_t widl_getline(char **linep, size_t *lenp, FILE *fp)
174 char *line = *linep;
175 size_t len = *lenp;
176 size_t n = 0;
178 if (!line)
180 len = 64;
181 line = xmalloc(len);
184 while (fgets(&line[n], len - n, fp))
186 n += strlen(&line[n]);
187 if (line[n - 1] == '\n')
188 break;
189 else if (n == len - 1)
191 len *= 2;
192 line = xrealloc(line, len);
196 *linep = line;
197 *lenp = len;
198 return n;
201 void *xmalloc(size_t size)
203 void *res;
205 assert(size > 0);
206 res = malloc(size);
207 if(res == NULL)
209 error("Virtual memory exhausted.\n");
211 memset(res, 0x55, size);
212 return res;
216 void *xrealloc(void *p, size_t size)
218 void *res;
220 assert(size > 0);
221 res = realloc(p, size);
222 if(res == NULL)
224 error("Virtual memory exhausted.\n");
226 return res;
229 char *strmake( const char* fmt, ... )
231 int n;
232 size_t size = 100;
233 va_list ap;
235 for (;;)
237 char *p = xmalloc( size );
238 va_start( ap, fmt );
239 n = vsnprintf( p, size, fmt, ap );
240 va_end( ap );
241 if (n == -1) size *= 2;
242 else if ((size_t)n >= size) size = n + 1;
243 else return p;
244 free( p );
248 size_t strappend(char **buf, size_t *len, size_t pos, const char* fmt, ...)
250 size_t size;
251 va_list ap;
252 char *ptr;
253 int n;
255 assert( buf && len );
256 assert( (*len == 0 && *buf == NULL) || (*len != 0 && *buf != NULL) );
258 if (*buf)
260 size = *len;
261 ptr = *buf;
263 else
265 size = 100;
266 ptr = xmalloc( size );
269 for (;;)
271 va_start( ap, fmt );
272 n = vsnprintf( ptr + pos, size - pos, fmt, ap );
273 va_end( ap );
274 if (n == -1) size *= 2;
275 else if (pos + (size_t)n >= size) size = pos + n + 1;
276 else break;
277 ptr = xrealloc( ptr, size );
280 *len = size;
281 *buf = ptr;
282 return n;
285 char *xstrdup(const char *str)
287 char *s;
289 assert(str != NULL);
290 s = xmalloc(strlen(str)+1);
291 return strcpy(s, str);
294 int strendswith(const char* str, const char* end)
296 int l = strlen(str);
297 int m = strlen(end);
298 return l >= m && strcmp(str + l - m, end) == 0;
301 /*******************************************************************
302 * buffer management
304 * Function for writing to a memory buffer.
307 int byte_swapped = 0;
308 unsigned char *output_buffer;
309 size_t output_buffer_pos;
310 size_t output_buffer_size;
312 static struct resource
314 unsigned char *data;
315 size_t size;
316 } resources[16];
317 static unsigned int nb_resources;
319 static void check_output_buffer_space( size_t size )
321 if (output_buffer_pos + size >= output_buffer_size)
323 output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
324 output_buffer = xrealloc( output_buffer, output_buffer_size );
328 void init_output_buffer(void)
330 output_buffer_size = 1024;
331 output_buffer_pos = 0;
332 output_buffer = xmalloc( output_buffer_size );
335 void flush_output_buffer( const char *name )
337 int fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
338 if (fd == -1) error( "Error creating %s\n", name );
339 if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
340 error( "Error writing to %s\n", name );
341 close( fd );
342 free( output_buffer );
345 static inline void put_resource_id( const char *str )
347 if (str[0] != '#')
349 while (*str)
351 unsigned char ch = *str++;
352 put_word( toupper(ch) );
354 put_word( 0 );
356 else
358 put_word( 0xffff );
359 put_word( atoi( str + 1 ));
363 void add_output_to_resources( const char *type, const char *name )
365 size_t data_size = output_buffer_pos;
366 size_t header_size = 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
368 assert( nb_resources < ARRAY_SIZE( resources ));
370 if (type[0] != '#') header_size += (strlen( type ) + 1) * sizeof(unsigned short);
371 else header_size += 2 * sizeof(unsigned short);
372 if (name[0] != '#') header_size += (strlen( name ) + 1) * sizeof(unsigned short);
373 else header_size += 2 * sizeof(unsigned short);
375 header_size = (header_size + 3) & ~3;
376 align_output( 4 );
377 check_output_buffer_space( header_size );
378 resources[nb_resources].size = header_size + output_buffer_pos;
379 memmove( output_buffer + header_size, output_buffer, output_buffer_pos );
381 output_buffer_pos = 0;
382 put_dword( data_size ); /* ResSize */
383 put_dword( header_size ); /* HeaderSize */
384 put_resource_id( type ); /* ResType */
385 put_resource_id( name ); /* ResName */
386 align_output( 4 );
387 put_dword( 0 ); /* DataVersion */
388 put_word( 0 ); /* Memory options */
389 put_word( 0 ); /* Language */
390 put_dword( 0 ); /* Version */
391 put_dword( 0 ); /* Characteristics */
393 resources[nb_resources++].data = output_buffer;
394 init_output_buffer();
397 void flush_output_resources( const char *name )
399 int fd;
400 unsigned int i;
402 /* all output must have been saved with add_output_to_resources() first */
403 assert( !output_buffer_pos );
405 put_dword( 0 ); /* ResSize */
406 put_dword( 32 ); /* HeaderSize */
407 put_word( 0xffff ); /* ResType */
408 put_word( 0x0000 );
409 put_word( 0xffff ); /* ResName */
410 put_word( 0x0000 );
411 put_dword( 0 ); /* DataVersion */
412 put_word( 0 ); /* Memory options */
413 put_word( 0 ); /* Language */
414 put_dword( 0 ); /* Version */
415 put_dword( 0 ); /* Characteristics */
417 fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
418 if (fd == -1) error( "Error creating %s\n", name );
419 if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
420 error( "Error writing to %s\n", name );
421 for (i = 0; i < nb_resources; i++)
423 if (write( fd, resources[i].data, resources[i].size ) != resources[i].size)
424 error( "Error writing to %s\n", name );
425 free( resources[i].data );
427 close( fd );
428 nb_resources = 0;
429 free( output_buffer );
432 void put_data( const void *data, size_t size )
434 check_output_buffer_space( size );
435 memcpy( output_buffer + output_buffer_pos, data, size );
436 output_buffer_pos += size;
439 void put_byte( unsigned char val )
441 check_output_buffer_space( 1 );
442 output_buffer[output_buffer_pos++] = val;
445 void put_word( unsigned short val )
447 if (byte_swapped) val = (val << 8) | (val >> 8);
448 put_data( &val, sizeof(val) );
451 void put_dword( unsigned int val )
453 if (byte_swapped)
454 val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
455 put_data( &val, sizeof(val) );
458 void put_qword( unsigned int val )
460 if (byte_swapped)
462 put_dword( 0 );
463 put_dword( val );
465 else
467 put_dword( val );
468 put_dword( 0 );
472 /* pointer-sized word */
473 void put_pword( unsigned int val )
475 if (pointer_size == 8) put_qword( val );
476 else put_dword( val );
479 void put_str( int indent, const char *format, ... )
481 int n;
482 va_list args;
484 check_output_buffer_space( 4 * indent );
485 memset( output_buffer + output_buffer_pos, ' ', 4 * indent );
486 output_buffer_pos += 4 * indent;
488 for (;;)
490 size_t size = output_buffer_size - output_buffer_pos;
491 va_start( args, format );
492 n = vsnprintf( (char *)output_buffer + output_buffer_pos, size, format, args );
493 va_end( args );
494 if (n == -1) size *= 2;
495 else if ((size_t)n >= size) size = n + 1;
496 else
498 output_buffer_pos += n;
499 return;
501 check_output_buffer_space( size );
505 void align_output( unsigned int align )
507 size_t size = align - (output_buffer_pos % align);
509 if (size == align) return;
510 check_output_buffer_space( size );
511 memset( output_buffer + output_buffer_pos, 0, size );
512 output_buffer_pos += size;