3 This file is part of libmspfs
4 Copyright © 2006-2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include <msp/core/except.h>
13 #include <msp/strings/glob.h>
15 #include <msp/strings/utils.h>
26 Fixes the case of the given path to match files / directories on the
27 filesystem. Intended to be used in programs that need to interact with
28 emulated Windows programs.
30 Path fix_case(const Path &path)
34 for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
42 files=list_files(result);
44 files=list_files(".");
47 for(list<string>::iterator j=files.begin(); (j!=files.end() && !found); ++j)
48 if(!strcasecmp(*j,*i))
62 void unlink(const Path &path)
64 if(::unlink(path.str().c_str())==-1)
65 throw SystemError("unlink failed", errno);
68 Filename splitext(const string &fn)
71 unsigned dot=fn.rfind('.');
72 result.base=fn.substr(0, dot);
74 result.ext=fn.substr(dot);
78 int fnmatch(const string &pat, const Path &fn)
81 return globcasematch(pat, fn.str());
83 return ::fnmatch(pat.c_str(), fn.str().c_str(), FNM_PATHNAME);
87 Path relative(const Path &path, const Path &base)
89 Path::Iterator i=path.begin();
90 Path::Iterator j=base.begin();
91 for(; (i!=path.end() && j!=base.end() && *i==*j); ++i, ++j) ;
94 for(; j!=base.end(); ++j)
96 for(; i!=path.end(); ++i)
103 Extracts the basename from the given path. Same thing as Path::Path(p)[-1],
106 string basename(const std::string &p)
108 unsigned slash=p.rfind(DIRCHAR);
109 if(slash==string::npos)
112 return p.substr(slash+1);