+ copyFile() & getLastPathElement()

This commit is contained in:
kelson42 2011-11-01 12:25:33 +00:00
parent f2280a6b02
commit 5922808e12
2 changed files with 27 additions and 1 deletions

View File

@ -33,7 +33,7 @@ string computeAbsolutePath(const string path, const string relativePath) {
#else #else
string separator = "/"; string separator = "/";
#endif #endif
string absolutePath = path; string absolutePath = path + "/";
char *cRelativePath = strdup(relativePath.c_str()); char *cRelativePath = strdup(relativePath.c_str());
char *token = strtok(cRelativePath, "/"); char *token = strtok(cRelativePath, "/");
@ -71,6 +71,16 @@ string removeLastPathElement(const string path, const bool removePreSeparator, c
return newPath; 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) { unsigned int getFileSize(const string &path) {
struct stat filestatus; struct stat filestatus;
stat(path.c_str(), &filestatus); stat(path.c_str(), &filestatus);
@ -98,3 +108,16 @@ bool makeDirectory(const string &path) {
int status = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); int status = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
return status == 0; 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;
}

View File

@ -28,6 +28,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <ios>
using namespace std; using namespace std;
@ -39,5 +40,7 @@ unsigned int getFileSize(const string &path);
string getFileSizeAsString(const string &path); string getFileSizeAsString(const string &path);
bool fileExists(const string &path); bool fileExists(const string &path);
bool makeDirectory(const string &path); bool makeDirectory(const string &path);
bool copyFile(const string &sourcePath, const string &destPath);
string getLastPathElement(const string &path);
#endif #endif