2 This file is part of libmsppath
3 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
10 #include <msp/strutils.h>
20 Fixes the case of the given path to match files / directories on the
21 filesystem. Intended to be used in programs that need to interact with
22 emulated Windows programs.
24 Path fix_case(const Path &path)
28 for(Path::iterator i=path.begin(); i!=path.end(); ++i)
36 files=list_files(result);
38 files=list_files(".");
41 for(list<string>::iterator j=files.begin(); (j!=files.end() && !found); ++j)
42 if(!strcasecmp(*j,*i))
56 int mkpath(const Path &path, int mode)
59 for(Path::iterator i=path.begin(); i!=path.end(); ++i)
63 if(p.size()==1 && is_windows_drive(*i)) continue;
66 int err=stat(p.str().c_str(),&st);
69 if(!S_ISDIR(st.st_mode))
76 else if(errno!=ENOENT)
81 // The win32 version of this function doesn't take the mode argument. Go figure.
82 err=mkdir(p.str().c_str());
84 err=mkdir(p.str().c_str(),mode);
86 if(err==-1) return -1;
93 int rmdir(const Path &path, bool recursive)
97 list<string> files=list_files(path);
98 for(list<string>::iterator i=files.begin(); i!=files.end(); ++i)
102 stat(p.str().c_str(),&st);
104 if(S_ISDIR(st.st_mode))
107 err=unlink(p.str().c_str());
111 return rmdir(path.str().c_str());
115 Lists all files in a directory except the implied . and .. entries.
117 list<string> list_files(const Path &path)
120 DIR *dir=opendir(path.str().c_str());
123 while(dirent *de=readdir(dir))
125 const char *fn=de->d_name;
126 if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0))) continue;
127 result.push_back(fn);
134 bool exists(const Path &path)
137 return !stat(path.str().c_str(),&st);
140 Filename splitext(const string &fn)
143 unsigned dot=fn.rfind('.');
144 result.base=fn.substr(0,dot);
145 if(dot!=string::npos)
146 result.ext=fn.substr(dot);
150 int fnmatch(const string &pat, const Path &fn)
152 return ::fnmatch(pat.c_str(), fn.str().c_str(), FNM_PATHNAME);