]> git.tdb.fi Git - libs/core.git/blob - source/utils.cpp
Add is_dir and get_home_dir functions
[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/except.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 bool is_dir(const Path &path)
162 {
163         struct stat st;
164         if(stat(path, st)==0)
165                 return S_ISDIR(st.st_mode);
166         return false;
167 }
168
169 Filename splitext(const string &fn)
170 {
171         Filename result;
172         unsigned dot=fn.rfind('.');
173         result.base=fn.substr(0, dot);
174         if(dot!=string::npos)
175                 result.ext=fn.substr(dot);
176         return result;
177 }
178
179 int fnmatch(const string &pat, const Path &fn)
180 {
181 #ifdef WIN32
182         return globcasematch(pat, fn.str());
183 #else
184         return ::fnmatch(pat.c_str(), fn.str().c_str(), FNM_PATHNAME);
185 #endif
186 }
187
188 Path relative(const Path &path, const Path &base)
189 {
190         Path::Iterator i=path.begin();
191         Path::Iterator j=base.begin();
192         for(; (i!=path.end() && j!=base.end() && *i==*j); ++i,++j);
193
194         Path result;
195         for(; j!=base.end(); ++j)
196                 result/="..";
197         for(; i!=path.end(); ++i)
198                 result/=*i;
199
200         return result;
201 }
202
203 /**
204 Extracts the basename from the given path.  Same thing as Path::Path(p)[-1],
205 but faster.
206 */
207 string basename(const std::string &p)
208 {
209         unsigned slash=p.rfind(DIRCHAR);
210         if(slash==string::npos)
211                 return p;
212         else
213                 return p.substr(slash+1);
214 }
215
216 int stat(const Path &fn, struct stat &st)
217 {
218         return ::stat(fn.str().c_str(), &st);
219 }
220
221 struct stat stat(const Path &fn)
222 {
223         struct stat st;
224         if(stat(fn, st)==-1)
225                 throw SystemError("stat failed", errno);
226         return st;
227 }
228
229 Path getcwd()
230 {
231         char buf[1024];
232         return ::getcwd(buf, sizeof(buf));
233 }
234
235 Path get_home_dir()
236 {
237 #ifndef WIN32
238         const char *home=getenv("HOME");
239         if(home)
240                 return home;
241         return ".";
242 #else
243         return ".";
244 #endif
245 }
246
247 } // namespace Msp