]> git.donarmstrong.com Git - lilypond.git/commitdiff
add File_name::canonicalized()
authorHan-Wen Nienhuys <hanwen@xs4all.nl>
Mon, 4 Jun 2007 00:43:11 +0000 (21:43 -0300)
committerHan-Wen Nienhuys <hanwen@xs4all.nl>
Mon, 4 Jun 2007 00:43:11 +0000 (21:43 -0300)
- add string_join()
- do the right thing with a//b for string_split()
- test functions

flower/file-name.cc
flower/include/file-name.hh
flower/include/std-vector.hh
flower/std-string.cc
flower/test-file-name.cc

index e5736e06b0834ed6c26e15e01f32d25d91682067..0f0bb61270beee42468ec305e31fb0b4106e06fa 100644 (file)
@@ -177,3 +177,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;  
+}
index e8ea1ea3c2d67667c535f5ad51b87d07ad9b72df..bd699e0b3151cdf12a207fb39cf6d8795d5a03be 100644 (file)
@@ -27,7 +27,7 @@ public:
 
   bool is_absolute () const;
   string to_string () const;
-
+  File_name canonicalized () const;
   string dir_part () const;
   string file_part () const;
 };
index 4c3dcc0e0e8f61ae506d8672cf81073b0799759b..5c2c09c9d0c908fc7880584774bb11cbda8c2823 100644 (file)
@@ -258,6 +258,7 @@ junk_pointers (vector<T> &v)
 #endif /* HAVE_BOOST_LAMBDA */
 
 vector<string> string_split (string str, char c);
+string string_join (vector<string> const &strs, string infix);
 
 #define iterof(i,s) typeof((s).begin()) i((s).begin())
 
index b78b0196616aa1adde6f46b9ab64df379b523ed5..1221609c6c0ce4bc2e76a140d4fcbb1ed35f3d11 100644 (file)
@@ -67,6 +67,9 @@ to_string (char const *format, ...)
   return str;
 }
 
+/*
+  TODO: this O(n^2) in #occurences of find, due to repeated copying.
+ */
 string &
 replace_all (string &str, string find, string replace)
 {
@@ -113,8 +116,7 @@ string_split (string str, char c)
     {
       string s = str.substr (0, i);
       a.push_back (s);
-      while (str[++i] == c)
-       ;
+      i ++;
       str = str.substr (i);
       i = str.find (c);
     }
@@ -122,3 +124,17 @@ string_split (string str, char c)
     a.push_back (str);
   return a;
 }
+
+string
+string_join (vector<string> const &strs, string infix)
+{
+  string result;
+  for (vsize i = 0; i < strs.size (); i ++)
+    {
+      if (i)
+       result += infix;
+      result += strs[i];
+    }
+
+  return result;
+}
index edae635e822e618d37a18f5fe92433109180091a..9bd06f121cf7201a1a7feea1874f5d0308ba2f5f 100644 (file)
@@ -34,3 +34,10 @@ TEST_STRING (File_name, Mingw_slashify_4, "\\tmp\\x.ly")
   string s = slashify (to_string ());
   EQUAL ("/tmp/x.ly", s);
 }
+
+TEST_STRING (File_name, Canonicalize, "foo//bar/..//bla//z.ly")
+{
+  string s = canonicalized ().to_string ();
+  EQUAL ("foo/bla/z.ly", s);
+}
+