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  return path.substr(pos + 1);
50  } else {
51  return path;
52  }
53  }
54 
55  std::string
56  toDayString(const Time& t)
57  {
58  return t.toDateString();
59  }
60 
61  std::string
63  {
65  }
66 
67  bool
68  isNumberString(const std::string& s)
69  {
70  for (char const& ch : s)
71  {
72  if (std::isdigit(ch) == 0)
73  {
74  return false;
75  }
76  }
77  return true;
78  }
79 
80  bool
81  isDateString(const std::string& s)
82  {
83  auto split = simox::alg::split(s, "-");
84  if (split.size() != 3)
85  {
86  return false;
87  }
88 
90  }
91  } // namespace detail
92 
93  std::filesystem::path
94  toPath(const std::filesystem::path& base, const armem::MemoryID& id)
95  {
96  ARMARX_CHECK(id.isWellDefined());
97 
98  std::filesystem::path p = base;
99  if (id.hasMemoryName())
100  {
102  }
103  if (id.hasCoreSegmentName())
104  {
105  p /= detail::escapeName(id.coreSegmentName);
106  }
107  if (id.hasProviderSegmentName())
108  {
109  p /= detail::escapeName(id.providerSegmentName);
110  }
111  if (id.hasEntityName())
112  {
113  p /= detail::escapeName(id.entityName);
114  }
115  if (id.hasTimestamp())
116  {
117  p /= detail::toDayString(id.timestamp);
118  p /= detail::toSecondsString(id.timestamp);
119  p /= id.timestampStr();
120  }
121  if (id.hasInstanceIndex())
122  {
123  p /= id.instanceIndexStr();
124  }
125 
126  return p;
127  }
128 
129  bool
130  directoryExists(const std::filesystem::path& p)
131  {
132  return std::filesystem::exists(p) and std::filesystem::is_directory(p);
133  }
134 
135  bool
136  fileExists(const std::filesystem::path& p)
137  {
138  return std::filesystem::exists(p) && std::filesystem::is_regular_file(p);
139  }
140 
141  void
142  ensureDirectoryExists(const std::filesystem::path& p, bool createIfNotExistent)
143  {
144  if (!directoryExists(p))
145  {
146  if (createIfNotExistent)
147  {
148  std::filesystem::create_directories(p);
149  }
150  else
151  {
152  throw armarx::LocalException("Directory existence cannot be ensured: " +
153  p.string());
154  }
155  }
156  }
157 
158  void
159  ensureFileExists(const std::filesystem::path& p, bool createIfNotExistent)
160  {
161  ensureDirectoryExists(p.parent_path(), createIfNotExistent);
162  if (!fileExists(p))
163  {
164  if (createIfNotExistent)
165  {
166  std::string content = "";
167  writeDataToFile(p, {content.begin(), content.end()});
168  }
169  else
170  {
171  // not found
172  throw armarx::LocalException("Could not find file: " + p.string());
173  }
174  }
175  }
176 
177  void
178  writeDataToFile(const std::filesystem::path& p, const std::vector<unsigned char>& data)
179  {
180  std::ofstream dataofs;
181  dataofs.open(p);
182  if (!dataofs)
183  {
184  throw armarx::LocalException("Could not write data to filesystem file '" + p.string() +
185  "'. Skipping this file.");
186  }
187  dataofs.write(reinterpret_cast<const char*>(data.data()), data.size());
188  dataofs.close();
189  }
190 
191  std::vector<unsigned char>
192  readDataFromFile(const std::filesystem::path& p)
193  {
194  std::ifstream dataifs(p);
195  std::vector<unsigned char> datafilecontent((std::istreambuf_iterator<char>(dataifs)),
196  (std::istreambuf_iterator<char>()));
197  dataifs.close();
198  return datafilecontent;
199  }
200 
201  std::vector<std::filesystem::path>
202  getAllDirectories(const std::filesystem::path& p)
203  {
204  std::vector<std::filesystem::path> ret;
205  for (const auto& subdir : std::filesystem::directory_iterator(p))
206  {
207  std::filesystem::path subdirPath = subdir.path();
208  if (std::filesystem::is_directory(subdirPath))
209  {
210  ret.push_back(subdirPath);
211  }
212  }
213  std::sort(ret.begin(),
214  ret.end(),
215  [](const std::filesystem::path& a, const std::filesystem::path& b) -> bool
216  { return a.string() < b.string(); });
217  return ret;
218  }
219 
220  std::vector<std::filesystem::path>
221  getAllFiles(const std::filesystem::path& p)
222  {
223  std::vector<std::filesystem::path> ret;
224  for (const auto& subdir : std::filesystem::directory_iterator(p))
225  {
226  std::filesystem::path subdirPath = subdir.path();
227  if (std::filesystem::is_regular_file(subdirPath))
228  {
229  ret.push_back(subdirPath);
230  }
231  }
232  std::sort(ret.begin(),
233  ret.end(),
234  [](const std::filesystem::path& a, const std::filesystem::path& b) -> bool
235  { return a.string() > b.string(); });
236  return ret;
237  }
238 } // namespace armarx::armem::server::ltm::util::fs
armarx::armem::server::ltm ::util::fs::detail::toSecondsString
std::string toSecondsString(const Time &t)
Definition: filesystem.cpp:62
armarx::armem::server::ltm ::util::fs::readDataFromFile
std::vector< unsigned char > readDataFromFile(const std::filesystem::path &p)
Definition: filesystem.cpp:192
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::core::time::DateTime::toDateString
std::string toDateString() const
Definition: DateTime.cpp:67
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:142
armarx::armem::server::ltm ::util::fs::writeDataToFile
void writeDataToFile(const std::filesystem::path &p, const std::vector< unsigned char > &data)
Definition: filesystem.cpp:178
armarx::armem::server::ltm ::util::fs::detail::escapeName
std::string escapeName(const std::string &segmentName)
Definition: filesystem.cpp:19
detail
Definition: OpenCVUtil.cpp:127
armarx::core::time::DateTime::toSecondsSinceEpoch
std::int64_t toSecondsSinceEpoch() const
Definition: DateTime.cpp:109
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:68
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:130
armarx::armem::server::ltm ::util::fs::ensureFileExists
void ensureFileExists(const std::filesystem::path &p, bool createIfNotExistent)
Definition: filesystem.cpp:159
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
armarx::armem::server::ltm ::util::fs::getAllDirectories
std::vector< std::filesystem::path > getAllDirectories(const std::filesystem::path &p)
Definition: filesystem.cpp:202
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
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:94
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:56
armarx::armem::server::ltm ::util::fs::getAllFiles
std::vector< std::filesystem::path > getAllFiles(const std::filesystem::path &p)
Definition: filesystem.cpp:221
armarx::armem::server::ltm ::util::fs::detail::isDateString
bool isDateString(const std::string &s)
Definition: filesystem.cpp:81
armarx::armem::server::ltm ::util::fs::fileExists
bool fileExists(const std::filesystem::path &p)
Definition: filesystem.cpp:136
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:36