4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "file_system.hpp"
32 std::string dirname(const std::string& filename)
34 std::string::size_type p = filename.find_last_of('/');
35 if(p == std::string::npos)
38 return filename.substr(0, p+1);
41 std::string basename(const std::string& filename)
43 std::string::size_type p = filename.find_last_of('/');
44 if(p == std::string::npos)
47 return filename.substr(p+1, filename.size()-p-1);
50 std::string strip_extension(const std::string& filename)
52 std::string::size_type p = filename.find_last_of('.');
53 if(p == std::string::npos)
56 return filename.substr(0, p);
59 std::string normalize(const std::string& filename)
61 std::vector<std::string> path_stack;
63 const char* p = filename.c_str();
71 const char* pstart = p;
72 while(*p != '/' && *p != 0) {
76 size_t len = p - pstart;
80 std::string pathelem(pstart, p-pstart);
84 if(pathelem == "..") {
85 if(path_stack.empty()) {
87 log_warning << "Invalid '..' in path '" << filename << "'" << std::endl;
88 // push it into the result path so that the users sees his error...
89 path_stack.push_back(pathelem);
91 path_stack.pop_back();
94 path_stack.push_back(pathelem);
99 std::ostringstream result;
100 for(std::vector<std::string>::iterator i = path_stack.begin();
101 i != path_stack.end(); ++i) {
104 if(path_stack.empty())