]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/file-name.cc
Run `make grand-replace'.
[lilypond.git] / flower / file-name.cc
index 787c27f25a2e18fa17fa0d28b58f10218010fbb8..bb07ebccd0f03576bedd90f0e206158d9bcf4955 100644 (file)
@@ -3,7 +3,7 @@
 
   source file of the Flower Library
 
-  (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  (c) 1997--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
   Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
@@ -12,6 +12,7 @@
 #include <cstdio>
 #include <cerrno>
 #include <unistd.h>
+#include <limits.h>
 
 using namespace std;
 
@@ -42,7 +43,7 @@ static string
 dos_to_posix (string file_name)
 {
   char buf[PATH_MAX] = "";
-  char s[PATH_MAX];
+  char s[PATH_MAX] = {0};
   file_name.copy (s, PATH_MAX - 1);
   /* ugh: char const* argument gets modified.  */
   int fail = cygwin_conv_to_posix_path (s, buf);
@@ -60,8 +61,8 @@ static /* avoid warning */
 string
 slashify (string file_name)
 {
-  replace_all (file_name, '\\', '/');
-  replace_all (file_name, string ("//"), "/");
+  replace_all (&file_name, '\\', '/');
+  replace_all (&file_name, string ("//"), "/");
   return file_name;
 }
 
@@ -74,9 +75,7 @@ dir_name (string const file_name)
   if (n && s[n - 1] == '/')
     s[n - 1] = 0;
   if (s.rfind ('/') != NPOS)
-    {
-      s = s.substr (0, s.rfind ('/'));
-    }
+    s = s.substr (0, s.rfind ('/'));
   else
     s = "";
   
@@ -179,3 +178,26 @@ File_name::is_absolute () const
   return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
 }
 
+
+
+File_name
+File_name::canonicalized () const
+{
+  File_name c = *this;
+
+  replace_all (&c.dir_, string ("//"), string ("/"));
+
+  vector<string> components =  string_split (c.dir_, '/');
+  vector<string> new_components;
+
+  for (vsize i = 0; i < components.size (); i++)
+    {
+      if (components[i] == "..")
+       new_components.pop_back ();
+      else
+       new_components.push_back (components[i]);
+    }
+
+  c.dir_ = string_join (new_components,  "/");
+  return c;  
+}