make_xftmpl: Use the standard output buffer routines.

Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Alexandre Julliard 2022-02-08 17:40:53 +01:00
parent 62910fffa4
commit 7d3938e811
1 changed files with 68 additions and 97 deletions

View File

@ -93,8 +93,10 @@ static FILE *infile;
static int line_no; static int line_no;
static const char *infile_name; static const char *infile_name;
static FILE *outfile; static FILE *outfile;
static BYTE *output_data;
static UINT output_pos, output_size; unsigned char *output_buffer = NULL;
size_t output_buffer_pos = 0;
size_t output_buffer_size = 0;
static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2))); static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
@ -136,63 +138,33 @@ static inline BOOL read_bytes( void *data, DWORD size )
static BOOL write_c_hex_bytes(void) static BOOL write_c_hex_bytes(void)
{ {
UINT i; UINT i;
for (i = 0; i < output_pos; i++) for (i = 0; i < output_buffer_pos; i++)
{ {
if (i % 12 == 0) if (i % 12 == 0)
fprintf(outfile, "\n "); fprintf(outfile, "\n ");
fprintf(outfile, " 0x%02x,", output_data[i]); fprintf(outfile, " 0x%02x,", output_buffer[i]);
} }
return TRUE; return TRUE;
} }
static BOOL write_raw_bytes(void) static BOOL write_raw_bytes(void)
{ {
return fwrite(output_data, output_pos, 1, outfile) > 0; return fwrite(output_buffer, output_buffer_pos, 1, outfile) > 0;
} }
static inline BOOL write_bytes(const void *data, DWORD size) static inline void put_float(float value)
{
if (output_pos + size > output_size)
{
output_size = max( output_size * 2, size );
output_data = realloc( output_data, output_size );
if (!output_data) return FALSE;
}
memcpy( output_data + output_pos, data, size );
output_pos += size;
return TRUE;
}
static inline BOOL write_byte(BYTE value)
{
return write_bytes( &value, sizeof(value) );
}
static inline BOOL write_word(WORD value)
{
return write_byte( value ) &&
write_byte( value >> 8 );
}
static inline BOOL write_dword(DWORD value)
{
return write_word( value ) &&
write_word( value >> 16 );
}
static inline BOOL write_float(float value)
{ {
DWORD val; DWORD val;
memcpy( &val, &value, sizeof(value) ); memcpy( &val, &value, sizeof(value) );
return write_dword( val ); return put_dword( val );
} }
static inline BOOL write_guid(const GUID *guid) static inline void put_guid(const GUID *guid)
{ {
return write_dword( guid->Data1 ) && put_dword( guid->Data1 );
write_word( guid->Data2 ) && put_word( guid->Data2 );
write_word( guid->Data3 ) && put_word( guid->Data3 );
write_bytes( guid->Data4, sizeof(guid->Data4) ); put_data( guid->Data4, sizeof(guid->Data4) );
} }
static int compare_names(const void *a, const void *b) static int compare_names(const void *a, const void *b)
@ -209,10 +181,11 @@ static BOOL parse_keyword( const char *name )
if (!keyword) if (!keyword)
return FALSE; return FALSE;
return write_word(keyword->token); put_word(keyword->token);
return TRUE;
} }
static BOOL parse_guid(void) static void parse_guid(void)
{ {
char buf[39]; char buf[39];
GUID guid; GUID guid;
@ -238,11 +211,11 @@ static BOOL parse_guid(void)
guid.Data4[6] = tab[8]; guid.Data4[6] = tab[8];
guid.Data4[7] = tab[9]; guid.Data4[7] = tab[9];
return write_word(TOKEN_GUID) && put_word(TOKEN_GUID);
write_guid(&guid); put_guid(&guid);
} }
static BOOL parse_name(void) static void parse_name(void)
{ {
char c; char c;
int len = 0; int len = 0;
@ -257,16 +230,15 @@ static BOOL parse_name(void)
unread_byte(c); unread_byte(c);
name[len] = 0; name[len] = 0;
if (parse_keyword(name)) { if (!parse_keyword(name))
return TRUE; {
} else { put_word(TOKEN_NAME);
return write_word(TOKEN_NAME) && put_dword(len);
write_dword(len) && put_data(name, len);
write_bytes(name, len);
} }
} }
static BOOL parse_number(void) static void parse_number(void)
{ {
int len = 0; int len = 0;
char c; char c;
@ -289,17 +261,15 @@ static BOOL parse_number(void)
float value; float value;
ret = sscanf(buffer, "%f", &value); ret = sscanf(buffer, "%f", &value);
if (!ret) fatal_error( "invalid float token\n" ); if (!ret) fatal_error( "invalid float token\n" );
ret = write_word(TOKEN_FLOAT) && put_word(TOKEN_FLOAT);
write_float(value); put_float(value);
} else { } else {
int value; int value;
ret = sscanf(buffer, "%d", &value); ret = sscanf(buffer, "%d", &value);
if (!ret) fatal_error( "invalid integer token\n" ); if (!ret) fatal_error( "invalid integer token\n" );
ret = write_word(TOKEN_INTEGER) && put_word(TOKEN_INTEGER);
write_dword(value); put_dword(value);
} }
return ret;
} }
static BOOL parse_token(void) static BOOL parse_token(void)
@ -317,17 +287,17 @@ static BOOL parse_token(void)
case '\r': case '\r':
case ' ': case ' ':
case '\t': case '\t':
return TRUE; break;
case '{': return write_word(TOKEN_OBRACE); case '{': put_word(TOKEN_OBRACE); break;
case '}': return write_word(TOKEN_CBRACE); case '}': put_word(TOKEN_CBRACE); break;
case '[': return write_word(TOKEN_OBRACKET); case '[': put_word(TOKEN_OBRACKET); break;
case ']': return write_word(TOKEN_CBRACKET); case ']': put_word(TOKEN_CBRACKET); break;
case '(': return write_word(TOKEN_OPAREN); case '(': put_word(TOKEN_OPAREN); break;
case ')': return write_word(TOKEN_CPAREN); case ')': put_word(TOKEN_CPAREN); break;
case ',': return write_word(TOKEN_COMMA); case ',': put_word(TOKEN_COMMA); break;
case ';': return write_word(TOKEN_SEMICOLON); case ';': put_word(TOKEN_SEMICOLON); break;
case '.': return write_word(TOKEN_DOT); case '.': put_word(TOKEN_DOT); break;
case '/': case '/':
if (!read_byte(&c) || c != '/') if (!read_byte(&c) || c != '/')
@ -342,11 +312,11 @@ static BOOL parse_token(void)
if (c != '\n') fatal_error( "line too long\n" ); if (c != '\n') fatal_error( "line too long\n" );
buffer[len] = 0; buffer[len] = 0;
tok = strtok( buffer, " \t" ); tok = strtok( buffer, " \t" );
if (!tok || strcmp( tok, "pragma" )) return TRUE; if (!tok || strcmp( tok, "pragma" )) break;
tok = strtok( NULL, " \t" ); tok = strtok( NULL, " \t" );
if (!tok || strcmp( tok, "xftmpl" )) return TRUE; if (!tok || strcmp( tok, "xftmpl" )) break;
tok = strtok( NULL, " \t" ); tok = strtok( NULL, " \t" );
if (!tok) return TRUE; if (!tok) break;
if (!strcmp( tok, "name" )) if (!strcmp( tok, "name" ))
{ {
tok = strtok( NULL, " \t" ); tok = strtok( NULL, " \t" );
@ -357,10 +327,11 @@ static BOOL parse_token(void)
tok = strtok( NULL, " \t" ); tok = strtok( NULL, " \t" );
if (tok && !option_inc_size_name) option_inc_size_name = xstrdup( tok ); if (tok && !option_inc_size_name) option_inc_size_name = xstrdup( tok );
} }
return TRUE; break;
case '<': case '<':
return parse_guid(); parse_guid();
break;
case '"': case '"':
len = 0; len = 0;
@ -371,17 +342,16 @@ static BOOL parse_token(void)
buffer[len++] = c; buffer[len++] = c;
} }
if (c != '"') fatal_error( "unterminated string\n" ); if (c != '"') fatal_error( "unterminated string\n" );
return write_word(TOKEN_STRING) && put_word(TOKEN_STRING);
write_dword(len) && put_dword(len);
write_bytes(buffer, len); put_data(buffer, len);
break;
default: default:
unread_byte(c); unread_byte(c);
if (isdigit(c) || c == '-') if (isdigit(c) || c == '-') parse_number();
return parse_number(); else if (isalpha(c) || c == '_') parse_name();
if (isalpha(c) || c == '_') else fatal_error( "invalid character '%c' to start token\n", c );
return parse_name();
fatal_error( "invalid character '%c' to start token\n", c );
} }
return TRUE; return TRUE;
@ -489,6 +459,19 @@ int main(int argc, char **argv)
goto error; goto error;
} }
init_output_buffer();
put_data("xof 0302bin 0064", 16);
line_no = 1;
while (parse_token());
if (ferror(infile))
{
perror(infile_name);
return 1;
}
fclose(infile);
if (!strcmp(option_outfile_name, "-")) { if (!strcmp(option_outfile_name, "-")) {
option_outfile_name = "stdout"; option_outfile_name = "stdout";
outfile = stdout; outfile = stdout;
@ -506,13 +489,7 @@ int main(int argc, char **argv)
} }
} }
if (!write_bytes("xof 0302bin 0064", 16)) if (ferror(outfile))
goto error;
line_no = 1;
while (parse_token());
if (ferror(outfile) || ferror(infile))
goto error; goto error;
if (option_header) if (option_header)
@ -543,24 +520,18 @@ int main(int argc, char **argv)
write_c_hex_bytes(); write_c_hex_bytes();
fprintf(outfile, "\n};\n\n"); fprintf(outfile, "\n};\n\n");
if (option_inc_size_name) if (option_inc_size_name)
fprintf(outfile, "#define %s %u\n\n", option_inc_size_name, output_pos); fprintf(outfile, "#define %s %u\n\n", option_inc_size_name, (unsigned int)output_buffer_pos);
fprintf(outfile, "#endif /* __WINE_%s */\n", header_name); fprintf(outfile, "#endif /* __WINE_%s */\n", header_name);
if (ferror(outfile)) if (ferror(outfile))
goto error; goto error;
} }
else write_raw_bytes(); else write_raw_bytes();
fclose(infile);
fclose(outfile); fclose(outfile);
output_file = NULL; output_file = NULL;
return 0; return 0;
error: error:
if (infile) {
if (ferror(infile))
perror(infile_name);
fclose(infile);
}
if (outfile) { if (outfile) {
if (ferror(outfile)) if (ferror(outfile))
perror(option_outfile_name); perror(option_outfile_name);