Path exe;
if(argv0.find('/')==string::npos)
{
- const char *path=getenv("PATH");
- vector<string> dirs=split(path, ':');
+ const char *path = getenv("PATH");
+ vector<string> dirs = split(path, ':');
for(vector<string>::const_iterator i=dirs.begin(); i!=dirs.end(); ++i)
if(exists(Path(*i)/argv0))
{
- exe=realpath(Path(*i)/argv0);
+ exe = realpath(Path(*i)/argv0);
break;
}
}
else
- exe=realpath(argv0);
+ exe = realpath(argv0);
- last_argv0=argv0;
- bin_dir=dirname(exe);
+ last_argv0 = argv0;
+ bin_dir = dirname(exe);
}
return bin_dir;
#ifdef WIN32
// The win32 version of this function doesn't take the mode argument. Go figure.
(void)mode;
- err=::mkdir(path.str().c_str());
+ err = ::mkdir(path.str().c_str());
#else
- err=::mkdir(path.str().c_str(), mode);
+ err = ::mkdir(path.str().c_str(), mode);
#endif
if(err==-1)
Path p;
for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
{
- p/=*i;
+ p /= *i;
#ifdef WIN32
if(p.size()==1 && p.is_absolute())
continue;
#endif
struct stat st;
- int err=stat(p, st);
+ int err = stat(p, st);
if(err==0)
{
if(!S_ISDIR(st.st_mode))
void rmdirs(const Path &path)
{
- list<string> files=list_files(path);
+ list<string> files = list_files(path);
for(list<string>::iterator i=files.begin(); i!=files.end(); ++i)
{
- Path p=path / *i;
- struct stat st=stat(p.str().c_str());
+ Path p = path / *i;
+ struct stat st = stat(p.str().c_str());
if(S_ISDIR(st.st_mode))
rmdirs(p);
else
Regex r_filter(filter);
list<string> result;
- DIR *dir=opendir(path.str().c_str());
+ DIR *dir = opendir(path.str().c_str());
if(dir)
{
- while(dirent *de=readdir(dir))
+ while(dirent *de = readdir(dir))
{
- const char *fn=de->d_name;
+ const char *fn = de->d_name;
if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0)))
continue;
if(r_filter.match(fn))
if(SHGetFolderPath(0, CSIDL_PERSONAL, 0, 0, home)==S_OK)
return home;
#else
- const char *home=getenv("HOME");
+ const char *home = getenv("HOME");
if(home)
return home;
#endif
Path get_sys_conf_dir(const string &argv0)
{
- Path dir=get_bin_dir(argv0);
+ Path dir = get_bin_dir(argv0);
if(dir[-1]=="bin" || dir[-1]=="sbin")
{
- dir/="..";
+ dir /= "..";
if(dir[-1]=="usr")
- dir/="..";
+ dir /= "..";
return dir/"etc";
}
else
Path get_sys_data_dir(const string &argv0, const string &appname)
{
- Path dir=get_bin_dir(argv0);
+ Path dir = get_bin_dir(argv0);
if(dir[-1]=="bin" || dir[-1]=="sbin")
return dir/".."/"share"/appname;
Path get_sys_lib_dir(const string &argv0, const string &appname)
{
- Path dir=get_bin_dir(argv0);
+ Path dir = get_bin_dir(argv0);
if(dir[-1]=="bin" || dir[-1]=="sbin")
return dir/".."/"lib"/appname;
if(path.size()==1 && path[0]==DIRSEP)
return 1;
- unsigned count=1;
+ unsigned count = 1;
for(string::const_iterator i=path.begin(); i!=path.end(); ++i)
if(*i==DIRSEP) ++count;
return count;
Path Path::subpath(unsigned start, unsigned count) const
{
Path result;
- Iterator i=begin();
+ Iterator i = begin();
for(unsigned j=0; (j<start && i!=end()); ++j)
++i;
for(unsigned j=0; (j<count && i!=end()); ++j)
{
- result/=*i;
+ result /= *i;
++i;
}
return result;
Path Path::operator/(const Path &p) const
{
- Path a=*this;
- a/=p;
+ Path a = *this;
+ a /= p;
return a;
}
Path &Path::operator/=(const Path &p)
{
if(p.is_absolute())
- path=p.path;
+ path = p.path;
else
{
for(Iterator i=p.begin(); i!=p.end(); ++i)
Path::Iterator Path::end() const
{
Iterator i(*this);
- i.start=i.end=std::string::npos;
+ i.start=i.end = std::string::npos;
return i;
}
void Path::init(const string &p)
{
- string::size_type start=0;
+ string::size_type start = 0;
if(p[0]=='/' || p[0]=='\\')
add_component(string(1, DIRSEP));
while(1)
{
- string::size_type slash=p.find_first_of("/\\", start);
+ string::size_type slash = p.find_first_of("/\\", start);
if(slash>start)
add_component(p.substr(start, slash-start));
if(slash==string::npos)
break;
- start=slash+1;
+ start = slash+1;
}
}
{
// Replace the path with the root directory
#ifdef WIN32
- unsigned slash=path.find(DIRSEP);
+ unsigned slash = path.find(DIRSEP);
if(is_windows_drive(path.substr(0, slash)))
- path=path.substr(0, 2);
+ path = path.substr(0, 2);
else
#endif
- path=comp;
+ path = comp;
}
#ifdef WIN32
else if(is_windows_drive(comp))
- path=comp;
+ path = comp;
#endif
else if(comp=="..")
{
if(path.empty() || path==".")
- path=comp;
+ path = comp;
// .. in root directory is a no-op
else if(path.size()==1 && path[0]==DIRSEP)
;
#endif
else
{
- string::size_type slash=path.rfind(DIRSEP);
- string::size_type start=(slash==string::npos ? 0 : slash+1);
+ string::size_type slash = path.rfind(DIRSEP);
+ string::size_type start = (slash==string::npos ? 0 : slash+1);
if(!path.compare(start, string::npos, ".."))
{
// If the last component already is a .., add another
- path+=DIRSEP;
- path+=comp;
+ path += DIRSEP;
+ path += comp;
}
else if(slash==string::npos)
- path=".";
+ path = ".";
else
{
if(slash==0)
- slash=1;
+ slash = 1;
// Otherwise, erase the last component
path.erase(slash, string::npos);
}
else if(comp!="." || path.empty())
{
if(comp!="." && path.empty())
- path=".";
+ path = ".";
if(path.size()>1 || (path.size()==1 && path[0]!=DIRSEP))
- path+=DIRSEP;
- path+=comp;
+ path += DIRSEP;
+ path += comp;
}
}
start(0)
{
if(path.path.empty())
- start=end=string::npos;
+ start=end = string::npos;
else if(path.path[0]==DIRSEP)
- end=1;
+ end = 1;
#ifdef WIN32
else if(path.path.size()>2 && path.path[2]==DIRSEP && is_windows_drive(path.path.substr(0, 2)))
- end=2;
+ end = 2;
#endif
else
- end=path.path.find(DIRSEP);
+ end = path.path.find(DIRSEP);
}
Path::Iterator &Path::Iterator::operator++()
{
- start=end;
+ start = end;
if(start>=path.path.size())
return *this;
if(path.path[start]==DIRSEP)
++start;
- end=path.path.find(DIRSEP, start);
+ end = path.path.find(DIRSEP, start);
return *this;
}
if(start==0)
return *this;
- end=start;
+ end = start;
if(end>1 && end<path.path.size() && path.path[end]!=DIRSEP)
--end;
- start=path.path.rfind(DIRSEP, end-1);
+ start = path.path.rfind(DIRSEP, end-1);
if(start==string::npos)
- start=0;
+ start = 0;
else if(start<end-1)
++start;
enum
{
#ifdef WIN32
- DIRSEP='\\'
+ DIRSEP = '\\'
#else
- DIRSEP='/'
+ DIRSEP = '/'
#endif
};
bool is_absolute() const;
/// Extracts a range of components from the path.
- Path subpath(unsigned start, unsigned count=static_cast<unsigned>(-1)) const;
+ Path subpath(unsigned start, unsigned count = static_cast<unsigned>(-1)) const;
/// Concatenates this path with another one, with usual filesystem semantics
Path operator/(const Path &p) const;
string basepart(const string &fn)
{
- unsigned dot=fn.rfind('.');
+ unsigned dot = fn.rfind('.');
return fn.substr(0, dot);
}
string extpart(const string &fn)
{
- string::size_type dot=fn.rfind('.');
+ string::size_type dot = fn.rfind('.');
if(dot==string::npos)
return string();
return fn.substr(dot);
Path fix_case(const Path &path)
{
- bool found=true;
+ bool found = true;
Path result;
for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
{
if(!found || *i=="/")
- result/=*i;
+ result /= *i;
else
{
list<string> files;
if(result.size())
- files=list_files(result);
+ files = list_files(result);
else
- files=list_files(".");
+ files = list_files(".");
- found=false;
+ found = false;
for(list<string>::iterator j=files.begin(); (j!=files.end() && !found); ++j)
if(!strcasecmp(*j,*i))
{
- result/=*j;
- found=true;
+ result /= *j;
+ found = true;
}
if(!found)
- result/=*i;
+ result /= *i;
}
}
throw Exception("No symbolic links on win32");
#else
char buf[4096];
- int len=::readlink(link.str().c_str(), buf, sizeof(buf));
+ int len = ::readlink(link.str().c_str(), buf, sizeof(buf));
if(len==-1)
throw SystemError("readlink failed", errno);
return string(buf, len);
list<string> queue(path.begin(), path.end());
if(!path.is_absolute())
{
- Path cwd=getcwd();
+ Path cwd = getcwd();
queue.insert(queue.begin(), cwd.begin(), cwd.end());
}
Path real;
- unsigned n_links=0;
+ unsigned n_links = 0;
while(!queue.empty())
{
- Path next=real/queue.front();
+ Path next = real/queue.front();
queue.pop_front();
- struct stat st=lstat(next);
+ struct stat st = lstat(next);
if(S_ISLNK(st.st_mode))
{
if(++n_links>64)
throw Exception("Ludicrous amount of symlinks detected in realpath, giving up");
- Path link=readlink(next);
+ Path link = readlink(next);
queue.insert(queue.begin(), link.begin(), link.end());
}
else
- real=next;
+ real = next;
}
return real;
Path relative(const Path &path, const Path &base)
{
- Path::Iterator i=path.begin();
- Path::Iterator j=base.begin();
+ Path::Iterator i = path.begin();
+ Path::Iterator j = base.begin();
for(; (i!=path.end() && j!=base.end() && *i==*j); ++i, ++j) ;
Path result;
for(; j!=base.end(); ++j)
- result/="..";
+ result /= "..";
for(; i!=path.end(); ++i)
- result/=*i;
+ result /= *i;
return result;
}
int descendant_depth(const Path &path, const Path &parent)
{
- Path::Iterator i=path.begin();
- Path::Iterator j=parent.begin();
+ Path::Iterator i = path.begin();
+ Path::Iterator j = parent.begin();
for(; (i!=path.end() && j!=parent.end() && *i==*j); ++i, ++j) ;
if(j!=parent.end())