ReactOS 0.4.16-dev-1946-g52006dd
utils.c
Go to the documentation of this file.
1/*
2 * Utility routines
3 *
4 * Copyright 1998 Bertho A. Stultiens
5 * Copyright 2002 Ove Kaaven
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include "config.h"
23
24#include <assert.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <stdarg.h>
28#include <string.h>
29#include <ctype.h>
30
31#include "widl.h"
32#include "utils.h"
33#include "parser.h"
34
35void error_at( const struct location *where, const char *s, ... )
36{
37 char buffer[1024];
38
39 va_list ap;
40 va_start( ap, s );
41 vsnprintf( buffer, sizeof(buffer), s, ap );
42 va_end( ap );
43
44 parser_error( where, buffer );
45 exit( 1 );
46}
47
48void error(const char *s, ...)
49{
50 va_list ap;
51 va_start(ap, s);
52 fprintf(stderr, "error: ");
54 va_end(ap);
55 exit(2);
56}
57
58void warning(const char *s, ...)
59{
60 va_list ap;
61 va_start(ap, s);
62 fprintf(stderr, "warning: ");
64 va_end(ap);
65}
66
67void warning_at( const struct location *where, const char *s, ... )
68{
69 char buffer[1024];
70
71 va_list ap;
72 va_start( ap, s );
73 vsnprintf( buffer, sizeof(buffer), s, ap );
74 va_end( ap );
75
76 parser_warning( where, buffer );
77}
78
79void chat(const char *s, ...)
80{
82 {
83 va_list ap;
84 va_start(ap, s);
85 fprintf(stderr, "chat: ");
87 va_end(ap);
88 }
89}
90
91size_t widl_getline(char **linep, size_t *lenp, FILE *fp)
92{
93 char *line = *linep;
94 size_t len = *lenp;
95 size_t n = 0;
96
97 if (!line)
98 {
99 len = 64;
100 line = xmalloc(len);
101 }
102
103 while (fgets(&line[n], len - n, fp))
104 {
105 n += strlen(&line[n]);
106 if (line[n - 1] == '\n')
107 break;
108 else if (n == len - 1)
109 {
110 len *= 2;
111 line = xrealloc(line, len);
112 }
113 }
114
115 *linep = line;
116 *lenp = len;
117 return n;
118}
119
120size_t strappend(char **buf, size_t *len, size_t pos, const char* fmt, ...)
121{
122 size_t size;
123 va_list ap;
124 char *ptr;
125 int n;
126
127 assert( buf && len );
128 assert( (*len == 0 && *buf == NULL) || (*len != 0 && *buf != NULL) );
129
130 if (*buf)
131 {
132 size = *len;
133 ptr = *buf;
134 }
135 else
136 {
137 size = 100;
138 ptr = xmalloc( size );
139 }
140
141 for (;;)
142 {
143 va_start( ap, fmt );
144 n = vsnprintf( ptr + pos, size - pos, fmt, ap );
145 va_end( ap );
146 if (n == -1) size *= 2;
147 else if (pos + (size_t)n >= size) size = pos + n + 1;
148 else break;
149 ptr = xrealloc( ptr, size );
150 }
151
152 *len = size;
153 *buf = ptr;
154 return n;
155}
156
157/*******************************************************************
158 * buffer management
159 *
160 * Function for writing to a memory buffer.
161 */
162
163unsigned char *output_buffer;
166
167static struct resource
168{
169 unsigned char *data;
170 size_t size;
172static unsigned int nb_resources;
173
174static inline void put_resource_id( const char *str )
175{
176 if (str[0] != '#')
177 {
178 while (*str)
179 {
180 unsigned char ch = *str++;
181 put_word( toupper(ch) );
182 }
183 put_word( 0 );
184 }
185 else
186 {
187 put_word( 0xffff );
188 put_word( atoi( str + 1 ));
189 }
190}
191
192void add_output_to_resources( const char *type, const char *name )
193{
194 size_t data_size = output_buffer_pos;
195 size_t header_size = 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
196
198
199 if (type[0] != '#') header_size += (strlen( type ) + 1) * sizeof(unsigned short);
200 else header_size += 2 * sizeof(unsigned short);
201 if (name[0] != '#') header_size += (strlen( name ) + 1) * sizeof(unsigned short);
202 else header_size += 2 * sizeof(unsigned short);
203
204 header_size = (header_size + 3) & ~3;
205 align_output( 4 );
206 check_output_buffer_space( header_size );
207 resources[nb_resources].size = header_size + output_buffer_pos;
209
211 put_dword( data_size ); /* ResSize */
212 put_dword( header_size ); /* HeaderSize */
213 put_resource_id( type ); /* ResType */
214 put_resource_id( name ); /* ResName */
215 align_output( 4 );
216 put_dword( 0 ); /* DataVersion */
217 put_word( 0 ); /* Memory options */
218 put_word( 0 ); /* Language */
219 put_dword( 0 ); /* Version */
220 put_dword( 0 ); /* Characteristics */
221
224}
225
226void flush_output_resources( const char *name )
227{
228 unsigned int i;
229
230 /* all output must have been saved with add_output_to_resources() first */
232
233 put_dword( 0 ); /* ResSize */
234 put_dword( 32 ); /* HeaderSize */
235 put_word( 0xffff ); /* ResType */
236 put_word( 0x0000 );
237 put_word( 0xffff ); /* ResName */
238 put_word( 0x0000 );
239 put_dword( 0 ); /* DataVersion */
240 put_word( 0 ); /* Memory options */
241 put_word( 0 ); /* Language */
242 put_dword( 0 ); /* Version */
243 put_dword( 0 ); /* Characteristics */
244
245 for (i = 0; i < nb_resources; i++)
246 {
248 free( resources[i].data );
249 }
251 nb_resources = 0;
252}
253
254/* pointer-sized word */
255void put_pword( unsigned int val )
256{
257 if (pointer_size == 8) put_qword( val );
258 else put_dword( val );
259}
260
261void put_str( int indent, const char *format, ... )
262{
263 int n;
265
269
270 for (;;)
271 {
273 va_start( args, format );
275 va_end( args );
276 if (n == -1) size *= 2;
277 else if ((size_t)n >= size) size = n + 1;
278 else
279 {
281 return;
282 }
284 }
285}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
int toupper(int c)
Definition: utclib.c:881
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
void * xmalloc(int size)
Definition: uimain.c:747
void * xrealloc(void *oldmem, size_t size)
Definition: uimain.c:736
#define ARRAY_SIZE(A)
Definition: main.h:20
static void put_dword(struct bytecode_buffer *buffer, DWORD value)
#define free
Definition: debug_ros.c:5
#define NULL
Definition: types.h:112
static const WCHAR indent[]
Definition: object.c:1156
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
unsigned short(__cdecl typeof(TIFFCurrentDirectory))(struct tiff *)
Definition: typeof.h:94
#define assert(x)
Definition: debug.h:53
FxCmResList * resources
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLdouble s
Definition: gl.h:2039
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLdouble n
Definition: glext.h:7729
GLuint buffer
Definition: glext.h:5915
GLsizeiptr size
Definition: glext.h:5919
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLuint GLfloat * val
Definition: glext.h:7180
GLenum GLsizei len
Definition: glext.h:6722
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_opt_ _CRTIMP int __cdecl vfprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format, va_list _ArgList)
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
#define error(str)
Definition: mkdosfs.c:1605
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
static PVOID ptr
Definition: dispmode.c:27
static HRESULT flush_output_buffer(mxwriter *This)
Definition: mxwriter.c:661
const WCHAR * str
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
#define warning(s)
Definition: debug.h:83
#define exit(n)
Definition: config.h:202
#define memset(x, y, z)
Definition: compat.h:39
#define args
Definition: format.c:66
static void align_output(unsigned int align)
Definition: tools.h:833
static void init_output_buffer(void)
Definition: tools.h:791
static void put_data(const void *data, size_t size)
Definition: tools.h:798
static void put_qword(unsigned int val)
Definition: tools.h:827
static void check_output_buffer_space(size_t size)
Definition: tools.h:782
static void put_word(unsigned short val)
Definition: tools.h:811
void parser_error(const struct location *where, const char *message)
void parser_warning(const struct location *where, const char *message)
size_t widl_getline(char **linep, size_t *lenp, FILE *fp)
Definition: utils.c:91
void put_pword(unsigned int val)
Definition: utils.c:255
size_t output_buffer_pos
Definition: utils.c:164
size_t output_buffer_size
Definition: utils.c:165
void add_output_to_resources(const char *type, const char *name)
Definition: utils.c:192
void warning_at(const struct location *where, const char *s,...)
Definition: utils.c:67
void error_at(const struct location *where, const char *s,...)
Definition: utils.c:35
void flush_output_resources(const char *name)
Definition: utils.c:226
void chat(const char *s,...)
Definition: utils.c:79
void put_str(int indent, const char *format,...)
Definition: utils.c:261
static void put_resource_id(const char *str)
Definition: utils.c:174
size_t strappend(char **buf, size_t *len, size_t pos, const char *fmt,...)
Definition: utils.c:120
static unsigned int nb_resources
Definition: utils.c:172
unsigned char * output_buffer
Definition: utils.c:163
Definition: match.c:390
Definition: dsound.c:943
Definition: format.c:58
Definition: parser.c:49
Definition: name.c:39
size_t size
Definition: utils.c:170
unsigned char * data
Definition: utils.c:169
#define vsnprintf
Definition: tif_win32.c:406
unsigned int pointer_size
Definition: widl.c:147
int debuglevel
Definition: widl.c:94
#define DEBUGLEVEL_CHAT
Definition: widl.h:31
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36