5 #include <msp/core/systemerror.h>
6 #include <msp/strings/utils.h>
17 string basename(const Path &p)
22 Path dirname(const Path &p)
30 return p.subpath(0, p.size()-1);
33 string basepart(const string &fn)
35 unsigned dot = fn.rfind('.');
36 return fn.substr(0, dot);
39 string extpart(const string &fn)
41 string::size_type dot = fn.rfind('.');
44 return fn.substr(dot);
47 Path fix_case(const Path &path)
51 for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
59 files = list_files(result);
61 files = list_files(".");
64 for(list<string>::iterator j=files.begin(); (j!=files.end() && !found); ++j)
65 if(!strcasecmp(*j,*i))
79 Path readlink(const Path &link)
83 throw logic_error("no symbolic links on win32");
86 int len = ::readlink(link.str().c_str(), buf, sizeof(buf));
88 throw system_error("readlink");
89 return string(buf, len);
93 Path realpath(const Path &path)
96 if(path.is_absolute())
101 list<string> queue(path.begin(), path.end());
102 if(!path.is_absolute())
105 queue.insert(queue.begin(), cwd.begin(), cwd.end());
109 unsigned n_links = 0;
110 while(!queue.empty())
112 Path next = real/queue.front();
118 throw runtime_error("too many symbolic links");
119 Path link = readlink(next);
120 queue.insert(queue.begin(), link.begin(), link.end());
130 void rename(const Path &from, const Path &to)
132 if(::rename(from.str().c_str(), to.str().c_str())==-1)
133 throw system_error("rename");
136 void unlink(const Path &path)
138 if(::unlink(path.str().c_str())==-1)
139 throw system_error("unlink");
142 Path relative(const Path &path, const Path &base)
144 Path::Iterator i = path.begin();
145 Path::Iterator j = base.begin();
146 for(; (i!=path.end() && j!=base.end() && *i==*j); ++i, ++j) ;
149 for(; j!=base.end(); ++j)
151 for(; i!=path.end(); ++i)
157 int descendant_depth(const Path &path, const Path &parent)
159 Path::Iterator i = path.begin();
160 Path::Iterator j = parent.begin();
161 for(; (i!=path.end() && j!=parent.end() && *i==*j); ++i, ++j) ;
167 for(; i!=path.end(); ++i)