Przeglądaj źródła

better windows support (hopefully), unix unit tests still pass

Former-commit-id: b517b345edb25dbf006ef6c1dfae0b3109c34824
Alec Jacobson 6 lat temu
rodzic
commit
8c5e9bd9bd
2 zmienionych plików z 14 dodań i 15 usunięć
  1. 9 15
      include/igl/dirname.cpp
  2. 5 0
      include/igl/dirname.h

+ 9 - 15
include/igl/dirname.cpp

@@ -1,5 +1,6 @@
 // This file is part of libigl, a simple c++ geometry processing library.
 // 
+// Copyright (C) 2018 Alec Jacobson <alecjacobson@gmail.com>
 // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
 // 
 // This Source Code Form is subject to the terms of the Mozilla Public License 
@@ -16,31 +17,24 @@ IGL_INLINE std::string igl::dirname(const std::string & path)
   {
     return std::string("");
   }
-#if defined (WIN32)
-  char del('\\');
-#else
-  char del('/');
-#endif
-  // http://stackoverflow.com/questions/5077693/dirnamephp-similar-function-in-c
-  std::string::const_reverse_iterator last_slash =
-    std::find(
-      path.rbegin(), 
-      path.rend(),del);
-  if( last_slash == path.rend() )
+  // https://stackoverflow.com/a/3071694/148668
+  size_t found = path.find_last_of("/\\");
+  if(found == std::string::npos)
   {
     // No slashes found
     return std::string(".");
-  }else if(1 == (last_slash.base() - path.begin()))
+  }else if(found == 0)
   {
     // Slash is first char
-    return std::string(1,del);
-  }else if(path.end() == last_slash.base() )
+    return std::string(path.begin(),path.begin()+1);
+  }else if(found == path.length()-1)
   {
     // Slash is last char
     std::string redo = std::string(path.begin(),path.end()-1);
     return igl::dirname(redo);
   }
-  return std::string(path.begin(),last_slash.base()-1);
+  // Return everything up to but not including last slash
+  return std::string(path.begin(),path.begin()+found);
 }
 
 

+ 5 - 0
include/igl/dirname.h

@@ -1,5 +1,6 @@
 // This file is part of libigl, a simple c++ geometry processing library.
 // 
+// Copyright (C) 2018 Alec Jacobson <alecjacobson@gmail.com>
 // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
 // 
 // This Source Code Form is subject to the terms of the Mozilla Public License 
@@ -19,6 +20,10 @@ namespace igl
   // Returns string containing dirname (see php's dirname)
   //
   // See also: basename, pathinfo
+  //
+  // **Note:** This function will have undefined behavior if **file names** in
+  // the path contain \ and / characters. This function interprets \ and / as
+  // file path separators.
   IGL_INLINE std::string dirname(const std::string & path);
 }