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