#303: Typo fixes from mathnerd314
[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     return "";
37
38   return filename.substr(0, p+1);
39 }
40
41 std::string basename(const std::string& filename)
42 {
43   std::string::size_type p = filename.find_last_of('/');
44   if(p == std::string::npos)
45     return filename;
46
47   return filename.substr(p+1, filename.size()-p-1);
48 }
49
50 std::string strip_extension(const std::string& filename)
51 {
52   std::string::size_type p = filename.find_last_of('.');
53   if(p == std::string::npos)
54     return filename;
55
56   return filename.substr(0, p);
57 }
58
59 std::string normalize(const std::string& filename)
60 {
61   std::vector<std::string> path_stack;
62
63   const char* p = filename.c_str();
64
65   while(true) {
66     while(*p == '/') {
67       p++;
68       continue;
69     }
70
71     const char* pstart = p;
72     while(*p != '/' && *p != 0) {
73       ++p;
74     }
75
76     size_t len = p - pstart;
77     if(len == 0)
78       break;
79
80     std::string pathelem(pstart, p-pstart);
81     if(pathelem == ".")
82       continue;
83
84     if(pathelem == "..") {
85       if(path_stack.empty()) {
86
87         log_warning << "Invalid '..' in path '" << filename << "'" << std::endl;
88         // push it into the result path so that the user sees his error...
89         path_stack.push_back(pathelem);
90       } else {
91         path_stack.pop_back();
92       }
93     } else {
94       path_stack.push_back(pathelem);
95     }
96   }
97
98   // construct path
99   std::ostringstream result;
100   for(std::vector<std::string>::iterator i = path_stack.begin();
101       i != path_stack.end(); ++i) {
102     result << '/' << *i;
103   }
104   if(path_stack.empty())
105     result << '/';
106
107   return result.str();
108 }
109
110 }