doc_and_tools/tools/apps/hgjson/DlgAddWords.cpp

442 lines
11 KiB
C++

// DlgAddWords.cpp: 实现文件
//
#include "stdafx.h"
#include "hgjson.h"
#include "afxdialogex.h"
#include "DlgAddWords.h"
// CDlgAddWords 对话框
static bool is_id_existing(uint32_t id, void* param)
{
std::vector<sdk_util::LANGID>* lang = (std::vector<sdk_util::LANGID>*)param;
return std::find(lang->begin(), lang->end(), (int)id) != lang->end();
}
IMPLEMENT_DYNAMIC(CDlgAddWords, CDialogEx)
CDlgAddWords::CDlgAddWords(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_BATCH_ADD_LANG, pParent)
{
}
CDlgAddWords::~CDlgAddWords()
{
}
void CDlgAddWords::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, words_);
}
BOOL CDlgAddWords::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
int ind = 0;
words_.InsertColumn(ind++, TEXT("No."), 0, 48);
words_.InsertColumn(ind++, TEXT("中文简体"), 0, 168);
words_.InsertColumn(ind++, TEXT("中文繁体"), 0, 168);
words_.InsertColumn(ind++, TEXT("英语"), 0, 250);
words_.SetExtendedStyle(words_.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_INFOTIP);
words_.ModifyStyle(0, LVS_SINGLESEL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CDlgAddWords::open_pak(int lan_id)
{
file_util::PATHFILE file = { 0 };
if (file_util::browser_file(m_hWnd, &file, L"PAK file(*.pak)\0All Files(*.*)\0\0"))
{
set_lang_pak_file(file.path, lan_id);
}
}
void CDlgAddWords::set_lang_pak_file(const wchar_t* file, int lan_id)
{
std::vector<sdk_util::LANGIDW> lans;
int id = 0;
bool ok = false;
if (sdk_util::load_lang_pak(file, lans, &id))
{
if (id == lan_id || lan_id == 0)
{
ok = true;
if (id == 936)
{
::SetDlgItemTextW(m_hWnd, IDC_EDIT_HZ_PAK, file);
lan_hz_ = std::move(lans);
}
else if (id == 950)
{
::SetDlgItemTextW(m_hWnd, IDC_EDIT_HZT, file);
lan_hzt_ = std::move(lans);
}
else if (id == 20127)
{
::SetDlgItemTextW(m_hWnd, IDC_EDIT_ENGLISH, file);
lan_eng_ = std::move(lans);
}
else
ok = false;
}
}
if (!ok && lan_id)
::MessageBoxW(m_hWnd, file, L"不是指定的语言包", MB_OK | MB_ICONERROR);
}
void CDlgAddWords::set_code_file(const wchar_t* file, bool tip)
{
std::wstring unic(sdk_util::load_file(file));
if (unic.find(L"unsigned char g_chinese[] = {") == std::wstring::npos)
{
if(tip)
::MessageBoxW(m_hWnd, file, L"不是指定的简体中文语言头文件", MB_OK | MB_ICONERROR);
}
else
::SetDlgItemTextW(m_hWnd, IDC_EDIT_CODE, file);
}
bool CDlgAddWords::add_words(const wchar_t* pak_file, std::vector<int>& ids, std::string& cont, int lan_id, int src_col, bool init_id)
{
std::vector<sdk_util::LANGIDW> prev;
int id = 0;
if (sdk_util::load_lang_pak(pak_file, prev, &id))
{
if (id == lan_id)
{
std::vector<sdk_util::LANGID> now;
for (auto& v : prev)
{
sdk_util::LANGID item;
item.cp = id;
item.id = v.id;
coding_util::unicode_2_utf8(v.unic.c_str(), sdk_util::got_str, &item.utf8);
now.push_back(std::move(item));
}
for (int i = 0; i < words_.GetItemCount(); ++i)
{
wchar_t text[256] = { 0 };
sdk_util::LANGID item;
words_.GetItemText(i, src_col, text, _countof(text) - 1);
if (init_id)
{
ids.push_back(sdk_util::make_string_id(text, &is_id_existing, &now));
}
item.cp = lan_id;
item.id = ids[i];
coding_util::unicode_2_utf8(text, sdk_util::got_str, &item.utf8);
now.push_back(std::move(item));
}
cont = "";
sdk_util::save_2_lang_pak(m_hWnd, now, lan_id, &cont, false);
return true;
}
::MessageBoxW(m_hWnd, L"语言包不匹配", L"错误", MB_OK | MB_ICONSTOP);
}
else
::MessageBoxW(m_hWnd, pak_file, L"文件加载失败", MB_OK | MB_ICONSTOP);
return false;
}
bool CDlgAddWords::update_head_file(const wchar_t* file, const std::string& cont)
{
int bom = sdk_util::BOM_ANSI;
std::wstring code(sdk_util::load_file(file, &bom)), ar(L"unsigned char g_chinese[] = {");
size_t pos = code.find(L"unsigned char g_chinese[] = {"), end = 0;
if (pos == std::wstring::npos)
return false;
for (int i = 0; i < cont.length(); ++i)
{
unsigned char ch = cont[i];
wchar_t buf[20] = { 0 };
swprintf_s(buf, _countof(buf) - 1, L"0x%02X", ch);
if ((i % 16) == 0)
{
if (i)
ar += L",";
ar += L"\r\n";
}
else if ((i % 8) == 0)
{
if (i)
ar += L",";
ar += L" ";
}
else if (i)
ar += L", ";
ar += buf;
}
ar += L"\r\n};\r\n";
end = code.find(L";", pos);
if (end == std::wstring::npos)
code.erase(pos);
else
code.erase(pos, end - pos + 1);
code.insert(pos, ar);
ar = file;
file_util::force_move_file(file, (ar + L".bak").c_str());
bom = sdk_util::save_file(file, code, bom);
if(bom)
file_util::force_move_file((ar + L".bak").c_str(), file);
return bom == 0;
}
BEGIN_MESSAGE_MAP(CDlgAddWords, CDialogEx)
ON_BN_CLICKED(IDC_BUTTON_BROWSE_HZ, &CDlgAddWords::OnBnClickedButtonBrowseHz)
ON_BN_CLICKED(IDC_BUTTON_BROWSE_HZT, &CDlgAddWords::OnBnClickedButtonBrowseHzt)
ON_BN_CLICKED(IDC_BUTTON_BROWSE_ENG, &CDlgAddWords::OnBnClickedButtonBrowseEng)
ON_BN_CLICKED(IDC_BUTTON_BROWSE_CODE, &CDlgAddWords::OnBnClickedButtonBrowseCode)
ON_BN_CLICKED(IDC_BUTTON_ADD, &CDlgAddWords::OnBnClickedButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_DEL, &CDlgAddWords::OnBnClickedButtonDel)
ON_BN_CLICKED(IDC_BUTTON_CLEAR, &CDlgAddWords::OnBnClickedButtonClear)
ON_BN_CLICKED(IDC_BUTTON_UPDATE, &CDlgAddWords::OnBnClickedButtonUpdate)
ON_WM_DROPFILES()
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CDlgAddWords::OnNMDblclkList1)
END_MESSAGE_MAP()
// CDlgAddWords 消息处理程序
void CDlgAddWords::OnDropFiles(HDROP hDropInfo)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
wchar_t path[MAX_PATH] = { 0 };
int cnt = DragQueryFileW(hDropInfo, -1, path, _countof(path) - 1);
for (int i = 0; i < cnt; ++i)
{
int len = DragQueryFileW(hDropInfo, i, path, _countof(path) - 1);
std::wstring file(path, len), ext(file);
STR_FILE_EXTENSION(ext);
if (wcsicmp(ext.c_str(), L"pak") == 0)
{
set_lang_pak_file(file.c_str());
}
else if (wcsicmp(ext.c_str(), L"h") == 0)
{
set_code_file(file.c_str(), false);
}
}
CDialogEx::OnDropFiles(hDropInfo);
}
void CDlgAddWords::OnNMDblclkList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
*pResult = 0;
wchar_t text[256] = { 0 };
words_.GetItemText(pNMItemActivate->iItem, 1, text, _countof(text) - 1);
::SetDlgItemTextW(m_hWnd, IDC_EDIT_HZ_WORD, text);
words_.GetItemText(pNMItemActivate->iItem, 2, text, _countof(text) - 1);
::SetDlgItemTextW(m_hWnd, IDC_EDIT_HZT_WORD, text);
words_.GetItemText(pNMItemActivate->iItem, 3, text, _countof(text) - 1);
::SetDlgItemTextW(m_hWnd, IDC_EDIT_ENG_WORD, text);
}
void CDlgAddWords::OnBnClickedButtonBrowseHz()
{
// TODO: 在此添加控件通知处理程序代码
open_pak(936);
}
void CDlgAddWords::OnBnClickedButtonBrowseHzt()
{
// TODO: 在此添加控件通知处理程序代码
open_pak(950);
}
void CDlgAddWords::OnBnClickedButtonBrowseEng()
{
// TODO: 在此添加控件通知处理程序代码
open_pak(20127);
}
void CDlgAddWords::OnBnClickedButtonBrowseCode()
{
// TODO: 在此添加控件通知处理程序代码
file_util::PATHFILE file = { 0 };
if (file_util::browser_file(m_hWnd, &file, L"Header file(*.h)\0\0"))
{
set_code_file(file.path);
}
}
void CDlgAddWords::OnBnClickedButtonAdd()
{
// TODO: 在此添加控件通知处理程序代码
std::wstring hz(sdk_util::get_wnd_text(m_hWnd, IDC_EDIT_HZ_WORD)),
hzt(sdk_util::get_wnd_text(m_hWnd, IDC_EDIT_HZT_WORD)),
eng(sdk_util::get_wnd_text(m_hWnd, IDC_EDIT_ENG_WORD));
wchar_t text[256] = { 0 };
int item = 0;
for (int i = 0; i < words_.GetItemCount(); ++i)
{
words_.GetItemText(i, 1, text, _countof(text) - 1);
if (hz == text)
{
::MessageBoxW(m_hWnd, text, L"词条已经存在", MB_OK | MB_ICONSTOP);
return;
}
}
item = words_.InsertItem(words_.GetItemCount(), std::to_wstring(words_.GetItemCount() + 1).c_str());
words_.SetItemText(item, 1, hz.c_str());
words_.SetItemText(item, 2, hzt.c_str());
words_.SetItemText(item, 3, eng.c_str());
}
void CDlgAddWords::OnBnClickedButtonDel()
{
// TODO: 在此添加控件通知处理程序代码
for (int i = 0; i < words_.GetItemCount(); ++i)
{
if (words_.GetItemState(i, LVIF_STATE) & LVIS_SELECTED)
{
words_.DeleteItem(i--);
}
}
for (int i = 0; i < words_.GetItemCount(); ++i)
words_.SetItemText(i, 0, std::to_wstring(i + 1).c_str());
}
void CDlgAddWords::OnBnClickedButtonClear()
{
// TODO: 在此添加控件通知处理程序代码
words_.DeleteAllItems();
}
void CDlgAddWords::OnBnClickedButtonUpdate()
{
// TODO: 在此添加控件通知处理程序代码
if (words_.GetItemCount() == 0)
{
::MessageBoxW(m_hWnd, L"No words should be added into language packet!", L"None", MB_OK);
return;
}
std::wstring hzf(sdk_util::get_wnd_text(m_hWnd, IDC_EDIT_HZ_PAK)),
hztf(sdk_util::get_wnd_text(m_hWnd, IDC_EDIT_HZT)),
engf(sdk_util::get_wnd_text(m_hWnd, IDC_EDIT_ENGLISH)),
hf(sdk_util::get_wnd_text(m_hWnd, IDC_EDIT_CODE));
bool hzb(file_util::is_file_existing(hzf.c_str())),
hztb(file_util::is_file_existing(hztf.c_str())),
engb(file_util::is_file_existing(engf.c_str())),
hb(file_util::is_file_existing(hf.c_str()));
if (!hzb)
{
::MessageBoxW(m_hWnd, hzf.c_str(), L"File Not Found", MB_OK | MB_ICONSTOP);
GotoDlgCtrl(GetDlgItem(IDC_EDIT_HZ_PAK));
return;
}
if (!hztb)
{
::MessageBoxW(m_hWnd, hztf.c_str(), L"File Not Found", MB_OK | MB_ICONSTOP);
GotoDlgCtrl(GetDlgItem(IDC_EDIT_HZT));
return;
}
if (!engb)
{
::MessageBoxW(m_hWnd, hzf.c_str(), L"File Not Found", MB_OK | MB_ICONSTOP);
GotoDlgCtrl(GetDlgItem(IDC_EDIT_ENGLISH));
return;
}
if (!hb)
{
::MessageBoxW(m_hWnd, hzf.c_str(), L"File Not Found", MB_OK | MB_ICONSTOP);
GotoDlgCtrl(GetDlgItem(IDC_EDIT_CODE));
return;
}
//
std::vector<int> ids;
std::string cont("");
int err = 0;
while (add_words(hzf.c_str(), ids, cont, 936, 1, true))
{
file_util::force_move_file(hzf.c_str(), (hzf + L".bak").c_str());
err = file_util::save_2_file(cont.c_str(), cont.length(), hzf.c_str());
if (err)
{
::MessageBoxW(m_hWnd, hzf.c_str(), L"Write file failed", MB_OK | MB_ICONERROR);
break;
}
// change head file ...
if (update_head_file(hf.c_str(), cont))
{
if (add_words(hztf.c_str(), ids, cont, 950, 2))
{
file_util::force_move_file(hztf.c_str(), (hztf + L".bak").c_str());
err = file_util::save_2_file(cont.c_str(), cont.length(), hztf.c_str());
if (err)
{
::MessageBoxW(m_hWnd, hztf.c_str(), L"Write file failed", MB_OK | MB_ICONERROR);
break;
}
if (add_words(engf.c_str(), ids, cont, 20127, 3))
{
file_util::force_move_file(engf.c_str(), (engf + L".bak").c_str());
err = file_util::save_2_file(cont.c_str(), cont.length(), engf.c_str());
}
}
else
err = -2;
}
else
{
err = -1;
}
break;
}
if (err == 0)
::MessageBoxW(m_hWnd, L"All languages have updated!", L"Congratulation", MB_OK | MB_ICONINFORMATION);
else
{
}
}