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
22 #include "wine/port.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
48 * The extra routine 'xyyerror' is used to exit after giving a real
51 int mcy_error(const char *s
, ...)
53 #ifndef SUPPRESS_YACC_ERROR_MESSAGE
56 generic_msg(s
, "Yacc error", ap
);
62 int xyyerror(const char *s
, ...)
66 generic_msg(s
, "Error", ap
);
72 int mcy_warning(const char *s
, ...)
76 generic_msg(s
, "Warning", ap
);
81 void internal_error(const char *file
, int line
, const char *s
, ...)
85 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
86 vfprintf(stderr
, s
, ap
);
91 void error(const char *s
, ...)
95 fprintf(stderr
, "Error: ");
96 vfprintf(stderr
, s
, ap
);
101 void warning(const char *s
, ...)
105 fprintf(stderr
, "Warning: ");
106 vfprintf(stderr
, s
, ap
);
110 char *dup_basename(const char *name
, const char *ext
)
113 int extlen
= strlen(ext
);
120 slash
= strrchr(name
, '/');
124 namelen
= strlen(name
);
126 /* +4 for later extension and +1 for '\0' */
127 base
= xmalloc(namelen
+4 +1);
129 if(!strcasecmp(name
+ namelen
-extlen
, ext
))
131 base
[namelen
- extlen
] = '\0';
136 void *xmalloc(size_t size
)
144 error("Virtual memory exhausted.\n");
146 memset(res
, 0x55, size
);
151 void *xrealloc(void *p
, size_t size
)
156 res
= realloc(p
, size
);
159 error("Virtual memory exhausted.\n");
164 char *xstrdup(const char *str
)
169 s
= xmalloc(strlen(str
)+1);
170 return strcpy(s
, str
);
173 char *strmake( const char* fmt
, ... )
181 char *p
= xmalloc( size
);
183 n
= vsnprintf( p
, size
, fmt
, ap
);
185 if (n
== -1) size
*= 2;
186 else if ((size_t)n
>= size
) size
= n
+ 1;
192 int unistrlen(const WCHAR
*s
)
195 for(n
= 0; *s
; n
++, s
++)
200 WCHAR
*unistrcpy(WCHAR
*dst
, const WCHAR
*src
)
209 WCHAR
*xunistrdup(const WCHAR
* str
)
214 s
= xmalloc((unistrlen(str
)+1) * sizeof(WCHAR
));
215 return unistrcpy(s
, str
);
218 int unistricmp(const WCHAR
*s1
, const WCHAR
*s2
)
222 static const char warn
[] = "Don't know the uppercase equivalent of non ascii characters;"
223 "comparison might yield wrong results";
226 if((*s1
& 0xffff) > 0x7f || (*s2
& 0xffff) > 0x7f)
236 i
= toupper(*s1
++) - toupper(*s2
++);
241 if((*s1
& 0xffff) > 0x7f || (*s2
& 0xffff) > 0x7f)
248 return toupper(*s1
) - toupper(*s2
);
251 int unistrcmp(const WCHAR
*s1
, const WCHAR
*s2
)
264 /*******************************************************************
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
);
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
)
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
;