From 1841da6de08dedd3f07a45ea6fa1299697b8e9d2 Mon Sep 17 00:00:00 2001 From: "R. Bernstein" Date: Sun, 11 Dec 2011 10:24:55 -0500 Subject: [PATCH] Add cdio_dirname which I think should complete what we need. Also add test of abs_path and dirname --- lib/driver/abs_path.c | 81 ++++++++--------------------------------- lib/driver/libcdio.sym | 1 + test/driver/.gitignore | 1 + test/driver/Makefile.am | 6 ++- test/driver/abs_path.c | 46 +++++++++++++++++++++++ 5 files changed, 68 insertions(+), 67 deletions(-) create mode 100644 test/driver/abs_path.c diff --git a/lib/driver/abs_path.c b/lib/driver/abs_path.c index 4e29f80a..adbbe1d9 100644 --- a/lib/driver/abs_path.c +++ b/lib/driver/abs_path.c @@ -58,22 +58,6 @@ # define CharNext(p) ((p) + 1) #endif -static char * -chompdirsep(const char *path) -{ - while (*path) { - if (isdirsep(*path)) { - const char *last = path++; - while (isdirsep(*path)) path++; - if (!*path) return (char *)last; - } - else { - path = CharNext(path); - } - } - return (char *)path; -} - static char * strrdirsep(const char *path) { @@ -92,57 +76,23 @@ strrdirsep(const char *path) return last; } +const char * cdio_dirname(const char *fname); + const char * -cdio_basename(const char *name) +cdio_dirname(const char *fname) { const char *p; -#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC - const char *root; -#endif - long f, n = -1; - - name = skipprefix(name); -#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC - root = name; -#endif - while (isdirsep(*name)) - name++; - if (!*name) { - p = name - 1; - f = 1; -#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC - if (name != root) { - /* has slashes */ - } -#ifdef DOSISH_DRIVE_LETTER - else if (*p == ':') { - p++; - f = 0; - } -#endif -#ifdef DOSISH_UNC - else { - p = CDIO_FILE_SEPARARATOR; - } -#endif -#endif - } - else { - if (!(p = strrdirsep(name))) { - p = name; - } - else { - while (isdirsep(*p)) p++; /* skip last / */ - } - n = chompdirsep(p) - p; - } - - return p; + p = strrdirsep(fname); + if (!p) return "."; + return strndup(fname, p - fname); } +const char *cdio_abspath(const char *cwd, const char *fname); + + /* If fname isn't absolute, add cwd to it. */ -char * -cdio_abspath(const char *cwd, char *fname) +const char * +cdio_abspath(const char *cwd, const char *fname) { if (isdirsep(*fname)) return fname; { @@ -154,12 +104,11 @@ cdio_abspath(const char *cwd, char *fname) } } -#define STANDALONE 1 #ifdef STANDALONE int main(int argc, char **argv) { - char *dest; - char *basename; + const char *dest; + const char *dirname; if (argc != 3) { fprintf(stderr, "Usage: %s FILE REPLACE_BASENAME\n", argv[0]); fprintf(stderr, @@ -167,8 +116,8 @@ int main(int argc, char **argv) exit(1); } - basename = cdio_basename(argv[1]); - dest = cdio_abspath (basename, argv[2]); + dirname = cdio_dirname(argv[1]); + dest = cdio_abspath (dirname, argv[2]); printf("%s -> %s\n", argv[1], dest); exit(0); } diff --git a/lib/driver/libcdio.sym b/lib/driver/libcdio.sym index 374f79b7..2aee1d09 100644 --- a/lib/driver/libcdio.sym +++ b/lib/driver/libcdio.sym @@ -33,6 +33,7 @@ cdio_close_tray cdio_debug cdio_destroy cdio_device_drivers +cdio_dirname cdio_driver_describe cdio_driver_errmsg cdio_drivers diff --git a/test/driver/.gitignore b/test/driver/.gitignore index a354bbcc..0c5a2ffa 100644 --- a/test/driver/.gitignore +++ b/test/driver/.gitignore @@ -5,6 +5,7 @@ /.libs /Makefile /Makefile.in +/abs_path /bincue /bincue /bincue.c diff --git a/test/driver/Makefile.am b/test/driver/Makefile.am index 89d35e9b..f8f4f9e4 100644 --- a/test/driver/Makefile.am +++ b/test/driver/Makefile.am @@ -16,6 +16,10 @@ INCLUDES = -I$(top_srcdir) $(LIBCDIO_CFLAGS) $(LIBISO9660_CFLAGS) DATA_DIR = $(abs_top_srcdir)/test/data +abs_path_SOURCES = abs_path.c +abs_path_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV) +abs_path_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\" + bincue_SOURCES = helper.c bincue.c bincue_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV) bincue_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\" @@ -54,7 +58,7 @@ win32_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV) win32_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\" check_PROGRAMS = \ - bincue cdrdao freebsd gnu_linux \ + abs_path bincue cdrdao freebsd gnu_linux \ mmc_read mmc_write nrg \ osx realpath solaris win32 diff --git a/test/driver/abs_path.c b/test/driver/abs_path.c new file mode 100644 index 00000000..e1c5edd3 --- /dev/null +++ b/test/driver/abs_path.c @@ -0,0 +1,46 @@ +#ifdef HAVE_CONFIG_H +# include "config.h" +# define __CDIO_CONFIG_H__ 1 +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif + +extern char * cdio_abspath(const char *cwd, const char *fname); +const char * cdio_dirname(const char *fname); + +int +main(int argc, const char *argv[]) +{ + const char *cue_filename[] = { + "/tmp/foo.bar", + "foo.bar" + }; + const char *expect[] = { + "/tmp/baz", + "./baz" + }; + + int rc=0; + char *dest; + int i; + + for (i=0; i<2; i++) { + const char *dirname = cdio_dirname(cue_filename[i]); + dest = cdio_abspath (dirname, "baz"); + if (0 != strcmp(expect[i], dest)) { + fprintf(stderr, + "Incorrect: expecting %s, got %s.\n", + expect[i], dest); + rc=i+1; + } + } + exit(rc); +}