PicoScenes API Docs
 
Loading...
Searching...
No Matches
SystemTools.hxx
Go to the documentation of this file.
1//
2// Created by Zhiping Jiang on 1/26/21.
3//
4
5#ifndef SYSTEM_TOOLS_HXX
6#define SYSTEM_TOOLS_HXX
7
8#include <chrono>
9#include <optional>
10#include <iostream>
11#include <fstream>
12#include <vector>
13#include <array>
14#include <random>
15#include <condition_variable>
16#include <filesystem>
17#include <type_traits>
18#include <net/if.h>
19#include <arpa/inet.h>
20#include "BoostHeaders.hxx"
21#include "LoggingService.hxx"
22#include "TimeTools.hxx"
23
24using namespace std::chrono_literals;
25namespace fs = std::filesystem;
26
27namespace SystemTools {
28 namespace Shared {
29 uint32_t getUID();
30
31 uint32_t getPID();
32
33 uint32_t getParentPID();
34
35 std::string getCurrentUserName();
36
37 std::string getFilePathForPID(uint32_t pid);
38
40
42
43 std::string getProcessName(uint32_t pid);
44
45 std::string invokeCommandLineCommand(const std::string& cmdString, int* returnStatus = nullptr);
46
47 std::string getKernelVersion();
48
49 uint8_t getCPUNumber();
50
52
53 void openURLWithDefaultBrowser(const std::string& url);
54 }
55
56 namespace File {
57 std::string getHomeDirectory();
58
59 bool checkCommandExists(const std::string& command);
60
61 std::vector<struct timespec> getAccessChangeModifyTimeOfFile(const std::string& file);
62
63 bool touchFile(const std::string& file, std::optional<struct timespec> targetTimeOrNow);
64
65 std::string getFileOwner(const std::string& filePath);
66
67 std::vector<uint8_t> readContentFromFile(const std::string& file);
68
69 std::string readContentFromFileAsString(const std::string& file);
70
71 std::vector<std::string> readContentFromFileLineByLine(const std::string& filePath);
72
73 void writeContent2File(const std::string& file, const uint8_t* buffer, size_t bufferLength);
74
75 void writeContent2File(const std::string& file, const std::string& content);
76
77 bool removeFile(const std::string& filePath);
78
79 std::vector<fs::path> listFilesWithExtension(fs::path folderPath, std::string prefix = "", std::string surfix = "");
80
81 std::vector<fs::path> listFilesRecursivelyWithExtension(fs::path folderPath, std::string prefix = "", std::string surfix = "");
82
83 template<typename ParseType>
84 ParseType readValueFromFilePath(const std::string& path) {
85 std::fstream fin(path, std::ios::in);
86 std::string line;
87 std::getline(fin, line);
88 fin.close();
89 if (line.empty()) {
90 throw std::invalid_argument(fmt::sprintf("The result of path \"%s\" is empty! \n", path));
91 }
92
93 if constexpr (std::is_same_v<ParseType, std::string>) {
94 return line;
95 } else {
96 double valueD = std::stod(line); // boost::lexical_cast cannot recognize '0x3' style
97 return static_cast<ParseType>(valueD);
98 }
99 }
100
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);
105 try {
106 fout << valueString;
107 fout.close();
108 return 0;
109 } catch (const std::exception&) {
110 throw std::runtime_error(fmt::format("Cannot write value {} to path {}.", value, path));
111 }
112 }
113 };
114
115 namespace Net {
117
118 std::tuple<bool, std::string, std::optional<TimePoint_System_Millisecond>> checkInternetConnectionAndGetGMTTime(bool forceRefresh = false);
119
120 std::vector<uint8_t> curlDownloadNoBody(const std::string& url, uint32_t timeout_ms = 0, long* httpResponseCode = nullptr);
121
122 std::vector<uint8_t> curlDownload(const std::string& url, uint32_t timeout_ms = 0, long* httpResponseCode = nullptr);
123
124 std::string curlDownloadContentAsString(const std::string& url, uint32_t timeout_ms = 0, long* httpResponseCode = nullptr);
125
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);
127 }
128
129 namespace Math {
130 template<typename DataType>
131 DataType uniformRandomNumberWithinRange(DataType min, DataType max) {
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);
136 }
137 }
138
139 namespace Persistence {
140 std::string boostPropertyTree2JSON(const pt::ptree& tree, bool prettyOutput = true);
141
142 pt::ptree jSON2BoostPropertyTree(const std::string& jsonContent);
143
144 std::string boostPropertyTree2INI(const pt::ptree& tree, bool prettyOutput = true);
145
146 pt::ptree INI2BoostPropertyTree(const std::string& iniContent);
147 }
148
149 namespace Thread {
150 std::string getThreadName(uint64_t threadId);
151
152 void setThreadName(uint64_t threadId, const std::string& threadName);
153
154 std::string getCurrentThreadName();
155
156 void setName4CurrentThread(const std::string& threadName);
157
159 }
160
161 namespace Misc {
162 std::string getCPUGovernor();
163
164 bool isPackageInstalled(const std::string& packageName);
165 }
166}
167
168#endif //SYSTEM_TOOLS_HXX
std::string getFileOwner(const std::string &filePath)
bool touchFile(const std::string &file, std::optional< struct timespec > targetTimeOrNow)
bool removeFile(const std::string &filePath)
std::vector< std::string > readContentFromFileLineByLine(const std::string &filePath)
std::vector< fs::path > listFilesRecursivelyWithExtension(fs::path folderPath, std::string prefix="", std::string surfix="")
std::vector< fs::path > listFilesWithExtension(fs::path folderPath, std::string prefix="", std::string surfix="")
std::string readContentFromFileAsString(const std::string &file)
void writeContent2File(const std::string &file, const uint8_t *buffer, size_t bufferLength)
int writeValue2FilePath(const std::string &path, const ParseType &value, const std::string &formatStr="{}")
bool checkCommandExists(const std::string &command)
ParseType readValueFromFilePath(const std::string &path)
std::string getHomeDirectory()
std::vector< struct timespec > getAccessChangeModifyTimeOfFile(const std::string &file)
std::vector< uint8_t > readContentFromFile(const std::string &file)
DataType uniformRandomNumberWithinRange(DataType min, DataType max)
std::string getCPUGovernor()
bool isPackageInstalled(const std::string &packageName)
std::vector< uint8_t > curlDownloadNoBody(const std::string &url, uint32_t timeout_ms=0, long *httpResponseCode=nullptr)
std::vector< uint8_t > curlDownload(const std::string &url, uint32_t timeout_ms=0, long *httpResponseCode=nullptr)
std::string curlDownloadContentAsString(const std::string &url, uint32_t timeout_ms=0, long *httpResponseCode=nullptr)
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)
bool hasInternetConnection()
std::tuple< bool, std::string, std::optional< TimePoint_System_Millisecond > > checkInternetConnectionAndGetGMTTime(bool forceRefresh=false)
std::string boostPropertyTree2INI(const pt::ptree &tree, bool prettyOutput=true)
pt::ptree jSON2BoostPropertyTree(const std::string &jsonContent)
std::string boostPropertyTree2JSON(const pt::ptree &tree, bool prettyOutput=true)
pt::ptree INI2BoostPropertyTree(const std::string &iniContent)
std::string getProcessName(uint32_t pid)
void openURLWithDefaultBrowser(const std::string &url)
std::string invokeCommandLineCommand(const std::string &cmdString, int *returnStatus=nullptr)
uint8_t getCPUNumber()
std::string getKernelVersion()
std::string getCurrentProcessFullPath()
uint32_t getParentPID()
std::string getCurrentCommandLineContent()
std::string getCurrentUserName()
uint64_t getSystemFreeMemory()
std::string getFilePathForPID(uint32_t pid)
void setName4CurrentThread(const std::string &threadName)
std::string getCurrentThreadName()
uint64_t getCurrentThreadId()
std::string getThreadName(uint64_t threadId)
void setThreadName(uint64_t threadId, const std::string &threadName)