util.cpp
Go to the documentation of this file.
1 #include "util.h"
2 
3 #include <chrono>
4 #include <thread>
5 
7 {
8  namespace util
9  {
10  // check whether a string is a number (e.g. timestamp folders)
11  bool
12  isNumber(const std::string& s)
13  {
14  for (char const& ch : s)
15  {
16  if (std::isdigit(ch) == 0)
17  {
18  return false;
19  }
20  }
21  return true;
22  }
23 
24  bool
25  checkIfBasePathExists(const std::filesystem::path& mPath)
26  {
27  if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
28  {
29  return minizip::util::checkZipFile(mPath);
30  }
32  }
33 
34  void
35  ensureBasePathExists(const std::filesystem::path& mPath, bool createIfNotExistent)
36  {
37  if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
38  {
39  auto z = minizip::util::ensureZipFile(mPath, createIfNotExistent);
40  zipClose(z, NULL);
41  return;
42  }
43  return filesystem::util::ensureFolderInFilesystemExists(mPath, createIfNotExistent);
44  }
45 
46  bool
47  checkIfFolderExists(const std::filesystem::path& mPath, const std::filesystem::path& p)
48  {
49  if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
50  {
51  return minizip::util::checkIfFolderInZipExists(mPath, p);
52  }
53 
55  }
56 
57  void
58  ensureFolderExists(const std::filesystem::path& mPath,
59  const std::filesystem::path& p,
60  bool createIfNotExistent)
61  {
62  if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
63  {
64  return minizip::util::ensureFolderInZipExists(mPath, p, createIfNotExistent);
65  }
66 
67  return filesystem::util::ensureFolderInFilesystemExists(mPath / p, createIfNotExistent);
68  }
69 
70  bool
71  checkIfFileExists(const std::filesystem::path& mPath, const std::filesystem::path& p)
72  {
73  if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
74  {
75  return minizip::util::checkIfFileInZipExists(mPath, p);
76  }
77 
79  }
80 
81  void
82  ensureFileExists(const std::filesystem::path& mPath, const std::filesystem::path& p)
83  {
84  if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
85  {
86  return minizip::util::ensureFileInZipExists(mPath, p);
87  }
88 
90  }
91 
92  void
93  writeDataToFile(const std::filesystem::path& mPath,
94  const std::filesystem::path& p,
95  const std::vector<unsigned char>& data)
96  {
97  if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
98  {
99  return minizip::util::writeDataInFileInZipFile(mPath, p, data);
100  }
101 
103  }
104 
105  void
106  writeDataToFileRepeated(const std::filesystem::path& mPath,
107  const std::filesystem::path& p,
108  const std::vector<unsigned char>& data,
109  const unsigned int maxTries,
110  const unsigned int sleepTimeMs)
111  {
112  for (unsigned int i = 0; i < maxTries; ++i)
113  {
114  try
115  {
116  writeDataToFile(mPath, p, data);
117  return;
118  }
119  catch (const error::ArMemError&)
120  {
121  // wait a bit to give the filesystem enough time to manage the workload
122  std::this_thread::sleep_for(std::chrono::milliseconds(sleepTimeMs));
123  }
124  }
125 
126  // even after all the tries we did not succeeded. This is very bad!
127  throw error::ArMemError(
128  "ATTENTION! Even after " + std::to_string(maxTries) +
129  " tries, the memory was not able to store the instance at path '" + p.string() +
130  "'. This means this instance will be lost!");
131  }
132 
133  std::vector<unsigned char>
134  readDataFromFile(const std::filesystem::path& mPath, const std::filesystem::path& p)
135  {
136  if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
137  {
138  return minizip::util::readDataFromFileInZipFile(mPath, p);
139  }
140 
142  }
143 
144  std::vector<std::string>
145  getAllDirectories(const std::filesystem::path& mPath, const std::filesystem::path& p)
146  {
147  if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
148  {
149  return {};
150  }
151 
153  }
154 
155  std::vector<std::string>
156  getAllFiles(const std::filesystem::path& mPath, const std::filesystem::path& p)
157  {
158  if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
159  {
160  return {};
161  }
162 
163  return filesystem::util::getAllFilesystemFiles(mPath / p);
164  }
165  } // namespace util
166 } // namespace armarx::armem::server::ltm::disk
armarx::armem::server::ltm::disk::util::writeDataToFileRepeated
void writeDataToFileRepeated(const std::filesystem::path &mPath, const std::filesystem::path &p, const std::vector< unsigned char > &data, const unsigned int maxTries, const unsigned int sleepTimeMs)
Definition: util.cpp:106
util.h
armarx::armem::server::ltm::disk::util::ensureFolderExists
void ensureFolderExists(const std::filesystem::path &mPath, const std::filesystem::path &p, bool createIfNotExistent)
Definition: util.cpp:58
armarx::armem::server::ltm::disk::util::checkIfFolderExists
bool checkIfFolderExists(const std::filesystem::path &mPath, const std::filesystem::path &p)
Definition: util.cpp:47
armarx::armem::server::ltm::disk::filesystem::util::readDataFromFilesystemFile
std::vector< unsigned char > readDataFromFilesystemFile(const std::filesystem::path path)
Definition: filesystem_util.cpp:93
armarx::armem::server::ltm::disk::util::ensureBasePathExists
void ensureBasePathExists(const std::filesystem::path &mPath, bool createIfNotExistent)
Definition: util.cpp:35
armarx::armem::server::ltm::disk::util::checkIfFileExists
bool checkIfFileExists(const std::filesystem::path &mPath, const std::filesystem::path &p)
Definition: util.cpp:71
armarx::armem::server::ltm::disk::filesystem::util::getAllFilesystemDirectories
std::vector< std::string > getAllFilesystemDirectories(const std::filesystem::path path)
Definition: filesystem_util.cpp:102
armarx::armem::error::ArMemError
Base class for all exceptions thrown by the armem library.
Definition: ArMemError.h:18
armarx::armem::server::ltm::disk::util::writeDataToFile
void writeDataToFile(const std::filesystem::path &mPath, const std::filesystem::path &p, const std::vector< unsigned char > &data)
Definition: util.cpp:93
armarx::armem::server::ltm::disk::filesystem::util::checkIfFileInFilesystemExists
bool checkIfFileInFilesystemExists(const std::filesystem::path &p)
Definition: filesystem_util.cpp:62
armarx::armem::server::ltm::disk::util::getAllDirectories
std::vector< std::string > getAllDirectories(const std::filesystem::path &mPath, const std::filesystem::path &p)
Definition: util.cpp:145
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::armem::server::ltm::disk::filesystem::util::writeDataInFilesystemFile
void writeDataInFilesystemFile(const std::filesystem::path &p, const std::vector< unsigned char > &data)
Definition: filesystem_util.cpp:78
armarx::armem::server::ltm::disk::util::getAllFiles
std::vector< std::string > getAllFiles(const std::filesystem::path &mPath, const std::filesystem::path &p)
Definition: util.cpp:156
armarx::armem::server::ltm::disk::filesystem::util::ensureFolderInFilesystemExists
void ensureFolderInFilesystemExists(const std::filesystem::path &p, bool createIfNotExistent)
Definition: filesystem_util.cpp:46
armarx::armem::server::ltm::disk::util::isNumber
bool isNumber(const std::string &s)
Definition: util.cpp:12
armarx::armem::server::ltm::disk::filesystem::util::checkIfFolderInFilesystemExists
bool checkIfFolderInFilesystemExists(const std::filesystem::path &p)
Definition: filesystem_util.cpp:40
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::armem::server::ltm::disk::util::readDataFromFile
std::vector< unsigned char > readDataFromFile(const std::filesystem::path &mPath, const std::filesystem::path &p)
Definition: util.cpp:134
armarx::armem::server::ltm::disk::filesystem::util::getAllFilesystemFiles
std::vector< std::string > getAllFilesystemFiles(const std::filesystem::path path)
Definition: filesystem_util.cpp:118
armarx::armem::server::ltm::disk::util::ensureFileExists
void ensureFileExists(const std::filesystem::path &mPath, const std::filesystem::path &p)
Definition: util.cpp:82
armarx::armem::server::ltm::disk
Definition: filesystem_util.cpp:7
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::armem::server::ltm::disk::filesystem::util::ensureFileInFilesystemExists
void ensureFileInFilesystemExists(const std::filesystem::path &p)
Definition: filesystem_util.cpp:68
armarx::armem::server::ltm::disk::util::checkIfBasePathExists
bool checkIfBasePathExists(const std::filesystem::path &mPath)
Definition: util.cpp:25