4 #include "file_system.hpp"
13 std::string dirname(const std::string& filename)
15 std::string::size_type p = filename.find_last_of('/');
16 if(p == std::string::npos)
19 return filename.substr(0, p+1);
22 std::string basename(const std::string& filename)
24 std::string::size_type p = filename.find_last_of('/');
25 if(p == std::string::npos)
28 return filename.substr(p, filename.size()-p);
31 std::string normalize(const std::string& filename)
33 std::vector<std::string> path_stack;
35 const char* p = filename.c_str();
43 const char* pstart = p;
44 while(*p != '/' && *p != 0) {
48 size_t len = p - pstart;
52 std::string pathelem(pstart, p-pstart);
56 if(pathelem == "..") {
57 if(path_stack.empty()) {
59 msg_warning("Invalid '..' in path '" << filename << "'");
60 // push it into the result path so that the users sees his error...
61 path_stack.push_back(pathelem);
63 path_stack.pop_back();
66 path_stack.push_back(pathelem);
71 std::ostringstream result;
72 for(std::vector<std::string>::iterator i = path_stack.begin();
73 i != path_stack.end(); ++i) {
76 if(path_stack.empty())