Add cdio_dirname which I think should complete what we need. Also add test of abs_path and dirname
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ cdio_close_tray
|
||||
cdio_debug
|
||||
cdio_destroy
|
||||
cdio_device_drivers
|
||||
cdio_dirname
|
||||
cdio_driver_describe
|
||||
cdio_driver_errmsg
|
||||
cdio_drivers
|
||||
|
||||
1
test/driver/.gitignore
vendored
1
test/driver/.gitignore
vendored
@@ -5,6 +5,7 @@
|
||||
/.libs
|
||||
/Makefile
|
||||
/Makefile.in
|
||||
/abs_path
|
||||
/bincue
|
||||
/bincue
|
||||
/bincue.c
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
46
test/driver/abs_path.c
Normal file
46
test/driver/abs_path.c
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user