CARLA
FileSystem.cpp
Go to the documentation of this file.
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 #include "carla/FileSystem.h"
8 
9 #include "carla/Exception.h"
10 #include "carla/StringUtil.h"
11 
12 #include <boost/filesystem/operations.hpp>
13 
14 namespace carla {
15 
16  namespace fs = boost::filesystem;
17 
18  void FileSystem::ValidateFilePath(std::string &filepath, const std::string &ext) {
19  fs::path path(filepath);
20  if (path.extension().empty() && !ext.empty()) {
21  if (ext[0] != '.') {
22  path += '.';
23  }
24  path += ext;
25  }
26  auto parent = path.parent_path();
27  if (!parent.empty()) {
28  fs::create_directories(parent);
29  }
30  filepath = path.string();
31  }
32 
33  std::vector<std::string> FileSystem::ListFolder(
34  const std::string &folder_path,
35  const std::string &wildcard_pattern) {
36  fs::path root(folder_path);
37  if (!fs::exists(root) || !fs::is_directory(root)) {
38  throw_exception(std::invalid_argument(folder_path + ": no such folder"));
39  }
40 
41  std::vector<std::string> results;
42  fs::directory_iterator end;
43  for (fs::directory_iterator it(root); it != end; ++it) {
44  if (fs::is_regular_file(*it)) {
45  const std::string filename = it->path().filename().string();
46  if (StringUtil::Match(filename, wildcard_pattern)) {
47  results.emplace_back(filename);
48  }
49  }
50  }
51  return results;
52  }
53 
54 } // 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
void throw_exception(const std::exception &e)
Definition: Carla.cpp:135
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
static bool Match(const char *str, const char *wildcard_pattern)
Match str with the Unix shell-style wildcard_pattern.
Definition: StringUtil.cpp:17
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.
Definition: FileSystem.cpp:33