2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "util/log.hpp"
22 #include <sys/types.h>
31 namespace FileSystem {
33 bool exists(const std::string& path)
36 DWORD dwAttrib = GetFileAttributes(path.c_str());
38 return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
39 !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
41 return !access(path.c_str(), F_OK);
45 bool is_directory(const std::string& path)
49 if (stat(path.c_str(), &info ) != 0)
54 else if (info.st_mode & S_IFDIR)
64 void mkdir(const std::string& directory)
67 if (!CreateDirectory(directory.c_str(), NULL))
69 throw std::runtime_error("failed to create directory: " + directory);
72 if (::mkdir(directory.c_str(), 0777) != 0)
74 throw std::runtime_error("failed to create directory: " + directory);
79 std::string dirname(const std::string& filename)
81 std::string::size_type p = filename.find_last_of('/');
82 if(p == std::string::npos)
83 p = filename.find_last_of('\\');
84 if(p == std::string::npos)
87 return filename.substr(0, p+1);
90 std::string basename(const std::string& filename)
92 std::string::size_type p = filename.find_last_of('/');
93 if(p == std::string::npos)
94 p = filename.find_last_of('\\');
95 if(p == std::string::npos)
98 return filename.substr(p+1, filename.size()-p-1);
101 std::string strip_extension(const std::string& filename)
103 std::string::size_type p = filename.find_last_of('.');
104 if(p == std::string::npos)
107 return filename.substr(0, p);
110 std::string normalize(const std::string& filename)
112 std::vector<std::string> path_stack;
114 const char* p = filename.c_str();
117 while(*p == '/' || *p == '\\') {
122 const char* pstart = p;
123 while(*p != '/' && *p != '\\' && *p != 0) {
127 size_t len = p - pstart;
131 std::string pathelem(pstart, p-pstart);
135 if(pathelem == "..") {
136 if(path_stack.empty()) {
138 log_warning << "Invalid '..' in path '" << filename << "'" << std::endl;
139 // push it into the result path so that the user sees his error...
140 path_stack.push_back(pathelem);
142 path_stack.pop_back();
145 path_stack.push_back(pathelem);
150 std::ostringstream result;
151 for(std::vector<std::string>::iterator i = path_stack.begin();
152 i != path_stack.end(); ++i) {
155 if(path_stack.empty())
161 std::string join(const std::string& lhs, const std::string& rhs)
167 else if (lhs.back() == '/')
173 return lhs + "/" + rhs;
177 } // namespace FileSystem