From 822794001cb4260b8c04a7bd2d50d890edae709a Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 16 Jun 2003 13:33:38 +0000 Subject: * Started implementing the new evaluation model. * Lots of refactorings. * Unit tests. --- src/util.cc | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) (limited to 'src/util.cc') diff --git a/src/util.cc b/src/util.cc index 299fc942f2ef..8c397aace8c8 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1,47 +1,55 @@ +#include + #include "util.hh" string thisSystem = SYSTEM; -string nixHomeDir = "/nix"; -string nixHomeDirEnvVar = "NIX"; +SysError::SysError(string msg) +{ + char * sysMsg = strerror(errno); + err = msg + ": " + sysMsg; +} + -string absPath(string filename, string dir) +string absPath(string path, string dir) { - if (filename[0] != '/') { + if (path[0] != '/') { if (dir == "") { char buf[PATH_MAX]; if (!getcwd(buf, sizeof(buf))) - throw Error("cannot get cwd"); + throw SysError("cannot get cwd"); dir = buf; } - filename = dir + "/" + filename; + path = dir + "/" + path; /* !!! canonicalise */ char resolved[PATH_MAX]; - if (!realpath(filename.c_str(), resolved)) - throw Error("cannot canonicalise path " + filename); - filename = resolved; + if (!realpath(path.c_str(), resolved)) + throw SysError("cannot canonicalise path " + path); + path = resolved; } - return filename; + return path; +} + + +string dirOf(string path) +{ + unsigned int pos = path.rfind('/'); + if (pos == string::npos) throw Error("invalid file name: " + path); + return string(path, 0, pos); } -/* Return the directory part of the given path, i.e., everything - before the final `/'. */ -string dirOf(string s) +string baseNameOf(string path) { - unsigned int pos = s.rfind('/'); - if (pos == string::npos) throw Error("invalid file name"); - return string(s, 0, pos); + unsigned int pos = path.rfind('/'); + if (pos == string::npos) throw Error("invalid file name: " + path); + return string(path, pos + 1); } -/* Return the base name of the given path, i.e., everything following - the final `/'. */ -string baseNameOf(string s) +void debug(string s) { - unsigned int pos = s.rfind('/'); - if (pos == string::npos) throw Error("invalid file name"); - return string(s, pos + 1); + cerr << "debug: " << s << endl; } -- cgit 1.4.1