You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/09/08 11:04:06 UTC

svn commit: r812405 - in /commons/sandbox/runtime/trunk/src/main/native/port: basename.c dirname.c

Author: mturk
Date: Tue Sep  8 09:04:06 2009
New Revision: 812405

URL: http://svn.apache.org/viewvc?rev=812405&view=rev
Log:
Add base and dirname function to the port API. Modified BSD version that works on Windows as well

Added:
    commons/sandbox/runtime/trunk/src/main/native/port/basename.c   (with props)
    commons/sandbox/runtime/trunk/src/main/native/port/dirname.c   (with props)

Added: commons/sandbox/runtime/trunk/src/main/native/port/basename.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/port/basename.c?rev=812405&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/port/basename.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/port/basename.c Tue Sep  8 09:04:06 2009
@@ -0,0 +1,89 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Copyright (c) 1998 Todd C. Miller <To...@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_port.h"
+
+#if defined(WIN32)
+#define IS_PS(c) ((c) == '/' || (c) == '\\')
+#define NO_PS(c) ((c) != '/' && (c) != '\\')
+#else
+#define IS_PS(c) ((c) == '/')
+#define NO_PS(c) ((c) != '/')
+#endif
+
+char *basename_r(const char *path, char *bname, size_t blen)
+{
+    size_t len;
+    const char *endp, *startp;
+    static char sbuf[ACR_PPATH_MAX];
+
+    if (!bname) {
+        bname = sbuf;
+        blen  = sizeof(sbuf);
+    }
+
+    /* Empty or NULL string gets treated as "." */
+    if (path == NULL || *path == '\0') {
+        bname[0] = '.';
+        bname[1] = '\0';
+        return bname;
+    }
+
+    /* Strip any trailing slashes */
+    endp = path + strlen(path) - 1;
+    while (endp > path && IS_PS(*endp))
+        endp--;
+
+    /* All slashes becomes "/" */
+    if (endp == path && IS_PS(*endp)) {
+        bname[0] = '/';
+        bname[1] = '\0';
+        return bname;
+    }
+
+    /* Find the start of the base */
+    startp = endp;
+    while (startp > path && NO_PS(*(startp - 1)))
+        startp--;
+
+    len = endp - startp + 1;
+    if (len >= blen) {
+        errno = ENOMEM;
+        return NULL;
+    }
+    memcpy(bname, startp, len);
+    bname[len] = '\0';
+    return bname;
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/port/basename.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/runtime/trunk/src/main/native/port/dirname.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/port/dirname.c?rev=812405&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/port/dirname.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/port/dirname.c Tue Sep  8 09:04:06 2009
@@ -0,0 +1,92 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Copyright (c) 1998 Todd C. Miller <To...@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_port.h"
+
+#if defined(WIN32)
+#define IS_PS(c) ((c) == '/' || (c) == '\\')
+#define NO_PS(c) ((c) != '/' && (c) != '\\')
+#else
+#define IS_PS(c) ((c) == '/')
+#define NO_PS(c) ((c) != '/')
+#endif
+
+char *dirname_r(const char *path, char *dname, size_t dlen)
+{
+    size_t len;
+    static char sbuf[ACR_PPATH_MAX];
+    const char *endp;
+
+    if (!dname) {
+        dname = sbuf;
+        dlen  = sizeof(sbuf);
+    }
+    /* Empty or NULL string gets treated as "." */
+    if (path == NULL || *path == '\0') {
+        dname[0] = '.';
+        dname[1] = '\0';
+        return dname;
+    }
+
+    /* Strip any trailing slashes */
+    endp = path + strlen(path) - 1;
+    while (endp > path && IS_PS(*endp))
+        endp--;
+
+    /* Find the start of the dir */
+    while (endp > path && NO_PS(*endp))
+        endp--;
+
+    /* Either the dir is "/" or there are no slashes */
+    if (endp == path) {
+        dname[0] = IS_PS(*endp) ? '/' : '.';
+        dname[1] = '\0';
+        return dname;
+    } else {
+        /* Move forward past the separating slashes */
+        do {
+            endp--;
+        } while (endp > path && IS_PS(*endp));
+    }
+
+    len = endp - path + 1;
+    if (len >= dlen) {
+        errno = ENOMEM;
+        return NULL;
+    }
+    memcpy(dname, path, len);
+    dname[len] = '\0';
+    return dname;
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/port/dirname.c
------------------------------------------------------------------------------
    svn:eol-style = native