From 011e0b894ce9b8ccadc797860ba126dee957b0fe Mon Sep 17 00:00:00 2001 Message-Id: <011e0b894ce9b8ccadc797860ba126dee957b0fe.1714793204.git.mdw@distorted.org.uk> From: Mark Wooding Date: Sun, 9 Dec 2007 18:42:54 +0000 Subject: [PATCH] tests + doxygen Organization: Straylight/Edgeware From: Richard Kettlewell --- lib/filepart.c | 42 +++++++++++++++++++++++++++++++++++++++++- lib/filepart.h | 5 ++++- lib/test.c | 27 +++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/lib/filepart.c b/lib/filepart.c index c385ef2..c080b4d 100644 --- a/lib/filepart.c +++ b/lib/filepart.c @@ -1,6 +1,6 @@ /* * 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 @@ -17,6 +17,9 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ +/** @file lib/filepart.c + * @brief Filename parsing + */ #include #include "types.h" @@ -26,10 +29,20 @@ #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 @@ -38,6 +51,16 @@ char *d_dirname(const char *path) { 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); @@ -48,12 +71,29 @@ static const char *find_extension(const char *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); diff --git a/lib/filepart.h b/lib/filepart.h index 446e684..fdc9074 100644 --- a/lib/filepart.h +++ b/lib/filepart.h @@ -1,6 +1,6 @@ /* * 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 @@ -17,6 +17,9 @@ * 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 diff --git a/lib/test.c b/lib/test.c index 5454fd3..e8e550f 100644 --- a/lib/test.c +++ b/lib/test.c @@ -883,12 +883,17 @@ static void test_cache(void) { 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"); @@ -896,6 +901,11 @@ static void test_filepart(void) { 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) { @@ -1045,6 +1055,8 @@ static void test_printf(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"); @@ -1122,6 +1134,21 @@ static void test_printf(void) { 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) { -- [mdw]