From 3f4096aa42870122d596f3d4091e3d2171a72044 Mon Sep 17 00:00:00 2001 From: Zhaohe Date: Fri, 9 Dec 2022 09:54:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=9B=AE=E5=BD=95module=20?= =?UTF-8?q?=3D=3D>=20components?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/components/file_util.cpp | 131 ++++++++++++++++++++++++++++++++++++++++ core/components/file_util.hpp | 46 ++++++++++++++ core/components/string_util.cpp | 99 ++++++++++++++++++++++++++++++ core/components/string_util.hpp | 52 ++++++++++++++++ core/components/time_util.hpp | 78 ++++++++++++++++++++++++ core/module/file_util.cpp | 131 ---------------------------------------- core/module/file_util.hpp | 46 -------------- core/module/string_util.cpp | 99 ------------------------------ core/module/string_util.hpp | 52 ---------------- core/module/time_util.hpp | 78 ------------------------ module.cmake | 4 +- 11 files changed, 408 insertions(+), 408 deletions(-) create mode 100644 core/components/file_util.cpp create mode 100644 core/components/file_util.hpp create mode 100644 core/components/string_util.cpp create mode 100644 core/components/string_util.hpp create mode 100644 core/components/time_util.hpp delete mode 100644 core/module/file_util.cpp delete mode 100644 core/module/file_util.hpp delete mode 100644 core/module/string_util.cpp delete mode 100644 core/module/string_util.hpp delete mode 100644 core/module/time_util.hpp diff --git a/core/components/file_util.cpp b/core/components/file_util.cpp new file mode 100644 index 0000000..e65f9be --- /dev/null +++ b/core/components/file_util.cpp @@ -0,0 +1,131 @@ +#include "file_util.hpp" +using namespace iflytopcpp; +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()); +} +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 new file mode 100644 index 0000000..00d2c84 --- /dev/null +++ b/core/components/file_util.hpp @@ -0,0 +1,46 @@ +// +// 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 iflytopcpp { +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); + // 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 iflytopcpp diff --git a/core/components/string_util.cpp b/core/components/string_util.cpp new file mode 100644 index 0000000..37ca9af --- /dev/null +++ b/core/components/string_util.cpp @@ -0,0 +1,99 @@ +#include "string_util.hpp" +using namespace iflytopcpp; +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; +} \ No newline at end of file diff --git a/core/components/string_util.hpp b/core/components/string_util.hpp new file mode 100644 index 0000000..a3aa657 --- /dev/null +++ b/core/components/string_util.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include +#include +#include +#include +#include +namespace iflytopcpp { +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); +}; + +} // namespace core +} // namespace iflytopcpp diff --git a/core/components/time_util.hpp b/core/components/time_util.hpp new file mode 100644 index 0000000..15bf47e --- /dev/null +++ b/core/components/time_util.hpp @@ -0,0 +1,78 @@ +// +// Created by zhaohe on 19-6-2. +// + +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace iflytopcpp { +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; +typedef T_TimeUtil tu_steady; + +typedef time_point tp_sys; +typedef time_point tp_steady; + +}; // namespace core +} // namespace iflytopcpp diff --git a/core/module/file_util.cpp b/core/module/file_util.cpp deleted file mode 100644 index e65f9be..0000000 --- a/core/module/file_util.cpp +++ /dev/null @@ -1,131 +0,0 @@ -#include "file_util.hpp" -using namespace iflytopcpp; -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()); -} -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/module/file_util.hpp b/core/module/file_util.hpp deleted file mode 100644 index 00d2c84..0000000 --- a/core/module/file_util.hpp +++ /dev/null @@ -1,46 +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 iflytopcpp { -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); - // 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 iflytopcpp diff --git a/core/module/string_util.cpp b/core/module/string_util.cpp deleted file mode 100644 index 37ca9af..0000000 --- a/core/module/string_util.cpp +++ /dev/null @@ -1,99 +0,0 @@ -#include "string_util.hpp" -using namespace iflytopcpp; -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; -} \ No newline at end of file diff --git a/core/module/string_util.hpp b/core/module/string_util.hpp deleted file mode 100644 index a3aa657..0000000 --- a/core/module/string_util.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -namespace iflytopcpp { -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); -}; - -} // namespace core -} // namespace iflytopcpp diff --git a/core/module/time_util.hpp b/core/module/time_util.hpp deleted file mode 100644 index 15bf47e..0000000 --- a/core/module/time_util.hpp +++ /dev/null @@ -1,78 +0,0 @@ -// -// Created by zhaohe on 19-6-2. -// - -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace iflytopcpp { -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; -typedef T_TimeUtil tu_steady; - -typedef time_point tp_sys; -typedef time_point tp_steady; - -}; // namespace core -} // namespace iflytopcpp diff --git a/module.cmake b/module.cmake index 5164721..1a60b2a 100644 --- a/module.cmake +++ b/module.cmake @@ -10,8 +10,8 @@ set(DEP_INCLUDE ${DEP_INCLUDE} ./dep/iflytopcpp/core/spdlog/include/) set(DEP_SRC ${DEP_SRC} # dep/iflytopcpp/core/spdlogfactory/logger_factory.cpp - dep/iflytopcpp/core/module/file_util.cpp - dep/iflytopcpp/core/module/string_util.cpp + dep/iflytopcpp/core/components/file_util.cpp + dep/iflytopcpp/core/components/string_util.cpp dep/iflytopcpp/core/basic/signal/signal.cpp dep/iflytopcpp/core/thread/thread.cpp dep/iflytopcpp/core/zexception/zexception.cpp