71 lines
1.6 KiB
C++
71 lines
1.6 KiB
C++
#include "Util.h"
|
|
#include <algorithm>
|
|
#include <filesystem>
|
|
#include <wx/stdpaths.h>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
TranUtil::TranUtil()
|
|
{
|
|
}
|
|
|
|
void MutBuffer::Push(const char* data, int len)
|
|
{
|
|
buffer_.insert(buffer_.end(), data, data + len);
|
|
}
|
|
|
|
int MutBuffer::IndexOf(const char* data, int len, int start_pos)
|
|
{
|
|
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;
|
|
}
|
|
|
|
int MutBuffer::Length() const
|
|
{
|
|
return static_cast<int>(buffer_.size());
|
|
}
|
|
|
|
void MutBuffer::RemoveOf(int start_pos, int len)
|
|
{
|
|
if (start_pos < 0 || start_pos >= static_cast<int>(buffer_.size()) || len <= 0) {
|
|
return;
|
|
}
|
|
auto end_pos = std::min(start_pos + len, static_cast<int>(buffer_.size()));
|
|
buffer_.erase(buffer_.begin() + start_pos, buffer_.begin() + end_pos);
|
|
}
|
|
|
|
void MutBuffer::Clear()
|
|
{
|
|
buffer_.clear();
|
|
}
|
|
|
|
const char* MutBuffer::GetData() const
|
|
{
|
|
return buffer_.data();
|
|
}
|
|
|
|
wxString wxUtil::GetConfigDir()
|
|
{
|
|
auto userDir = wxGetUserHome();
|
|
fs::path path(userDir.ToStdString());
|
|
path.append(".config");
|
|
return wxString::FromUTF8(path.string());
|
|
}
|
|
|
|
bool wxUtil::CreateConfigDir(const wxString& dir, const wxString& name, wxString& fullPath)
|
|
{
|
|
fs::path path(dir.ToStdString());
|
|
path.append(name.ToStdString());
|
|
fullPath = wxString::FromUTF8(path.string());
|
|
if (fs::exists(path)) {
|
|
return true;
|
|
}
|
|
return fs::create_directories(path);
|
|
}
|