5#ifndef SYSTEM_TOOLS_HXX
6#define SYSTEM_TOOLS_HXX
15#include <condition_variable>
18#if !(defined(_WIN32) || defined(WIN32))
26using namespace std::chrono_literals;
27namespace fs = std::filesystem;
141 bool touchFile(
const std::string& file, std::optional<struct timespec> targetTimeOrNow);
217 template<
typename ParseType>
219 std::fstream fin(path, std::ios::in);
221 std::getline(fin, line);
224 throw std::invalid_argument(fmt::sprintf(
"The result of path \"%s\" is empty! \n", path));
227 if constexpr (std::is_same_v<ParseType, std::string>) {
230 double valueD = std::stod(line);
231 return static_cast<ParseType
>(valueD);
243 template<
typename ParseType>
244 int writeValue2FilePath(
const std::string& path,
const ParseType& value,
const std::string& formatStr =
"{}") {
245 auto valueString = fmt::format(fmt::runtime(formatStr), value);
246 std::fstream fout(path, std::ios::out);
251 }
catch (
const std::exception&) {
252 throw std::runtime_error(fmt::format(
"Cannot write value {} to path {}.", value, path));
278 std::vector<uint8_t>
curlDownloadNoBody(
const std::string& url, uint32_t timeout_ms = 0,
long* httpResponseCode =
nullptr);
287 std::vector<uint8_t>
curlDownload(
const std::string& url, uint32_t timeout_ms = 0,
long* httpResponseCode =
nullptr);
308 int udpSendData(
const std::string& sessionName,
const uint8_t* data, uint32_t bufferLength,
const std::string& targetIP, uint16_t targetPoint, std::optional<uint16_t> sourcePort = std::nullopt);
319 template<
typename DataType>
321 static std::random_device r;
322 static std::default_random_engine randomEngine(r());
323 static std::uniform_int_distribution<DataType> randomGenerator(min, max);
324 return randomGenerator(randomEngine);
328 namespace Persistence {