Skip to content
Snippets Groups Projects
Commit 257ed509 authored by Valerio Mariani's avatar Valerio Mariani
Browse files

Improved Path class

parent 3de39d3e
Branches
No related tags found
No related merge requests found
...@@ -28,24 +28,38 @@ ...@@ -28,24 +28,38 @@
namespace ost { namespace ost {
Path& Path::operator/(const String& str)
Path& Path::operator/=(const Path& path)
{ {
this->path_ += OST_DIRECTORY_SEPARATOR; if (path_.size()==0 || (path_[path_.size()-1] != OST_DIRECTORY_SEPARATOR)) {
this->path_ += str; this->path_ += OST_DIRECTORY_SEPARATOR;
}
this->path_ += path.GetFullPath();
return *this; return *this;
} }
Path& Path::operator/(const Path& path) Path& Path::operator/=(const String& str)
{ {
this->path_ += OST_DIRECTORY_SEPARATOR; if (path_.size()==0 || (path_[path_.size()-1] != OST_DIRECTORY_SEPARATOR)) {
this->path_ += path.GetFullPath(); this->path_ += OST_DIRECTORY_SEPARATOR;
}
this->path_ += str;
return *this; 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); Path retpath(*this);
return p1/str2; retpath /= str;
return retpath;
} }
String Path::GetFileName() const String Path::GetFileName() const
...@@ -79,14 +93,13 @@ String Path::GetExtension() const ...@@ -79,14 +93,13 @@ String Path::GetExtension() const
return (filename_sr_split.rbegin())->str(); return (filename_sr_split.rbegin())->str();
} }
#ifdef WIN32 #ifdef WIN32
// Insert Windows Code Here // Insert Windows Code Here
#else #else
String Path::GetAbsolutePath() const String Path::GetAbsolutePath() const
{ {
if (path_[0]==OST_DIRECTORY_SEPARATOR) { if (path_.size() !=0 && path_[0]==OST_DIRECTORY_SEPARATOR) {
return path_; return path_;
} }
char path[MAXPATHLEN]; // This is a buffer for the text char path[MAXPATHLEN]; // This is a buffer for the text
...@@ -99,20 +112,15 @@ String Path::GetAbsolutePath() const ...@@ -99,20 +112,15 @@ String Path::GetAbsolutePath() const
bool Path::Exists() const bool Path::Exists() const
{ {
if (access(path_.c_str(), F_OK)==0) if (access(path_.c_str(), F_OK)==0) {
{ return true;
return true;
} }
return false; return false;
} }
bool Path::IsWritable() const bool Path::IsWritable() const
{ {
if (access(path_.c_str(), W_OK)==0) return (access(path_.c_str(), W_OK)==0);
{
return true;
}
return false;
} }
#endif #endif
......
...@@ -44,8 +44,10 @@ public: ...@@ -44,8 +44,10 @@ public:
Path (const String& path): path_(path) {}; Path (const String& path): path_(path) {};
String GetFullPath() const { return path_; } String GetFullPath() const { return path_; }
operator std::string() const { return path_;} operator std::string() const { return path_;}
Path& operator/(const String& str); Path operator/(const Path& path) const;
Path& operator/(const Path& path); Path operator/(const String& path) const;
Path& operator/=(const Path& path);
Path& operator/=(const String& path);
String GetDirName() const; String GetDirName() const;
String GetFileName() const; String GetFileName() const;
String GetExtension() const; String GetExtension() const;
...@@ -58,6 +60,23 @@ private: ...@@ -58,6 +60,23 @@ private:
String path_; String path_;
}; };
Path RootPath() {
#ifdef WIN32
return Path("c:\\");
#else
return Path("/");
#endif
}
#ifdef WIN32
Path DiscRootPath(char disc) {
return Path("\");
}
#endif
} // ost } // ost
#endif // OST_PATH_REF #endif // OST_PATH_REF
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <stdio.h>
using namespace ost; using namespace ost;
...@@ -58,23 +59,31 @@ BOOST_AUTO_TEST_CASE(test_operators) ...@@ -58,23 +59,31 @@ BOOST_AUTO_TEST_CASE(test_operators)
full_path2 += "another"; full_path2 += "another";
full_path2 += OST_DIRECTORY_SEPARATOR; full_path2 += OST_DIRECTORY_SEPARATOR;
full_path2 += fullpath_end; full_path2 += fullpath_end;
Path test_path1("this"); 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); BOOST_CHECK(test_path1.GetFullPath()==full_path1);
Path test_path2b("is"); Path test_path2b("is");
Path test_path2c("another"); Path test_path2c("another");
Path test_path2d("test"); Path test_path2d("test");
Path test_path2e("path"); Path test_path2e("path");
Path test_path2("this"); 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_CHECK(test_path2.GetFullPath()==full_path2);
} }
BOOST_AUTO_TEST_CASE(test_path_disassembling_operators) BOOST_AUTO_TEST_CASE(test_path_disassembling_operators)
{ {
Path test_path(""); Path test_path = RootPath();
test_path / "etc" / "rc.d" / "rc.conf"; test_path /= "etc";
test_path /= "rc.d";
test_path /= "rc.conf";
String dirname(""); String dirname("");
dirname += OST_DIRECTORY_SEPARATOR; dirname += OST_DIRECTORY_SEPARATOR;
dirname += "etc"; dirname += "etc";
...@@ -85,7 +94,8 @@ BOOST_AUTO_TEST_CASE(test_path_disassembling_operators) ...@@ -85,7 +94,8 @@ BOOST_AUTO_TEST_CASE(test_path_disassembling_operators)
BOOST_CHECK(test_path.GetFileName()=="rc.conf"); BOOST_CHECK(test_path.GetFileName()=="rc.conf");
BOOST_CHECK(test_path.GetExtension()=="conf"); BOOST_CHECK(test_path.GetExtension()=="conf");
Path test_path2("etc"); Path test_path2("etc");
test_path2 / "rc.d" / ""; test_path2 /= "rc.d";
test_path2 /= "";
String dirname2("etc"); String dirname2("etc");
dirname2 += OST_DIRECTORY_SEPARATOR; dirname2 += OST_DIRECTORY_SEPARATOR;
dirname2 += "rc.d"; dirname2 += "rc.d";
...@@ -100,10 +110,10 @@ BOOST_AUTO_TEST_CASE(test_existence_and_accessibility) ...@@ -100,10 +110,10 @@ BOOST_AUTO_TEST_CASE(test_existence_and_accessibility)
{ {
std::ofstream filestr; std::ofstream filestr;
Path filename("testfiles"); Path filename("testfiles");
filename / "path_test_file.txt"; filename /= "path_test_file.txt";
Path test_path("testfiles"); Path test_path("testfiles");
test_path / "path_test_file.txt"; test_path /= "path_test_file.txt";
BOOST_CHECK(test_path.Exists()==false); std::cout << filename.GetFullPath() << std::endl;
filestr.open(filename.GetFullPath().c_str()); filestr.open(filename.GetFullPath().c_str());
filestr << "This file is created by the unitest for the Path class" << std::endl; filestr << "This file is created by the unitest for the Path class" << std::endl;
filestr.close(); filestr.close();
...@@ -113,10 +123,11 @@ BOOST_AUTO_TEST_CASE(test_existence_and_accessibility) ...@@ -113,10 +123,11 @@ BOOST_AUTO_TEST_CASE(test_existence_and_accessibility)
#ifdef WIN32 #ifdef WIN32
// Put windows code here // Put windows code here
#else #else
chmod(test_path.GetFullPath().c_str(),S_IRUSR | S_IRGRP | S_IROTH); chmod(test_path.GetFullPath().c_str(),S_IRUSR | S_IRGRP | S_IROTH);
BOOST_CHECK(test_path.IsWritable()==false); BOOST_CHECK(test_path.IsWritable()==false);
chmod(test_path.GetFullPath().c_str(),S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 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 #endif
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment