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;
+}
bool is_absolute () const;
string to_string () const;
-
+ File_name canonicalized () const;
string dir_part () const;
string file_part () const;
};
#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())
return str;
}
+/*
+ TODO: this O(n^2) in #occurences of find, due to repeated copying.
+ */
string &
replace_all (string &str, string find, string replace)
{
{
string s = str.substr (0, i);
a.push_back (s);
- while (str[++i] == c)
- ;
+ i ++;
str = str.substr (i);
i = str.find (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;
+}
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);
+}
+