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 string basename(const Path &p)
31 Path dirname(const Path &p)
39 return p.subpath(0, p.size()-1);
42 string basepart(const string &fn)
44 unsigned dot=fn.rfind('.');
45 return fn.substr(0, dot);
48 string extpart(const string &fn)
50 string::size_type dot=fn.rfind('.');
53 return fn.substr(dot);
56 Path fix_case(const Path &path)
60 for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
68 files=list_files(result);
70 files=list_files(".");
73 for(list<string>::iterator j=files.begin(); (j!=files.end() && !found); ++j)
74 if(!strcasecmp(*j,*i))
88 Path readlink(const Path &link)
92 throw Exception("No symbolic links on win32");
95 int len=::readlink(link.str().c_str(), buf, sizeof(buf));
97 throw SystemError("readlink failed", errno);
98 return string(buf, len);
102 Path realpath(const Path &path)
105 if(path.is_absolute())
108 return getcwd()/path;
110 list<string> queue(path.begin(), path.end());
111 if(!path.is_absolute())
114 queue.insert(queue.begin(), cwd.begin(), cwd.end());
119 while(!queue.empty())
121 Path next=real/queue.front();
124 struct stat st=lstat(next);
125 if(S_ISLNK(st.st_mode))
128 throw Exception("Ludicrous amount of symlinks detected in realpath, giving up");
129 Path link=readlink(next);
130 queue.insert(queue.begin(), link.begin(), link.end());
140 void unlink(const Path &path)
142 if(::unlink(path.str().c_str())==-1)
143 throw SystemError("unlink failed", errno);
146 Path relative(const Path &path, const Path &base)
148 Path::Iterator i=path.begin();
149 Path::Iterator j=base.begin();
150 for(; (i!=path.end() && j!=base.end() && *i==*j); ++i, ++j) ;
153 for(; j!=base.end(); ++j)
155 for(; i!=path.end(); ++i)