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)
36 p = filename.find_last_of('\\');
37 if(p == std::string::npos)
40 return filename.substr(0, p+1);
43 std::string basename(const std::string& filename)
45 std::string::size_type p = filename.find_last_of('/');
46 if(p == std::string::npos)
47 p = filename.find_last_of('\\');
48 if(p == std::string::npos)
51 return filename.substr(p+1, filename.size()-p-1);
54 std::string strip_extension(const std::string& filename)
56 std::string::size_type p = filename.find_last_of('.');
57 if(p == std::string::npos)
60 return filename.substr(0, p);
63 std::string normalize(const std::string& filename)
65 std::vector<std::string> path_stack;
67 const char* p = filename.c_str();
70 while(*p == '/' || *p == '\\') {
75 const char* pstart = p;
76 while(*p != '/' && *p != '\\' && *p != 0) {
80 size_t len = p - pstart;
84 std::string pathelem(pstart, p-pstart);
88 if(pathelem == "..") {
89 if(path_stack.empty()) {
91 log_warning << "Invalid '..' in path '" << filename << "'" << std::endl;
92 // push it into the result path so that the user sees his error...
93 path_stack.push_back(pathelem);
95 path_stack.pop_back();
98 path_stack.push_back(pathelem);
103 std::ostringstream result;
104 for(std::vector<std::string>::iterator i = path_stack.begin();
105 i != path_stack.end(); ++i) {
108 if(path_stack.empty())