2 #include <msp/core/systemerror.h>
6 #include <msp/strings/glob.h>
8 #include <msp/strings/utils.h>
19 string basename(const Path &p)
24 Path dirname(const Path &p)
32 return p.subpath(0, p.size()-1);
35 string basepart(const string &fn)
37 unsigned dot = fn.rfind('.');
38 return fn.substr(0, dot);
41 string extpart(const string &fn)
43 string::size_type dot = fn.rfind('.');
46 return fn.substr(dot);
49 Path fix_case(const Path &path)
53 for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
61 files = list_files(result);
63 files = list_files(".");
66 for(list<string>::iterator j=files.begin(); (j!=files.end() && !found); ++j)
67 if(!strcasecmp(*j,*i))
81 Path readlink(const Path &link)
85 throw logic_error("no symbolic links on win32");
88 int len = ::readlink(link.str().c_str(), buf, sizeof(buf));
90 throw system_error("readlink");
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());
111 unsigned n_links = 0;
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 runtime_error("too many symbolic links");
122 Path link = readlink(next);
123 queue.insert(queue.begin(), link.begin(), link.end());
133 void rename(const Path &from, const Path &to)
135 if(::rename(from.str().c_str(), to.str().c_str())==-1)
136 throw system_error("rename");
139 void unlink(const Path &path)
141 if(::unlink(path.str().c_str())==-1)
142 throw system_error("unlink");
145 Path relative(const Path &path, const Path &base)
147 Path::Iterator i = path.begin();
148 Path::Iterator j = base.begin();
149 for(; (i!=path.end() && j!=base.end() && *i==*j); ++i, ++j) ;
152 for(; j!=base.end(); ++j)
154 for(; i!=path.end(); ++i)
160 int descendant_depth(const Path &path, const Path &parent)
162 Path::Iterator i = path.begin();
163 Path::Iterator j = parent.begin();
164 for(; (i!=path.end() && j!=parent.end() && *i==*j); ++i, ++j) ;
170 for(; i!=path.end(); ++i)