Commit 2064181c authored by Alexandre Julliard's avatar Alexandre Julliard

winegcc: Unify the strarray implementation with the one from makedep.

parent c878a0a4
...@@ -34,12 +34,6 @@ ...@@ -34,12 +34,6 @@
# define min(x,y) (((x) < (y)) ? (x) : (y)) # define min(x,y) (((x) < (y)) ? (x) : (y))
#endif #endif
#if defined(_WIN32) && !defined(__CYGWIN__)
# define PATH_SEPARATOR ';'
#else
# define PATH_SEPARATOR ':'
#endif
int verbose = 0; int verbose = 0;
void error(const char* s, ...) void error(const char* s, ...)
...@@ -83,8 +77,8 @@ int strendswith(const char* str, const char* end) ...@@ -83,8 +77,8 @@ int strendswith(const char* str, const char* end)
{ {
int l = strlen(str); int l = strlen(str);
int m = strlen(end); int m = strlen(end);
return l >= m && strcmp(str + l - m, end) == 0; return l >= m && strcmp(str + l - m, end) == 0;
} }
char* strmake(const char* fmt, ...) char* strmake(const char* fmt, ...)
...@@ -106,82 +100,60 @@ char* strmake(const char* fmt, ...) ...@@ -106,82 +100,60 @@ char* strmake(const char* fmt, ...)
} }
} }
strarray* strarray_alloc(void) void strarray_add( struct strarray *array, const char *str )
{
strarray* arr = xmalloc(sizeof(*arr));
arr->maximum = arr->size = 0;
arr->base = NULL;
return arr;
}
void strarray_free(strarray* arr)
{
free(arr->base);
free(arr);
}
void strarray_add(strarray* arr, const char* str)
{ {
if (arr->size == arr->maximum) if (array->count == array->size)
{ {
arr->maximum += 10; if (array->size) array->size *= 2;
arr->base = xrealloc(arr->base, sizeof(*(arr->base)) * arr->maximum); else array->size = 16;
array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
} }
arr->base[arr->size++] = str; array->str[array->count++] = str;
}
void strarray_del(strarray* arr, unsigned int i)
{
if (i >= arr->size) error("Invalid index i=%d\n", i);
memmove(&arr->base[i], &arr->base[i + 1], (arr->size - i - 1) * sizeof(arr->base[0]));
arr->size--;
} }
void strarray_addall(strarray* arr, const strarray* from) void strarray_addall( struct strarray *array, struct strarray added )
{ {
unsigned int i; unsigned int i;
for (i = 0; i < from->size; i++) for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
strarray_add(arr, from->base[i]);
} }
strarray* strarray_dup(const strarray* arr) struct strarray strarray_fromstring( const char *str, const char *delim )
{ {
strarray* dup = strarray_alloc(); struct strarray array = empty_strarray;
unsigned int i; char *buf = xstrdup( str );
const char *tok;
for (i = 0; i < arr->size; i++)
strarray_add(dup, arr->base[i]); for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim ))
strarray_add( &array, xstrdup( tok ));
return dup; free( buf );
return array;
} }
strarray* strarray_fromstring(const char* str, const char* delim) static struct strarray strarray_frompath( const char *path )
{ {
strarray* arr = strarray_alloc(); if (!path) return empty_strarray;
char* buf = strdup(str); #if defined(_WIN32) && !defined(__CYGWIN__)
const char* tok; return strarray_fromstring( path, ";" );
#else
for(tok = strtok(buf, delim); tok; tok = strtok(0, delim)) return strarray_fromstring( path, ":" );
strarray_add(arr, strdup(tok)); #endif
free(buf);
return arr;
} }
char* strarray_tostring(const strarray* arr, const char* sep) char *strarray_tostring( struct strarray array, const char *sep )
{ {
char *str, *newstr; char *str;
unsigned int i; unsigned int i, len = 1 + (array.count - 1) * strlen(sep);
str = strmake("%s", arr->base[0]); if (!array.count) return xstrdup("");
for (i = 1; i < arr->size; i++) 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++)
{ {
newstr = strmake("%s%s%s", str, sep, arr->base[i]); strcat( str, sep );
free(str); strcat( str, array.str[i] );
str = newstr;
} }
return str; return str;
} }
...@@ -296,24 +268,25 @@ static file_type guess_lib_type(enum target_platform platform, const char* dir, ...@@ -296,24 +268,25 @@ static file_type guess_lib_type(enum target_platform platform, const char* dir,
return file_na; return file_na;
} }
file_type get_lib_type(enum target_platform platform, strarray* path, const char *library, file_type get_lib_type(enum target_platform platform, struct strarray path, const char *library,
const char *prefix, const char *suffix, char** file) const char *prefix, const char *suffix, char** file)
{ {
unsigned int i; unsigned int i;
if (!suffix) suffix = ".a"; if (!suffix) suffix = ".a";
for (i = 0; i < path->size; i++) for (i = 0; i < path.count; i++)
{ {
file_type type = guess_lib_type(platform, path->base[i], library, prefix, suffix, file); file_type type = guess_lib_type(platform, path.str[i], library, prefix, suffix, file);
if (type != file_na) return type; if (type != file_na) return type;
} }
return file_na; return file_na;
} }
const char *find_binary( const strarray* prefix, const char *name ) const char *find_binary( struct strarray prefix, const char *name )
{ {
char *file_name, *args; char *file_name, *args;
static strarray *path; struct strarray dirs = empty_strarray;
static struct strarray path;
unsigned int i; unsigned int i;
if (strchr( name, '/' )) return name; if (strchr( name, '/' )) return name;
...@@ -321,47 +294,29 @@ const char *find_binary( const strarray* prefix, const char *name ) ...@@ -321,47 +294,29 @@ const char *find_binary( const strarray* prefix, const char *name )
file_name = xstrdup( name ); file_name = xstrdup( name );
if ((args = strchr( file_name, ' ' ))) *args++ = 0; if ((args = strchr( file_name, ' ' ))) *args++ = 0;
if (prefix) if (!path.count) strarray_addall( &path, strarray_frompath( getenv( "PATH" )));
{
for (i = 0; i < prefix->size; i++) strarray_addall( &dirs, prefix );
{ strarray_addall( &dirs, path );
struct stat st; for (i = 0; i < dirs.count; i++)
char *prog = strmake( "%s/%s%s", prefix->base[i], file_name, EXEEXT );
if (stat( prog, &st ) == 0 && S_ISREG( st.st_mode ) && (st.st_mode & 0111))
return args ? strmake( "%s %s", prog, args ) : prog;
free( prog );
}
}
if (!path)
{ {
path = strarray_alloc(); struct stat st;
char *prog = strmake( "%s/%s%s", dirs.str[i], file_name, EXEEXT );
/* then append the PATH directories */ if (stat( prog, &st ) == 0 && S_ISREG( st.st_mode ) && (st.st_mode & 0111))
if (getenv( "PATH" )) return args ? strmake( "%s %s", prog, args ) : prog;
{ free( prog );
char *p = xstrdup( getenv( "PATH" ));
while (*p)
{
strarray_add( path, p );
while (*p && *p != PATH_SEPARATOR) p++;
if (!*p) break;
*p++ = 0;
}
}
} }
return prefix == path ? NULL : find_binary( path, name ); return NULL;
} }
int spawn(const strarray* prefix, const strarray* args, int ignore_errors) int spawn(struct strarray prefix, struct strarray args, int ignore_errors)
{ {
unsigned int i; unsigned int i;
int status; int status;
strarray* arr = strarray_dup(args);
const char** argv; const char** argv;
char* prog = 0;
strarray_add(arr, NULL); strarray_add( &args, NULL);
argv = arr->base; argv = args.str;
argv[0] = find_binary( prefix, argv[0] ); argv[0] = find_binary( prefix, argv[0] );
if (verbose) if (verbose)
...@@ -381,7 +336,5 @@ int spawn(const strarray* prefix, const strarray* args, int ignore_errors) ...@@ -381,7 +336,5 @@ int spawn(const strarray* prefix, const strarray* args, int ignore_errors)
exit(3); exit(3);
} }
free(prog);
strarray_free(arr);
return status; return status;
} }
...@@ -61,20 +61,19 @@ char *xstrdup( const char *str ); ...@@ -61,20 +61,19 @@ char *xstrdup( const char *str );
char* strmake(const char* fmt, ...) __attribute__((__format__ (__printf__, 1, 2 ))); char* strmake(const char* fmt, ...) __attribute__((__format__ (__printf__, 1, 2 )));
int strendswith(const char* str, const char* end); int strendswith(const char* str, const char* end);
typedef struct { struct strarray
size_t maximum; {
size_t size; unsigned int count; /* strings in use */
const char** base; unsigned int size; /* total allocated size */
} strarray; const char **str;
};
static const struct strarray empty_strarray;
strarray* strarray_alloc(void); void strarray_add( struct strarray *array, const char *str );
strarray* strarray_dup(const strarray* arr); void strarray_addall( struct strarray *array, struct strarray added );
void strarray_free(strarray* arr); struct strarray strarray_fromstring( const char *str, const char *delim );
void strarray_add(strarray* arr, const char* str); char *strarray_tostring( struct strarray array, const char *sep );
void strarray_del(strarray* arr, unsigned int i);
void strarray_addall(strarray* arr, const strarray* from);
strarray* strarray_fromstring(const char* str, const char* delim);
char* strarray_tostring(const strarray* arr, const char* sep);
typedef enum { typedef enum {
file_na, file_other, file_obj, file_res, file_rc, file_na, file_other, file_obj, file_res, file_rc,
...@@ -84,9 +83,9 @@ typedef enum { ...@@ -84,9 +83,9 @@ typedef enum {
char* get_basename(const char* file); char* get_basename(const char* file);
void create_file(const char* name, int mode, const char* fmt, ...); void create_file(const char* name, int mode, const char* fmt, ...);
file_type get_file_type(const char* filename); file_type get_file_type(const char* filename);
file_type get_lib_type(enum target_platform platform, strarray* path, const char *library, file_type get_lib_type(enum target_platform platform, struct strarray path, const char *library,
const char *prefix, const char *suffix, char** file); const char *prefix, const char *suffix, char** file);
const char *find_binary( const strarray* prefix, const char *name ); const char *find_binary( struct strarray prefix, const char *name );
int spawn(const strarray* prefix, const strarray* arr, int ignore_errors); int spawn(struct strarray prefix, struct strarray arr, int ignore_errors);
extern int verbose; extern int verbose;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment