Browse Source

temporary code submission

master
zhaohe 2 years ago
parent
commit
f82553449a
  1. 12
      core/components/config_template/config_template.hpp
  2. 34
      core/components/fileutils.cpp
  3. 4
      core/components/fileutils.hpp
  4. 2
      core/components/jobs/thread_pool_task_scheduler.hpp
  5. 21
      core/components/process/process.hpp
  6. 18
      core/components/stringutils.cpp
  7. 6
      core/components/stringutils.hpp
  8. 10
      core/components/timeutils.hpp
  9. 8
      core/components/uart/uart.cpp
  10. 42
      core/linuxcoreutils/linuxcoreutils.cpp
  11. 55
      core/linuxcoreutils/linuxcoreutils.hpp
  12. 7
      module.cmake

12
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"); \
} \

34
core/components/file_util.cpp → 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<char> FileUtil::readFileAsBuffer(const string &filePath) {
std::vector<char> FileUtils::readFileAsBuffer(const string &filePath) {
std::vector<char> ret;
if (!exist(filePath)) {
return std::move(ret);
@ -79,17 +79,17 @@ std::vector<char> 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<ofstream> FileUtil::openTrunc(const string &fileName) {
shared_ptr<ofstream> FileUtils::openTrunc(const string &fileName) {
makeDirIfNoExist(fileName);
shared_ptr<ofstream> 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 + "/";
}
}

4
core/components/file_util.hpp → 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

2
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__))

21
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.

18
core/components/string_util.cpp → 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<uint8_t>& byteTable) {
bool StringUtils::hexStringToBytes(string in, string delims, vector<uint8_t>& byteTable) {
string hexTable;
byteTable.clear();
if (!delims.empty()) {
@ -104,7 +104,7 @@ bool StringUtil::hexStringToBytes(string in, string delims, vector<uint8_t>& 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;

6
core/components/string_util.hpp → 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);

10
core/components/time_util.hpp → core/components/timeutils.hpp

@ -20,7 +20,7 @@ using namespace std;
using namespace chrono;
template <class T>
class T_TimeUtil {
class T_TimeUtils {
public:
time_point<T> zero() { return time_point<T>(nanoseconds(0)); }
time_point<T> now() { return move(T::now()); }
@ -68,14 +68,14 @@ class T_TimeUtil {
int64_t countdownTimeNs(time_point<T> endtime) { return dToNs(endtime - T::now()); }
};
typedef T_TimeUtil<system_clock> tu_sys; // not use in future
typedef T_TimeUtil<steady_clock> tu_steady; // not use in future
typedef T_TimeUtils<system_clock> tu_sys; // not use in future
typedef T_TimeUtils<steady_clock> tu_steady; // not use in future
typedef time_point<system_clock> tp_sys; // not use in future
typedef time_point<steady_clock> tp_steady; // not use in future
// new api name
typedef T_TimeUtil<system_clock> zsystem_clock;
typedef T_TimeUtil<steady_clock> zsteady_clock;
typedef T_TimeUtils<system_clock> zsystem_clock;
typedef T_TimeUtils<steady_clock> zsteady_clock;
typedef time_point<system_clock> zsystem_tp;
typedef time_point<steady_clock> zsteady_tp;

8
core/components/uart/uart.cpp

@ -18,8 +18,8 @@
#include <string.h>
#include <termios.h>
#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;

42
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<string> &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;
}

55
core/linuxcoreutils/linuxcoreutils.hpp

@ -0,0 +1,55 @@
//
// Created by zwsd
//
#pragma once
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#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<string>& files);
};
} // namespace iflytop

7
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

Loading…
Cancel
Save