Skip to content

Commit a802f69

Browse files
authored
Merge pull request #8 from laurence6/master
Fix Path::expand, close #7.
2 parents 81fc6f7 + 9e5c930 commit a802f69

2 files changed

Lines changed: 28 additions & 22 deletions

File tree

src/FS.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -258,56 +258,59 @@ bool Path::rename (const std::string& new_name)
258258
// ~ --> /home/user
259259
// ~foo/x --> /home/foo/s
260260
// ~/x --> /home/foo/x
261+
// . --> $PWD
261262
// ./x --> $PWD/x
262263
// x --> $PWD/x
263264
std::string Path::expand (const std::string& in)
264265
{
265266
std::string copy = in;
266267

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

270-
if (tilde != std::string::npos)
270+
if (in.empty ())
271+
{ }
272+
273+
else if (in.front () == '~')
271274
{
272-
const char *home = getenv("HOME");
275+
const char* home = getenv ("HOME");
273276
if (home == nullptr)
274277
{
275278
struct passwd* pw = getpwuid (getuid ());
276279
home = pw->pw_dir;
277280
}
278281

279282
// Convert: ~ --> /home/user
280-
if (copy.length () == 1)
283+
if (in.length () == 1)
281284
copy = home;
282285

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

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

300300
// Relative paths
301-
else if (in.length () > 2 &&
302-
in.substr (0, 2) == "./")
303-
{
304-
copy = Directory::cwd () + in.substr (1);
305-
}
306-
else if (in.length () > 1 &&
307-
in[0] != '.' &&
308-
in[0] != '/')
301+
else if (in.front () != '/')
309302
{
310-
copy = Directory::cwd () + '/' + in;
303+
// Convert: ., ./ --> $PWD
304+
if (in == ".")
305+
copy = Directory::cwd ();
306+
307+
// Convert: ./x --> $PWD/x
308+
else if (in.substr (0, 2) == "./")
309+
copy = Directory::cwd () + in.substr (1);
310+
311+
// Convert: x --> $PWD/x
312+
else
313+
copy = Directory::cwd () + '/' + in;
311314
}
312315

313316
return copy;

test/fs.t.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
int main (int, char**)
3535
{
36-
UnitTest t (117);
36+
UnitTest t (120);
3737

3838
try
3939
{
@@ -99,6 +99,9 @@ int main (int, char**)
9999
// static std::string expand (const std::string&);
100100
t.ok (Path::expand ("~") != "~", "Path::expand ~ != ~");
101101
t.ok (Path::expand ("~/") != "~/", "Path::expand ~/ != ~/");
102+
t.ok (Path::expand (".") != ".", "Path::expand . != .");
103+
t.ok (Path::expand ("./") != "./", "Path::expand ./ != ./");
104+
t.ok (Path::expand (".a") != ".a", "Path::expand .a != .a");
102105

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

0 commit comments

Comments
 (0)