5 #include <msp/strutils.h>
15 Fixes the case of the given path to match files / directories on the
16 filesystem. Intended to be used in programs that need to interact with
17 emulated Windows programs.
19 Path fix_case(const Path &path)
23 for(Path::iterator i=path.begin(); i!=path.end(); ++i)
31 files=list_files(result);
33 files=list_files(".");
36 for(list<string>::iterator j=files.begin(); (j!=files.end() && !found); ++j)
37 if(!strcasecmp(*j,*i))
51 int mkpath(const Path &path, int mode)
54 for(Path::iterator i=path.begin(); i!=path.end(); ++i)
58 if(p.size()==1 && is_windows_drive(*i)) continue;
61 int err=stat(p.str().c_str(),&st);
64 if(!S_ISDIR(st.st_mode))
71 else if(errno!=ENOENT)
76 // The win32 version of this function doesn't take the mode argument. Go figure.
77 err=mkdir(p.str().c_str());
79 err=mkdir(p.str().c_str(),mode);
81 if(err==-1) return -1;
88 int rmdir(const Path &path, bool recursive)
92 list<string> files=list_files(path);
93 for(list<string>::iterator i=files.begin(); i!=files.end(); ++i)
97 stat(p.str().c_str(),&st);
99 if(S_ISDIR(st.st_mode))
102 err=unlink(p.str().c_str());
106 return rmdir(path.str().c_str());
110 Lists all files in a directory except the implied . and .. entries.
112 list<string> list_files(const Path &path)
115 DIR *dir=opendir(path.str().c_str());
118 while(dirent *de=readdir(dir))
120 const char *fn=de->d_name;
121 if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0))) continue;
122 result.push_back(fn);
129 bool exists(const Path &path)
132 return !stat(path.str().c_str(),&st);
135 Filename splitext(const string &fn)
138 unsigned dot=fn.rfind('.');
139 result.base=fn.substr(0,dot);
140 if(dot!=string::npos)
141 result.ext=fn.substr(dot);
145 int fnmatch(const string &pat, const Path &fn)
147 return ::fnmatch(pat.c_str(), fn.str().c_str(), FNM_PATHNAME);