Skip to content

Commit

Permalink
Merge pull request #8 from laurence6/master
Browse files Browse the repository at this point in the history
Fix Path::expand, close #7.
  • Loading branch information
pbeckingham committed May 26, 2018
2 parents 81fc6f7 + 9e5c930 commit a802f69
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
45 changes: 24 additions & 21 deletions src/FS.cpp
Expand Up @@ -258,56 +258,59 @@ bool Path::rename (const std::string& new_name)
// ~ --> /home/user // ~ --> /home/user
// ~foo/x --> /home/foo/s // ~foo/x --> /home/foo/s
// ~/x --> /home/foo/x // ~/x --> /home/foo/x
// . --> $PWD
// ./x --> $PWD/x // ./x --> $PWD/x
// x --> $PWD/x // x --> $PWD/x
std::string Path::expand (const std::string& in) std::string Path::expand (const std::string& in)
{ {
std::string copy = in; std::string copy = in;


auto tilde = copy.find ('~');
std::string::size_type slash; std::string::size_type slash;


if (tilde != std::string::npos) if (in.empty ())
{ }

else if (in.front () == '~')
{ {
const char *home = getenv("HOME"); const char* home = getenv ("HOME");
if (home == nullptr) if (home == nullptr)
{ {
struct passwd* pw = getpwuid (getuid ()); struct passwd* pw = getpwuid (getuid ());
home = pw->pw_dir; home = pw->pw_dir;
} }


// Convert: ~ --> /home/user // Convert: ~ --> /home/user
if (copy.length () == 1) if (in.length () == 1)
copy = home; copy = home;


// Convert: ~/x --> /home/user/x // Convert: ~/x --> /home/user/x
else if (copy.length () > tilde + 1 && else if (in.at (1) == '/')
copy[tilde + 1] == '/') copy = home + in.substr (1);
{
copy.replace (tilde, 1, home);
}


// Convert: ~foo/x --> /home/foo/x // Convert: ~foo/x --> /home/foo/x
else if ((slash = copy.find ('/', tilde)) != std::string::npos) else if ((slash = in.find ('/', 1)) != std::string::npos)
{ {
std::string name = copy.substr (tilde + 1, slash - tilde - 1); std::string name = in.substr (1, slash - 1);
struct passwd* pw = getpwnam (name.c_str ()); struct passwd* pw = getpwnam (name.c_str ());
if (pw) if (pw)
copy.replace (tilde, slash - tilde, pw->pw_dir); copy = pw->pw_dir + in.substr (slash);
} }
} }


// Relative paths // Relative paths
else if (in.length () > 2 && else if (in.front () != '/')
in.substr (0, 2) == "./")
{
copy = Directory::cwd () + in.substr (1);
}
else if (in.length () > 1 &&
in[0] != '.' &&
in[0] != '/')
{ {
copy = Directory::cwd () + '/' + in; // Convert: ., ./ --> $PWD
if (in == ".")
copy = Directory::cwd ();

// Convert: ./x --> $PWD/x
else if (in.substr (0, 2) == "./")
copy = Directory::cwd () + in.substr (1);

// Convert: x --> $PWD/x
else
copy = Directory::cwd () + '/' + in;
} }


return copy; return copy;
Expand Down
5 changes: 4 additions & 1 deletion test/fs.t.cpp
Expand Up @@ -33,7 +33,7 @@


int main (int, char**) int main (int, char**)
{ {
UnitTest t (117); UnitTest t (120);


try try
{ {
Expand Down Expand Up @@ -99,6 +99,9 @@ int main (int, char**)
// static std::string expand (const std::string&); // static std::string expand (const std::string&);
t.ok (Path::expand ("~") != "~", "Path::expand ~ != ~"); t.ok (Path::expand ("~") != "~", "Path::expand ~ != ~");
t.ok (Path::expand ("~/") != "~/", "Path::expand ~/ != ~/"); t.ok (Path::expand ("~/") != "~/", "Path::expand ~/ != ~/");
t.ok (Path::expand (".") != ".", "Path::expand . != .");
t.ok (Path::expand ("./") != "./", "Path::expand ./ != ./");
t.ok (Path::expand (".a") != ".a", "Path::expand .a != .a");


// static std::vector <std::string> glob (const std::string&); // static std::vector <std::string> glob (const std::string&);
std::vector <std::string> out = Path::glob ("/tmp"); std::vector <std::string> out = Path::glob ("/tmp");
Expand Down

0 comments on commit a802f69

Please sign in to comment.