Move implementation of class to .cpp
This commit is contained in:
parent
5850bfcbdf
commit
71102a2336
|
@ -5,14 +5,62 @@
|
||||||
// For a copy, see <https://opensource.org/licenses/MIT>.
|
// For a copy, see <https://opensource.org/licenses/MIT>.
|
||||||
|
|
||||||
#include "FileTransfer.h"
|
#include "FileTransfer.h"
|
||||||
|
|
||||||
namespace carla {
|
namespace carla {
|
||||||
namespace client {
|
namespace client {
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::string FileTransfer::_filesBaseFolder = std::string(getenv("USERPROFILE")) + "/carlaCache/";
|
std::string FileTransfer::_filesBaseFolder = std::string(getenv("USERPROFILE")) + "/carlaCache/";
|
||||||
#else
|
#else
|
||||||
std::string FileTransfer::_filesBaseFolder = std::string(getenv("HOME")) + "/carlaCache/";
|
std::string FileTransfer::_filesBaseFolder = std::string(getenv("HOME")) + "/carlaCache/";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
bool FileTransfer::SetFilesBaseFolder(const std::string &path) {
|
||||||
|
if (path.empty()) return false;
|
||||||
|
|
||||||
|
// Check that the path ends in a slash, add it otherwise
|
||||||
|
if (path[path.size() - 1] != '/' && path[path.size() - 1] != '\\') {
|
||||||
|
_filesBaseFolder = path + "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& FileTransfer::GetFilesBaseFolder() {
|
||||||
|
return _filesBaseFolder;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FileTransfer::FileExists(std::string file) {
|
||||||
|
// Check if the file exists or not
|
||||||
|
struct stat buffer;
|
||||||
|
return (stat((_filesBaseFolder + file).c_str(), &buffer) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FileTransfer::WriteFile(std::string path, std::vector<uint8_t> content) {
|
||||||
|
std::string writePath = _filesBaseFolder + path;
|
||||||
|
|
||||||
|
// Validate and create the file path
|
||||||
|
carla::FileSystem::ValidateFilePath(writePath);
|
||||||
|
|
||||||
|
// Open the file to truncate it in binary mode
|
||||||
|
std::ofstream out(writePath, std::ios::trunc | std::ios::binary);
|
||||||
|
if(!out.good()) return false;
|
||||||
|
|
||||||
|
// Write the content on and close it
|
||||||
|
for(auto file : content) {
|
||||||
|
out << file;
|
||||||
|
}
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<uint8_t> FileTransfer::ReadFile(std::string path) {
|
||||||
|
// Read the binary file from the base folder
|
||||||
|
std::ifstream file(_filesBaseFolder + path, std::ios::binary);
|
||||||
|
std::vector<uint8_t> content(std::istreambuf_iterator<char>(file), {});
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace client
|
} // namespace client
|
||||||
} // namespace carla
|
} // namespace carla
|
||||||
|
|
|
@ -22,52 +22,15 @@ namespace client {
|
||||||
|
|
||||||
FileTransfer() = delete;
|
FileTransfer() = delete;
|
||||||
|
|
||||||
static bool SetFilesBaseFolder(const std::string &path) {
|
static bool SetFilesBaseFolder(const std::string &path);
|
||||||
if (path.empty()) return false;
|
|
||||||
|
|
||||||
// Check that the path ends in a slash, add it otherwise
|
static const std::string& GetFilesBaseFolder();
|
||||||
if (path[path.size() - 1] != '/' && path[path.size() - 1] != '\\') {
|
|
||||||
_filesBaseFolder = path + "/";
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
static bool FileExists(std::string file);
|
||||||
}
|
|
||||||
|
|
||||||
static const std::string& GetFilesBaseFolder() {
|
static bool WriteFile(std::string path, std::vector<uint8_t> content);
|
||||||
return _filesBaseFolder;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool FileExists(std::string file) {
|
static std::vector<uint8_t> ReadFile(std::string path);
|
||||||
// Check if the file exists or not
|
|
||||||
struct stat buffer;
|
|
||||||
return (stat((_filesBaseFolder + file).c_str(), &buffer) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool WriteFile(std::string path, std::vector<uint8_t> content) {
|
|
||||||
std::string writePath = _filesBaseFolder + path;
|
|
||||||
|
|
||||||
// Validate and create the file path
|
|
||||||
carla::FileSystem::ValidateFilePath(writePath);
|
|
||||||
|
|
||||||
// Open the file to truncate it in binary mode
|
|
||||||
std::ofstream out(writePath, std::ios::trunc | std::ios::binary);
|
|
||||||
if(!out.good()) return false;
|
|
||||||
|
|
||||||
// Write the content on and close it
|
|
||||||
for(auto file : content) {
|
|
||||||
out << file;
|
|
||||||
}
|
|
||||||
out.close();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::vector<uint8_t> ReadFile(std::string path) {
|
|
||||||
// Read the binary file from the base folder
|
|
||||||
std::ifstream file(_filesBaseFolder + path, std::ios::binary);
|
|
||||||
std::vector<uint8_t> content(std::istreambuf_iterator<char>(file), {});
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue