Revision: 69069
          http://sourceforge.net/p/brlcad/code/69069
Author:   starseeker
Date:     2016-10-14 22:05:17 +0000 (Fri, 14 Oct 2016)
Log Message:
-----------
Update bu_basename test to use a combination of compatibility wrapper logic and 
Windows APIs to emulate basename system functionality.

Modified Paths:
--------------
    brlcad/trunk/src/libbu/tests/bu_basename.c

Modified: brlcad/trunk/src/libbu/tests/bu_basename.c
===================================================================
--- brlcad/trunk/src/libbu/tests/bu_basename.c  2016-10-14 20:56:28 UTC (rev 
69068)
+++ brlcad/trunk/src/libbu/tests/bu_basename.c  2016-10-14 22:05:17 UTC (rev 
69069)
@@ -29,26 +29,67 @@
 
 #include "bu.h"
 
-#ifdef HAVE_BASENAME
-/* These two functions wrap the system and bu implementations to
+/* These functions wrap the system and bu implementations to
  * standardize the memory behavior. The input string is unmodified,
  * the output string is dynamically allocated and must be freed by the
  * caller.
  */
 
+#ifdef HAVE_BASENAME
 char *
 get_system_output(const char *input)
 {
-    char *in = input ? bu_strdup(input) : NULL;
-    char *out = bu_strdup(basename(in));
+       char *in = input ? bu_strdup(input) : NULL;
+       char *out = bu_strdup(basename(in));
 
-    if (in) {
-       bu_free(in, "input copy");
-    }
-    return out;
+       if (in) {
+               bu_free(in, "input copy");
+       }
+       return out;
 }
+#endif
 
+#if !defined(HAVE_BASENAME) && defined(HAVE__SPLITPATH)
 char *
+get_system_output(const char *input)
+{
+       char fname[_MAX_FNAME];
+       char dir[_MAX_DIR];
+       char *base = NULL;
+       if (input && !strlen(input) == 0) {
+               char *in = bu_strdup(input);
+               if (!strchr(in, '/') && !strchr(in, '\\')) return in;
+               if (BU_STR_EQUAL(in, "/")) {
+                       base = bu_strdup("/");
+                       bu_free(in, "input copy");
+                       return base;
+               }
+               _splitpath(in, NULL, dir, fname, NULL);
+               if (strlen(dir) != strlen(in) && strlen(fname) == 0 && 
in[strlen(in) - 1] == '.') {
+                       base = bu_strdup(&(in[strlen(in) - 1]));
+                       bu_free(in, "input copy");
+                       return base;
+               }
+               while (strlen(dir) > 1 && strlen(fname) == 0) {
+                       in[strlen(in) - 1] = '\0';
+                       _splitpath(in, NULL, dir, fname, NULL);
+               }
+               if (strlen(fname) > 0) {
+                       base = bu_strdup(fname);
+               } else {
+                       if (in[strlen(in) - 1] == '/') {
+                               base = bu_strdup("/");
+                       }
+               }
+               bu_free(in, "input copy");
+       } else {
+               base = bu_strdup(".");
+       }
+    return base;
+}
+#endif
+
+char *
 get_bu_output(const char *input)
 {
     /* basename should return "." when given a NULL string */
@@ -61,12 +102,11 @@
 
     return output;
 }
-#endif
 
 void
 compare_bu_to_system_basename(const char *input)
 {
-#ifdef HAVE_BASENAME
+#if defined(HAVE_BASENAME) || defined(HAVE__SPLITPATH)
     char *sys_out = get_system_output(input);
     char *bu_out = get_bu_output(input);
 
@@ -81,9 +121,7 @@
        bu_exit(EXIT_FAILURE, "");
     }
 #else
-    /* TODO - need to add tests using Windows specific routines as well:
-     * https://msdn.microsoft.com/en-us/library/e737s6tf.aspx */
-    bu_exit(EXIT_SUCCESS, "BASENAME not available on this platform\n");
+    bu_exit(EXIT_FAILURE, "BASENAME not available on this platform\n");
 #endif
 }
 

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most 
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
BRL-CAD Source Commits mailing list
brlcad-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to