ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Paths.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Paths.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
12 #include <boost/filesystem.hpp>
13 #include <charconv>
14 #include <cstdio>
15 #include <regex>
16 #include <sstream>
17 #include <stdexcept>
18 
19 std::string FW::ensureWritableDirectory(const std::string& dir) {
20  using namespace boost::filesystem;
21 
22  auto dir_path = dir.empty() ? current_path() : path(dir);
23  if (exists(dir_path) and not is_directory(dir_path)) {
24  throw std::runtime_error("'" + dir +
25  "' already exists but is not a directory");
26  }
27  create_directories(dir_path);
28  return canonical(dir_path).native();
29 }
30 
31 std::string FW::joinPaths(const std::string& dir, const std::string& name) {
32  if (dir.empty()) {
33  return name;
34  } else {
35  return dir + '/' + name;
36  }
37 }
38 
39 std::string FW::perEventFilepath(const std::string& dir,
40  const std::string& name, size_t event) {
41  char prefix[64];
42 
43  snprintf(prefix, sizeof(prefix), "event%09zu-", event);
44 
45  if (dir.empty()) {
46  return prefix + name;
47  } else {
48  return dir + '/' + prefix + name;
49  }
50 }
51 
52 std::pair<size_t, size_t> FW::determineEventFilesRange(
53  const std::string& dir, const std::string& name) {
54  using namespace boost::filesystem;
55 
58 
59  // ensure directory path is valid
60  auto dir_path = dir.empty() ? current_path() : path(dir);
61  if (not exists(dir_path)) {
62  throw std::runtime_error("'" + dir_path.native() + "' does not exists");
63  }
64  if (not is_directory(dir_path)) {
65  throw std::runtime_error("'" + dir_path.native() + "' is not a directory");
66  }
67 
68  // invalid default range that allows simple restriction later on
69  size_t eventMin = SIZE_MAX;
70  size_t eventMax = 0;
71 
72  // filter matching event files from the directory listing
73  std::string filename;
74  std::regex re("^event([0-9]+)-" + name + "$");
75  std::cmatch match;
76 
77  for (const auto& f : directory_iterator(dir_path)) {
78  if ((not exists(f.status())) or (not is_regular_file(f.status()))) {
79  continue;
80  }
81  // keep a copy so the match can refer to the underlying const char*
82  filename = f.path().filename().native();
83  if (std::regex_match(filename.c_str(), match, re)) {
84  ACTS_VERBOSE("Matching file " << filename);
85 
86  // first sub_match is the whole string, second should be the event number
87  size_t event = 0;
88  auto ret = std::from_chars(match[1].first, match[1].second, event);
89  if (ret.ptr == match[1].first) {
90  throw std::runtime_error(
91  "Could not extract event number from filename");
92  }
93  // enlarge available event range
94  eventMin = std::min(eventMin, event);
95  eventMax = std::max(eventMax, event);
96  }
97  }
98  ACTS_VERBOSE("Detected event range [" << eventMin << "," << eventMax << "]");
99 
100  // should only occur if no files matched and the initial values persisted.
101  if (eventMax < eventMin) {
102  return std::make_pair(0u, 0u);
103  }
104  return std::make_pair(eventMin, eventMax + 1);
105 }