2 #include <msp/core/systemerror.h>
3 #include <msp/strings/utils.h>
14 string basename(const Path &p)
19 Path dirname(const Path &p)
27 return p.subpath(0, p.size()-1);
30 string basepart(const string &fn)
32 unsigned dot = fn.rfind('.');
33 return fn.substr(0, dot);
36 string extpart(const string &fn)
38 string::size_type dot = fn.rfind('.');
41 return fn.substr(dot);
44 Path fix_case(const Path &path)
48 for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
56 files = list_files(result);
58 files = list_files(".");
61 for(list<string>::iterator j=files.begin(); (j!=files.end() && !found); ++j)
62 if(!strcasecmp(*j,*i))
76 Path readlink(const Path &link)
80 throw logic_error("no symbolic links on win32");
83 int len = ::readlink(link.str().c_str(), buf, sizeof(buf));
85 throw system_error("readlink");
86 return string(buf, len);
90 Path realpath(const Path &path)
93 if(path.is_absolute())
98 list<string> queue(path.begin(), path.end());
99 if(!path.is_absolute())
102 queue.insert(queue.begin(), cwd.begin(), cwd.end());
106 unsigned n_links = 0;
107 while(!queue.empty())
109 Path next = real/queue.front();
115 throw runtime_error("too many symbolic links");
116 Path link = readlink(next);
117 queue.insert(queue.begin(), link.begin(), link.end());
127 void rename(const Path &from, const Path &to)
129 if(::rename(from.str().c_str(), to.str().c_str())==-1)
130 throw system_error("rename");
133 void unlink(const Path &path)
135 if(::unlink(path.str().c_str())==-1)
136 throw system_error("unlink");
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)
154 int descendant_depth(const Path &path, const Path &parent)
156 Path::Iterator i = path.begin();
157 Path::Iterator j = parent.begin();
158 for(; (i!=path.end() && j!=parent.end() && *i==*j); ++i, ++j) ;
164 for(; i!=path.end(); ++i)