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 normalize(const std::string& filename)
52 std::vector<std::string> path_stack;
54 const char* p = filename.c_str();
62 const char* pstart = p;
63 while(*p != '/' && *p != 0) {
67 size_t len = p - pstart;
71 std::string pathelem(pstart, p-pstart);
75 if(pathelem == "..") {
76 if(path_stack.empty()) {
78 log_warning << "Invalid '..' in path '" << filename << "'" << std::endl;
79 // push it into the result path so that the users sees his error...
80 path_stack.push_back(pathelem);
82 path_stack.pop_back();
85 path_stack.push_back(pathelem);
90 std::ostringstream result;
91 for(std::vector<std::string>::iterator i = path_stack.begin();
92 i != path_stack.end(); ++i) {
95 if(path_stack.empty())