CARLA
 
载入中...
搜索中...
未找到
FIleTransfer.cpp
浏览该文件的文档.
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" // 引入FileTransfer.h头文件,该文件包含文件传输功能的声明
8#include "carla/Version.h" // 引入carla版本信息头文件,用于获取当前Carla的版本
9
10namespace carla {
11namespace client {
12
13 // 根据操作系统设置文件存储的基础文件夹路径
14 #ifdef _WIN32
15 std::string FileTransfer::_filesBaseFolder = std::string(getenv("USERPROFILE")) + "/carlaCache/";
16 #else
17 std::string FileTransfer::_filesBaseFolder = std::string(getenv("HOME")) + "/carlaCache/";
18 #endif
19 // 设置文件传输的基文件夹路径,确保路径以斜杠结尾
20 bool FileTransfer::SetFilesBaseFolder(const std::string &path) {
21 // 首先判断传入的路径字符串是否为空。如果path为空字符串(即没有实际内容,长度为0)
22 // 那就意味着传入的路径不合法或者不符合预期,无法进行有效的基础文件夹路径设置操作
23 // 所以此时函数直接返回false,表示设置失败
24 if (path.empty()) return false;
25
26 // 如果路径不以斜杠结尾,自动添加斜杠
27 if (path[path.size() - 1] != '/' && path[path.size() - 1] != '\\') {
28 _filesBaseFolder = path + "/";
29 }
30
31 return true;
32 }
33
34 // 获取当前的文件基础路径
35 const std::string& FileTransfer::GetFilesBaseFolder() {
36 return _filesBaseFolder;
37 }
38
39 // 检查指定的文件是否存在
40 bool FileTransfer::FileExists(std::string file) {
41 // 构建文件的完整路径
42 struct stat buffer;
43 std::string fullpath = _filesBaseFolder;
44 fullpath += "/";
45 fullpath += ::carla::version(); // 加入当前的Carla版本号
46 fullpath += "/";
47 fullpath += file; // 添加目标文件名
48
49 // 使用 stat 函数检查文件是否存在
50 return (stat(fullpath.c_str(), &buffer) == 0);
51 }
52
53 // 将内容写入指定路径的文件
54 bool FileTransfer::WriteFile(std::string path, std::vector<uint8_t> content) {
55 // 构建文件的完整路径
56 std::string writePath = _filesBaseFolder;
57 writePath += "/";
58 writePath += ::carla::version(); // 加入当前的Carla版本号
59 writePath += "/";
60 writePath += path; // 添加目标文件的路径
61
62 // 验证文件路径并创建所需的目录
64
65 // 以二进制模式打开文件,如果文件不存在则创建
66 std::ofstream out(writePath, std::ios::trunc | std::ios::binary);
67 if(!out.good()) return false;
68
69 // 将内容写入文件
70 for(auto file : content) {
71 out << file;
72 }
73 out.close();
74
75 return true;
76 }
77
78 // 读取指定路径的文件内容,并返回一个字节向量
79 std::vector<uint8_t> FileTransfer::ReadFile(std::string path) {
80 // 构建文件的完整路径
81 std::string fullpath = _filesBaseFolder;
82 fullpath += "/";
83 fullpath += ::carla::version(); // 加入当前的Carla版本号
84 fullpath += "/";
85 fullpath += path; // 添加目标文件路径
86 // 从文件中读取内容并返回字节向量
87 std::ifstream file(fullpath, std::ios::binary);
88 std::vector<uint8_t> content(std::istreambuf_iterator<char>(file), {});
89 return content;
90 }
91
92} // namespace client
93} // namespace carla
static void ValidateFilePath(std::string &filepath, const std::string &default_extension="")
在创建文件之前验证路径的方便函数。
static std::vector< uint8_t > ReadFile(std::string path)
static bool FileExists(std::string file)
static bool SetFilesBaseFolder(const std::string &path)
static const std::string & GetFilesBaseFolder()
static std::string _filesBaseFolder
static bool WriteFile(std::string path, std::vector< uint8_t > content)
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
包含CARLA客户端相关类和函数的命名空间。