widl: Copy the output buffer management functions from winebuild.

This commit is contained in:
Alexandre Julliard 2010-11-27 11:42:09 +01:00
parent b51f8d4812
commit 00dda6f687
2 changed files with 126 additions and 0 deletions

View File

@ -234,3 +234,104 @@ char *xstrdup(const char *str)
s = xmalloc(strlen(str)+1); s = xmalloc(strlen(str)+1);
return strcpy(s, str); return strcpy(s, str);
} }
int strendswith(const char* str, const char* end)
{
int l = strlen(str);
int m = strlen(end);
return l >= m && strcmp(str + l - m, end) == 0;
}
/*******************************************************************
* buffer management
*
* Function for writing to a memory buffer.
*/
int byte_swapped = 0;
unsigned char *output_buffer;
size_t output_buffer_pos;
size_t output_buffer_size;
static void check_output_buffer_space( size_t size )
{
if (output_buffer_pos + size >= output_buffer_size)
{
output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
output_buffer = xrealloc( output_buffer, output_buffer_size );
}
}
void init_output_buffer(void)
{
output_buffer_size = 1024;
output_buffer_pos = 0;
output_buffer = xmalloc( output_buffer_size );
}
void flush_output_buffer( const char *name )
{
int fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
if (fd == -1) error( "Error creating %s\n", name );
if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
error( "Error writing to %s\n", name );
close( fd );
free( output_buffer );
}
void put_data( const void *data, size_t size )
{
check_output_buffer_space( size );
memcpy( output_buffer + output_buffer_pos, data, size );
output_buffer_pos += size;
}
void put_byte( unsigned char val )
{
check_output_buffer_space( 1 );
output_buffer[output_buffer_pos++] = val;
}
void put_word( unsigned short val )
{
if (byte_swapped) val = (val << 8) | (val >> 8);
put_data( &val, sizeof(val) );
}
void put_dword( unsigned int val )
{
if (byte_swapped)
val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
put_data( &val, sizeof(val) );
}
void put_qword( unsigned int val )
{
if (byte_swapped)
{
put_dword( 0 );
put_dword( val );
}
else
{
put_dword( val );
put_dword( 0 );
}
}
/* pointer-sized word */
void put_pword( unsigned int val )
{
if (pointer_size == 8) put_qword( val );
else put_dword( val );
}
void align_output( unsigned int align )
{
size_t size = align - (output_buffer_pos % align);
if (size == align) return;
check_output_buffer_space( size );
memset( output_buffer + output_buffer_pos, 0, size );
output_buffer_pos += size;
}

View File

@ -28,6 +28,7 @@
void *xmalloc(size_t); void *xmalloc(size_t);
void *xrealloc(void *, size_t); void *xrealloc(void *, size_t);
char *xstrdup(const char *str); char *xstrdup(const char *str);
int strendswith(const char* str, const char* end);
#ifndef __GNUC__ #ifndef __GNUC__
#define __attribute__(X) #define __attribute__(X)
@ -48,10 +49,34 @@ size_t widl_getline(char **linep, size_t *lenp, FILE *fp);
UUID *parse_uuid(const char *u); UUID *parse_uuid(const char *u);
int is_valid_uuid(const char *s); int is_valid_uuid(const char *s);
/* buffer management */
extern int byte_swapped;
extern unsigned char *output_buffer;
extern size_t output_buffer_pos;
extern size_t output_buffer_size;
extern void init_output_buffer(void);
extern void flush_output_buffer( const char *name );
extern void put_data( const void *data, size_t size );
extern void put_byte( unsigned char val );
extern void put_word( unsigned short val );
extern void put_dword( unsigned int val );
extern void put_qword( unsigned int val );
extern void put_pword( unsigned int val );
extern void align_output( unsigned int align );
/* typelibs expect the minor version to be stored in the higher bits and /* typelibs expect the minor version to be stored in the higher bits and
* major to be stored in the lower bits */ * major to be stored in the lower bits */
#define MAKEVERSION(major, minor) ((((minor) & 0xffff) << 16) | ((major) & 0xffff)) #define MAKEVERSION(major, minor) ((((minor) & 0xffff) << 16) | ((major) & 0xffff))
#define MAJORVERSION(version) ((version) & 0xffff) #define MAJORVERSION(version) ((version) & 0xffff)
#define MINORVERSION(version) (((version) >> 16) & 0xffff) #define MINORVERSION(version) (((version) >> 16) & 0xffff)
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#endif #endif