Move implementation of class to .cpp

This commit is contained in:
bernat 2021-07-20 20:01:44 +02:00
parent 5850bfcbdf
commit 71102a2336
2 changed files with 59 additions and 48 deletions

View File

@ -5,14 +5,62 @@
// For a copy, see <https://opensource.org/licenses/MIT>.
#include "FileTransfer.h"
namespace carla {
namespace client {
#ifdef _WIN32
std::string FileTransfer::_filesBaseFolder = std::string(getenv("USERPROFILE")) + "/carlaCache/";
#else
std::string FileTransfer::_filesBaseFolder = std::string(getenv("HOME")) + "/carlaCache/";
#endif
#ifdef _WIN32
std::string FileTransfer::_filesBaseFolder = std::string(getenv("USERPROFILE")) + "/carlaCache/";
#else
std::string FileTransfer::_filesBaseFolder = std::string(getenv("HOME")) + "/carlaCache/";
#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 carla
} // namespace carla

View File

@ -22,52 +22,15 @@ namespace client {
FileTransfer() = delete;
static bool SetFilesBaseFolder(const std::string &path) {
if (path.empty()) return false;
static bool SetFilesBaseFolder(const std::string &path);
// Check that the path ends in a slash, add it otherwise
if (path[path.size() - 1] != '/' && path[path.size() - 1] != '\\') {
_filesBaseFolder = path + "/";
}
static const std::string& GetFilesBaseFolder();
return true;
}
static bool FileExists(std::string file);
static const std::string& GetFilesBaseFolder() {
return _filesBaseFolder;
}
static bool WriteFile(std::string path, std::vector<uint8_t> content);
static bool FileExists(std::string file) {
// 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;
}
static std::vector<uint8_t> ReadFile(std::string path);
private: