From f82553449a91313b252dff89467bfd0af5dbeed5 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Wed, 15 Mar 2023 17:56:26 +0800 Subject: [PATCH] temporary code submission --- .../components/config_template/config_template.hpp | 12 +- core/components/file_util.cpp | 138 --------------------- core/components/file_util.hpp | 49 -------- core/components/fileutils.cpp | 138 +++++++++++++++++++++ core/components/fileutils.hpp | 49 ++++++++ .../components/jobs/thread_pool_task_scheduler.hpp | 2 +- core/components/process/process.hpp | 21 ---- core/components/string_util.cpp | 124 ------------------ core/components/string_util.hpp | 67 ---------- core/components/stringutils.cpp | 124 ++++++++++++++++++ core/components/stringutils.hpp | 67 ++++++++++ core/components/time_util.hpp | 83 ------------- core/components/timeutils.hpp | 83 +++++++++++++ core/components/uart/uart.cpp | 8 +- core/linuxcoreutils/linuxcoreutils.cpp | 42 +++++++ core/linuxcoreutils/linuxcoreutils.hpp | 55 ++++++++ module.cmake | 7 +- 17 files changed, 573 insertions(+), 496 deletions(-) delete mode 100644 core/components/file_util.cpp delete mode 100644 core/components/file_util.hpp create mode 100644 core/components/fileutils.cpp create mode 100644 core/components/fileutils.hpp delete mode 100644 core/components/string_util.cpp delete mode 100644 core/components/string_util.hpp create mode 100644 core/components/stringutils.cpp create mode 100644 core/components/stringutils.hpp delete mode 100644 core/components/time_util.hpp create mode 100644 core/components/timeutils.hpp create mode 100644 core/linuxcoreutils/linuxcoreutils.cpp create mode 100644 core/linuxcoreutils/linuxcoreutils.hpp diff --git a/core/components/config_template/config_template.hpp b/core/components/config_template/config_template.hpp index 8981235..3881a56 100644 --- a/core/components/config_template/config_template.hpp +++ b/core/components/config_template/config_template.hpp @@ -22,7 +22,7 @@ // #include "zwtimecpp/core/utils/data.hpp" #include "iflytopcpp/core/basic/marco/data.hpp" #include "iflytopcpp/core/basic/nlohmann/json.hpp" -#include "iflytopcpp/core/components/file_util.hpp" +#include "iflytopcpp/core/components/fileutils.hpp" #include "iflytopcpp/core/spdlogfactory/logger.hpp" #include "iflytopcpp/core/zexception/zexception.hpp" // #include "zwtimecpp/core/utils/zmath.hpp" @@ -76,15 +76,15 @@ json j3; \ \ string config; \ - if (!FileUtil().exist(configFile)) { \ + if (!FileUtils().exist(configFile)) { \ logger->error("can't find configfile ,create default config:{}", configFile); \ - auto defaultConfigFile = FileUtil().openTrunc(configFile); \ + auto defaultConfigFile = FileUtils().openTrunc(configFile); \ defaultConfigFile->write("{}", 2); \ } \ - config = FileUtil().readFileAsString(configFile); \ + config = FileUtils().readFileAsString(configFile); \ if (config.empty()) { \ flushConfigToFile(); \ - config = FileUtil().readFileAsString(configFile); \ + config = FileUtils().readFileAsString(configFile); \ } \ \ try { \ @@ -102,7 +102,7 @@ void flushConfigToFile() { \ json j; \ configList(configTemplateSAVE_CONFIG2); \ - bool flushSuccess = FileUtil().writeToFile(configFile, j.dump(2)); \ + bool flushSuccess = FileUtils().writeToFile(configFile, j.dump(2)); \ if (!flushSuccess) { \ logger->error("flush config to logfile fail"); \ } \ diff --git a/core/components/file_util.cpp b/core/components/file_util.cpp deleted file mode 100644 index 48b1cf6..0000000 --- a/core/components/file_util.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#include "file_util.hpp" -using namespace iflytop; -using namespace core; - -bool FileUtil::exist(const string &path) { - struct stat statInfo; - if (stat(path.c_str(), &statInfo) == 0) { - return true; - } - return false; -} -bool FileUtil::isDirectory(const string &path) { - struct stat statInfo; - if (stat(path.c_str(), &statInfo) == 0) { - if (S_ISDIR(statInfo.st_mode)) { - return true; - } else { - return false; - } - } - return false; -} - -// mkfile -bool FileUtil::makeDirIfNoExist(const string &path) { - string::size_type sepPos = path.find_last_of("/"); - if (sepPos == string::npos) { - return false; - } - string dirPath = path.substr(0, sepPos); - if (exist(dirPath)) { - return true; - } - int ret = system(("mkdir -p " + dirPath).c_str()); - // mkdir(dirPath.c_str(), S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); - return 0 == ret ? true : false; -} -// readfile -string FileUtil::readFileAsString(const string &filePath) { - if (!exist(filePath)) { - return ""; - } - - fstream file(filePath.c_str(), ios::in | ios::binary); - stringstream sstream; - - sstream << file.rdbuf(); - file.close(); - - string str(sstream.str()); - sstream.clear(); - - return str; -} -std::vector FileUtil::readFileAsBuffer(const string &filePath) { - std::vector ret; - if (!exist(filePath)) { - return std::move(ret); - } - - fstream fs; - fs.open(filePath.c_str(), ios::in | ios::binary); - - if (!fs.is_open()) { - return std::move(ret); - } - - fs.seekg(0, ios::end); - int fileSize = fs.tellg(); - fs.seekg(0, ios::beg); - - ret.resize(fileSize); - - char *dataAddr = (char *)ret.data(); - - fs.read(dataAddr, fileSize); - fs.close(); - - return std::move(ret); -} -// write file -bool FileUtil::writeToFile(const string &fileName, const string &buf) { - return writeToFile(fileName, buf.c_str(), buf.size()); -} - -shared_ptr FileUtil::openTrunc(const string &fileName) { - makeDirIfNoExist(fileName); - shared_ptr file(new ofstream(fileName, std::ios::trunc | std::ios::binary)); - return file; -} - -bool FileUtil::writeToFile(const string &fileName, const char *buf, size_t size) { - std::ofstream outfile1(fileName, std::ios::trunc | std::ios::binary); - if (outfile1.is_open()) { - outfile1.write((char *)buf, size); - if (outfile1.good()) return true; - return false; - } else { - return false; - } -} -#if 0 -bool FileUtil::getRepetitionFileName(string fileName, string suffix, string &outfilename, int maxNum = -1) {} -bool FileUtil::getRepetitionDirName(string fileName, string &outfilename, int maxNum = -1) {} -// filename operat -string FileUtil::getRepetitionFileName(string fileName, string suffix, int maxNum = -1) { - string curName; - if (maxNum >= 0) { - for (int i = 0; i < maxNum; i++) { - curName = fmt::format("{}{}.{}", fileName, i, suffix); - if (!FileUtil::exist(curName)) return curName; - } - } else { - for (int i = 0; i <= 65536; i++) { - curName = fmt::format("{}{}.{}", fileName, i, suffix); - if (!FileUtil::exist(curName)) return curName; - } - } - - return fmt::format("{}{}.{}", fileName, "reachMax", suffix); -} -string FileUtil::getRepetitionDirName(string fileName, int maxNum = -1) { - string curName; - if (maxNum >= 0) { - for (int i = 0; i < maxNum; i++) { - curName = fmt::format("{}{}", fileName, i); - if (!FileUtil::exist(curName)) return curName + "/"; - } - } else { - for (int i = 0; i <= 65536; i++) { - curName = fmt::format("{}{}", fileName, i); - if (!FileUtil::exist(curName)) return curName + "/"; - } - } - - return ""; -} -#endif \ No newline at end of file diff --git a/core/components/file_util.hpp b/core/components/file_util.hpp deleted file mode 100644 index c695226..0000000 --- a/core/components/file_util.hpp +++ /dev/null @@ -1,49 +0,0 @@ -// -// Created by zhaohe on 19-5-31. -// - -#pragma once -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "iflytopcpp/core/spdlogfactory/logger.hpp" - -namespace iflytop { -namespace core { -using namespace std; - -class FileUtil { - ENABLE_LOGGER(FileUtil); - - public: - // judegefile - bool exist(const string &path); - bool isDirectory(const string &path); - // bool delFile(const string &filePath); - // mkfile - bool makeDirIfNoExist(const string &path); - // readfile - string readFileAsString(const string &filePath); - std::vector readFileAsBuffer(const string &filePath); - // write file - bool writeToFile(const string &fileName, const string &buf); - bool writeToFile(const string &fileName, const char *buf, size_t size); - - shared_ptr openTrunc(const string &fileName); - - // filename operat - // bool getRepetitionFileName(string fileName, string suffix, string &outfilename, int maxNum = -1); - // bool getRepetitionDirName(string fileName, string &outfilename, int maxNum = -1); -}; -} // namespace core -} // namespace iflytop diff --git a/core/components/fileutils.cpp b/core/components/fileutils.cpp new file mode 100644 index 0000000..8c6d9ca --- /dev/null +++ b/core/components/fileutils.cpp @@ -0,0 +1,138 @@ +#include "fileutils.hpp" +using namespace iflytop; +using namespace core; + +bool FileUtils::exist(const string &path) { + struct stat statInfo; + if (stat(path.c_str(), &statInfo) == 0) { + return true; + } + return false; +} +bool FileUtils::isDirectory(const string &path) { + struct stat statInfo; + if (stat(path.c_str(), &statInfo) == 0) { + if (S_ISDIR(statInfo.st_mode)) { + return true; + } else { + return false; + } + } + return false; +} + +// mkfile +bool FileUtils::makeDirIfNoExist(const string &path) { + string::size_type sepPos = path.find_last_of("/"); + if (sepPos == string::npos) { + return false; + } + string dirPath = path.substr(0, sepPos); + if (exist(dirPath)) { + return true; + } + int ret = system(("mkdir -p " + dirPath).c_str()); + // mkdir(dirPath.c_str(), S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + return 0 == ret ? true : false; +} +// readfile +string FileUtils::readFileAsString(const string &filePath) { + if (!exist(filePath)) { + return ""; + } + + fstream file(filePath.c_str(), ios::in | ios::binary); + stringstream sstream; + + sstream << file.rdbuf(); + file.close(); + + string str(sstream.str()); + sstream.clear(); + + return str; +} +std::vector FileUtils::readFileAsBuffer(const string &filePath) { + std::vector ret; + if (!exist(filePath)) { + return std::move(ret); + } + + fstream fs; + fs.open(filePath.c_str(), ios::in | ios::binary); + + if (!fs.is_open()) { + return std::move(ret); + } + + fs.seekg(0, ios::end); + int fileSize = fs.tellg(); + fs.seekg(0, ios::beg); + + ret.resize(fileSize); + + char *dataAddr = (char *)ret.data(); + + fs.read(dataAddr, fileSize); + fs.close(); + + return std::move(ret); +} +// write file +bool FileUtils::writeToFile(const string &fileName, const string &buf) { + return writeToFile(fileName, buf.c_str(), buf.size()); +} + +shared_ptr FileUtils::openTrunc(const string &fileName) { + makeDirIfNoExist(fileName); + shared_ptr file(new ofstream(fileName, std::ios::trunc | std::ios::binary)); + return file; +} + +bool FileUtils::writeToFile(const string &fileName, const char *buf, size_t size) { + std::ofstream outfile1(fileName, std::ios::trunc | std::ios::binary); + if (outfile1.is_open()) { + outfile1.write((char *)buf, size); + if (outfile1.good()) return true; + return false; + } else { + return false; + } +} +#if 0 +bool FileUtils::getRepetitionFileName(string fileName, string suffix, string &outfilename, int maxNum = -1) {} +bool FileUtils::getRepetitionDirName(string fileName, string &outfilename, int maxNum = -1) {} +// filename operat +string FileUtils::getRepetitionFileName(string fileName, string suffix, int maxNum = -1) { + string curName; + if (maxNum >= 0) { + for (int i = 0; i < maxNum; i++) { + curName = fmt::format("{}{}.{}", fileName, i, suffix); + if (!FileUtils::exist(curName)) return curName; + } + } else { + for (int i = 0; i <= 65536; i++) { + curName = fmt::format("{}{}.{}", fileName, i, suffix); + if (!FileUtils::exist(curName)) return curName; + } + } + + return fmt::format("{}{}.{}", fileName, "reachMax", suffix); +} +string FileUtils::getRepetitionDirName(string fileName, int maxNum = -1) { + string curName; + if (maxNum >= 0) { + for (int i = 0; i < maxNum; i++) { + curName = fmt::format("{}{}", fileName, i); + if (!FileUtils::exist(curName)) return curName + "/"; + } + } else { + for (int i = 0; i <= 65536; i++) { + curName = fmt::format("{}{}", fileName, i); + if (!FileUtils::exist(curName)) return curName + "/"; + } + } + + return ""; +} +#endif \ No newline at end of file diff --git a/core/components/fileutils.hpp b/core/components/fileutils.hpp new file mode 100644 index 0000000..4665f38 --- /dev/null +++ b/core/components/fileutils.hpp @@ -0,0 +1,49 @@ +// +// Created by zhaohe on 19-5-31. +// + +#pragma once +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "iflytopcpp/core/spdlogfactory/logger.hpp" + +namespace iflytop { +namespace core { +using namespace std; + +class FileUtils { + ENABLE_LOGGER(FileUtils); + + public: + // judegefile + bool exist(const string &path); + bool isDirectory(const string &path); + // bool delFile(const string &filePath); + // mkfile + bool makeDirIfNoExist(const string &path); + // readfile + string readFileAsString(const string &filePath); + std::vector readFileAsBuffer(const string &filePath); + // write file + bool writeToFile(const string &fileName, const string &buf); + bool writeToFile(const string &fileName, const char *buf, size_t size); + + shared_ptr openTrunc(const string &fileName); + + // filename operat + // bool getRepetitionFileName(string fileName, string suffix, string &outfilename, int maxNum = -1); + // bool getRepetitionDirName(string fileName, string &outfilename, int maxNum = -1); +}; +} // namespace core +} // namespace iflytop diff --git a/core/components/jobs/thread_pool_task_scheduler.hpp b/core/components/jobs/thread_pool_task_scheduler.hpp index ad88c15..88ad92b 100644 --- a/core/components/jobs/thread_pool_task_scheduler.hpp +++ b/core/components/jobs/thread_pool_task_scheduler.hpp @@ -19,7 +19,7 @@ #include "iflytopcpp/core/spdlogfactory/logger.hpp" #include "iflytopcpp/core/thread/thread.hpp" #include "iflytopcpp/core/basic/concurrentqueue/concurrentqueue.h" -#include "iflytopcpp/core/components/time_util.hpp" +#include "iflytopcpp/core/components/timeutils.hpp" #define CODE_LOCATION (fmt::format("{}:{}", __FILE__, __LINE__)) diff --git a/core/components/process/process.hpp b/core/components/process/process.hpp index d8b8100..cd8f28e 100644 --- a/core/components/process/process.hpp +++ b/core/components/process/process.hpp @@ -18,27 +18,6 @@ struct Config { /// Set to true to inherit file descriptors from parent process. Default is false. /// On Windows: has no effect unless read_stdout==nullptr, read_stderr==nullptr and open_stdin==false. bool inherit_file_descriptors = false; - - /// On Windows only: controls how the process is started, mimics STARTUPINFO's wShowWindow. - /// See: https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/ns-processthreadsapi-startupinfoa - /// and https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-showwindow - enum class ShowWindow { - hide = 0, - show_normal = 1, - show_minimized = 2, - maximize = 3, - show_maximized = 3, - show_no_activate = 4, - show = 5, - minimize = 6, - show_min_no_active = 7, - show_na = 8, - restore = 9, - show_default = 10, - force_minimize = 11 - }; - /// On Windows only: controls how the window is shown. - ShowWindow show_window{ShowWindow::show_default}; }; /// Platform independent class for creating processes. diff --git a/core/components/string_util.cpp b/core/components/string_util.cpp deleted file mode 100644 index 2cee45e..0000000 --- a/core/components/string_util.cpp +++ /dev/null @@ -1,124 +0,0 @@ -#include "string_util.hpp" -using namespace iflytop; -using namespace core; -using namespace std; - -/*********************************************************************************************************************** - * ======================================================private====================================================== * - ***********************************************************************************************************************/ -char StringUtil::byteToChar(uint8_t byte) { - if (byte < 10) { - return '0' + byte; - } else { - return 'A' + (byte - 10); - } - throw std::out_of_range("byteToChar out of range"); - return 'x'; -} -bool StringUtil::isLegalHex(char c) { - if (c >= '0' && c <= '9') { - return true; - } else if (c >= 'A' && c <= 'F') { - return true; - } else if (c >= 'a' && c <= 'f') { - return true; - } - return false; -} -/*********************************************************************************************************************** - * ======================================================public======================================================= * - ***********************************************************************************************************************/ - -string StringUtil::upper(const string& value) { - string cpy = value; - transform(cpy.begin(), cpy.end(), cpy.begin(), ::toupper); - return cpy; -} -string StringUtil::lower(const string& value) { - string cpy = value; - transform(cpy.begin(), cpy.end(), cpy.begin(), ::tolower); - return cpy; -} - -string StringUtil::bytesToString(const uint8_t* data, size_t size) { - string ret; - for (unsigned i = 0; i < size; ++i) { - uint8_t hight4 = data[i] >> 4 & 0x0f; - uint8_t low4 = data[i] >> 0 & 0x0f; - ret += byteToChar(hight4); - ret += byteToChar(low4); - } - return ret; -} -string& StringUtil::replaceAllDistinct(string& str, const string& old_value, const string& new_value) { - for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) { - if ((pos = str.find(old_value, pos)) != string::npos) { - str.replace(pos, old_value.length(), new_value); - } else { - break; - } - } - return str; -} - -bool StringUtil::hexStringToBytes(string in, string delims, vector& byteTable) { - string hexTable; - byteTable.clear(); - if (!delims.empty()) { - hexTable = replaceAllDistinct(in, delims, ""); - /* code */ - } else { - hexTable = in; - } - - if (hexTable.length() % 2 != 0) { - // printf("ss\n"); - return false; - } - - try { - for (unsigned i = 0; i < hexTable.length(); i += 2) { - string hex = hexTable.substr(i, 2); - // printf("ss1 %s\n", hex.c_str()); - - if (!isLegalHex(hex.c_str()[0])) { - return false; - } - if (!isLegalHex(hex.c_str()[1])) { - return false; - } - - int value = std::stoi(hex, 0, 16); - byteTable.push_back((uint8_t)value); - } - } catch (const std::exception& e) { - // printf("ss1\n"); - return false; - } - return true; -} - -/** - * @brief - * - * @param value - * @return string - */ -string StringUtil::bytet2Binary(uint32_t value, int bitCount, bool remoteZero) { - string ret; - for (int i = 0; i < bitCount; ++i) { - uint32_t bit = value & 0x01; - value = value >> 1; - if (bit == 0) { - ret = "0" + ret; - } else { - ret = "1" + ret; - } - } - if (remoteZero) { - while (ret[0] == '0') { - ret = ret.substr(1); - } - } - return ret; -} diff --git a/core/components/string_util.hpp b/core/components/string_util.hpp deleted file mode 100644 index eeae046..0000000 --- a/core/components/string_util.hpp +++ /dev/null @@ -1,67 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -namespace iflytop { -namespace core { - -using namespace std; -class StringUtil { - char byteToChar(uint8_t byte); - bool isLegalHex(char c); - - public: - string upper(const string& value); - string lower(const string& value); - string bytesToString(const uint8_t* data, size_t size); - string bytesToString(const vector& byteTable) { return bytesToString(byteTable.data(), byteTable.size()); } - - /** - * @brief 替换字符串中的字符,如果new_value="",则删除旧字符 - * - * @param str - * @param old_value - * @param new_value - * @return string& - */ - string& replaceAllDistinct(string& str, const string& old_value, const string& new_value); - /** - * @brief 转换HEX字符串成Bytes数组例如 - * - * // string("12,0a,2a,ab") ===> char buf[] = {0x12,0x0a,0x2a,0xab} - * vector tobytes; - * hexStringToBytes("12,0a,2a,ab",",",tobytes); - * - * // string("120a2aab") ===> char buf[] = {0x12,0x0a,0x2a,0xab} - * vector tobytes; - * hexStringToBytes("120a2aab","",tobytes); - * - * @param in 输入字符串 - * @param delims 分割符 - * @param byteTable 输出的byte数组 - * @return true 转换成功 - * @return false 转换失败 - */ - bool hexStringToBytes(string in, string delims, vector& byteTable); - - /** - * @brief - * - * @param value 0x55 - * @param bitCount 8 - * @param remoteZero true/false - * @return string 01010101 - * - * Demo: - * StringUtil::bytet2Binary(0x55,8,true) ===> 1010101 - * StringUtil::bytet2Binary(0x55,8,false) ===> 01010101 - * - */ - string bytet2Binary(uint32_t value, int bitCount, bool remoteZero = true); -}; - -} // namespace core -} // namespace iflytop diff --git a/core/components/stringutils.cpp b/core/components/stringutils.cpp new file mode 100644 index 0000000..f5ccdd0 --- /dev/null +++ b/core/components/stringutils.cpp @@ -0,0 +1,124 @@ +#include "stringutils.hpp" +using namespace iflytop; +using namespace core; +using namespace std; + +/*********************************************************************************************************************** + * ======================================================private====================================================== * + ***********************************************************************************************************************/ +char StringUtils::byteToChar(uint8_t byte) { + if (byte < 10) { + return '0' + byte; + } else { + return 'A' + (byte - 10); + } + throw std::out_of_range("byteToChar out of range"); + return 'x'; +} +bool StringUtils::isLegalHex(char c) { + if (c >= '0' && c <= '9') { + return true; + } else if (c >= 'A' && c <= 'F') { + return true; + } else if (c >= 'a' && c <= 'f') { + return true; + } + return false; +} +/*********************************************************************************************************************** + * ======================================================public======================================================= * + ***********************************************************************************************************************/ + +string StringUtils::upper(const string& value) { + string cpy = value; + transform(cpy.begin(), cpy.end(), cpy.begin(), ::toupper); + return cpy; +} +string StringUtils::lower(const string& value) { + string cpy = value; + transform(cpy.begin(), cpy.end(), cpy.begin(), ::tolower); + return cpy; +} + +string StringUtils::bytesToString(const uint8_t* data, size_t size) { + string ret; + for (unsigned i = 0; i < size; ++i) { + uint8_t hight4 = data[i] >> 4 & 0x0f; + uint8_t low4 = data[i] >> 0 & 0x0f; + ret += byteToChar(hight4); + ret += byteToChar(low4); + } + return ret; +} +string& StringUtils::replaceAllDistinct(string& str, const string& old_value, const string& new_value) { + for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) { + if ((pos = str.find(old_value, pos)) != string::npos) { + str.replace(pos, old_value.length(), new_value); + } else { + break; + } + } + return str; +} + +bool StringUtils::hexStringToBytes(string in, string delims, vector& byteTable) { + string hexTable; + byteTable.clear(); + if (!delims.empty()) { + hexTable = replaceAllDistinct(in, delims, ""); + /* code */ + } else { + hexTable = in; + } + + if (hexTable.length() % 2 != 0) { + // printf("ss\n"); + return false; + } + + try { + for (unsigned i = 0; i < hexTable.length(); i += 2) { + string hex = hexTable.substr(i, 2); + // printf("ss1 %s\n", hex.c_str()); + + if (!isLegalHex(hex.c_str()[0])) { + return false; + } + if (!isLegalHex(hex.c_str()[1])) { + return false; + } + + int value = std::stoi(hex, 0, 16); + byteTable.push_back((uint8_t)value); + } + } catch (const std::exception& e) { + // printf("ss1\n"); + return false; + } + return true; +} + +/** + * @brief + * + * @param value + * @return string + */ +string StringUtils::bytet2Binary(uint32_t value, int bitCount, bool remoteZero) { + string ret; + for (int i = 0; i < bitCount; ++i) { + uint32_t bit = value & 0x01; + value = value >> 1; + if (bit == 0) { + ret = "0" + ret; + } else { + ret = "1" + ret; + } + } + if (remoteZero) { + while (ret[0] == '0') { + ret = ret.substr(1); + } + } + return ret; +} diff --git a/core/components/stringutils.hpp b/core/components/stringutils.hpp new file mode 100644 index 0000000..c81924e --- /dev/null +++ b/core/components/stringutils.hpp @@ -0,0 +1,67 @@ +#pragma once + +#include +#include +#include +#include +#include +namespace iflytop { +namespace core { + +using namespace std; +class StringUtils { + char byteToChar(uint8_t byte); + bool isLegalHex(char c); + + public: + string upper(const string& value); + string lower(const string& value); + string bytesToString(const uint8_t* data, size_t size); + string bytesToString(const vector& byteTable) { return bytesToString(byteTable.data(), byteTable.size()); } + + /** + * @brief 替换字符串中的字符,如果new_value="",则删除旧字符 + * + * @param str + * @param old_value + * @param new_value + * @return string& + */ + string& replaceAllDistinct(string& str, const string& old_value, const string& new_value); + /** + * @brief 转换HEX字符串成Bytes数组例如 + * + * // string("12,0a,2a,ab") ===> char buf[] = {0x12,0x0a,0x2a,0xab} + * vector tobytes; + * hexStringToBytes("12,0a,2a,ab",",",tobytes); + * + * // string("120a2aab") ===> char buf[] = {0x12,0x0a,0x2a,0xab} + * vector tobytes; + * hexStringToBytes("120a2aab","",tobytes); + * + * @param in 输入字符串 + * @param delims 分割符 + * @param byteTable 输出的byte数组 + * @return true 转换成功 + * @return false 转换失败 + */ + bool hexStringToBytes(string in, string delims, vector& byteTable); + + /** + * @brief + * + * @param value 0x55 + * @param bitCount 8 + * @param remoteZero true/false + * @return string 01010101 + * + * Demo: + * StringUtils::bytet2Binary(0x55,8,true) ===> 1010101 + * StringUtils::bytet2Binary(0x55,8,false) ===> 01010101 + * + */ + string bytet2Binary(uint32_t value, int bitCount, bool remoteZero = true); +}; + +} // namespace core +} // namespace iflytop diff --git a/core/components/time_util.hpp b/core/components/time_util.hpp deleted file mode 100644 index 949c497..0000000 --- a/core/components/time_util.hpp +++ /dev/null @@ -1,83 +0,0 @@ -// -// Created by zhaohe on 19-6-2. -// - -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace iflytop { -namespace core { -using namespace std; -using namespace chrono; - -template -class T_TimeUtil { - public: - time_point zero() { return time_point(nanoseconds(0)); } - time_point now() { return move(T::now()); } - int64_t getus() { return duration_cast(T::now().time_since_epoch()).count(); } - int64_t gets() { return duration_cast(T::now().time_since_epoch()).count(); } - int64_t getms() { return duration_cast(T::now().time_since_epoch()).count(); } - int64_t geth() { return duration_cast(T::now().time_since_epoch()).count(); } - int64_t tpToMs(time_point t) { return duration_cast(t.time_since_epoch()).count(); } - int64_t tpToUs(time_point t) { return duration_cast(t.time_since_epoch()).count(); } - int64_t tpToS(time_point t) { return duration_cast(t.time_since_epoch()).count(); } - int64_t dToMs(nanoseconds ns) { return duration_cast(ns).count(); } - int64_t dToUs(nanoseconds ns) { return duration_cast(ns).count(); } - int64_t dToS(nanoseconds ns) { return duration_cast(ns).count(); } - int64_t dToNs(nanoseconds ns) { return ns.count(); } - inline time_point msToTp(int64_t ms) { - time_point tp = time_point(milliseconds(ms)); - return move(tp); - } - // 时间操作 - inline time_point addh(time_point point, int value) { return point + hours(value); } - inline time_point adds(time_point point, int value) { return point + seconds(value); } - inline time_point addms(time_point point, int value) { return point + milliseconds(value); } - inline time_point addus(time_point point, int value) { return point + microseconds(value); } - inline time_point addh(int value) { return addh(T::now(), value); } - inline time_point adds(int value) { return adds(T::now(), value); } - inline time_point addms(int value) { return addms(T::now(), value); } - inline time_point addus(int value) { return addus(T::now(), value); } - inline int64_t ms2us(int64_t ms) { return ms * 1000; } - /** - * @brief 计算流逝时间 - */ - int64_t elapsedTimeS(time_point begin) { return dToS(T::now() - begin); } - int64_t elapsedTimeMs(time_point begin) { return dToMs(T::now() - begin); } - int64_t elapsedTimeUs(time_point begin) { return dToUs(T::now() - begin); } - int64_t elapsedTimeS(int64_t ms) { return (getms() - ms) / 1000; } - int64_t inline elapsedTimeMs(int64_t ms) { return (getms() - ms); } - int64_t inline elapsedTimeMs(int64_t now, int64_t ms) { return (now - ms); } - int64_t inline elapsedTimeUs(int64_t ms) { return (getms() - ms) / 1000 / 1000; } - /** - * @brief 倒计时 还剩多久 - */ - int64_t countdownTimeS(time_point endtime) { return dToS(endtime - T::now()); } - int64_t countdownTimeMs(time_point endtime) { return dToMs(endtime - T::now()); } - int64_t countdownTimeUs(time_point endtime) { return dToUs(endtime - T::now()); } - int64_t countdownTimeNs(time_point endtime) { return dToNs(endtime - T::now()); } -}; - -typedef T_TimeUtil tu_sys; // not use in future -typedef T_TimeUtil tu_steady; // not use in future -typedef time_point tp_sys; // not use in future -typedef time_point tp_steady; // not use in future - -// new api name -typedef T_TimeUtil zsystem_clock; -typedef T_TimeUtil zsteady_clock; - -typedef time_point zsystem_tp; -typedef time_point zsteady_tp; -}; // namespace core -} // namespace iflytop diff --git a/core/components/timeutils.hpp b/core/components/timeutils.hpp new file mode 100644 index 0000000..ac13cd3 --- /dev/null +++ b/core/components/timeutils.hpp @@ -0,0 +1,83 @@ +// +// Created by zhaohe on 19-6-2. +// + +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace iflytop { +namespace core { +using namespace std; +using namespace chrono; + +template +class T_TimeUtils { + public: + time_point zero() { return time_point(nanoseconds(0)); } + time_point now() { return move(T::now()); } + int64_t getus() { return duration_cast(T::now().time_since_epoch()).count(); } + int64_t gets() { return duration_cast(T::now().time_since_epoch()).count(); } + int64_t getms() { return duration_cast(T::now().time_since_epoch()).count(); } + int64_t geth() { return duration_cast(T::now().time_since_epoch()).count(); } + int64_t tpToMs(time_point t) { return duration_cast(t.time_since_epoch()).count(); } + int64_t tpToUs(time_point t) { return duration_cast(t.time_since_epoch()).count(); } + int64_t tpToS(time_point t) { return duration_cast(t.time_since_epoch()).count(); } + int64_t dToMs(nanoseconds ns) { return duration_cast(ns).count(); } + int64_t dToUs(nanoseconds ns) { return duration_cast(ns).count(); } + int64_t dToS(nanoseconds ns) { return duration_cast(ns).count(); } + int64_t dToNs(nanoseconds ns) { return ns.count(); } + inline time_point msToTp(int64_t ms) { + time_point tp = time_point(milliseconds(ms)); + return move(tp); + } + // 时间操作 + inline time_point addh(time_point point, int value) { return point + hours(value); } + inline time_point adds(time_point point, int value) { return point + seconds(value); } + inline time_point addms(time_point point, int value) { return point + milliseconds(value); } + inline time_point addus(time_point point, int value) { return point + microseconds(value); } + inline time_point addh(int value) { return addh(T::now(), value); } + inline time_point adds(int value) { return adds(T::now(), value); } + inline time_point addms(int value) { return addms(T::now(), value); } + inline time_point addus(int value) { return addus(T::now(), value); } + inline int64_t ms2us(int64_t ms) { return ms * 1000; } + /** + * @brief 计算流逝时间 + */ + int64_t elapsedTimeS(time_point begin) { return dToS(T::now() - begin); } + int64_t elapsedTimeMs(time_point begin) { return dToMs(T::now() - begin); } + int64_t elapsedTimeUs(time_point begin) { return dToUs(T::now() - begin); } + int64_t elapsedTimeS(int64_t ms) { return (getms() - ms) / 1000; } + int64_t inline elapsedTimeMs(int64_t ms) { return (getms() - ms); } + int64_t inline elapsedTimeMs(int64_t now, int64_t ms) { return (now - ms); } + int64_t inline elapsedTimeUs(int64_t ms) { return (getms() - ms) / 1000 / 1000; } + /** + * @brief 倒计时 还剩多久 + */ + int64_t countdownTimeS(time_point endtime) { return dToS(endtime - T::now()); } + int64_t countdownTimeMs(time_point endtime) { return dToMs(endtime - T::now()); } + int64_t countdownTimeUs(time_point endtime) { return dToUs(endtime - T::now()); } + int64_t countdownTimeNs(time_point endtime) { return dToNs(endtime - T::now()); } +}; + +typedef T_TimeUtils tu_sys; // not use in future +typedef T_TimeUtils tu_steady; // not use in future +typedef time_point tp_sys; // not use in future +typedef time_point tp_steady; // not use in future + +// new api name +typedef T_TimeUtils zsystem_clock; +typedef T_TimeUtils zsteady_clock; + +typedef time_point zsystem_tp; +typedef time_point zsteady_tp; +}; // namespace core +} // namespace iflytop diff --git a/core/components/uart/uart.cpp b/core/components/uart/uart.cpp index 77c1050..4179eb5 100644 --- a/core/components/uart/uart.cpp +++ b/core/components/uart/uart.cpp @@ -18,8 +18,8 @@ #include #include -#include "iflytopcpp/core/components/string_util.hpp" -#include "iflytopcpp/core/components/time_util.hpp" +#include "iflytopcpp/core/components/stringutils.hpp" +#include "iflytopcpp/core/components/timeutils.hpp" using namespace iflytop; using namespace iflytop::core; @@ -196,7 +196,7 @@ Uart::~Uart() { int Uart::start(unsigned char canonic) { return uartStart(&this->device, canonic); } int Uart::send(char *data, int size) { if (logger->level() <= level::debug) { - logger->debug("{} send: {}", this->device.name, StringUtil().bytesToString((const uint8_t *)data, size)); + logger->debug("{} send: {}", this->device.name, StringUtils().bytesToString((const uint8_t *)data, size)); } return uartSend(&this->device, data, size); } @@ -204,7 +204,7 @@ int Uart::receive(char *data, int size_max) { int ret = uartReceive(&this->device, data, size_max); if (logger->level() <= level::debug) { if (ret > 0) { - logger->debug("{} receive: {}[{}]", this->device.name, StringUtil().bytesToString((const uint8_t *)data, ret)); + logger->debug("{} receive: {}[{}]", this->device.name, StringUtils().bytesToString((const uint8_t *)data, ret)); } } return ret; diff --git a/core/linuxcoreutils/linuxcoreutils.cpp b/core/linuxcoreutils/linuxcoreutils.cpp new file mode 100644 index 0000000..eea8a81 --- /dev/null +++ b/core/linuxcoreutils/linuxcoreutils.cpp @@ -0,0 +1,42 @@ +#include "linuxcoreutils.hpp" + +#include "iflytopcpp/core/components/process/process.hpp" +using namespace iflytop; +using namespace core; + +static void docmd(string cmd, string &stdout_str, string &stderr_str) { + Process process( + cmd, "", // + [&](const char *bytes, size_t n) { stdout_str += string(bytes, n); }, + [&](const char *bytes, size_t n) { stderr_str += string(bytes, n); }); + process.get_exit_status(); + return; +} + +int LinuxCoreUtils::ls(string order, vector &files) { + /** + * @brief 检查ls指令是否包含-1参数 + */ + if (order.find("-1") == string::npos) { + logger->error("ls must contain -1 parameter"); + return -1; + } + + string stdoutstr; + string stderrstr; + docmd(order, stdoutstr, stderrstr); + if (stderrstr.size() > 0) { + return -1; + } + // + string file; + int filestrbegin = 0; + for (size_t i = 0; i < stdoutstr.size(); i++) { + if (stdoutstr[i] == '\n') { + file = string(stdoutstr.begin() + filestrbegin, stdoutstr.begin() + i); + files.push_back(file); + filestrbegin = i + 1; + } + } + return 0; +} diff --git a/core/linuxcoreutils/linuxcoreutils.hpp b/core/linuxcoreutils/linuxcoreutils.hpp new file mode 100644 index 0000000..ef16d57 --- /dev/null +++ b/core/linuxcoreutils/linuxcoreutils.hpp @@ -0,0 +1,55 @@ +// +// Created by zwsd +// + +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "iflytopcpp/core/components/process/process.hpp" +#include "iflytopcpp/core/spdlogfactory/logger.hpp" +#include "iflytopcpp/core/thread/thread.hpp" + +/** + * @brief + * + * service: LinuxCoreUtils + * + * 监听事件: + * 依赖状态: + * 依赖服务: + * 作用: + * + */ + +namespace iflytop { +using namespace std; +using namespace core; +using namespace process; +class LinuxCoreUtils { + ENABLE_LOGGER(LinuxCoreUtils); + + public: + LinuxCoreUtils(){}; + + + /** + * @brief 调用ls指令并返回结果 + * + * @param order + * ls -1 + * ls -1 *.txt + * + * @param files + * @return int + */ + int ls(string order, vector& files); +}; +} // namespace iflytop \ No newline at end of file diff --git a/module.cmake b/module.cmake index 45fb324..5d1d399 100644 --- a/module.cmake +++ b/module.cmake @@ -10,8 +10,8 @@ set(PUBLIC_INCLUDE_DIRECTORIES ${PUBLIC_INCLUDE_DIRECTORIES} ./dep/iflytopcpp/co set(DEP_SRC ${DEP_SRC} # dep/iflytopcpp/core/spdlogfactory/logger_factory.cpp - dep/iflytopcpp/core/components/file_util.cpp - dep/iflytopcpp/core/components/string_util.cpp + dep/iflytopcpp/core/components/fileutils.cpp + dep/iflytopcpp/core/components/stringutils.cpp dep/iflytopcpp/core/basic/signal/signal.cpp dep/iflytopcpp/core/thread/thread.cpp dep/iflytopcpp/core/zexception/zexception.cpp @@ -25,7 +25,8 @@ set(DEP_SRC dep/iflytopcpp/core/components/modbus/zmodbus_master.c dep/iflytopcpp/core/components/modbus/zmodbus_slave.c - + dep/iflytopcpp/core/linuxcoreutils/linuxcoreutils.cpp + dep/iflytopcpp/core/components/process/process_unix.cpp dep/iflytopcpp/core/components/process/process.cpp