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>
|
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
|
|
|
|
|
|
|
#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
|
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
|