CARLA
 
载入中...
搜索中...
未找到
FileSystem.cpp
浏览该文件的文档.
1// Copyright (c) 2017 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// 引入 carla 库中的文件系统相关的头文件
8#include "carla/FileSystem.h"
9
10// 引入 carla 库中的异常处理头文件
11#include "carla/Exception.h"
12// 引入 carla 库中的字符串处理工具头文件
13#include "carla/StringUtil.h"
14
15// 引入 boost 文件系统库的操作相关头文件
16#include <boost/filesystem/operations.hpp>
17
18// 定义命名空间 carla
19namespace carla {
20
21// 引入 boost 文件系统库命名空间,并起别名 fs
22namespace fs = boost::filesystem;
23
24// 函数:ValidateFilePath
25// 作用:验证文件路径,确保路径有扩展名,并创建路径的父目录(如果需要)
26// 验证并修正文件路径
27 void FileSystem::ValidateFilePath(std::string &filepath, const std::string &ext) {
28 // 引用传递的文件路径字符串
29 // 创建一个fs::path对象pat
30 // 初始化为filepath
31 fs::path path(filepath);
32 if (path.extension().empty() && !ext.empty()) {
33 if (ext[0] != '.') {
34 path += '.'; // 在ext前加上'.'
35 }
36 path += ext; // 将ext添加到path
37 }
38 auto parent = path.parent_path(); // 获取path的父目录parent
39 if (!parent.empty()) {
40 fs::create_directories(parent);h // 创建父目录
41 }
42 filepath = path.string();将修正后的路径转换回字符串并赋值给filepath
43 }
44// 函数:ListFolder
45// 作用:列出指定文件夹下符合特定通配符模式的文件列表
46 std::vector<std::string> FileSystem::ListFolder(
47 const std::string &folder_path,
48 const std::string &wildcard_pattern) {
49 // 将输入的文件夹路径转换为 boost::filesystem::path 类型的对象
50 fs::path root(folder_path);
51 // 如果root不存在或不是一个目录,抛出异常
52 if (!fs::exists(root) || !fs::is_directory(root)) {
53 throw_exception(std::invalid_argument(folder_path + ": no such folder"));
54 }
55// 创建一个字符串向量results,用于存储匹配的文件名
56 std::vector<std::string> results;
57 fs::directory_iterator end;// 使用fs::directory_iterator遍历目录中的每个文件和子目录
58 for (fs::directory_iterator it(root); it != end; ++it) {
59 if (fs::is_regular_file(*it)) {
60 const std::string filename = it->path().filename().string();// 对于每个常规文件,获取其文件名filename
61 if (StringUtil::Match(filename, wildcard_pattern)) {
62 results.emplace_back(filename);// 返回匹配的文件名列表results
63 }
64 }
65 }
66 return results;
67 }
68
69} // namespace carla
auto end() const noexcept
static std::vector< std::string > ListFolder(const std::string &folder_path, const std::string &wildcard_pattern)
列出 folder_path 中匹配 wildcard_pattern 的常规文件 (不递归)。
static void ValidateFilePath(std::string &filepath, const std::string &default_extension="")
在创建文件之前验证路径的方便函数。
static bool Match(const char *str, const char *wildcard_pattern)
Match str with the Unix shell-style wildcard_pattern.
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
void throw_exception(const std::exception &e)
Definition Carla.cpp:142