Fix sndfile-regtest paths handling on Windows platform

This commit is contained in:
Gisle Vanem 2020-09-11 10:50:02 +02:00 committed by GitHub
parent d8f82d88dc
commit 03bcd1dfff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -38,6 +38,7 @@
#if HAVE_SQLITE3
#include <ctype.h>
#include <sqlite3.h>
typedef struct
@ -71,6 +72,16 @@ static int check_file_by_ekey (REGTEST_DB * db, int ekey) ;
static int count_callback (REGTEST_DB * db, int argc, char **argv, char **colname) ;
static int ekey_max_callback (REGTEST_DB * db, int argc, char **argv, char **colname) ;
static int callback (void *unused, int argc, char **argv, char **colname) ;
static const char *db_basename (const char *fname);
/* Windows accepts both '\\' and '/' in paths */
#ifdef _WIN32
#define IS_SLASH(c) ((c) == '\\' || (c) == '/')
#define HAS_DRIVELETTER(path) (isalpha ((int)(path[0])) && path[1] == ':' && IS_SLASH(path[2]))
#else
#define IS_SLASH(c) ((c) == '/')
#define HAS_DRIVELETTER(path) 0
#endif
REG_DB *
db_open (const char * db_name)
@ -140,14 +151,12 @@ db_close (REG_DB * db_handle)
int
db_file_exists (REG_DB * db_handle, const char * filename)
{ REGTEST_DB * db ;
const char * cptr ;
char * errmsg ;
int err ;
db = (REGTEST_DB *) db_handle ;
if ((cptr = strrchr (filename, '/')) != NULL)
filename = cptr + 1 ;
filename = db_basename (filename);
snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select fname from sndfile where fname='%s'", filename) ;
@ -183,7 +192,7 @@ db_add_file (REG_DB * db_handle, const char * filepath)
sf_close (sndfile) ;
if (sndfile == NULL)
{ printf (" %s : could not open : %s\n", db->filename, sf_strerror (NULL)) ;
{ printf (" %s : could not open : %s, filepath: '%s'\n", db->filename, sf_strerror (NULL), filepath) ;
puts (db->logbuf) ;
return 1 ;
} ;
@ -417,10 +426,13 @@ check_file_by_ekey (REGTEST_DB * db, int ekey)
static void
get_filename_pathname (REGTEST_DB * db, const char *filepath)
{ const char * cptr ;
{
const char * basename = db_basename (filepath) ;
int slen ;
if (filepath [0] != '/')
/* Test for a relative path
*/
if (!IS_SLASH(filepath [0]) && !HAS_DRIVELETTER(filepath))
{ memset (db->pathname, 0, sizeof (db->pathname)) ;
if (getcwd (db->pathname, sizeof (db->pathname)) == NULL)
{ perror ("\ngetcwd failed") ;
@ -428,18 +440,19 @@ get_filename_pathname (REGTEST_DB * db, const char *filepath)
} ;
slen = strlen (db->pathname) ;
db->pathname [slen ++] = '/' ;
snprintf (db->pathname + slen, sizeof (db->pathname) - slen, "%s", filepath) ;
/* a '/' is fine for Windows too */
snprintf (db->pathname + slen, sizeof (db->pathname) - slen, "/%s", filepath) ;
}
else
snprintf (db->pathname, sizeof (db->pathname), "%s", filepath) ;
if ((cptr = strrchr (db->pathname, '/')) == NULL)
snprintf (db->filename, sizeof (db->filename), "%s", basename) ;
basename = db_basename (db->pathname) ;
if (basename == db->pathname)
{ printf ("\nError : bad pathname %s\n", filepath) ;
exit (1) ;
} ;
snprintf (db->filename, sizeof (db->filename), "%s", cptr + 1) ;
} /* get filename_pathname */
static void
@ -486,6 +499,33 @@ callback (void *unused, int argc, char **argv, char **colname)
return 0 ;
} /* callback */
/*
* Win32: Strip drive-letter and directory from a filename.
* non-Win32: Strip directory from a filename.
*/
static const char *db_basename (const char *fname)
{
const char *base = fname;
#if !defined(_WIN32)
const char *slash = strrchr (base, '/');
if (slash)
base = slash + 1 ;
#else
if (fname[0] && fname[1] == ':') {
fname += 2;
base = fname;
}
while (*fname) {
if (IS_SLASH(*fname))
base = fname + 1;
fname++;
}
#endif
return base ;
}
#else
int dummy (void) ;