2024-12-11 10:21:57 +08:00
|
|
|
#include "of_util.h"
|
2024-12-13 23:02:30 +08:00
|
|
|
#include <chrono>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2025-01-14 10:30:07 +08:00
|
|
|
#include <utf8.h>
|
2024-12-11 10:21:57 +08:00
|
|
|
|
2024-12-17 08:56:01 +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;
|
|
|
|
}
|
2024-12-17 08:56:01 +08:00
|
|
|
#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
|
2024-12-17 08:56:01 +08:00
|
|
|
auto end_pos = min(start_pos + len, static_cast<int>(buffer_.size()));
|
2024-12-24 12:46:52 +08:00
|
|
|
#endif
|
2024-12-17 08:56:01 +08:00
|
|
|
#else
|
2024-12-11 10:21:57 +08:00
|
|
|
auto end_pos = std::min(start_pos + len, static_cast<int>(buffer_.size()));
|
2024-12-17 08:56:01 +08:00
|
|
|
#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();
|
|
|
|
}
|
2024-12-13 23:02:30 +08:00
|
|
|
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
|
2024-12-13 23:02:30 +08:00
|
|
|
std::ostringstream oss;
|
2024-12-24 22:07:10 +08:00
|
|
|
oss << std::put_time(&tm_now, ofT("%Y-%m-%d %H:%M:%S"));
|
2024-12-13 23:02:30 +08:00
|
|
|
return oss.str();
|
2024-12-24 22:07:10 +08:00
|
|
|
#endif
|
2024-12-13 23:02:30 +08:00
|
|
|
}
|
2024-12-17 08:56:01 +08:00
|
|
|
|
2025-01-07 12:04:00 +08:00
|
|
|
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;
|
|
|
|
|
2025-01-18 19:17:13 +08:00
|
|
|
#if defined(UNICODE_OFSTR)
|
|
|
|
std::wostringstream oss;
|
|
|
|
#else
|
2025-01-07 12:04:00 +08:00
|
|
|
std::ostringstream oss;
|
2025-01-18 19:17:13 +08:00
|
|
|
#endif
|
|
|
|
|
2025-01-07 12:04:00 +08:00
|
|
|
if (gb > 0) {
|
2025-01-07 13:46:59 +08:00
|
|
|
oss << gb << ofT("G_");
|
2025-01-07 12:04:00 +08:00
|
|
|
}
|
|
|
|
if (mb > 0 || gb > 0) {
|
2025-01-07 13:46:59 +08:00
|
|
|
oss << mb << ofT("M_");
|
2025-01-07 12:04:00 +08:00
|
|
|
}
|
|
|
|
if (kb > 0 || mb > 0 || gb > 0) {
|
2025-01-07 13:46:59 +08:00
|
|
|
oss << kb << ofT("K");
|
2025-01-07 12:04:00 +08:00
|
|
|
}
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2024-12-17 08:56:01 +08:00
|
|
|
#ifdef _WIN32
|
2025-01-14 10:32:57 +08:00
|
|
|
std::string CCodec::u8_to_ansi(const std::string& str)
|
2024-12-17 08:56:01 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2025-01-14 10:32:57 +08:00
|
|
|
std::string CCodec::ansi_to_u8(const std::string& str)
|
2024-12-17 08:56:01 +08:00
|
|
|
{
|
|
|
|
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
|
2025-01-14 10:30:07 +08:00
|
|
|
|
|
|
|
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
|
2025-01-14 12:06:55 +08:00
|
|
|
|
2025-01-14 10:30:07 +08:00
|
|
|
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;
|
2025-01-14 12:06:55 +08:00
|
|
|
|
|
|
|
bool last_was_whitespace = false;
|
2025-01-14 12:14:43 +08:00
|
|
|
for (char32_t current : unicode_chars) {
|
2025-01-14 12:06:55 +08:00
|
|
|
bool is_whitespace = (current == U' ' || current == U'\t' || current == U'\n' || current == U'\r');
|
2025-01-14 12:14:43 +08:00
|
|
|
|
2025-01-14 12:06:55 +08:00
|
|
|
if (is_whitespace) {
|
2025-01-14 12:14:43 +08:00
|
|
|
last_was_whitespace = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (last_was_whitespace) {
|
|
|
|
last_was_whitespace = false;
|
2025-01-14 10:30:07 +08:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2025-01-18 19:17:13 +08:00
|
|
|
if (milsec <= 0) {
|
2024-12-18 13:55:24 +08:00
|
|
|
timeout_ = default_timeout_;
|
2025-01-18 19:17:13 +08:00
|
|
|
} else {
|
2024-12-18 13:55:24 +08:00
|
|
|
timeout_ = milsec;
|
2025-01-18 19:17:13 +08:00
|
|
|
}
|
2024-12-18 13:55:24 +08:00
|
|
|
}
|
|
|
|
bool CThreadSleep::get_status() const
|
|
|
|
{
|
|
|
|
return is_stop_sleep_;
|
|
|
|
}
|
2024-12-11 10:21:57 +08:00
|
|
|
} // namespace ofen
|