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/fileutils.cpp similarity index 68% rename from core/components/file_util.cpp rename to core/components/fileutils.cpp index 48b1cf6..8c6d9ca 100644 --- a/core/components/file_util.cpp +++ b/core/components/fileutils.cpp @@ -1,15 +1,15 @@ -#include "file_util.hpp" +#include "fileutils.hpp" using namespace iflytop; using namespace core; -bool FileUtil::exist(const string &path) { +bool FileUtils::exist(const string &path) { struct stat statInfo; if (stat(path.c_str(), &statInfo) == 0) { return true; } return false; } -bool FileUtil::isDirectory(const string &path) { +bool FileUtils::isDirectory(const string &path) { struct stat statInfo; if (stat(path.c_str(), &statInfo) == 0) { if (S_ISDIR(statInfo.st_mode)) { @@ -22,7 +22,7 @@ bool FileUtil::isDirectory(const string &path) { } // mkfile -bool FileUtil::makeDirIfNoExist(const string &path) { +bool FileUtils::makeDirIfNoExist(const string &path) { string::size_type sepPos = path.find_last_of("/"); if (sepPos == string::npos) { return false; @@ -36,7 +36,7 @@ bool FileUtil::makeDirIfNoExist(const string &path) { return 0 == ret ? true : false; } // readfile -string FileUtil::readFileAsString(const string &filePath) { +string FileUtils::readFileAsString(const string &filePath) { if (!exist(filePath)) { return ""; } @@ -52,7 +52,7 @@ string FileUtil::readFileAsString(const string &filePath) { return str; } -std::vector FileUtil::readFileAsBuffer(const string &filePath) { +std::vector FileUtils::readFileAsBuffer(const string &filePath) { std::vector ret; if (!exist(filePath)) { return std::move(ret); @@ -79,17 +79,17 @@ std::vector FileUtil::readFileAsBuffer(const string &filePath) { return std::move(ret); } // write file -bool FileUtil::writeToFile(const string &fileName, const string &buf) { +bool FileUtils::writeToFile(const string &fileName, const string &buf) { return writeToFile(fileName, buf.c_str(), buf.size()); } -shared_ptr FileUtil::openTrunc(const string &fileName) { +shared_ptr FileUtils::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) { +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); @@ -100,36 +100,36 @@ bool FileUtil::writeToFile(const string &fileName, const char *buf, size_t size) } } #if 0 -bool FileUtil::getRepetitionFileName(string fileName, string suffix, string &outfilename, int maxNum = -1) {} -bool FileUtil::getRepetitionDirName(string fileName, string &outfilename, int maxNum = -1) {} +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 FileUtil::getRepetitionFileName(string fileName, string suffix, int maxNum = -1) { +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 (!FileUtil::exist(curName)) return curName; + if (!FileUtils::exist(curName)) return curName; } } else { for (int i = 0; i <= 65536; i++) { curName = fmt::format("{}{}.{}", fileName, i, suffix); - if (!FileUtil::exist(curName)) return curName; + if (!FileUtils::exist(curName)) return curName; } } return fmt::format("{}{}.{}", fileName, "reachMax", suffix); } -string FileUtil::getRepetitionDirName(string fileName, int maxNum = -1) { +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 (!FileUtil::exist(curName)) return curName + "/"; + if (!FileUtils::exist(curName)) return curName + "/"; } } else { for (int i = 0; i <= 65536; i++) { curName = fmt::format("{}{}", fileName, i); - if (!FileUtil::exist(curName)) return curName + "/"; + if (!FileUtils::exist(curName)) return curName + "/"; } } diff --git a/core/components/file_util.hpp b/core/components/fileutils.hpp similarity index 96% rename from core/components/file_util.hpp rename to core/components/fileutils.hpp index c695226..4665f38 100644 --- a/core/components/file_util.hpp +++ b/core/components/fileutils.hpp @@ -22,8 +22,8 @@ namespace iflytop { namespace core { using namespace std; -class FileUtil { - ENABLE_LOGGER(FileUtil); +class FileUtils { + ENABLE_LOGGER(FileUtils); public: // judegefile 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/stringutils.cpp similarity index 83% rename from core/components/string_util.cpp rename to core/components/stringutils.cpp index 2cee45e..f5ccdd0 100644 --- a/core/components/string_util.cpp +++ b/core/components/stringutils.cpp @@ -1,4 +1,4 @@ -#include "string_util.hpp" +#include "stringutils.hpp" using namespace iflytop; using namespace core; using namespace std; @@ -6,7 +6,7 @@ using namespace std; /*********************************************************************************************************************** * ======================================================private====================================================== * ***********************************************************************************************************************/ -char StringUtil::byteToChar(uint8_t byte) { +char StringUtils::byteToChar(uint8_t byte) { if (byte < 10) { return '0' + byte; } else { @@ -15,7 +15,7 @@ char StringUtil::byteToChar(uint8_t byte) { throw std::out_of_range("byteToChar out of range"); return 'x'; } -bool StringUtil::isLegalHex(char c) { +bool StringUtils::isLegalHex(char c) { if (c >= '0' && c <= '9') { return true; } else if (c >= 'A' && c <= 'F') { @@ -29,18 +29,18 @@ bool StringUtil::isLegalHex(char c) { * ======================================================public======================================================= * ***********************************************************************************************************************/ -string StringUtil::upper(const string& value) { +string StringUtils::upper(const string& value) { string cpy = value; transform(cpy.begin(), cpy.end(), cpy.begin(), ::toupper); return cpy; } -string StringUtil::lower(const string& value) { +string StringUtils::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 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; @@ -50,7 +50,7 @@ string StringUtil::bytesToString(const uint8_t* data, size_t size) { } return ret; } -string& StringUtil::replaceAllDistinct(string& str, const string& old_value, const string& new_value) { +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); @@ -61,7 +61,7 @@ string& StringUtil::replaceAllDistinct(string& str, const string& old_value, con return str; } -bool StringUtil::hexStringToBytes(string in, string delims, vector& byteTable) { +bool StringUtils::hexStringToBytes(string in, string delims, vector& byteTable) { string hexTable; byteTable.clear(); if (!delims.empty()) { @@ -104,7 +104,7 @@ bool StringUtil::hexStringToBytes(string in, string delims, vector& byt * @param value * @return string */ -string StringUtil::bytet2Binary(uint32_t value, int bitCount, bool remoteZero) { +string StringUtils::bytet2Binary(uint32_t value, int bitCount, bool remoteZero) { string ret; for (int i = 0; i < bitCount; ++i) { uint32_t bit = value & 0x01; diff --git a/core/components/string_util.hpp b/core/components/stringutils.hpp similarity index 92% rename from core/components/string_util.hpp rename to core/components/stringutils.hpp index eeae046..c81924e 100644 --- a/core/components/string_util.hpp +++ b/core/components/stringutils.hpp @@ -9,7 +9,7 @@ namespace iflytop { namespace core { using namespace std; -class StringUtil { +class StringUtils { char byteToChar(uint8_t byte); bool isLegalHex(char c); @@ -56,8 +56,8 @@ class StringUtil { * @return string 01010101 * * Demo: - * StringUtil::bytet2Binary(0x55,8,true) ===> 1010101 - * StringUtil::bytet2Binary(0x55,8,false) ===> 01010101 + * StringUtils::bytet2Binary(0x55,8,true) ===> 1010101 + * StringUtils::bytet2Binary(0x55,8,false) ===> 01010101 * */ string bytet2Binary(uint32_t value, int bitCount, bool remoteZero = true); diff --git a/core/components/time_util.hpp b/core/components/timeutils.hpp similarity index 93% rename from core/components/time_util.hpp rename to core/components/timeutils.hpp index 949c497..ac13cd3 100644 --- a/core/components/time_util.hpp +++ b/core/components/timeutils.hpp @@ -20,7 +20,7 @@ using namespace std; using namespace chrono; template -class T_TimeUtil { +class T_TimeUtils { public: time_point zero() { return time_point(nanoseconds(0)); } time_point now() { return move(T::now()); } @@ -68,14 +68,14 @@ class T_TimeUtil { 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 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_TimeUtil zsystem_clock; -typedef T_TimeUtil zsteady_clock; +typedef T_TimeUtils zsystem_clock; +typedef T_TimeUtils zsteady_clock; typedef time_point zsystem_tp; typedef time_point zsteady_tp; 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