3 This file is part of libmspfs
4 Copyright © 2006-2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
15 #include <msp/core/except.h>
16 #include <msp/strings/regex.h>
27 void mkdir(const Path &path, int mode)
31 // The win32 version of this function doesn't take the mode argument. Go figure.
33 err=::mkdir(path.str().c_str());
35 err=::mkdir(path.str().c_str(), mode);
39 throw SystemError("mkdir failed", errno);
42 void mkpath(const Path &path, int mode)
45 for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
49 if(p.size()==1 && is_windows_drive(*i))
56 if(!S_ISDIR(st.st_mode))
57 throw Exception("A component exists and is not a directory");
60 else if(errno!=ENOENT)
61 throw SystemError("stat failed", errno);
67 void rmdir(const Path &path)
69 if(::rmdir(path.str().c_str())==-1)
70 throw SystemError("rmdir failed", errno);
73 void rmdirs(const Path &path)
75 list<string> files=list_files(path);
76 for(list<string>::iterator i=files.begin(); i!=files.end(); ++i)
79 struct stat st=stat(p.str().c_str());
80 if(S_ISDIR(st.st_mode))
89 list<string> list_files(const Path &path)
91 return list_filtered(path, string());
94 list<string> list_filtered(const Path &path, const string &filter)
96 Regex r_filter(filter);
99 DIR *dir=opendir(path.str().c_str());
102 while(dirent *de=readdir(dir))
104 const char *fn=de->d_name;
105 if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0)))
107 if(r_filter.match(fn))
108 result.push_back(fn);
119 return ::getcwd(buf, sizeof(buf));
126 if(SHGetFolderPath(0, CSIDL_PERSONAL, 0, 0, home)==S_OK)
129 const char *home=getenv("HOME");
136 Path get_user_data_dir(const string &appname)
139 char datadir[MAX_PATH];
140 if(SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0, 0, datadir)==S_OK)
141 return Path(datadir)/appname;
144 return get_home_dir()/("."+appname);
148 void chdir(const Path &path)
150 if(::chdir(path.str().c_str())==-1)
151 throw SystemError("chdir failed", errno);