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