filesystem.cpp
Go to the documentation of this file.
1 #include "filesystem.h"
2 
3 #include <fstream>
4 #include <iostream>
5 
6 #include <SimoxUtility/algorithm/string.h>
7 
9 
12 
13 namespace armarx::armem::server::ltm ::util::fs
14 {
15 
16  namespace detail
17  {
18  std::string
19  escapeName(const std::string& segmentName)
20  {
21  std::string ret = segmentName;
22  //simox::alg::replace_all(ret, Prefix, PrefixEscaped);
23  for (const auto& [s, r] : EscapeTable)
24  {
25  ret = simox::alg::replace_all(ret, s, r);
26  }
27  return ret;
28  }
29 
30  std::string
31  unescapeName(const std::string& escapedName)
32  {
33  std::string ret = escapedName;
34  for (
35  const auto& [s, r] :
36  EscapeTable) // Here we assume that noone uses the replaced char usually in the segment name... TODO
37  {
38  ret = simox::alg::replace_all(ret, r, s);
39  }
40  return ret;
41  }
42 
43  std::string
44  extractLastDirectoryFromPath(const std::string& path)
45  {
46  size_t pos = path.rfind('/');
47 
48  if (pos != std::string::npos)
49  {
50  return path.substr(pos + 1);
51  }
52  else
53  {
54  return path;
55  }
56  }
57 
58  std::string
59  toDayString(const Time& t)
60  {
61  return t.toDateString();
62  }
63 
64  std::string
66  {
68  }
69 
70  bool
71  isNumberString(const std::string& s)
72  {
73  for (char const& ch : s)
74  {
75  if (std::isdigit(ch) == 0)
76  {
77  return false;
78  }
79  }
80  return true;
81  }
82 
83  bool
84  isDateString(const std::string& s)
85  {
86  auto split = simox::alg::split(s, "-");
87  if (split.size() != 3)
88  {
89  return false;
90  }
91 
93  }
94  } // namespace detail
95 
96  std::filesystem::path
97  toPath(const std::filesystem::path& base, const armem::MemoryID& id)
98  {
99  ARMARX_CHECK(id.isWellDefined());
100 
101  std::filesystem::path p = base;
102  if (id.hasMemoryName())
103  {
105  }
106  if (id.hasCoreSegmentName())
107  {
108  p /= detail::escapeName(id.coreSegmentName);
109  }
110  if (id.hasProviderSegmentName())
111  {
112  p /= detail::escapeName(id.providerSegmentName);
113  }
114  if (id.hasEntityName())
115  {
116  p /= detail::escapeName(id.entityName);
117  }
118  if (id.hasTimestamp())
119  {
122  p /= id.timestampStr();
123  }
124  if (id.hasInstanceIndex())
125  {
126  p /= id.instanceIndexStr();
127  }
128 
129  return p;
130  }
131 
132  bool
133  directoryExists(const std::filesystem::path& p)
134  {
135  return std::filesystem::exists(p) and std::filesystem::is_directory(p);
136  }
137 
138  bool
139  fileExists(const std::filesystem::path& p)
140  {
141  return std::filesystem::exists(p) && std::filesystem::is_regular_file(p);
142  }
143 
144  void
145  ensureDirectoryExists(const std::filesystem::path& p, bool createIfNotExistent)
146  {
147  if (!directoryExists(p))
148  {
149  if (createIfNotExistent)
150  {
151  std::filesystem::create_directories(p);
152  }
153  else
154  {
155  throw armarx::LocalException("Directory existence cannot be ensured: " +
156  p.string());
157  }
158  }
159  }
160 
161  void
162  ensureFileExists(const std::filesystem::path& p, bool createIfNotExistent)
163  {
164  ensureDirectoryExists(p.parent_path(), createIfNotExistent);
165  if (!fileExists(p))
166  {
167  if (createIfNotExistent)
168  {
169  std::string content = "";
170  writeDataToFile(p, {content.begin(), content.end()});
171  }
172  else
173  {
174  // not found
175  throw armarx::LocalException("Could not find file: " + p.string());
176  }
177  }
178  }
179 
180  void
181  writeDataToFile(const std::filesystem::path& p, const std::vector<unsigned char>& data)
182  {
183  std::ofstream dataofs;
184  dataofs.open(p);
185  if (!dataofs)
186  {
187  throw armarx::LocalException("Could not write data to filesystem file '" + p.string() +
188  "'. Skipping this file.");
189  }
190  dataofs.write(reinterpret_cast<const char*>(data.data()), data.size());
191  dataofs.close();
192  }
193 
194  std::vector<unsigned char>
195  readDataFromFile(const std::filesystem::path& p)
196  {
197  std::ifstream dataifs(p);
198  std::vector<unsigned char> datafilecontent((std::istreambuf_iterator<char>(dataifs)),
199  (std::istreambuf_iterator<char>()));
200  dataifs.close();
201  return datafilecontent;
202  }
203 
204  std::vector<std::filesystem::path>
205  getAllDirectories(const std::filesystem::path& p)
206  {
207  std::vector<std::filesystem::path> ret;
208  for (const auto& subdir : std::filesystem::directory_iterator(p))
209  {
210  std::filesystem::path subdirPath = subdir.path();
211  if (std::filesystem::is_directory(subdirPath))
212  {
213  ret.push_back(subdirPath);
214  }
215  }
216  std::sort(ret.begin(),
217  ret.end(),
218  [](const std::filesystem::path& a, const std::filesystem::path& b) -> bool
219  { return a.string() < b.string(); });
220  return ret;
221  }
222 
223  std::vector<std::filesystem::path>
224  getAllFiles(const std::filesystem::path& p)
225  {
226  std::vector<std::filesystem::path> ret;
227  for (const auto& subdir : std::filesystem::directory_iterator(p))
228  {
229  std::filesystem::path subdirPath = subdir.path();
230  if (std::filesystem::is_regular_file(subdirPath))
231  {
232  ret.push_back(subdirPath);
233  }
234  }
235  std::sort(ret.begin(),
236  ret.end(),
237  [](const std::filesystem::path& a, const std::filesystem::path& b) -> bool
238  { return a.string() > b.string(); });
239  return ret;
240  }
241 } // namespace armarx::armem::server::ltm::util::fs
armarx::armem::server::ltm ::util::fs::detail::toSecondsString
std::string toSecondsString(const Time &t)
Definition: filesystem.cpp:65
armarx::armem::server::ltm ::util::fs::readDataFromFile
std::vector< unsigned char > readDataFromFile(const std::filesystem::path &p)
Definition: filesystem.cpp:195
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:13
armarx::core::time::DateTime::toDateString
std::string toDateString() const
Definition: DateTime.cpp:63
armarx::armem::server::ltm ::util::fs::detail::extractLastDirectoryFromPath
std::string extractLastDirectoryFromPath(const std::string &path)
Definition: filesystem.cpp:44
armarx::armem::server::ltm ::util::fs::detail::unescapeName
std::string unescapeName(const std::string &escapedName)
Definition: filesystem.cpp:31
MemoryID.h
armarx::armem::server::ltm ::util::fs::ensureDirectoryExists
void ensureDirectoryExists(const std::filesystem::path &p, bool createIfNotExistent)
Definition: filesystem.cpp:145
armarx::armem::server::ltm ::util::fs::writeDataToFile
void writeDataToFile(const std::filesystem::path &p, const std::vector< unsigned char > &data)
Definition: filesystem.cpp:181
armarx::armem::server::ltm ::util::fs::detail::escapeName
std::string escapeName(const std::string &segmentName)
Definition: filesystem.cpp:19
detail
Definition: OpenCVUtil.cpp:128
armarx::core::time::DateTime::toSecondsSinceEpoch
std::int64_t toSecondsSinceEpoch() const
Definition: DateTime.cpp:99
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
armarx::armem::server::ltm ::util::fs::detail::isNumberString
bool isNumberString(const std::string &s)
Definition: filesystem.cpp:71
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::server::ltm ::util::fs::directoryExists
bool directoryExists(const std::filesystem::path &p)
Definition: filesystem.cpp:133
armarx::armem::server::ltm ::util::fs::ensureFileExists
void ensureFileExists(const std::filesystem::path &p, bool createIfNotExistent)
Definition: filesystem.cpp:162
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
error.h
armarx::armem::server::ltm::util::fs::detail::EscapeTable
const std::map< std::string, std::string > EscapeTable
Definition: filesystem.h:14
timestamp
std::string timestamp()
Definition: CartographerAdapter.cpp:85
armarx::armem::server::ltm ::util::fs::getAllDirectories
std::vector< std::filesystem::path > getAllDirectories(const std::filesystem::path &p)
Definition: filesystem.cpp:205
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
ExpressionException.h
armarx::armem::server::ltm
Definition: forward_declarations.h:20
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::armem::server::ltm ::util::fs::toPath
std::filesystem::path toPath(const std::filesystem::path &base, const armem::MemoryID &id)
Definition: filesystem.cpp:97
armarx::armem::laser_scans::constants::memoryName
const std::string memoryName
Definition: constants.h:28
filesystem.h
armarx::armem::server::ltm ::util::fs::detail::toDayString
std::string toDayString(const Time &t)
Definition: filesystem.cpp:59
armarx::armem::server::ltm ::util::fs::getAllFiles
std::vector< std::filesystem::path > getAllFiles(const std::filesystem::path &p)
Definition: filesystem.cpp:224
armarx::armem::server::ltm ::util::fs::detail::isDateString
bool isDateString(const std::string &s)
Definition: filesystem.cpp:84
armarx::armem::server::ltm ::util::fs::fileExists
bool fileExists(const std::filesystem::path &p)
Definition: filesystem.cpp:139
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:38