From 97ca9f8a3ddf7347f589cd928ef3fe1a7ef23fc3 Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Tue, 28 Sep 2021 10:08:06 +0200 Subject: [PATCH] tools: Add a shared header for common helper functions. Signed-off-by: Alexandre Julliard --- configure | 2 +- configure.ac | 2 +- tools/make_xftmpl.c | 19 ++-- tools/makedep.c | 151 +--------------------------- tools/sfnt2fon/sfnt2fon.c | 13 +-- tools/tools.h | 204 ++++++++++++++++++++++++++++++++++++++ tools/widl/utils.c | 63 ------------ tools/widl/utils.h | 21 ---- tools/widl/widl.h | 1 + tools/winebuild/build.h | 34 +------ tools/winebuild/utils.c | 116 ---------------------- tools/winegcc/utils.c | 114 --------------------- tools/winegcc/utils.h | 27 +---- tools/wmc/utils.c | 63 ------------ tools/wmc/utils.h | 13 +-- tools/wrc/utils.c | 63 ------------ tools/wrc/utils.h | 12 --- tools/wrc/wpp_private.h | 1 + tools/wrc/wrc.h | 1 + 19 files changed, 226 insertions(+), 694 deletions(-) create mode 100644 tools/tools.h diff --git a/configure b/configure index e32b363b3fc..6acdac3c858 100755 --- a/configure +++ b/configure @@ -21997,7 +21997,7 @@ cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 wine_fn_output_makedep () { as_dir=tools; as_fn_mkdir_p - $CC -Iinclude -I$srcdir/include -D__WINESRC__ $EXTRACFLAGS $CPPFLAGS $CFLAGS -o tools/makedep$ac_exeext $srcdir/tools/makedep.c $LDFLAGS + $CC -I$srcdir/tools -Iinclude -I$srcdir/include -D__WINESRC__ $EXTRACFLAGS $CPPFLAGS $CFLAGS -o tools/makedep$ac_exeext $srcdir/tools/makedep.c $LDFLAGS } wine_fn_output_makefile () { diff --git a/configure.ac b/configure.ac index 9fa8ff3e394..94d1b472142 100644 --- a/configure.ac +++ b/configure.ac @@ -4013,7 +4013,7 @@ then [wine_fn_output_makedep () { AS_MKDIR_P(tools) - $CC -Iinclude -I$srcdir/include -D__WINESRC__ $EXTRACFLAGS $CPPFLAGS $CFLAGS -o tools/makedep$ac_exeext $srcdir/tools/makedep.c $LDFLAGS + $CC -I$srcdir/tools -Iinclude -I$srcdir/include -D__WINESRC__ $EXTRACFLAGS $CPPFLAGS $CFLAGS -o tools/makedep$ac_exeext $srcdir/tools/makedep.c $LDFLAGS }]) fi diff --git a/tools/make_xftmpl.c b/tools/make_xftmpl.c index 49a1273bad4..e7ff2f8d1c3 100644 --- a/tools/make_xftmpl.c +++ b/tools/make_xftmpl.c @@ -34,6 +34,7 @@ #include "windef.h" #include "guiddef.h" +#include "tools.h" #define TOKEN_NAME 1 #define TOKEN_STRING 2 @@ -104,10 +105,6 @@ static FILE *outfile; static BYTE *output_data; static UINT output_pos, output_size; -#ifndef __GNUC__ -#define __attribute__(x) -#endif - static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2))); static void fatal_error( const char *msg, ... ) @@ -362,12 +359,12 @@ static BOOL parse_token(void) if (!strcmp( tok, "name" )) { tok = strtok( NULL, " \t" ); - if (tok && !option_inc_var_name) option_inc_var_name = strdup( tok ); + if (tok && !option_inc_var_name) option_inc_var_name = xstrdup( tok ); } else if (!strcmp( tok, "size" )) { tok = strtok( NULL, " \t" ); - if (tok && !option_inc_size_name) option_inc_size_name = strdup( tok ); + if (tok && !option_inc_size_name) option_inc_size_name = xstrdup( tok ); } return TRUE; @@ -438,13 +435,13 @@ static char **parse_options(int argc, char **argv) break; case 'i': option_header = TRUE; - option_inc_var_name = strdup(optarg); + option_inc_var_name = xstrdup(optarg); break; case 'o': - option_outfile_name = strdup(optarg); + option_outfile_name = xstrdup(optarg); break; case 's': - option_inc_size_name = strdup(optarg); + option_inc_size_name = xstrdup(optarg); break; } } @@ -539,9 +536,9 @@ int main(int argc, char **argv) header_name = strrchr(option_outfile_name, '/'); if (header_name) - header_name = strdup(header_name + 1); + header_name = xstrdup(header_name + 1); else - header_name = strdup(option_outfile_name); + header_name = xstrdup(option_outfile_name); if (!header_name) { fprintf(stderr, "Out of memory\n"); goto error; diff --git a/tools/makedep.c b/tools/makedep.c index 6e10e4d3be4..26e6da9e823 100644 --- a/tools/makedep.c +++ b/tools/makedep.c @@ -39,15 +39,9 @@ #include #define mkdir(path,mode) mkdir(path) #endif +#include "tools.h" #include "wine/list.h" -struct strarray -{ - unsigned int count; /* strings in use */ - unsigned int size; /* total allocated size */ - const char **str; -}; - enum incl_type { INCL_NORMAL, /* #include "foo.h" */ @@ -130,8 +124,6 @@ static const struct static struct list files[HASH_SIZE]; -static const struct strarray empty_strarray; - enum install_rules { INSTALL_LIB, INSTALL_DEV, NB_INSTALL_RULES }; /* variables common to all makefiles */ @@ -256,10 +248,6 @@ static const char Usage[] = " -fxxx Store output in file 'xxx' (default: Makefile)\n"; -#ifndef __GNUC__ -#define __attribute__(x) -#endif - static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2))); static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2))); static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2))); @@ -325,76 +313,6 @@ static void exit_on_signal( int sig ) } -/******************************************************************* - * xmalloc - */ -static void *xmalloc( size_t size ) -{ - void *res; - if (!(res = malloc (size ? size : 1))) - fatal_error( "Virtual memory exhausted.\n" ); - return res; -} - - -/******************************************************************* - * xrealloc - */ -static void *xrealloc (void *ptr, size_t size) -{ - void *res; - assert( size ); - if (!(res = realloc( ptr, size ))) - fatal_error( "Virtual memory exhausted.\n" ); - return res; -} - -/******************************************************************* - * xstrdup - */ -static char *xstrdup( const char *str ) -{ - char *res = strdup( str ); - if (!res) fatal_error( "Virtual memory exhausted.\n" ); - return res; -} - - -/******************************************************************* - * strmake - */ -static char *strmake( const char* fmt, ... ) -{ - int n; - size_t size = 100; - va_list ap; - - for (;;) - { - char *p = xmalloc (size); - va_start(ap, fmt); - n = vsnprintf (p, size, fmt, ap); - va_end(ap); - if (n == -1) size *= 2; - else if ((size_t)n >= size) size = n + 1; - else return xrealloc( p, n + 1 ); - free(p); - } -} - - -/******************************************************************* - * strendswith - */ -static int strendswith( const char* str, const char* end ) -{ - size_t l = strlen( str ); - size_t m = strlen( end ); - - return l >= m && strcmp(str + l - m, end) == 0; -} - - /******************************************************************* * output */ @@ -412,64 +330,6 @@ static void output( const char *format, ... ) } -/******************************************************************* - * strarray_add - */ -static void strarray_add( struct strarray *array, const char *str ) -{ - if (array->count == array->size) - { - if (array->size) array->size *= 2; - else array->size = 16; - array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size ); - } - array->str[array->count++] = str; -} - - -/******************************************************************* - * strarray_addall - */ -static void strarray_addall( struct strarray *array, struct strarray added ) -{ - unsigned int i; - - for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] ); -} - - -/******************************************************************* - * strarray_exists - */ -static int strarray_exists( const struct strarray *array, const char *str ) -{ - unsigned int i; - - for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1; - return 0; -} - - -/******************************************************************* - * strarray_add_uniq - */ -static void strarray_add_uniq( struct strarray *array, const char *str ) -{ - if (!strarray_exists( array, str )) strarray_add( array, str ); -} - - -/******************************************************************* - * strarray_addall_uniq - */ -static void strarray_addall_uniq( struct strarray *array, struct strarray added ) -{ - unsigned int i; - - for (i = 0; i < added.count; i++) strarray_add_uniq( array, added.str[i] ); -} - - /******************************************************************* * strarray_get_value * @@ -519,15 +379,6 @@ static void strarray_set_value( struct strarray *array, const char *name, const } -/******************************************************************* - * strarray_set_qsort - */ -static void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) ) -{ - if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func ); -} - - /******************************************************************* * normalize_arch */ diff --git a/tools/sfnt2fon/sfnt2fon.c b/tools/sfnt2fon/sfnt2fon.c index 6427eb9f840..7209bae074e 100644 --- a/tools/sfnt2fon/sfnt2fon.c +++ b/tools/sfnt2fon/sfnt2fon.c @@ -45,6 +45,7 @@ #include "winbase.h" #include "wingdi.h" #include "basetsd.h" +#include "../tools.h" #include "pshpack1.h" @@ -449,10 +450,6 @@ static void usage(char **argv) fprintf(stderr, " -s Single .fnt file mode\n" ); } -#ifndef __GNUC__ -#define __attribute__(X) -#endif - /* atexit handler to cleanup files */ static void cleanup(void) { @@ -580,7 +577,7 @@ static struct fontinfo *fill_fontinfo( const char *face_name, int ppem, int enc, fprintf(stderr,"Can't find EBLC table\n"); else { - eblc = malloc(needed); + eblc = xmalloc(needed); FT_Load_Sfnt_Table(face, TTAG_EBLC, 0, (FT_Byte *)eblc, &needed); num_sizes = GET_BE_DWORD(&eblc->numSizes); @@ -841,7 +838,7 @@ static char **parse_options( int argc, char **argv ) option_defchar = atoi( optarg ); break; case 'o': - option_output = strdup( optarg ); + option_output = xstrdup( optarg ); break; case 'q': option_quiet = 1; @@ -903,7 +900,7 @@ int main(int argc, char **argv) if (option_fnt_mode && num_files > 1) error( "can only specify one font in .fnt mode\n" ); - info = malloc( num_files * sizeof(*info) ); + info = xmalloc( num_files * sizeof(*info) ); for (i = 0; i < num_files; i++) { int ppem, enc, avg_width; @@ -978,7 +975,7 @@ int main(int argc, char **argv) char *p = strrchr( input_file, '/' ); if (p) p++; else p = input_file; - option_output = malloc( strlen(p) + sizeof(".fon") ); + option_output = xmalloc( strlen(p) + sizeof(".fon") ); strcpy( option_output, p ); p = strrchr( option_output, '.' ); if (!p) p = option_output + strlen(option_output); diff --git a/tools/tools.h b/tools/tools.h new file mode 100644 index 00000000000..bfbbb60b9e8 --- /dev/null +++ b/tools/tools.h @@ -0,0 +1,204 @@ +/* + * Helper functions for the Wine tools + * + * Copyright 2021 Alexandre Julliard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef __WINE_TOOLS_H +#define __WINE_TOOLS_H + +#include +#include +#include +#include + +#if !defined(__GNUC__) && !defined(__attribute__) +#define __attribute__(x) +#endif + +#ifndef max +#define max(a,b) (((a) > (b)) ? (a) : (b)) +#endif +#ifndef min +#define min(a,b) (((a) < (b)) ? (a) : (b)) +#endif + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#endif + + +static inline void *xmalloc( size_t size ) +{ + void *res = malloc( size ? size : 1 ); + + if (res == NULL) + { + fprintf( stderr, "Virtual memory exhausted.\n" ); + exit(1); + } + return res; +} + +static inline void *xrealloc (void *ptr, size_t size) +{ + void *res = realloc( ptr, size ); + + if (size && res == NULL) + { + fprintf( stderr, "Virtual memory exhausted.\n" ); + exit(1); + } + return res; +} + +static inline char *xstrdup( const char *str ) +{ + return strcpy( xmalloc( strlen(str)+1 ), str ); +} + +static inline int strendswith( const char *str, const char *end ) +{ + int l = strlen( str ); + int m = strlen( end ); + return l >= m && !strcmp( str + l - m, end ); +} + +static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2))); +static inline char *strmake( const char* fmt, ... ) +{ + int n; + size_t size = 100; + va_list ap; + + for (;;) + { + char *p = xmalloc( size ); + va_start( ap, fmt ); + n = vsnprintf( p, size, fmt, ap ); + va_end( ap ); + if (n == -1) size *= 2; + else if ((size_t)n >= size) size = n + 1; + else return p; + free( p ); + } +} + +/* string array functions */ + +struct strarray +{ + unsigned int count; /* strings in use */ + unsigned int size; /* total allocated size */ + const char **str; +}; + +static const struct strarray empty_strarray; + +static inline void strarray_add( struct strarray *array, const char *str ) +{ + if (array->count == array->size) + { + if (array->size) array->size *= 2; + else array->size = 16; + array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size ); + } + array->str[array->count++] = str; +} + +static inline void strarray_addall( struct strarray *array, struct strarray added ) +{ + unsigned int i; + + for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] ); +} + +static inline int strarray_exists( const struct strarray *array, const char *str ) +{ + unsigned int i; + + for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1; + return 0; +} + +static inline void strarray_add_uniq( struct strarray *array, const char *str ) +{ + if (!strarray_exists( array, str )) strarray_add( array, str ); +} + +static inline void strarray_addall_uniq( struct strarray *array, struct strarray added ) +{ + unsigned int i; + + for (i = 0; i < added.count; i++) strarray_add_uniq( array, added.str[i] ); +} + +static inline struct strarray strarray_fromstring( const char *str, const char *delim ) +{ + struct strarray array = empty_strarray; + char *buf = xstrdup( str ); + const char *tok; + + for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim )) + strarray_add( &array, xstrdup( tok )); + free( buf ); + return array; +} + +static inline struct strarray strarray_frompath( const char *path ) +{ + if (!path) return empty_strarray; +#if defined(_WIN32) && !defined(__CYGWIN__) + return strarray_fromstring( path, ";" ); +#else + return strarray_fromstring( path, ":" ); +#endif +} + +static inline char *strarray_tostring( struct strarray array, const char *sep ) +{ + char *str; + unsigned int i, len = 1 + (array.count - 1) * strlen(sep); + + if (!array.count) return xstrdup(""); + for (i = 0; i < array.count; i++) len += strlen( array.str[i] ); + str = xmalloc( len ); + strcpy( str, array.str[0] ); + for (i = 1; i < array.count; i++) + { + strcat( str, sep ); + strcat( str, array.str[i] ); + } + return str; +} + +static inline void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) ) +{ + if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func ); +} + +static inline const char *strarray_bsearch( const struct strarray *array, const char *str, + int (*func)(const char **, const char **) ) +{ + char **res = NULL; + + if (array->count) res = bsearch( &str, array->str, array->count, sizeof(*array->str), (void *)func ); + return res ? *res : NULL; +} + + +#endif /* __WINE_TOOLS_H */ diff --git a/tools/widl/utils.c b/tools/widl/utils.c index 634bd12a0ba..c934657d286 100644 --- a/tools/widl/utils.c +++ b/tools/widl/utils.c @@ -198,53 +198,6 @@ size_t widl_getline(char **linep, size_t *lenp, FILE *fp) return n; } -void *xmalloc(size_t size) -{ - void *res; - - assert(size > 0); - res = malloc(size); - if(res == NULL) - { - error("Virtual memory exhausted.\n"); - } - memset(res, 0x55, size); - return res; -} - - -void *xrealloc(void *p, size_t size) -{ - void *res; - - assert(size > 0); - res = realloc(p, size); - if(res == NULL) - { - error("Virtual memory exhausted.\n"); - } - return res; -} - -char *strmake( const char* fmt, ... ) -{ - int n; - size_t size = 100; - va_list ap; - - for (;;) - { - char *p = xmalloc( size ); - va_start( ap, fmt ); - n = vsnprintf( p, size, fmt, ap ); - va_end( ap ); - if (n == -1) size *= 2; - else if ((size_t)n >= size) size = n + 1; - else return p; - free( p ); - } -} - size_t strappend(char **buf, size_t *len, size_t pos, const char* fmt, ...) { size_t size; @@ -282,22 +235,6 @@ size_t strappend(char **buf, size_t *len, size_t pos, const char* fmt, ...) return n; } -char *xstrdup(const char *str) -{ - char *s; - - assert(str != NULL); - s = xmalloc(strlen(str)+1); - 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 * diff --git a/tools/widl/utils.h b/tools/widl/utils.h index 82e0a6ae489..85f620080c2 100644 --- a/tools/widl/utils.h +++ b/tools/widl/utils.h @@ -23,19 +23,6 @@ #include "widltypes.h" -#include /* size_t */ - -void *xmalloc(size_t); -void *xrealloc(void *, size_t); -char *xstrdup(const char *str); -int strendswith(const char* str, const char* end); - -#ifndef __GNUC__ -#define __attribute__(X) -#endif - -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) - void parser_error(const char *s) __attribute__((noreturn)); int parser_warning(const char *s, ...) __attribute__((format (printf, 1, 2))); void error_loc(const char *s, ...) __attribute__((format (printf, 1, 2))) __attribute__((noreturn)); @@ -44,7 +31,6 @@ void error_loc_info(const loc_info_t *, const char *s, ...) __attribute__((forma void warning(const char *s, ...) __attribute__((format (printf, 1, 2))); void warning_loc_info(const loc_info_t *, const char *s, ...) __attribute__((format (printf, 2, 3))); void chat(const char *s, ...) __attribute__((format (printf, 1, 2))); -char *strmake(const char* fmt, ...) __attribute__((__format__ (__printf__, 1, 2 ))); size_t strappend(char **buf, size_t *len, size_t pos, const char* fmt, ...) __attribute__((__format__ (__printf__, 4, 5 ))); char *dup_basename(const char *name, const char *ext); @@ -79,11 +65,4 @@ extern void align_output( unsigned int align ); #define MAJORVERSION(version) ((version) & 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 diff --git a/tools/widl/widl.h b/tools/widl/widl.h index bc6226c54fe..6dc19b3dafa 100644 --- a/tools/widl/widl.h +++ b/tools/widl/widl.h @@ -21,6 +21,7 @@ #ifndef __WIDL_WIDL_H #define __WIDL_WIDL_H +#include "../tools.h" #include "widltypes.h" #include diff --git a/tools/winebuild/build.h b/tools/winebuild/build.h index a1646e737a4..ad893225265 100644 --- a/tools/winebuild/build.h +++ b/tools/winebuild/build.h @@ -29,16 +29,7 @@ #include #include -#include - -#ifndef max -#define max(a,b) (((a) > (b)) ? (a) : (b)) -#endif -#ifndef min -#define min(a,b) (((a) < (b)) ? (a) : (b)) -#endif - -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#include "../tools.h" typedef enum { @@ -169,13 +160,6 @@ static inline int is_pe(void) return target_platform == PLATFORM_MINGW || target_platform == PLATFORM_WINDOWS; } -struct strarray -{ - unsigned int count; /* strings in use */ - unsigned int size; /* total allocated size */ - const char **str; -}; - /* entry point flags */ #define FLAG_NORELAY 0x0001 /* don't use relay debugging for this function */ #define FLAG_NONAME 0x0002 /* don't export function by name */ @@ -229,10 +213,6 @@ struct strarray /* global functions */ -#ifndef __GNUC__ -#define __attribute__(X) -#endif - #ifndef DECLSPEC_NORETURN # if defined(_MSC_VER) && (_MSC_VER >= 1200) && !defined(MIDL_PASS) # define DECLSPEC_NORETURN __declspec(noreturn) @@ -240,19 +220,7 @@ struct strarray # define DECLSPEC_NORETURN __attribute__((noreturn)) # endif #endif - -extern void *xmalloc (size_t size); -extern void *xrealloc (void *ptr, size_t size); -extern char *xstrdup( const char *str ); extern char *strupper(char *s); -extern int strendswith(const char* str, const char* end); -extern char *strmake(const char* fmt, ...) __attribute__((__format__ (__printf__, 1, 2 ))); -extern struct strarray strarray_fromstring( const char *str, const char *delim ); -extern void strarray_add( struct strarray *array, const char *str ); -extern void strarray_addall( struct strarray *array, struct strarray args ); -extern void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) ); -extern const char *strarray_bsearch( const struct strarray *array, const char *str, - int (*func)(const char **, const char **) ); extern DECLSPEC_NORETURN void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2))); extern DECLSPEC_NORETURN void fatal_perror( const char *msg, ... ) diff --git a/tools/winebuild/utils.c b/tools/winebuild/utils.c index b7001e64a11..f6468312902 100644 --- a/tools/winebuild/utils.c +++ b/tools/winebuild/utils.c @@ -37,7 +37,6 @@ #include "build.h" static struct strarray tmp_files; -static struct strarray empty_strarray; static const char *output_file_source_name; static const struct @@ -71,41 +70,6 @@ void cleanup_tmp_files(void) } -void *xmalloc (size_t size) -{ - void *res; - - res = malloc (size ? size : 1); - if (res == NULL) - { - fprintf (stderr, "Virtual memory exhausted.\n"); - exit (1); - } - return res; -} - -void *xrealloc (void *ptr, size_t size) -{ - void *res = realloc (ptr, size); - if (size && res == NULL) - { - fprintf (stderr, "Virtual memory exhausted.\n"); - exit (1); - } - return res; -} - -char *xstrdup( const char *str ) -{ - char *res = strdup( str ); - if (!res) - { - fprintf (stderr, "Virtual memory exhausted.\n"); - exit (1); - } - return res; -} - char *strupper(char *s) { char *p; @@ -113,86 +77,6 @@ char *strupper(char *s) return s; } -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; -} - -char *strmake( const char* fmt, ... ) -{ - int n; - size_t size = 100; - va_list ap; - - for (;;) - { - char *p = xmalloc( size ); - va_start( ap, fmt ); - n = vsnprintf( p, size, fmt, ap ); - va_end( ap ); - if (n == -1) size *= 2; - else if ((size_t)n >= size) size = n + 1; - else return p; - free( p ); - } -} - -void strarray_add( struct strarray *array, const char *str ) -{ - if (array->count == array->size) - { - if (array->size) array->size *= 2; - else array->size = 16; - array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size ); - } - array->str[array->count++] = str; -} - -void strarray_addall( struct strarray *array, struct strarray args ) -{ - unsigned int i; - - for (i = 0; i < args.count; i++) strarray_add( array, args.str[i] ); -} - -struct strarray strarray_fromstring( const char *str, const char *delim ) -{ - const char *tok; - struct strarray array = empty_strarray; - char *buf = xstrdup( str ); - - for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim )) - strarray_add( &array, xstrdup( tok )); - free( buf ); - return array; -} - -static struct strarray strarray_frompath( const char *path ) -{ - if (!path) return empty_strarray; -#if defined(_WIN32) && !defined(__CYGWIN__) - return strarray_fromstring( path, ";" ); -#else - return strarray_fromstring( path, ":" ); -#endif -} - -void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) ) -{ - if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func ); -} - -const char *strarray_bsearch( const struct strarray *array, const char *str, - int (*func)(const char **, const char **) ) -{ - char **res = NULL; - - if (array->count) res = bsearch( &str, array->str, array->count, sizeof(*array->str), (void *)func ); - return res ? *res : NULL; -} - void fatal_error( const char *msg, ... ) { va_list valist; diff --git a/tools/winegcc/utils.c b/tools/winegcc/utils.c index 8bd20321d1f..95764289391 100644 --- a/tools/winegcc/utils.c +++ b/tools/winegcc/utils.c @@ -30,10 +30,6 @@ #include "utils.h" -#if !defined(min) -# define min(x,y) (((x) < (y)) ? (x) : (y)) -#endif - int verbose = 0; void error(const char* s, ...) @@ -47,116 +43,6 @@ void error(const char* s, ...) exit(2); } -void* xmalloc(size_t size) -{ - void* p; - - if ((p = malloc (size)) == NULL) - error("Could not malloc %d bytes\n", size); - - return p; -} - -void *xrealloc(void* p, size_t size) -{ - void* p2 = realloc (p, size); - if (size && !p2) - error("Could not realloc %d bytes\n", size); - - return p2; -} - -char *xstrdup( const char *str ) -{ - char *res = strdup( str ); - if (!res) error("Virtual memory exhausted.\n"); - return res; -} - -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; -} - -char* strmake(const char* fmt, ...) -{ - int n; - size_t size = 100; - va_list ap; - - while (1) - { - char *p = xmalloc (size); - va_start(ap, fmt); - n = vsnprintf (p, size, fmt, ap); - va_end(ap); - if (n == -1) size *= 2; - else if ((size_t)n >= size) size = n + 1; - else return p; - free(p); - } -} - -void strarray_add( struct strarray *array, const char *str ) -{ - if (array->count == array->size) - { - if (array->size) array->size *= 2; - else array->size = 16; - array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size ); - } - array->str[array->count++] = str; -} - -void strarray_addall( struct strarray *array, struct strarray added ) -{ - unsigned int i; - - for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] ); -} - -struct strarray strarray_fromstring( const char *str, const char *delim ) -{ - struct strarray array = empty_strarray; - char *buf = xstrdup( str ); - const char *tok; - - for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim )) - strarray_add( &array, xstrdup( tok )); - free( buf ); - return array; -} - -static struct strarray strarray_frompath( const char *path ) -{ - if (!path) return empty_strarray; -#if defined(_WIN32) && !defined(__CYGWIN__) - return strarray_fromstring( path, ";" ); -#else - return strarray_fromstring( path, ":" ); -#endif -} - -char *strarray_tostring( struct strarray array, const char *sep ) -{ - char *str; - unsigned int i, len = 1 + (array.count - 1) * strlen(sep); - - if (!array.count) return xstrdup(""); - for (i = 0; i < array.count; i++) len += strlen( array.str[i] ); - str = xmalloc( len ); - strcpy( str, array.str[0] ); - for (i = 1; i < array.count; i++) - { - strcat( str, sep ); - strcat( str, array.str[i] ); - } - return str; -} - char* get_basename(const char* file) { const char* name; diff --git a/tools/winegcc/utils.h b/tools/winegcc/utils.h index 09bda31e8a9..72ec2abbe2b 100644 --- a/tools/winegcc/utils.h +++ b/tools/winegcc/utils.h @@ -20,10 +20,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ - -#ifndef __GNUC__ -#define __attribute__(X) -#endif +#include "../tools.h" #ifndef DECLSPEC_NORETURN # if defined(_MSC_VER) && (_MSC_VER >= 1200) && !defined(MIDL_PASS) @@ -35,8 +32,6 @@ # endif #endif -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) - enum target_cpu { CPU_x86, CPU_x86_64, CPU_POWERPC, CPU_ARM, CPU_ARM64 @@ -55,26 +50,6 @@ enum target_platform void DECLSPEC_NORETURN error(const char* s, ...); -void* xmalloc(size_t size); -void* xrealloc(void* p, size_t size); -char *xstrdup( const char *str ); -char* strmake(const char* fmt, ...) __attribute__((__format__ (__printf__, 1, 2 ))); -int strendswith(const char* str, const char* end); - -struct strarray -{ - unsigned int count; /* strings in use */ - unsigned int size; /* total allocated size */ - const char **str; -}; - -static const struct strarray empty_strarray; - -void strarray_add( struct strarray *array, const char *str ); -void strarray_addall( struct strarray *array, struct strarray added ); -struct strarray strarray_fromstring( const char *str, const char *delim ); -char *strarray_tostring( struct strarray array, const char *sep ); - typedef enum { file_na, file_other, file_obj, file_res, file_rc, file_arh, file_dll, file_so, file_def, file_spec diff --git a/tools/wmc/utils.c b/tools/wmc/utils.c index bd01f6cfe79..2a96fcd85e5 100644 --- a/tools/wmc/utils.c +++ b/tools/wmc/utils.c @@ -145,69 +145,6 @@ char *dup_basename(const char *name, const char *ext) return base; } -void *xmalloc(size_t size) -{ - void *res; - - assert(size > 0); - res = malloc(size); - if(res == NULL) - { - error("Virtual memory exhausted.\n"); - } - memset(res, 0x55, size); - return res; -} - - -void *xrealloc(void *p, size_t size) -{ - void *res; - - assert(size > 0); - res = realloc(p, size); - if(res == NULL) - { - error("Virtual memory exhausted.\n"); - } - return res; -} - -char *xstrdup(const char *str) -{ - char *s; - - assert(str != NULL); - s = xmalloc(strlen(str)+1); - return strcpy(s, str); -} - -char *strmake( const char* fmt, ... ) -{ - int n; - size_t size = 100; - va_list ap; - - for (;;) - { - char *p = xmalloc( size ); - va_start( ap, fmt ); - n = vsnprintf( p, size, fmt, ap ); - va_end( ap ); - if (n == -1) size *= 2; - else if ((size_t)n >= size) size = n + 1; - else return p; - free( p ); - } -} - -int strendswith( const char *str, const char *end ) -{ - int l = strlen(str); - int m = strlen(end); - return l >= m && !strcmp( str + l - m, end ); -} - int unistrlen(const WCHAR *s) { int n; diff --git a/tools/wmc/utils.h b/tools/wmc/utils.h index 726a36731ab..f479dbada16 100644 --- a/tools/wmc/utils.h +++ b/tools/wmc/utils.h @@ -21,19 +21,9 @@ #ifndef __WMC_UTILS_H #define __WMC_UTILS_H -#include /* size_t */ - +#include "../tools.h" #include "wmctypes.h" -void *xmalloc(size_t); -void *xrealloc(void *, size_t); -char *xstrdup(const char *str); - -#ifndef __GNUC__ -#define __attribute__(X) -#endif - -char *strmake(const char* fmt, ...) __attribute__((__format__ (__printf__, 1, 2 ))); int mcy_error(const char *s, ...) __attribute__((format (printf, 1, 2))); int xyyerror(const char *s, ...) __attribute__((format (printf, 1, 2))); int mcy_warning(const char *s, ...) __attribute__((format (printf, 1, 2))); @@ -43,7 +33,6 @@ void error(const char *s, ...) __attribute__((format (printf, 1, 2))); void warning(const char *s, ...) __attribute__((format (printf, 1, 2))); char *dup_basename(const char *name, const char *ext); -int strendswith( const char *str, const char *end ); WCHAR *xunistrdup(const WCHAR * str); WCHAR *unistrcpy(WCHAR *dst, const WCHAR *src); diff --git a/tools/wrc/utils.c b/tools/wrc/utils.c index 52b6f6f0d12..b37463ee6bf 100644 --- a/tools/wrc/utils.c +++ b/tools/wrc/utils.c @@ -153,69 +153,6 @@ char *dup_basename(const char *name, const char *ext) return base; } -void *xmalloc(size_t size) -{ - void *res; - - assert(size > 0); - res = malloc(size); - if(res == NULL) - { - error("Virtual memory exhausted.\n"); - } - memset(res, 0x55, size); - return res; -} - - -void *xrealloc(void *p, size_t size) -{ - void *res; - - assert(size > 0); - res = realloc(p, size); - if(res == NULL) - { - error("Virtual memory exhausted.\n"); - } - return res; -} - -char *strmake( const char* fmt, ... ) -{ - int n; - size_t size = 100; - va_list ap; - - for (;;) - { - char *p = xmalloc( size ); - va_start( ap, fmt ); - n = vsnprintf( p, size, fmt, ap ); - va_end( ap ); - if (n == -1) size *= 2; - else if ((size_t)n >= size) size = n + 1; - else return p; - free( p ); - } -} - -char *xstrdup(const char *str) -{ - char *s; - - assert(str != NULL); - s = xmalloc(strlen(str)+1); - 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 ); -} - int compare_striA( const char *str1, const char *str2 ) { for (;;) diff --git a/tools/wrc/utils.h b/tools/wrc/utils.h index af182173361..d5f608b3650 100644 --- a/tools/wrc/utils.h +++ b/tools/wrc/utils.h @@ -21,22 +21,10 @@ #ifndef __WRC_UTILS_H #define __WRC_UTILS_H -#include /* size_t */ - #include "wrctypes.h" -void *xmalloc(size_t); -void *xrealloc(void *, size_t); -char *xstrdup(const char *str); - -#ifndef __GNUC__ -#define __attribute__(X) -#endif - int compare_striA( const char *str1, const char *str2 ); int compare_striW( const WCHAR *str1, const WCHAR *str2 ); -char *strmake(const char* fmt, ...) __attribute__((__format__ (__printf__, 1, 2 ))); -int strendswith( const char *str, const char *end ); int parser_error(const char *s, ...) __attribute__((format (printf, 1, 2))); int parser_warning(const char *s, ...) __attribute__((format (printf, 1, 2))); void fatal_perror( const char *msg, ... ) __attribute__((format (printf, 1, 2), noreturn)); diff --git a/tools/wrc/wpp_private.h b/tools/wrc/wpp_private.h index 435dbcc005a..be118b020bd 100644 --- a/tools/wrc/wpp_private.h +++ b/tools/wrc/wpp_private.h @@ -22,6 +22,7 @@ #include #include +#include "../tools.h" #include "wine/list.h" extern void wpp_del_define( const char *name ); diff --git a/tools/wrc/wrc.h b/tools/wrc/wrc.h index d086c5c60f9..4c572074e5e 100644 --- a/tools/wrc/wrc.h +++ b/tools/wrc/wrc.h @@ -21,6 +21,7 @@ #ifndef __WRC_WRC_H #define __WRC_WRC_H +#include "../tools.h" #include "wrctypes.h" /* From wrc.c */