be39082e7d0c9bd2e93a0aca2417c2a3ddc94cf2
[supertux.git] / src / file_system.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include "log.hpp"
23 #include "file_system.hpp"
24
25 #include <string>
26 #include <vector>
27 #include <sstream>
28
29 namespace FileSystem
30 {
31
32 std::string dirname(const std::string& filename)
33 {
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)
38     return "./";
39
40   return filename.substr(0, p+1);
41 }
42
43 std::string basename(const std::string& filename)
44 {
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)
49     return filename;
50
51   return filename.substr(p+1, filename.size()-p-1);
52 }
53
54 std::string strip_extension(const std::string& filename)
55 {
56   std::string::size_type p = filename.find_last_of('.');
57   if(p == std::string::npos)
58     return filename;
59
60   return filename.substr(0, p);
61 }
62
63 std::string normalize(const std::string& filename)
64 {
65   std::vector<std::string> path_stack;
66
67   const char* p = filename.c_str();
68
69   while(true) {
70     while(*p == '/' || *p == '\\') {
71       p++;
72       continue;
73     }
74
75     const char* pstart = p;
76     while(*p != '/' && *p != '\\' && *p != 0) {
77       ++p;
78     }
79
80     size_t len = p - pstart;
81     if(len == 0)
82       break;
83
84     std::string pathelem(pstart, p-pstart);
85     if(pathelem == ".")
86       continue;
87
88     if(pathelem == "..") {
89       if(path_stack.empty()) {
90
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);
94       } else {
95         path_stack.pop_back();
96       }
97     } else {
98       path_stack.push_back(pathelem);
99     }
100   }
101
102   // construct path
103   std::ostringstream result;
104   for(std::vector<std::string>::iterator i = path_stack.begin();
105       i != path_stack.end(); ++i) {
106     result << '/' << *i;
107   }
108   if(path_stack.empty())
109     result << '/';
110
111   return result.str();
112 }
113
114 }