add:添加时间字符串、去空白等内容

This commit is contained in:
taynpg 2024-12-13 23:02:30 +08:00
parent 44fb416ca1
commit 45d22c927b
6 changed files with 61 additions and 2 deletions

View File

@ -19,6 +19,7 @@ public:
static ofString get_full(const ofString& path, const ofString& sub_file_path);
static bool exist(const ofString& path);
static bool write(const ofString& path, const char* data, int len);
static ofString to_full(const ofString& path);
};
}; // namespace ofen
#endif

View File

@ -10,11 +10,12 @@ class COfStr
public:
COfStr();
~COfStr();
public:
static ofString replace(const ofString& str, const ofString& from, const ofString& to);
static std::vector<ofString> split(const ofString& input, const ofString& delimiter);
static ofString trim(const ofString& input);
};
}; // namespace ofen
#endif

View File

@ -38,6 +38,14 @@ private:
template <typename T> std::shared_ptr<T> OfSingleton<T>::instance = nullptr;
template <typename T> std::once_flag OfSingleton<T>::init_flag;
class OfUtil {
public:
OfUtil();
~OfUtil();
public:
static ofString now_time();
};
class CMutBuffer
{
public:

View File

@ -1,7 +1,7 @@
#include "of_path.h"
#include "of_str.h"
#include <filesystem>
#include <fstream>
#include "of_str.h"
#ifdef _WIN32
#include <windows.h>
@ -139,4 +139,21 @@ bool COfPath::write(const ofString& path, const char* data, int len)
return true;
}
ofString COfPath::to_full(const ofString& path)
{
fs::path base;
if (fs::path(path).is_relative()) {
base = fs::current_path();
base.append(path);
} else {
base = fs::path(path);
}
fs::path ret = fs::absolute(base);
#ifdef UNICODE_OFSTR
return ofString(ret.wstring());
#else
return ofString(ret.string());
#endif
}
} // namespace ofen

View File

@ -31,4 +31,13 @@ std::vector<ofString> COfStr::split(const ofString& input, const ofString& delim
result.push_back(input.substr(prev));
return result;
}
ofString COfStr::trim(const ofString& input)
{
size_t start = input.find_first_not_of(" \t\n\r\f\v");
if (start == std::string::npos) {
return "";
}
size_t end = input.find_last_not_of(" \t\n\r\f\v");
return input.substr(start, end - start + 1);
}
} // namespace ofen

View File

@ -1,4 +1,7 @@
#include "of_util.h"
#include <chrono>
#include <iomanip>
#include <sstream>
namespace ofen {
void CMutBuffer::push(const char* data, int len)
@ -43,4 +46,24 @@ void CMutBuffer::clear()
std::lock_guard<std::mutex> lock(mutex_);
buffer_.clear();
}
OfUtil::OfUtil()
{
}
OfUtil::~OfUtil()
{
}
ofString OfUtil::now_time()
{
auto now = std::chrono::system_clock::now();
auto time_t_now = std::chrono::system_clock::to_time_t(now);
std::tm tm_now;
#if defined(_WIN32) || defined(_WIN64)
localtime_s(&tm_now, &time_t_now);
#else
localtime_r(&time_t_now, &tm_now);
#endif
std::ostringstream oss;
oss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S");
return oss.str();
}
} // namespace ofen