ofen/src/of_util.cpp

232 lines
6.1 KiB
C++
Raw Normal View History

2024-12-11 10:21:57 +08:00
#include "of_util.h"
#include <chrono>
#include <iomanip>
#include <sstream>
#include <utf8.h>
2024-12-11 10:21:57 +08:00
#ifdef _WIN32
#include <windows.h>
#endif
2024-12-11 10:21:57 +08:00
namespace ofen {
void CMutBuffer::push(const char* data, int len)
{
std::lock_guard<std::mutex> lock(mutex_);
buffer_.insert(buffer_.end(), data, data + len);
}
int CMutBuffer::index_of(const char* data, int len, int start_pos)
{
std::lock_guard<std::mutex> lock(mutex_);
if (start_pos < 0 || start_pos >= static_cast<int>(buffer_.size()) || len <= 0) {
return -1;
}
auto it = std::search(buffer_.begin() + start_pos, buffer_.end(), data, data + len);
if (it != buffer_.end()) {
return std::distance(buffer_.begin(), it);
}
return -1;
}
void CMutBuffer::remove_of(int start_pos, int len)
{
std::lock_guard<std::mutex> lock(mutex_);
if (start_pos < 0 || start_pos >= static_cast<int>(buffer_.size()) || len <= 0) {
return;
}
#ifdef _WIN32
2024-12-24 12:46:52 +08:00
#if defined(__MINGW32__) || defined(__MINGW64__)
auto end_pos = std::min(start_pos + len, static_cast<int>(buffer_.size()));
#else
auto end_pos = min(start_pos + len, static_cast<int>(buffer_.size()));
2024-12-24 12:46:52 +08:00
#endif
#else
2024-12-11 10:21:57 +08:00
auto end_pos = std::min(start_pos + len, static_cast<int>(buffer_.size()));
#endif
2024-12-11 10:21:57 +08:00
buffer_.erase(buffer_.begin() + start_pos, buffer_.begin() + end_pos);
}
2024-12-11 17:00:42 +08:00
const char* CMutBuffer::get_data() const
{
return buffer_.data();
}
2024-12-11 22:49:46 +08:00
int CMutBuffer::get_len() const
{
return static_cast<int>(buffer_.size());
}
2024-12-11 10:21:57 +08:00
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
2024-12-24 22:07:10 +08:00
#ifdef UNICODE_OFSTR
std::wostringstream oss;
oss << std::put_time(&tm_now, ofT("%Y-%m-%d %H:%M:%S"));
return oss.str();
#else
std::ostringstream oss;
2024-12-24 22:07:10 +08:00
oss << std::put_time(&tm_now, ofT("%Y-%m-%d %H:%M:%S"));
return oss.str();
2024-12-24 22:07:10 +08:00
#endif
}
ofString OfUtil::get_file_size(long long bytes)
{
constexpr long long gbblk = 1024 * 1024 * 1024;
constexpr long long mbblk = 1024 * 1024;
long long gb = bytes / gbblk;
bytes %= gbblk;
long long mb = bytes / mbblk;
bytes %= mbblk;
long long kb = bytes / 1024;
std::ostringstream oss;
if (gb > 0) {
oss << gb << ofT("G_");
}
if (mb > 0 || gb > 0) {
oss << mb << ofT("M_");
}
if (kb > 0 || mb > 0 || gb > 0) {
oss << kb << ofT("K");
}
return oss.str();
}
#ifdef _WIN32
std::string CCodec::u8ToGBK(const std::string& str)
{
int wideCharLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
if (wideCharLen <= 0) {
return "";
}
std::wstring wideStr(wideCharLen, L'\0');
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wideStr[0], wideCharLen);
int gbkLen = WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (gbkLen <= 0) {
return "";
}
std::string gbkStr(gbkLen, '\0');
WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, &gbkStr[0], gbkLen, nullptr, nullptr);
gbkStr.resize(gbkLen - 1);
return gbkStr;
}
std::string CCodec::GBKTou8(const std::string& str)
{
int wideCharLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0);
if (wideCharLen <= 0) {
return "";
}
std::wstring wideStr(wideCharLen, L'\0');
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &wideStr[0], wideCharLen);
int utf8Len = WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (utf8Len <= 0) {
return "";
}
std::string utf8Str(utf8Len, '\0');
WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, &utf8Str[0], utf8Len, nullptr, nullptr);
utf8Str.resize(utf8Len - 1);
return utf8Str;
}
#endif
ofString CCodec::rbs(const ofString& str)
{
std::string utf8_str;
#ifdef UNICODE_OFSTR
utf8::utf16to8(str.begin(), str.end(), std::back_inserter(utf8_str));
#else
utf8_str = str;
#endif
std::vector<char32_t> unicode_chars;
utf8::utf8to32(utf8_str.begin(), utf8_str.end(), std::back_inserter(unicode_chars));
std::vector<char32_t> processed_chars;
for (size_t i = 0; i < unicode_chars.size(); ++i) {
char32_t current = unicode_chars[i];
if (current == U' ' || current == U'\t' || current == U'\n' || current == U'\r') {
bool near_non_ascii = false;
if (i > 0 && unicode_chars[i - 1] > 0x7F) {
near_non_ascii = true;
}
if (i + 1 < unicode_chars.size() && unicode_chars[i + 1] > 0x7F) {
near_non_ascii = true;
}
if (near_non_ascii) {
continue;
}
}
processed_chars.push_back(current);
}
std::string result_utf8;
utf8::utf32to8(processed_chars.begin(), processed_chars.end(), std::back_inserter(result_utf8));
ofString result;
#ifdef UNICODE_OFSTR
utf8::utf8to16(result_utf8.begin(), result_utf8.end(), std::back_inserter(result));
#else
result = result_utf8;
#endif
return result;
}
2024-12-18 13:55:24 +08:00
CThreadSleep::CThreadSleep()
{
is_stop_sleep_ = false;
timeout_ = 10;
}
void CThreadSleep::sleep(int milsec)
{
int sleep_time = timeout_;
if (milsec > 0)
sleep_time = milsec;
is_stop_sleep_ = false;
std::unique_lock<std::mutex> lock(mutex_);
std::chrono::system_clock::time_point nowtime = std::chrono::system_clock::now();
if (cv_.wait_until(lock, nowtime + (std::chrono::milliseconds(sleep_time)), [this] { return get_status(); })) {
// 手动
} else {
// 超时
}
}
void CThreadSleep::contiune()
{
is_stop_sleep_ = true;
cv_.notify_all();
}
void CThreadSleep::set_timeout(int milsec)
{
if (milsec <= 0)
timeout_ = default_timeout_;
else
timeout_ = milsec;
}
bool CThreadSleep::get_status() const
{
return is_stop_sleep_;
}
2024-12-11 10:21:57 +08:00
} // namespace ofen