Hi,

    I've recently needed functionality to portably analyze the different
    parts of an OS path (ie, /home/foo.com/www/test.xml), therefore,
    I've written the following function, loosely based on
    apr_filename_from_pathname() which returns the information on a OS path
    into a structure (the name of the file, the directory path, and the
    file ending).

    Attached is a patch which removes the apr_filename_from_pathname()
    function, and adds the apr_pathinfo() function.  I've split the
    apr_pathinfo() function into a seperate file (apr_path.c) simply
    because apr_cpystrn.c (where apr_filename_from_pathname() was
    implemented) just didn't seem to be the proper place to put the
    function.  Actually, not to start another thread, but.. there
    doesn't really seem to be a standard file to put some assorted
    string functions in (like apr_strlib.c or something).

    Another note is that these functions take a pool to allocate memory,
    historically these type of functions don't behave like this (dirname()
    and basename() for example).  But, I've found (personally) that it
    leads to some annoying errors, therefore each portion of the string
    that is extracted is duplicated.  I don't anticipate this being a
    huge deal, mainly because at most you're copying a couple of characters
    (*duck*).

    -Sterling

    Ps: The path splitting functionality is not at all equivalent to
    dirname() or basename(), I think it makes sense this way, however,
    if people really want, I can make it dirname() and basename()
    compliant.

    Pps: Again sorry the patch is a bit big ;)
Index: include/apr_strings.h
===================================================================
RCS file: /home/cvspublic/apr/include/apr_strings.h,v
retrieving revision 1.21
diff -u -r1.21 apr_strings.h
--- include/apr_strings.h       2001/07/25 21:32:45     1.21
+++ include/apr_strings.h       2001/08/01 23:22:33
@@ -94,6 +94,13 @@
  * @package APR strings library
  */
 
+typedef struct {
+    char *file;
+    char *directory;
+    char *ending;
+}
+apr_pathinfo_t;
+
 /**
  * Do a natural order comparison of two strings.
  * @param a The first string to compare
@@ -237,6 +244,17 @@
  * @deffunc char *apr_strtok(char *str, const char *sep, char **last)
  */
 APR_DECLARE(char *) apr_strtok(char *str, const char *sep, char **last);
+
+/**
+ * Takes the full path to a filename and returns the different components of 
that
+ * file into a apr_pathinfo_t structure
+ * @param p The pool to allocate memory from
+ * @param path The path to analyze
+ * @param pi The path information is returned into this structure
+ * @deffunc void apr_pathinfo(apr_pool_t *p, const char *path, apr_pathinfo_t 
*pi)
+ */
+APR_DECLARE(void) apr_pathinfo(apr_pool_t *p, const char *path,
+                               apr_pathinfo_t *pi);
 
 /*
  * These are snprintf implementations based on apr_vformatter().
Index: include/apr_lib.h
===================================================================
RCS file: /home/cvspublic/apr/include/apr_lib.h,v
retrieving revision 1.53
diff -u -r1.53 apr_lib.h
--- include/apr_lib.h   2001/02/16 04:15:45     1.53
+++ include/apr_lib.h   2001/08/01 23:22:35
@@ -91,21 +91,6 @@
     char *endpos;
 };
 
-/**
- * return the final element of the pathname
- * @param pathname The path to get the final element of
- * @return the final element of the path
- * @tip Examples:  
- * <PRE>
- *                 "/foo/bar/gum"   -> "gum"
- *                 "/foo/bar/gum/"  -> ""
- *                 "gum"            -> "gum"
- *                 "wi\\n32\\stuff" -> "stuff"
- * </PRE>
- * @deffunc const char * apr_filename_of_pathname(const char *pathname)
- */
-APR_DECLARE(const char *) apr_filename_of_pathname(const char *pathname);
-
 /* These macros allow correct support of 8-bit characters on systems which
  * support 8-bit characters.  Pretty dumb how the cast is required, but
  * that's legacy libc for ya.  These new macros do not support EOF like
Index: strings/apr_cpystrn.c
===================================================================
RCS file: /home/cvspublic/apr/strings/apr_cpystrn.c,v
retrieving revision 1.8
diff -u -r1.8 apr_cpystrn.c
--- strings/apr_cpystrn.c       2001/07/25 16:09:35     1.8
+++ strings/apr_cpystrn.c       2001/08/01 23:22:37
@@ -199,34 +199,6 @@
     return(rc);
 }
 
-/* Filename_of_pathname returns the final element of the pathname.
- * Using the current platform's filename syntax.
- *   "/foo/bar/gum" -> "gum"
- *   "/foo/bar/gum/" -> ""
- *   "gum" -> "gum"
- *   "wi\\n32\\stuff" -> "stuff
- *
- * Corrected Win32 to accept "a/b\\stuff", "a:stuff"
- */
-
-APR_DECLARE(const char *) apr_filename_of_pathname(const char *pathname)
-{
-    const char path_separator = '/';
-    const char *s = strrchr(pathname, path_separator);
-
-#ifdef WIN32
-    const char path_separator_win = '\\';
-    const char drive_separator_win = ':';
-    const char *s2 = strrchr(pathname, path_separator_win);
-
-    if (s2 > s) s = s2;
-
-    if (!s) s = strrchr(pathname, drive_separator_win);
-#endif
-
-    return s ? ++s : pathname;
-}
-
 /* length of dest assumed >= length of src
  * collapse in place (src == dest) is legal.
  * returns terminating null ptr to dest string.
Index: strings/Makefile.in
===================================================================
RCS file: /home/cvspublic/apr/strings/Makefile.in,v
retrieving revision 1.9
diff -u -r1.9 Makefile.in
--- strings/Makefile.in 2001/05/23 14:15:43     1.9
+++ strings/Makefile.in 2001/08/01 23:22:38
@@ -5,7 +5,8 @@
        apr_strnatcmp.lo \
        apr_strings.lo \
        apr_fnmatch.lo \
-       apr_strtok.lo
+       apr_strtok.lo \
+       apr_path.lo
 
 # bring in rules.mk for standard functionality
 @INCLUDE_RULES@
/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org>.
 */

#include "apr.h"
#include "apr_strings.h"
#include "apr_private.h"
#include "apr_lib.h"

#if APR_HAVE_STRING_H
#include <string.h>
#endif

APR_DECLARE(void) apr_pathinfo(apr_pool_t *p,
                               const char *path,
                               apr_pathinfo_t *pi)
{
    const char *ending;
    const char *sep = strrchr(path, '/');
#ifdef WIN32
    const char *w32sep = strrchr(path, '\\');

    if (w32sep > sep)
    sep = w32sep;

    if (!sep)
    sep = strrchr(path, ':');
#endif

    pi->file = apr_pstrdup(p, sep ? ++sep : path);
    pi->directory = apr_pstrndup(p, sep ? path : ".", 
                                 sep ? sep - path : 2);

    ending = strrchr(pi->file, '.');
    pi->ending = apr_pstrdup(p, ending ? ++ending : "");
}

Reply via email to