tools: Add a few helper functions for file names and extensions.

Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Alexandre Julliard 2021-09-29 11:13:36 +02:00
parent 55701c6672
commit 9e3959bd9b
24 changed files with 88 additions and 282 deletions

View File

@ -534,16 +534,7 @@ int main(int argc, char **argv)
if (!option_inc_var_name)
fatal_error( "variable name must be specified with -i or #pragma name\n" );
header_name = strrchr(option_outfile_name, '/');
if (header_name)
header_name = xstrdup(header_name + 1);
else
header_name = xstrdup(option_outfile_name);
if (!header_name) {
fprintf(stderr, "Out of memory\n");
goto error;
}
header_name = get_basename( option_outfile_name );
str_ptr = header_name;
while (*str_ptr) {
if (*str_ptr == '.')

View File

@ -478,23 +478,6 @@ static const char *get_base_name( const char *name )
}
/*******************************************************************
* replace_extension
*/
static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
{
char *ret;
size_t name_len = strlen( name );
size_t ext_len = strlen( old_ext );
if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
ret = xmalloc( name_len + strlen( new_ext ) + 1 );
memcpy( ret, name, name_len );
strcpy( ret + name_len, new_ext );
return ret;
}
/*******************************************************************
* replace_filename
*/

View File

@ -971,16 +971,8 @@ int main(int argc, char **argv)
#endif
if (!option_output) /* build a default output name */
{
char *p = strrchr( input_file, '/' );
if (p) p++;
else p = input_file;
option_output = xmalloc( strlen(p) + sizeof(".fon") );
strcpy( option_output, p );
p = strrchr( option_output, '.' );
if (!p) p = option_output + strlen(option_output);
strcpy( p, option_fnt_mode ? ".fnt" : ".fon" );
}
option_output = strmake( "%s%s", get_basename_noext( input_file ),
option_fnt_mode ? ".fnt" : ".fon" );
if (!(ofp = fopen(option_output, "wb")))
{

View File

@ -251,6 +251,36 @@ static inline int strarray_spawn( struct strarray args )
#endif
}
static inline char *get_basename( const char *file )
{
const char *ret = strrchr( file, '/' );
return xstrdup( ret ? ret + 1 : file );
}
static inline char *get_basename_noext( const char *file )
{
char *ext, *ret = get_basename( file );
if ((ext = strrchr( ret, '.' ))) *ext = 0;
return ret;
}
static inline char *get_dirname( const char *file )
{
const char *end = strrchr( file, '/' );
if (!end) return xstrdup( "." );
if (end == file) end++;
return strmake( "%.*s", (int)(end - file), file );
}
static inline char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
{
int name_len = strlen( name );
if (strendswith( name, old_ext )) name_len -= strlen( old_ext );
return strmake( "%.*s%s", name_len, name, new_ext );
}
static inline int make_temp_file( const char *prefix, const char *suffix, char **name )
{
static unsigned int value;

View File

@ -1929,12 +1929,9 @@ static void write_runtimeclass_forward(FILE *header, type_t *runtimeclass)
static void write_import(FILE *header, const char *fname)
{
char *hname, *p;
hname = dup_basename(fname, ".idl");
p = hname + strlen(hname) - 2;
if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
char *hname = replace_extension( get_basename(fname), ".idl", "" );
if (!strendswith( hname, ".h" )) hname = strmake( "%s.h", hname );
fprintf(header, "#include <%s>\n", hname);
free(hname);
}

View File

@ -2262,7 +2262,7 @@ char *gen_name(void)
if (! file_id)
{
char *dst = dup_basename(input_idl_name, ".idl");
char *dst = replace_extension( get_basename(input_idl_name), ".idl", "" );
file_id = dst;
for (; *dst; ++dst)

View File

@ -340,17 +340,8 @@ static void read_importlib(importlib_t *importlib)
fd = open_typelib(importlib->name);
/* widl extension: if importlib name has no .tlb extension, try using .tlb */
if(fd < 0) {
const char *p = strrchr(importlib->name, '.');
size_t len = p ? p - importlib->name : strlen(importlib->name);
if(strcmp(importlib->name + len, ".tlb")) {
char *tlb_name = xmalloc(len + 5);
memcpy(tlb_name, importlib->name, len);
strcpy(tlb_name + len, ".tlb");
fd = open_typelib(tlb_name);
free(tlb_name);
}
}
if (fd < 0 && !strendswith( importlib->name, ".tlb" ))
fd = open_typelib( strmake( "%s.tlb", importlib->name ));
if(fd < 0)
error("Could not find importlib %s.\n", importlib->name);

View File

@ -140,35 +140,6 @@ void chat(const char *s, ...)
}
}
char *dup_basename(const char *name, const char *ext)
{
int namelen;
int extlen = strlen(ext);
char *base;
char *slash;
if(!name)
name = "widl.tab";
slash = strrchr(name, '/');
if (!slash)
slash = strrchr(name, '\\');
if (slash)
name = slash + 1;
namelen = strlen(name);
/* +6 for later extension (strlen("_r.rgs")) and +1 for '\0' */
base = xmalloc(namelen +6 +1);
strcpy(base, name);
if(!strcasecmp(name + namelen-extlen, ext))
{
base[namelen - extlen] = '\0';
}
return base;
}
size_t widl_getline(char **linep, size_t *lenp, FILE *fp)
{
char *line = *linep;

View File

@ -33,7 +33,6 @@ void warning_loc_info(const loc_info_t *, const char *s, ...) __attribute__((for
void chat(const char *s, ...) __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);
size_t widl_getline(char **linep, size_t *lenp, FILE *fp);
UUID *parse_uuid(const char *u);

View File

@ -248,16 +248,9 @@ enum stub_mode get_stub_mode(void)
static char *make_token(const char *name)
{
char *token;
char *slash;
int i;
slash = strrchr(name, '/');
if(!slash)
slash = strrchr(name, '\\');
if (slash) name = slash + 1;
token = xstrdup(name);
token = get_basename( name );
for (i=0; token[i]; i++) {
if (!isalnum(token[i])) token[i] = '_';
else token[i] = tolower(token[i]);
@ -268,7 +261,7 @@ static char *make_token(const char *name)
/* duplicate a basename into a valid C token */
static char *dup_basename_token(const char *name, const char *ext)
{
char *p, *ret = dup_basename( name, ext );
char *p, *ret = replace_extension( get_basename(name), ext, "" );
/* map invalid characters to '_' */
for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
return ret;
@ -318,14 +311,8 @@ static void set_cpu( const char *cpu, int error_out )
* If not found, or not matching a known CPU name, just proceed silently. */
static void init_argv0_target( const char *argv0 )
{
char *p, *name;
char *p, *name = get_basename( argv0 );
if ((p = strrchr(argv0, '/')) != NULL)
argv0 = p + 1;
if ((p = strrchr(argv0, '\\')) != NULL)
argv0 = p + 1;
name = xstrdup( argv0 );
if (!(p = strchr(name, '-')))
{
free( name );
@ -390,7 +377,7 @@ typedef struct
static void add_filename_node(struct list *list, const char *name)
{
filename_node_t *node = xmalloc(sizeof *node);
node->filename = dup_basename( name, ".idl" );
node->filename = replace_extension( get_basename( name ), ".idl", "" );
list_add_tail(list, &node->link);
}
@ -603,7 +590,7 @@ void write_id_data(const statement_list_t *stmts)
static void init_argv0_dir( const char *argv0 )
{
#ifndef _WIN32
char *p, *dir;
char *dir;
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
dir = realpath( "/proc/self/exe", NULL );
@ -613,11 +600,7 @@ static void init_argv0_dir( const char *argv0 )
dir = realpath( argv0, NULL );
#endif
if (!dir) return;
if (!(p = strrchr( dir, '/' ))) return;
if (p == dir) p++;
*p = 0;
includedir = strmake( "%s/%s", dir, BIN_TO_INCLUDEDIR );
free( dir );
includedir = strmake( "%s/%s", get_dirname( dir ), BIN_TO_INCLUDEDIR );
#endif
}
@ -915,40 +898,26 @@ int main(int argc,char *argv[])
(debuglevel & DEBUGLEVEL_PPTRACE) != 0,
(debuglevel & DEBUGLEVEL_PPMSG) != 0 );
if (!header_name) {
header_name = dup_basename(input_name, ".idl");
strcat(header_name, ".h");
}
if (!header_name)
header_name = replace_extension( get_basename(input_name), ".idl", ".h" );
if (!typelib_name && do_typelib) {
typelib_name = dup_basename(input_name, ".idl");
strcat(typelib_name, ".tlb");
}
if (!typelib_name && do_typelib)
typelib_name = replace_extension( get_basename(input_name), ".idl", ".tlb" );
if (!proxy_name && do_proxies) {
proxy_name = dup_basename(input_name, ".idl");
strcat(proxy_name, "_p.c");
}
if (!proxy_name && do_proxies)
proxy_name = replace_extension( get_basename(input_name), ".idl", "_p.c" );
if (!client_name && do_client) {
client_name = dup_basename(input_name, ".idl");
strcat(client_name, "_c.c");
}
if (!client_name && do_client)
client_name = replace_extension( get_basename(input_name), ".idl", "_c.c" );
if (!server_name && do_server) {
server_name = dup_basename(input_name, ".idl");
strcat(server_name, "_s.c");
}
if (!server_name && do_server)
server_name = replace_extension( get_basename(input_name), ".idl", "_s.c" );
if (!regscript_name && do_regscript) {
regscript_name = dup_basename(input_name, ".idl");
strcat(regscript_name, "_r.rgs");
}
if (!regscript_name && do_regscript)
regscript_name = replace_extension( get_basename(input_name), ".idl", "_r.rgs" );
if (!idfile_name && do_idfile) {
idfile_name = dup_basename(input_name, ".idl");
strcat(idfile_name, "_i.c");
}
if (!idfile_name && do_idfile)
idfile_name = replace_extension( get_basename(input_name), ".idl", "_i.c" );
if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
if (do_client) client_token = dup_basename_token(client_name,"_c.c");

View File

@ -329,9 +329,7 @@ static char *get_dll_name( const char *name, const char *filename )
if (filename)
{
const char *basename = strrchr( filename, '/' );
if (!basename) basename = filename;
else basename++;
const char *basename = get_basename( filename );
if (!strncmp( basename, "lib", 3 )) basename += 3;
ret = xmalloc( strlen(basename) + 5 );
strcpy( ret, basename );

View File

@ -151,8 +151,7 @@ static void set_dll_file_name( const char *name, DLLSPEC *spec )
if (spec->file_name) return;
if ((p = strrchr( name, '\\' ))) name = p + 1;
if ((p = strrchr( name, '/' ))) name = p + 1;
name = get_basename( name );
spec->file_name = xmalloc( strlen(name) + 5 );
strcpy( spec->file_name, name );
if ((p = strrchr( spec->file_name, '.' )))
@ -695,11 +694,10 @@ static struct strarray load_import_libs( struct strarray files )
static int parse_input_file( DLLSPEC *spec )
{
FILE *input_file = open_input_file( NULL, spec_file_name );
char *extension = strrchr( spec_file_name, '.' );
int result;
spec->src_name = xstrdup( input_file_name );
if (extension && !strcmp( extension, ".def" ))
if (strendswith( spec_file_name, ".def" ))
result = parse_def_file( input_file, spec );
else
result = parse_spec_file( input_file, spec );

View File

@ -438,16 +438,9 @@ const char *get_nm_command(void)
char *get_temp_file_name( const char *prefix, const char *suffix )
{
char *name;
const char *ext, *basename;
int fd;
if (prefix)
{
if ((basename = strrchr( prefix, '/' ))) basename++;
else basename = prefix;
if ((ext = strchr( basename, '.' ))) prefix = strmake( "%.*s", ext - basename, basename );
else prefix = basename;
}
if (prefix) prefix = get_basename_noext( prefix );
fd = make_temp_file( prefix, suffix, &name );
close( fd );
strarray_add( &tmp_files, name );

View File

@ -43,20 +43,6 @@ void error(const char* s, ...)
exit(2);
}
char* get_basename(const char* file)
{
const char* name;
char *base_name, *p;
if ((name = strrchr(file, '/'))) name++;
else name = file;
base_name = strdup(name);
if ((p = strrchr(base_name, '.'))) *p = 0;
return base_name;
}
void create_file(const char* name, int mode, const char* fmt, ...)
{
va_list ap;

View File

@ -55,7 +55,6 @@ typedef enum {
file_arh, file_dll, file_so, file_def, file_spec
} file_type;
char* get_basename(const char* file);
void create_file(const char* name, int mode, const char* fmt, ...);
file_type get_file_type(const char* filename);
file_type get_lib_type(enum target_platform platform, struct strarray path, const char *library,

View File

@ -743,7 +743,7 @@ static char *get_lib_dir( struct options *opts )
static void init_argv0_dir( const char *argv0 )
{
#ifndef _WIN32
char *p, *dir;
char *dir;
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
dir = realpath( "/proc/self/exe", NULL );
@ -753,12 +753,9 @@ static void init_argv0_dir( const char *argv0 )
dir = realpath( argv0, NULL );
#endif
if (!dir) return;
if (!(p = strrchr( dir, '/' ))) return;
if (p == dir) p++;
*p = 0;
bindir = dir;
includedir = strmake( "%s/%s", dir, BIN_TO_INCLUDEDIR );
libdir = strmake( "%s/%s", dir, BIN_TO_LIBDIR );
bindir = get_dirname( dir );
includedir = strmake( "%s/%s", bindir, BIN_TO_INCLUDEDIR );
libdir = strmake( "%s/%s", bindir, BIN_TO_LIBDIR );
#endif
}
@ -943,19 +940,15 @@ no_compat_defines:
static const char* compile_to_object(struct options* opts, const char* file, const char* lang)
{
struct options copts;
char* base_name;
/* make a copy so we don't change any of the initial stuff */
/* a shallow copy is exactly what we want in this case */
base_name = get_basename(file);
copts = *opts;
copts.output_name = get_temp_file(base_name, ".o");
copts.output_name = get_temp_file(get_basename_noext(file), ".o");
copts.compile_only = 1;
copts.files = empty_strarray;
strarray_add(&copts.files, file);
compile(&copts, lang);
free(base_name);
return copts.output_name;
}
@ -1104,8 +1097,7 @@ static const char *build_spec_obj( struct options *opts, const char *spec_file,
int fake_module = strendswith(output_file, ".fake");
/* get the filename from the path */
if ((output_name = strrchr(output_file, '/'))) output_name++;
else output_name = output_file;
output_name = get_basename( output_file );
tool = build_tool_name( opts, TOOL_CC );
strarray_add( &spec_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
@ -1231,17 +1223,14 @@ static void build(struct options* opts)
if (strendswith(output_file, ".fake")) fake_module = 1;
/* normalize the filename a bit: strip .so, ensure it has proper ext */
if ((output_name = strrchr(output_file, '/'))) output_name++;
else output_name = output_file;
if (!strchr(output_name, '.'))
if (!strchr(get_basename( output_file ), '.'))
output_file = strmake("%s.%s", output_file, opts->shared ? "dll" : "exe");
else if (strendswith(output_file, ".so"))
output_file[strlen(output_file) - 3] = 0;
output_path = is_pe ? output_file : strmake( "%s.so", output_file );
/* get the filename from the path */
if ((output_name = strrchr(output_file, '/'))) output_name++;
else output_name = output_file;
output_name = get_basename( output_file );
/* prepare the linking path */
if (!opts->wine_objdir)
@ -1286,9 +1275,7 @@ static void build(struct options* opts)
case file_arh:
if (opts->use_msvcrt)
{
const char *p = strrchr(file, '/');
if (p) p++;
else p = file;
char *p = get_basename( file );
if (!strncmp(p, "libmsvcr", 8) || !strncmp(p, "libucrt", 7)) crt_lib = file;
}
strarray_add(&files, strmake("-a%s", file));
@ -1421,22 +1408,14 @@ static void build(struct options* opts)
/* turn the path back into -Ldir -lfoo options
* this makes sure that we use the specified libs even
* when mingw adds its own import libs to the link */
char *lib = xstrdup( name );
char *p = strrchr( lib, '/' );
const char *p = get_basename( name );
*p++ = 0;
if (!strncmp( p, "lib", 3 ) && strcmp( p, "libmsvcrt.a" ))
{
char *ext = strrchr( p, '.' );
if (ext) *ext = 0;
p += 3;
strarray_add(&link_args, strmake("-L%s", lib ));
strarray_add(&link_args, strmake("-l%s", p ));
free( lib );
strarray_add(&link_args, strmake("-L%s", get_dirname(name) ));
strarray_add(&link_args, strmake("-l%s", get_basename_noext( p + 3 )));
break;
}
free( lib );
}
strarray_add(&link_args, name);
break;

View File

@ -119,32 +119,6 @@ void warning(const char *s, ...)
va_end(ap);
}
char *dup_basename(const char *name, const char *ext)
{
int namelen;
int extlen = strlen(ext);
char *base;
char *slash;
if(!name)
name = "wmc.tab";
slash = strrchr(name, '/');
if (slash)
name = slash + 1;
namelen = strlen(name);
/* +4 for later extension and +1 for '\0' */
base = xmalloc(namelen +4 +1);
strcpy(base, name);
if(!strcasecmp(name + namelen-extlen, ext))
{
base[namelen - extlen] = '\0';
}
return base;
}
int unistrlen(const WCHAR *s)
{
int n;

View File

@ -32,8 +32,6 @@ void fatal_perror( const char *msg, ... ) __attribute__((format (printf, 1, 2),
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);
WCHAR *xunistrdup(const WCHAR * str);
WCHAR *unistrcpy(WCHAR *dst, const WCHAR *src);
int unistrlen(const WCHAR *s);

View File

@ -163,7 +163,7 @@ static void exit_on_signal( int sig )
static void init_argv0_dir( const char *argv0 )
{
#ifndef _WIN32
char *p, *dir;
char *dir;
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
dir = realpath( "/proc/self/exe", NULL );
@ -173,12 +173,9 @@ static void init_argv0_dir( const char *argv0 )
dir = realpath( argv0, NULL );
#endif
if (!dir) return;
if (!(p = strrchr( dir, '/' ))) return;
if (p == dir) p++;
*p = 0;
dir = get_dirname( dir );
if (strendswith( dir, "/tools/wmc" )) nlsdirs[0] = strmake( "%s/../../nls", dir );
else nlsdirs[0] = strmake( "%s/%s", dir, BIN_TO_NLSDIR );
free( dir );
#endif
}
@ -328,14 +325,14 @@ int main(int argc,char *argv[])
/* Generate appropriate outfile names */
if(!output_name)
{
output_name = dup_basename(input_name, ".mc");
strcat(output_name, ".rc");
const char *name = input_name ? get_basename(input_name) : "wmc.tab";
output_name = replace_extension( name, ".mc", ".rc" );
}
if(!header_name)
{
header_name = dup_basename(input_name, ".mc");
strcat(header_name, ".h");
const char *name = input_name ? get_basename(input_name) : "wmc.tab";
header_name = replace_extension( name, ".mc", ".h" );
}
if(input_name)

View File

@ -692,10 +692,7 @@ static unsigned int flush_po_files( const char *output_name )
char *name = get_po_file_name( &po_file->lang );
if (output_name)
{
const char *p = strrchr( output_name, '/' );
if (p) p++;
else p = output_name;
if (!strcmp( p, name ))
if (!strcmp( get_basename(output_name), name ))
{
po_file_write( po_file->po, name, &po_xerror_handler );
count++;

View File

@ -128,32 +128,6 @@ void chat(const char *s, ...)
}
}
char *dup_basename(const char *name, const char *ext)
{
int namelen;
int extlen = strlen(ext);
char *base;
char *slash;
if(!name)
name = "wrc.tab";
slash = strrchr(name, '/');
if (slash)
name = slash + 1;
namelen = strlen(name);
/* +4 for later extension and +1 for '\0' */
base = xmalloc(namelen +4 +1);
strcpy(base, name);
if(!strcasecmp(name + namelen-extlen, ext))
{
base[namelen - extlen] = '\0';
}
return base;
}
int compare_striA( const char *str1, const char *str2 )
{
for (;;)

View File

@ -32,7 +32,6 @@ void error(const char *s, ...) __attribute__((format (printf, 1, 2), noreturn));
void warning(const char *s, ...) __attribute__((format (printf, 1, 2)));
void chat(const char *s, ...) __attribute__((format (printf, 1, 2)));
char *dup_basename(const char *name, const char *ext);
int compare_name_id(const name_id_t *n1, const name_id_t *n2);
string_t *convert_string_unicode( const string_t *str, int codepage );
char *convert_string_utf8( const string_t *str, int codepage );

View File

@ -89,13 +89,7 @@ static char *wpp_lookup(const char *name, int type, const char *parent_name,
if(type && parent_name)
{
/* Search directory of parent include and then -I path */
const char *p;
if ((p = strrchr( parent_name, '/' ))) p++;
else p = parent_name;
path = xmalloc( (p - parent_name) + strlen(cpy) + 1 );
memcpy( path, parent_name, p - parent_name );
strcpy( path + (p - parent_name), cpy );
path = strmake( "%s/%s", get_dirname(parent_name), cpy );
fd = open( path, O_RDONLY );
if (fd != -1)
{

View File

@ -333,7 +333,7 @@ static void set_target( const char *target )
static void init_argv0_dir( const char *argv0 )
{
#ifndef _WIN32
char *p, *dir;
char *dir;
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
dir = realpath( "/proc/self/exe", NULL );
@ -343,13 +343,10 @@ static void init_argv0_dir( const char *argv0 )
dir = realpath( argv0, NULL );
#endif
if (!dir) return;
if (!(p = strrchr( dir, '/' ))) return;
if (p == dir) p++;
*p = 0;
dir = get_dirname( dir );
includedir = strmake( "%s/%s", dir, BIN_TO_INCLUDEDIR );
if (strendswith( dir, "/tools/wrc" )) nlsdirs[0] = strmake( "%s/../../nls", dir );
else nlsdirs[0] = strmake( "%s/%s", dir, BIN_TO_NLSDIR );
free( dir );
#endif
}
@ -588,8 +585,8 @@ int main(int argc,char *argv[])
{
if (!output_name)
{
output_name = dup_basename( nb_files ? files[0] : NULL, ".rc" );
strcat( output_name, ".pot" );
const char *name = nb_files ? get_basename(files[0]) : "wrc.tab";
output_name = replace_extension( name, ".rc", ".pot" );
}
write_pot_file( output_name );
}
@ -605,8 +602,8 @@ int main(int argc,char *argv[])
chat("Writing .res-file\n");
if (!output_name)
{
output_name = dup_basename( nb_files ? files[0] : NULL, ".rc" );
strcat(output_name, ".res");
const char *name = nb_files ? get_basename(files[0]) : "wrc.tab";
output_name = replace_extension( name, ".rc", ".res" );
}
write_resfile(output_name, resource_top);
output_name = NULL;