CARLA
Loading...
Searching...
No Matches
FileSystem.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 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 "carla/FileSystem.h"
8
9#include "carla/Exception.h"
10#include "carla/StringUtil.h"
11
12#include <filesystem>
13
14namespace carla {
15
16 namespace fs = std::filesystem;
17
18 void FileSystem::ValidateFilePath(std::string &filepath, const std::string &ext) {
19 fs::path path(filepath);
20 if (!ext.empty() && path.extension() != ext)
21 path.replace_extension(ext);
22 auto parent = fs::absolute(path.parent_path());
23 if (!fs::exists(parent))
24 fs::create_directories(parent);
25 filepath = path.string();
26 }
27
28 std::vector<std::string> FileSystem::ListFolder(
29 const std::string &folder_path,
30 const std::string &wildcard_pattern) {
31 fs::path root(folder_path);
32 if (!fs::exists(root) || !fs::is_directory(root)) {
33 throw_exception(std::invalid_argument(folder_path + ": no such folder"));
34 }
35
36 std::vector<std::string> results;
37 fs::directory_iterator end;
38 for (fs::directory_iterator it(root); it != end; ++it) {
39 if (fs::is_regular_file(*it)) {
40 const std::string filename = it->path().filename().string();
41 if (StringUtil::Match(filename, wildcard_pattern)) {
42 results.emplace_back(filename);
43 }
44 }
45 }
46 return results;
47 }
48
49} // namespace carla
static std::vector< std::string > ListFolder(const std::string &folder_path, const std::string &wildcard_pattern)
List (not recursively) regular files at folder_path matching wildcard_pattern.
static void ValidateFilePath(std::string &filepath, const std::string &default_extension="")
Convenient function to validate a path before creating a file.
static bool Match(const char *str, const char *wildcard_pattern)
Match str with the Unix shell-style wildcard_pattern.
This file contains definitions of common data structures used in traffic manager.
void throw_exception(const ExceptionT &e)
Templated on the static type so the thrown object keeps its dynamic type: taking a const std::excepti...
Definition Exception.h:44