diff --git a/modules/base/src/path.cc b/modules/base/src/path.cc index f85718a7e2fb5315d3380524b410e12bec3281db..6d34f4d95d8dc8990caeb725de408fec8604b40b 100644 --- a/modules/base/src/path.cc +++ b/modules/base/src/path.cc @@ -28,24 +28,38 @@ namespace ost { -Path& Path::operator/(const String& str) + + +Path& Path::operator/=(const Path& path) { - this->path_ += OST_DIRECTORY_SEPARATOR; - this->path_ += str; + if (path_.size()==0 || (path_[path_.size()-1] != OST_DIRECTORY_SEPARATOR)) { + this->path_ += OST_DIRECTORY_SEPARATOR; + } + this->path_ += path.GetFullPath(); return *this; } -Path& Path::operator/(const Path& path) +Path& Path::operator/=(const String& str) { - this->path_ += OST_DIRECTORY_SEPARATOR; - this->path_ += path.GetFullPath(); + if (path_.size()==0 || (path_[path_.size()-1] != OST_DIRECTORY_SEPARATOR)) { + this->path_ += OST_DIRECTORY_SEPARATOR; + } + this->path_ += str; return *this; } -Path& operator/(const String& str1, const Path& str2) +Path Path::operator/(const Path& path) const +{ + Path retpath(*this); + retpath /= path; + return retpath; +} + +Path Path::operator/(const String& str) const { - Path p1(str1); - return p1/str2; + Path retpath(*this); + retpath /= str; + return retpath; } String Path::GetFileName() const @@ -79,14 +93,13 @@ String Path::GetExtension() const return (filename_sr_split.rbegin())->str(); } - #ifdef WIN32 // Insert Windows Code Here #else String Path::GetAbsolutePath() const { - if (path_[0]==OST_DIRECTORY_SEPARATOR) { + if (path_.size() !=0 && path_[0]==OST_DIRECTORY_SEPARATOR) { return path_; } char path[MAXPATHLEN]; // This is a buffer for the text @@ -99,20 +112,15 @@ String Path::GetAbsolutePath() const bool Path::Exists() const { - if (access(path_.c_str(), F_OK)==0) - { - return true; + if (access(path_.c_str(), F_OK)==0) { + return true; } return false; } bool Path::IsWritable() const { - if (access(path_.c_str(), W_OK)==0) - { - return true; - } - return false; + return (access(path_.c_str(), W_OK)==0); } #endif diff --git a/modules/base/src/path.hh b/modules/base/src/path.hh index b6fefcb5e4b4274990138e50e085009a5257f3fd..f8a41080fcff1295701c823d32cd604d093815c2 100644 --- a/modules/base/src/path.hh +++ b/modules/base/src/path.hh @@ -44,8 +44,10 @@ public: Path (const String& path): path_(path) {}; String GetFullPath() const { return path_; } operator std::string() const { return path_;} - Path& operator/(const String& str); - Path& operator/(const Path& path); + Path operator/(const Path& path) const; + Path operator/(const String& path) const; + Path& operator/=(const Path& path); + Path& operator/=(const String& path); String GetDirName() const; String GetFileName() const; String GetExtension() const; @@ -58,6 +60,23 @@ private: String path_; }; + +Path RootPath() { +#ifdef WIN32 + return Path("c:\\"); +#else + return Path("/"); +#endif +} + +#ifdef WIN32 + +Path DiscRootPath(char disc) { + return Path("\"); +} + +#endif + } // ost #endif // OST_PATH_REF diff --git a/modules/base/tests/test_path.cc b/modules/base/tests/test_path.cc index 24af23e9205ba0149c21de147b685af916121e8d..4c78f625d719e98e5b05e1ca360931335dd30d3f 100644 --- a/modules/base/tests/test_path.cc +++ b/modules/base/tests/test_path.cc @@ -27,6 +27,7 @@ #include <sys/stat.h> #include <iostream> #include <fstream> +#include <stdio.h> using namespace ost; @@ -58,23 +59,31 @@ BOOST_AUTO_TEST_CASE(test_operators) full_path2 += "another"; full_path2 += OST_DIRECTORY_SEPARATOR; full_path2 += fullpath_end; - Path test_path1("this"); - test_path1 / "is" / "a" / "test" / "path"; + Path test_path1r = test_path1 / "is" / "a" / "test" / "path"; + BOOST_CHECK(test_path1r.GetFullPath()==full_path1); + test_path1 /= "is"; + test_path1 /= "a"; + test_path1 /= "test"; + test_path1 /= "path"; BOOST_CHECK(test_path1.GetFullPath()==full_path1); Path test_path2b("is"); Path test_path2c("another"); Path test_path2d("test"); Path test_path2e("path"); Path test_path2("this"); - test_path2 / test_path2b / test_path2c / test_path2d / test_path2e; + Path test_path2r = test_path2 / test_path2b / test_path2c / test_path2d / test_path2e; + BOOST_CHECK(test_path2r.GetFullPath()==full_path2); + test_path2 /= test_path2b /= test_path2c /= test_path2d /= test_path2e; BOOST_CHECK(test_path2.GetFullPath()==full_path2); } BOOST_AUTO_TEST_CASE(test_path_disassembling_operators) { - Path test_path(""); - test_path / "etc" / "rc.d" / "rc.conf"; + Path test_path = RootPath(); + test_path /= "etc"; + test_path /= "rc.d"; + test_path /= "rc.conf"; String dirname(""); dirname += OST_DIRECTORY_SEPARATOR; dirname += "etc"; @@ -85,7 +94,8 @@ BOOST_AUTO_TEST_CASE(test_path_disassembling_operators) BOOST_CHECK(test_path.GetFileName()=="rc.conf"); BOOST_CHECK(test_path.GetExtension()=="conf"); Path test_path2("etc"); - test_path2 / "rc.d" / ""; + test_path2 /= "rc.d"; + test_path2 /= ""; String dirname2("etc"); dirname2 += OST_DIRECTORY_SEPARATOR; dirname2 += "rc.d"; @@ -100,10 +110,10 @@ BOOST_AUTO_TEST_CASE(test_existence_and_accessibility) { std::ofstream filestr; Path filename("testfiles"); - filename / "path_test_file.txt"; + filename /= "path_test_file.txt"; Path test_path("testfiles"); - test_path / "path_test_file.txt"; - BOOST_CHECK(test_path.Exists()==false); + test_path /= "path_test_file.txt"; + std::cout << filename.GetFullPath() << std::endl; filestr.open(filename.GetFullPath().c_str()); filestr << "This file is created by the unitest for the Path class" << std::endl; filestr.close(); @@ -113,10 +123,11 @@ BOOST_AUTO_TEST_CASE(test_existence_and_accessibility) #ifdef WIN32 // Put windows code here #else - chmod(test_path.GetFullPath().c_str(),S_IRUSR | S_IRGRP | S_IROTH); BOOST_CHECK(test_path.IsWritable()==false); chmod(test_path.GetFullPath().c_str(),S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + remove(test_path.GetFullPath().c_str()); + BOOST_CHECK(test_path.Exists()==false); #endif }