CARLA
FIleTransfer.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #include "FileTransfer.h"
8 #include "carla/Version.h"
9 
10 namespace carla {
11 namespace client {
12 
13  #ifdef _WIN32
14  std::string FileTransfer::_filesBaseFolder = std::string(getenv("USERPROFILE")) + "/carlaCache/";
15  #else
16  std::string FileTransfer::_filesBaseFolder = std::string(getenv("HOME")) + "/carlaCache/";
17  #endif
18 
19  bool FileTransfer::SetFilesBaseFolder(const std::string &path) {
20  if (path.empty()) return false;
21 
22  // Check that the path ends in a slash, add it otherwise
23  if (path[path.size() - 1] != '/' && path[path.size() - 1] != '\\') {
24  _filesBaseFolder = path + "/";
25  }
26 
27  return true;
28  }
29 
30  const std::string& FileTransfer::GetFilesBaseFolder() {
31  return _filesBaseFolder;
32  }
33 
34  bool FileTransfer::FileExists(std::string file) {
35  // Check if the file exists or not
36  struct stat buffer;
37  std::string fullpath = _filesBaseFolder;
38  fullpath += "/";
39  fullpath += ::carla::version();
40  fullpath += "/";
41  fullpath += file;
42 
43  return (stat(fullpath.c_str(), &buffer) == 0);
44  }
45 
46  bool FileTransfer::WriteFile(std::string path, std::vector<uint8_t> content) {
47  std::string writePath = _filesBaseFolder;
48  writePath += "/";
49  writePath += ::carla::version();
50  writePath += "/";
51  writePath += path;
52 
53  // Validate and create the file path
55 
56  // Open the file to truncate it in binary mode
57  std::ofstream out(writePath, std::ios::trunc | std::ios::binary);
58  if(!out.good()) return false;
59 
60  // Write the content on and close it
61  for(auto file : content) {
62  out << file;
63  }
64  out.close();
65 
66  return true;
67  }
68 
69  std::vector<uint8_t> FileTransfer::ReadFile(std::string path) {
70  std::string fullpath = _filesBaseFolder;
71  fullpath += "/";
72  fullpath += ::carla::version();
73  fullpath += "/";
74  fullpath += path;
75  // Read the binary file from the base folder
76  std::ifstream file(fullpath, std::ios::binary);
77  std::vector<uint8_t> content(std::istreambuf_iterator<char>(file), {});
78  return content;
79  }
80 
81 } // namespace client
82 } // namespace carla
static void ValidateFilePath(std::string &filepath, const std::string &default_extension="")
Convenient function to validate a path before creating a file.
Definition: FileSystem.cpp:18
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
static std::vector< uint8_t > ReadFile(std::string path)
static const std::string & GetFilesBaseFolder()
static bool WriteFile(std::string path, std::vector< uint8_t > content)
static std::string _filesBaseFolder
Definition: FileTransfer.h:37
static bool FileExists(std::string file)
static bool SetFilesBaseFolder(const std::string &path)