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)
33 return p.subpath(0, p.size()-1);
36 string basepart(const string &fn)
38 unsigned dot=fn.rfind('.');
39 return fn.substr(0, dot);
42 string extpart(const string &fn)
44 unsigned dot=fn.rfind('.');
47 return fn.substr(dot);
50 Path fix_case(const Path &path)
54 for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
62 files=list_files(result);
64 files=list_files(".");
67 for(list<string>::iterator j=files.begin(); (j!=files.end() && !found); ++j)
68 if(!strcasecmp(*j,*i))
82 Path readlink(const Path &link)
85 throw Exception("No symbolic links on win32");
88 int len=::readlink(link.str().c_str(), buf, sizeof(buf));
90 throw SystemError("readlink failed", errno);
91 return string(buf, len);
95 Path realpath(const Path &path)
98 if(path.is_absolute())
101 return getcwd()/path;
103 list<string> queue(path.begin(), path.end());
104 if(!path.is_absolute())
107 queue.insert(queue.begin(), cwd.begin(), cwd.end());
112 while(!queue.empty())
114 Path next=real/queue.front();
117 struct stat st=lstat(next);
118 if(S_ISLNK(st.st_mode))
121 throw Exception("Ludicrous amount of symlinks detected in realpath, giving up");
122 Path link=readlink(next);
123 queue.insert(queue.begin(), link.begin(), link.end());
133 void unlink(const Path &path)
135 if(::unlink(path.str().c_str())==-1)
136 throw SystemError("unlink failed", errno);
139 Path relative(const Path &path, const Path &base)
141 Path::Iterator i=path.begin();
142 Path::Iterator j=base.begin();
143 for(; (i!=path.end() && j!=base.end() && *i==*j); ++i, ++j) ;
146 for(; j!=base.end(); ++j)
148 for(; i!=path.end(); ++i)