From 5922808e12c8b4fc95e87f32a0c8e1a679e569c1 Mon Sep 17 00:00:00 2001 From: kelson42 Date: Tue, 1 Nov 2011 12:25:33 +0000 Subject: [PATCH] + copyFile() & getLastPathElement() --- src/common/pathTools.cpp | 25 ++++++++++++++++++++++++- src/common/pathTools.h | 3 +++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/common/pathTools.cpp b/src/common/pathTools.cpp index 356669c3f..cbe1158f9 100644 --- a/src/common/pathTools.cpp +++ b/src/common/pathTools.cpp @@ -33,7 +33,7 @@ string computeAbsolutePath(const string path, const string relativePath) { #else string separator = "/"; #endif - string absolutePath = path; + string absolutePath = path + "/"; char *cRelativePath = strdup(relativePath.c_str()); char *token = strtok(cRelativePath, "/"); @@ -70,6 +70,16 @@ string removeLastPathElement(const string path, const bool removePreSeparator, c newPath = removePostSeparator ? newPath.substr(0, offset) : newPath.substr(0, offset+1); return newPath; } + +string getLastPathElement(const string &path) { +#ifdef _WIN32 + string separator = "\\"; +#else + string separator = "/"; +#endif + + return path.substr(path.find_last_of(separator)); +} unsigned int getFileSize(const string &path) { struct stat filestatus; @@ -98,3 +108,16 @@ bool makeDirectory(const string &path) { int status = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); return status == 0; } + +bool copyFile(const string &sourcePath, const string &destPath) { + try { + std::ifstream infile(sourcePath.c_str(), std::ios_base::binary); + std::ofstream outfile(destPath.c_str(), std::ios_base::binary); + outfile << infile.rdbuf(); + } catch (exception &e) { + cerr << e.what() << endl; + return false; + } + + return true; +} diff --git a/src/common/pathTools.h b/src/common/pathTools.h index 68f3dc0a4..f87155a77 100644 --- a/src/common/pathTools.h +++ b/src/common/pathTools.h @@ -28,6 +28,7 @@ #include #include #include +#include using namespace std; @@ -39,5 +40,7 @@ unsigned int getFileSize(const string &path); string getFileSizeAsString(const string &path); bool fileExists(const string &path); bool makeDirectory(const string &path); +bool copyFile(const string &sourcePath, const string &destPath); +string getLastPathElement(const string &path); #endif