OneLevelXmlOpr/public_def.cpp

103 lines
2.9 KiB
C++
Raw Normal View History

#include "public_def.h"
#include <QMessageBox>
#include <QFileDialog>
2024-05-24 09:18:06 +08:00
#ifdef _WIN32
#include <windows.h>
#endif
void CUtil::msg(QWidget* parent, const QString& content)
{
QMessageBox::information(parent, u8"提示", content);
}
bool CUtil::affirm(QWidget* parent, const QString& titile, const QString& content)
{
QMessageBox questionBox(parent);
questionBox.setText(content);
questionBox.setWindowTitle(titile);
questionBox.setIcon(QMessageBox::Question);
questionBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
int result = questionBox.exec();
if (result != QMessageBox::Yes) {
return false;
} else {
return true;
}
}
QString CUtil::select_file(QWidget* parent, const QString& info, const QString& filter)
{
2024-05-24 09:18:06 +08:00
QString filePath = QFileDialog::getOpenFileName(parent, info, QDir::homePath(), filter);
return filePath;
}
2024-05-18 01:26:52 +08:00
void CUtil::sort_by_repeat(std::vector<std::string>& vec)
{
2024-05-24 09:18:06 +08:00
std::size_t len1 = 0;
std::size_t len2 = 0;
std::size_t cur = 0;
auto compare = [&](const std::string& str1, const std::string& str2) {
len1 = str1.size();
len2 = str2.size();
if (cur >= len1 || cur >= len2) {
return len1 > len2;
}
return str1[cur] > str2[cur];
};
std::size_t max_len = 0;
for (const auto& data : vec) {
max_len = data.size() > max_len ? data.size() : max_len;
}
for (cur = 0; cur < max_len; ++cur) {
std::sort(vec.begin(), vec.end(), compare);
}
}
#ifdef _WIN32
std::string CUtil::utf8_to_gbk(const std::string& utf8_str)
{
// UTF-8 to Wide Char (UTF-16)
int wide_char_len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, nullptr, 0);
if (wide_char_len == 0) {
return "";
}
std::wstring wide_str(wide_char_len, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, &wide_str[0], wide_char_len);
// Wide Char (UTF-16) to GBK
int gbk_len = WideCharToMultiByte(CP_ACP, 0, wide_str.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (gbk_len == 0) {
return "";
}
std::string gbk_str(gbk_len, 0);
WideCharToMultiByte(CP_ACP, 0, wide_str.c_str(), -1, &gbk_str[0], gbk_len, nullptr, nullptr);
return gbk_str;
}
std::vector<std::string> CUtil::splitString(const std::string& input, const std::string& delimiter)
{
std::vector<std::string> tokens;
size_t pos = 0;
std::string backup = input;
std::string token;
while ((pos = backup.find(delimiter)) != std::string::npos) {
token = backup.substr(0, pos);
tokens.push_back(token);
backup.erase(0, pos + delimiter.length());
}
// Push the remaining part after the last delimiter
tokens.push_back(backup);
return tokens;
}
#else
std::string CUtil::utf8_to_gbk(const std::string& utf8_str)
{
return utf8_str;
}
#endif