/*
* This file is part of DisOrder
- * Copyright (C) 2005 Richard Kettlewell
+ * Copyright (C) 2005, 2007 Richard Kettlewell
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
+/** @file lib/filepart.c
+ * @brief Filename parsing
+ */
#include <config.h>
#include "types.h"
#include "filepart.h"
#include "mem.h"
+/** @brief Return the directory part of @p path
+ * @param path Path to parse
+ * @return Directory part of @p path
+ *
+ * Extracts the directory part of @p path. This is a simple lexical
+ * transformation and no canonicalization is performed. The result will only
+ * ever end "/" if it is the root directory.
+ */
char *d_dirname(const char *path) {
const char *s;
if((s = strrchr(path, '/'))) {
+ while(s > path && s[-1] == '/')
+ --s;
if(s == path)
return xstrdup("/");
else
return xstrdup(".");
}
+/** @brief Find the extension part of @p path
+ * @param path Path to parse
+ * @return Start of extension in @p path, or NULL
+ *
+ * The return value points into @p path and points at the "." at the start of
+ * the path. If the basename has no extension the result is NULL. Extensions
+ * are assumed to only contain the ASCII digits and letters.
+ *
+ * See also extension().
+ */
static const char *find_extension(const char *path) {
const char *q = path + strlen(path);
return *q == '.' ? q : 0;
}
+/** @brief Strip the extension from @p path
+ * @param path Path to parse
+ * @return @p path with extension removed, or @p path
+ *
+ * The extension is defined exactly as for find_extension(). The result might
+ * or might not point into @p path.
+ */
const char *strip_extension(const char *path) {
const char *q = find_extension(path);
return q ? xstrndup(path, q - path) : path;
}
+/** @brief Find the extension part of @p path
+ * @param path Path to parse
+ * @return Start of extension in @p path, or ""
+ *
+ * The return value may points into @p path and if so points at the "." at the
+ * start of the path. If the basename has no extension the result is "".
+ * Extensions are assumed to only contain the ASCII digits and letters.
+ *
+ * See also find_extension().
+ */
const char *extension(const char *path) {
const char *q = find_extension(path);
/*
* This file is part of DisOrder
- * Copyright (C) 2005 Richard Kettlewell
+ * Copyright (C) 2005, 2007 Richard Kettlewell
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
+/** @file lib/filepart.h
+ * @brief Filename parsing
+ */
#ifndef FILEPART_H
#define FILEPART_H
static void test_filepart(void) {
fprintf(stderr, "test_filepart\n");
check_string(d_dirname("/"), "/");
+ check_string(d_dirname("////"), "/");
check_string(d_dirname("/spong"), "/");
+ check_string(d_dirname("////spong"), "/");
check_string(d_dirname("/foo/bar"), "/foo");
+ check_string(d_dirname("////foo/////bar"), "////foo");
check_string(d_dirname("./bar"), ".");
+ check_string(d_dirname(".//bar"), ".");
check_string(d_dirname("."), ".");
check_string(d_dirname(".."), ".");
check_string(d_dirname("../blat"), "..");
+ check_string(d_dirname("..//blat"), "..");
check_string(d_dirname("wibble"), ".");
check_string(extension("foo.c"), ".c");
check_string(extension(".c"), ".c");
check_string(extension("foo"), "");
check_string(extension("./foo"), "");
check_string(extension("./foo.c"), ".c");
+ check_string(strip_extension("foo.c"), "foo");
+ check_string(strip_extension("foo.mp3"), "foo");
+ check_string(strip_extension("foo.---"), "foo.---");
+ check_string(strip_extension("foo.---xyz"), "foo.---xyz");
+ check_string(strip_extension("foo.bar/wibble.spong"), "foo.bar/wibble");
}
static void test_selection(void) {
intmax_t m;
ssize_t ssz;
ptrdiff_t p;
+ char *cp;
+ char buffer[16];
fprintf(stderr, "test_printf\n");
check_string(do_printf("%d", 999), "999");
check_string(do_printf("wibble"), "wibble");
insist(do_printf("%") == 0);
insist(do_printf("%=") == 0);
+ i = byte_asprintf(&cp, "xyzzy %d", 999);
+ insist(i == 9);
+ check_string(cp, "xyzzy 999");
+ i = byte_snprintf(buffer, sizeof buffer, "xyzzy %d", 999);
+ insist(i == 9);
+ check_string(buffer, "xyzzy 999");
+ i = byte_snprintf(buffer, sizeof buffer, "%*d", 32, 99);
+ insist(i == 32);
+ check_string(buffer, " ");
+ {
+ /* bizarre workaround for compiler checking of format strings */
+ char f[] = "xyzzy %";
+ i = byte_asprintf(&cp, f);
+ insist(i == -1);
+ }
}
static void test_basen(void) {