chiark / gitweb /
tests + doxygen
authorRichard Kettlewell <rjk@greenend.org.uk>
Sun, 9 Dec 2007 18:42:54 +0000 (18:42 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sun, 9 Dec 2007 18:42:54 +0000 (18:42 +0000)
lib/filepart.c
lib/filepart.h
lib/test.c

index c385ef256eb2c59c013bedd484f6207e967caa91..c080b4d033beb914f22a433e177b05d0b4aa774a 100644 (file)
@@ -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 <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
@@ -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);
 
index 446e6847bb10458650682ee8aedf7a8a2c015b0b..fdc90745064eceacb06174c68fc9136315782e2d 100644 (file)
@@ -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
index 5454fd327b3d589c10d301f4bff053b4b8298e27..e8e550fd44b06e1786c690da5efb7b50956f5fac 100644 (file)
@@ -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) {