5#ifndef SYSTEM_TOOLS_HXX
6#define SYSTEM_TOOLS_HXX
15#include <condition_variable>
24using namespace std::chrono_literals;
25namespace fs = std::filesystem;
63 bool touchFile(
const std::string& file, std::optional<struct timespec> targetTimeOrNow);
73 void writeContent2File(
const std::string& file,
const uint8_t* buffer,
size_t bufferLength);
83 template<
typename ParseType>
85 std::fstream fin(path, std::ios::in);
87 std::getline(fin, line);
90 throw std::invalid_argument(fmt::sprintf(
"The result of path \"%s\" is empty! \n", path));
93 if constexpr (std::is_same_v<ParseType, std::string>) {
96 double valueD = std::stod(line);
97 return static_cast<ParseType
>(valueD);
101 template<
typename ParseType>
102 int writeValue2FilePath(
const std::string& path,
const ParseType& value,
const std::string& formatStr =
"{}") {
103 auto valueString = fmt::format(fmt::runtime(formatStr), value);
104 std::fstream fout(path, std::ios::out);
109 }
catch (
const std::exception&) {
110 throw std::runtime_error(fmt::format(
"Cannot write value {} to path {}.", value, path));
120 std::vector<uint8_t>
curlDownloadNoBody(
const std::string& url, uint32_t timeout_ms = 0,
long* httpResponseCode =
nullptr);
122 std::vector<uint8_t>
curlDownload(
const std::string& url, uint32_t timeout_ms = 0,
long* httpResponseCode =
nullptr);
126 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);
130 template<
typename DataType>
132 static std::random_device r;
133 static std::default_random_engine randomEngine(r());
134 static std::uniform_int_distribution<DataType> randomGenerator(min, max);
135 return randomGenerator(randomEngine);
139 namespace Persistence {