Add cdio_dirname which I think should complete what we need. Also add test of abs_path and dirname

This commit is contained in:
R. Bernstein
2011-12-11 10:24:55 -05:00
parent 592983743f
commit 1841da6de0
5 changed files with 68 additions and 67 deletions

View File

@@ -58,22 +58,6 @@
# define CharNext(p) ((p) + 1) # define CharNext(p) ((p) + 1)
#endif #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 * static char *
strrdirsep(const char *path) strrdirsep(const char *path)
{ {
@@ -92,57 +76,23 @@ strrdirsep(const char *path)
return last; return last;
} }
const char * cdio_dirname(const char *fname);
const char * const char *
cdio_basename(const char *name) cdio_dirname(const char *fname)
{ {
const char *p; const char *p;
#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC p = strrdirsep(fname);
const char *root; if (!p) return ".";
#endif return strndup(fname, p - fname);
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;
} }
const char *cdio_abspath(const char *cwd, const char *fname);
/* If fname isn't absolute, add cwd to it. */ /* If fname isn't absolute, add cwd to it. */
char * const char *
cdio_abspath(const char *cwd, char *fname) cdio_abspath(const char *cwd, const char *fname)
{ {
if (isdirsep(*fname)) return fname; if (isdirsep(*fname)) return fname;
{ {
@@ -154,12 +104,11 @@ cdio_abspath(const char *cwd, char *fname)
} }
} }
#define STANDALONE 1
#ifdef STANDALONE #ifdef STANDALONE
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char *dest; const char *dest;
char *basename; const char *dirname;
if (argc != 3) { if (argc != 3) {
fprintf(stderr, "Usage: %s FILE REPLACE_BASENAME\n", argv[0]); fprintf(stderr, "Usage: %s FILE REPLACE_BASENAME\n", argv[0]);
fprintf(stderr, fprintf(stderr,
@@ -167,8 +116,8 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
basename = cdio_basename(argv[1]); dirname = cdio_dirname(argv[1]);
dest = cdio_abspath (basename, argv[2]); dest = cdio_abspath (dirname, argv[2]);
printf("%s -> %s\n", argv[1], dest); printf("%s -> %s\n", argv[1], dest);
exit(0); exit(0);
} }

View File

@@ -33,6 +33,7 @@ cdio_close_tray
cdio_debug cdio_debug
cdio_destroy cdio_destroy
cdio_device_drivers cdio_device_drivers
cdio_dirname
cdio_driver_describe cdio_driver_describe
cdio_driver_errmsg cdio_driver_errmsg
cdio_drivers cdio_drivers

View File

@@ -5,6 +5,7 @@
/.libs /.libs
/Makefile /Makefile
/Makefile.in /Makefile.in
/abs_path
/bincue /bincue
/bincue /bincue
/bincue.c /bincue.c

View File

@@ -16,6 +16,10 @@
INCLUDES = -I$(top_srcdir) $(LIBCDIO_CFLAGS) $(LIBISO9660_CFLAGS) INCLUDES = -I$(top_srcdir) $(LIBCDIO_CFLAGS) $(LIBISO9660_CFLAGS)
DATA_DIR = $(abs_top_srcdir)/test/data 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_SOURCES = helper.c bincue.c
bincue_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV) bincue_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV)
bincue_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\" bincue_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\"
@@ -54,7 +58,7 @@ win32_LDADD = $(LIBCDIO_LIBS) $(LTLIBICONV)
win32_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\" win32_CFLAGS = -DDATA_DIR=\"$(DATA_DIR)\"
check_PROGRAMS = \ check_PROGRAMS = \
bincue cdrdao freebsd gnu_linux \ abs_path bincue cdrdao freebsd gnu_linux \
mmc_read mmc_write nrg \ mmc_read mmc_write nrg \
osx realpath solaris win32 osx realpath solaris win32

46
test/driver/abs_path.c Normal file
View File

@@ -0,0 +1,46 @@
#ifdef HAVE_CONFIG_H
# include "config.h"
# define __CDIO_CONFIG_H__ 1
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#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);
}