]> git.tdb.fi Git - libs/core.git/blob - source/utils.cpp
Get rid of the Path namespace
[libs/core.git] / source / utils.cpp
1 /* $Id$
2
3 This file is part of libmsppath
4 Copyright © 2006-2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <dirent.h>
9 #include <errno.h>
10 #include <msp/core/error.h>
11 #ifndef WIN32
12 #include <fnmatch.h>
13 #else
14 #include <msp/strings/glob.h>
15 #endif
16 #include <msp/strings/utils.h>
17 #include "path.h"
18 #include "utils.h"
19
20 using namespace std;
21
22 namespace Msp {
23
24 /**
25 Fixes the case of the given path to match files / directories on the
26 filesystem.  Intended to be used in programs that need to interact with
27 emulated Windows programs.
28 */
29 Path fix_case(const Path &path)
30 {
31         bool found=true;
32         Path result;
33         for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
34         {
35                 if(!found || *i=="/")
36                         result/=*i;
37                 else
38                 {
39                         list<string> files;
40                         if(result.size())
41                                 files=list_files(result);
42                         else
43                                 files=list_files(".");
44
45                         found=false;
46                         for(list<string>::iterator j=files.begin(); (j!=files.end() && !found); ++j)
47                                 if(!strcasecmp(*j,*i))
48                                 {
49                                         result/=*j;
50                                         found=true;
51                                 }
52
53                         if(!found)
54                                 result/=*i;
55                 }
56         }
57
58         return result;
59 }
60
61 void mkdir(const Path &path, int mode)
62 {
63         int err;
64 #ifdef WIN32
65         // The win32 version of this function doesn't take the mode argument.  Go figure.
66         (void)mode;
67         err=::mkdir(path.str().c_str());
68 #else
69         err=::mkdir(path.str().c_str(), mode);
70 #endif
71
72         if(err==-1)
73                 throw SystemError("mkdir failed", errno);
74 }
75
76 /**
77 Creates a directory and any parent directories if needed.
78
79 @param   path  The path to create
80 @param   mode  Access mode for new directories
81
82 @return  0 on success, -1 on error
83 */
84 void mkpath(const Path &path, int mode)
85 {
86         Path p;
87         for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
88         {
89                 p/=*i;
90 #ifdef WIN32
91                 if(p.size()==1 && is_windows_drive(*i))
92                         continue;
93 #endif
94                 struct stat st;
95                 int err=stat(p, st);
96                 if(err==0)
97                 {
98                         if(!S_ISDIR(st.st_mode))
99                                 throw Exception("A component exists and is not a directory");
100                         continue;
101                 }
102                 else if(errno!=ENOENT)
103                         throw SystemError("stat failed", errno);
104                 else
105                         mkdir(p, mode);
106         }
107 }
108
109 void rmdir(const Path &path)
110 {
111         if(::rmdir(path.str().c_str())==-1)
112                 throw SystemError("rmdir failed", errno);
113 }
114
115 void rmdirs(const Path &path)
116 {
117         list<string> files=list_files(path);
118         for(list<string>::iterator i=files.begin(); i!=files.end(); ++i)
119         {
120                 Path p=path / *i;
121                 struct stat st=stat(p.str().c_str());
122                 if(S_ISDIR(st.st_mode))
123                         rmdirs(p);
124                 else
125                         unlink(p);
126         }
127
128         rmdir(path);
129 }
130
131 void unlink(const Path &path)
132 {
133         if(::unlink(path.str().c_str())==-1)
134                 throw SystemError("unlink failed", errno);
135 }
136
137 list<string> list_files(const Path &path)
138 {
139         list<string> result;
140         DIR *dir=opendir(path.str().c_str());
141         if(dir)
142         {
143                 while(dirent *de=readdir(dir))
144                 {
145                         const char *fn=de->d_name;
146                         if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0))) 
147                                 continue;
148                         result.push_back(fn);
149                 }
150                 closedir(dir);
151         }
152
153         return result;
154 }
155
156 bool exists(const Path &path)
157 {
158         return access(path.str().c_str(), F_OK)==0;
159 }
160
161 Filename splitext(const string &fn)
162 {
163         Filename result;
164         unsigned dot=fn.rfind('.');
165         result.base=fn.substr(0, dot);
166         if(dot!=string::npos)
167                 result.ext=fn.substr(dot);
168         return result;
169 }
170
171 int fnmatch(const string &pat, const Path &fn)
172 {
173 #ifdef WIN32
174         return globcasematch(pat, fn.str());
175 #else
176         return ::fnmatch(pat.c_str(), fn.str().c_str(), FNM_PATHNAME);
177 #endif
178 }
179
180 Path relative(const Path &path, const Path &base)
181 {
182         Path::Iterator i=path.begin();
183         Path::Iterator j=base.begin();
184         for(; (i!=path.end() && j!=base.end() && *i==*j); ++i,++j);
185
186         Path result;
187         for(; j!=base.end(); ++j)
188                 result/="..";
189         for(; i!=path.end(); ++i)
190                 result/=*i;
191
192         return result;
193 }
194
195 /**
196 Extracts the basename from the given path.  Same thing as Path::Path(p)[-1],
197 but faster.
198 */
199 string basename(const std::string &p)
200 {
201         unsigned slash=p.rfind(DIRCHAR);
202         if(slash==string::npos)
203                 return p;
204         else
205                 return p.substr(slash+1);
206 }
207
208 int stat(const Path &fn, struct stat &st)
209 {
210         return ::stat(fn.str().c_str(), &st);
211 }
212
213 struct stat stat(const Path &fn)
214 {
215         struct stat st;
216         if(stat(fn, st)==-1)
217                 throw SystemError("stat failed", errno);
218         return st;
219 }
220
221 Path getcwd()
222 {
223         char buf[1024];
224         return ::getcwd(buf, sizeof(buf));
225 }
226
227 } // namespace Msp