From e1f3f2355ade892ea678d605b5c848a431f41125 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Tue, 12 Jan 2010 01:30:59 -0500 Subject: [PATCH] Enhancement - Added Path::is_absolute, and corresponding unit tests. - Replaced expandPath and isAbsolutePath call in Config.cpp. --- src/Config.cpp | 4 ++-- src/Path.cpp | 9 +++++++++ src/Path.h | 1 + src/tests/path.t.cpp | 9 ++++++++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index ce41a2f38..d08701c3f 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -100,8 +100,8 @@ bool Config::load (const std::string& file, int nest /* = 1 */) std::string::size_type include = line.find ("include"); // no i18n. if (include != std::string::npos) { - Path included (expandPath (trim (line.substr (include + 7), " \t"))); - if (isAbsolutePath (included.data)) + Path included (trim (line.substr (include + 7), " \t")); + if (included.is_absolute ()) { if (included.readable ()) this->load (included.data, nest + 1); diff --git a/src/Path.cpp b/src/Path.cpp index b74c4d8f0..8bcd41342 100644 --- a/src/Path.cpp +++ b/src/Path.cpp @@ -121,6 +121,15 @@ bool Path::is_directory () const return false; } +//////////////////////////////////////////////////////////////////////////////// +bool Path::is_absolute () const +{ + if (data.length () && data.substr (0, 1) == "/") + return true; + + return false; +} + //////////////////////////////////////////////////////////////////////////////// bool Path::readable () const { diff --git a/src/Path.h b/src/Path.h index 9dc2ad440..5f1ba2c1e 100644 --- a/src/Path.h +++ b/src/Path.h @@ -44,6 +44,7 @@ public: std::string extension () const; bool exists () const; bool is_directory () const; + bool is_absolute () const; bool readable () const; bool writable () const; bool executable () const; diff --git a/src/tests/path.t.cpp b/src/tests/path.t.cpp index 4fc679a1e..aaaed861b 100644 --- a/src/tests/path.t.cpp +++ b/src/tests/path.t.cpp @@ -33,7 +33,7 @@ Context context; int main (int argc, char** argv) { - UnitTest t (26); + UnitTest t (31); // Path (); Path p0; @@ -101,6 +101,13 @@ int main (int argc, char** argv) t.ok (out.size () == 1, "/[s-u]mp -> 1 result"); t.is (out[0], "/tmp", "/[s-u]mp -> /tmp"); + // bool is_absolute () const; + t.notok (p0.is_absolute (), "'' !is_absolute"); + t.notok (p1.is_absolute (), "foo !is_absolute"); + t.ok (p2.is_absolute (), "~ is_absolute (after expansion)"); + t.ok (p3.is_absolute (), "/tmp is_absolute"); + t.ok (p4.is_absolute (), "/a/b/c/file.ext is_absolute"); + return 0; }