Skip to content

Commit

Permalink
Fix #76
Browse files Browse the repository at this point in the history
  • Loading branch information
katahiromz committed Apr 19, 2024
1 parent 46dd39c commit 5c9f45d
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 95 deletions.
82 changes: 82 additions & 0 deletions Dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,85 @@ void XgSelectDict(HWND hwnd, size_t iDict)
xg_vMarkedCands.clear();
xg_iMarkedCand = -1;
}

// カギを使って辞書を更新する。
BOOL XgUpdateDictionaryUsingClues(HWND hwnd, const XGStringW& dict_name)
{
// カギがなければ失敗。
if (!xg_bSolved || xg_vecHorzHints.empty() || xg_vecVertHints.empty())
return FALSE;

// 辞書名がなければ失敗。
if (dict_name.empty())
return FALSE;

// 単語からヒントへの写像を作成する。
std::map<XGStringW, XGStringW> word_to_hint_map;
for (auto& hint : xg_vecHorzHints) {
word_to_hint_map[XgNormalizeString(hint.m_strWord)] = hint.m_strHint;
}
for (auto& hint : xg_vecVertHints) {
word_to_hint_map[XgNormalizeString(hint.m_strWord)] = hint.m_strHint;
}

// 辞書ファイルをすべて読み込む。
XGStringW str;
if (!XgReadTextFileAll(dict_name.c_str(), str))
return FALSE;

// 改行コードを正規化。
xg_str_replace_all(str, L"\r\n", L"\n");

// 行で分割する。
std::vector<XGStringW> lines;
mstr_split(lines, str, L"\n");

// 一行ずつ処理する。
for (auto& line : lines) {
if (line[0] == L'#')
continue;

// タブで分割する。
std::vector<XGStringW> fields;
mstr_split(fields, line, L"\t");

if (fields.empty())
continue;

auto strWord = XgNormalizeString(fields[0]);
auto strNormalized = XgNormalizeString(strWord);

auto it = word_to_hint_map.find(strNormalized);
if (it != word_to_hint_map.end()) {
fields[1] = it->second;
word_to_hint_map.erase(it);
line = mstr_join(fields, L"\t");
}
}

for (auto& pair : word_to_hint_map) {
XGStringW line = XgNormalizeStringEx(pair.first, TRUE, TRUE);
line += L'\t';
line += pair.second;
line += L'\n';
lines.push_back(line);
}

// 辞書ファイルに書き込む。
FILE *fp = _wfopen(dict_name.c_str(), L"w");
if (!fp)
return FALSE;

fprintf(fp, "\xEF\xBB\xBF"); // UTF-8 BOM

for (auto& line : lines) {
auto psz = XgUnicodeToUtf8(line).c_str();
if (!*psz)
continue;
fprintf(fp, "%s\n", psz);
}

fclose(fp);

return TRUE;
}
2 changes: 2 additions & 0 deletions Dictionary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,5 @@ BOOL XgLoadDictsAll(void);
void XgSelectDict(HWND hwnd, size_t iDict);
// 辞書からタイトルを取得。
XGStringW XgLoadTitleFromDict(LPCWSTR pszPath);
// カギを使って辞書を更新する。
BOOL XgUpdateDictionaryUsingClues(HWND hwnd, const XGStringW& dict_name);
2 changes: 2 additions & 0 deletions HISTORY.txt
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@
- Added a function to display the dictionary update date and time.
- The "Jump" dialog has been made modeless.
- Added "Jump to letter or word" function.
- Added the function "Update the dictionary file using the clues" to the "Advanced" settings.

# 開発履歴 (Japanese)

Expand Down Expand Up @@ -961,3 +962,4 @@
- 辞書の更新日時を表示する機能を追加。
- 「ジャンプ」ダイアログをモードレス化した。
- 「文字または単語にジャンプ」機能を追加。
- 「上級者向け」設定に「問題のカギを使って辞書ファイルを更新する」機能を追加。
24 changes: 24 additions & 0 deletions XG_HiddenDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class XG_HiddenDialog
{
case psh1: // PAT.txtに追加。
if (codeNotify == BN_CLICKED) {
HCURSOR hOldCursor = ::SetCursor(::LoadCursor(NULL, IDC_WAIT));
if (XgPatEdit(hwnd, TRUE)) {
// 成功メッセージ。
XgCenterMessageBoxW(hwnd, XgLoadStringDx1(IDS_WROTEPAT),
Expand All @@ -29,10 +30,12 @@ class XG_HiddenDialog
XgCenterMessageBoxW(hwnd, XgLoadStringDx1(IDS_CANTWRITEPAT),
nullptr, MB_ICONERROR);
}
::SetCursor(hOldCursor);
}
break;
case psh2: // PAT.txtから削除。
if (codeNotify == BN_CLICKED) {
HCURSOR hOldCursor = ::SetCursor(::LoadCursor(NULL, IDC_WAIT));
if (XgPatEdit(hwnd, FALSE)) {
// 成功メッセージ。
XgCenterMessageBoxW(hwnd, XgLoadStringDx1(IDS_WROTEPAT),
Expand All @@ -42,6 +45,7 @@ class XG_HiddenDialog
XgCenterMessageBoxW(hwnd, XgLoadStringDx1(IDS_CANTWRITEPAT),
nullptr, MB_ICONERROR);
}
::SetCursor(hOldCursor);
}
break;
case psh3: // アプリのフォルダを開く。
Expand All @@ -51,6 +55,26 @@ class XG_HiddenDialog
PathRemoveFileSpecW(szPath);
ShellExecuteW(hwnd, NULL, szPath, NULL, NULL, SW_SHOWNORMAL);
}
break;
case psh4: // カギを使って辞書を更新する。
if (codeNotify == BN_CLICKED) {
WCHAR szText[MAX_PATH * 2];
HCURSOR hOldCursor = ::SetCursor(::LoadCursor(NULL, IDC_WAIT));
XG_HintsWnd::UpdateHintData(); // ヒントに変更があれば、更新する。
if (XgUpdateDictionaryUsingClues(hwnd, xg_dict_name)) {
// 成功メッセージ。
StringCchPrintfW(szText, _countof(szText), XgLoadStringDx1(IDS_UPDATEDICTOK),
PathFindFileNameW(xg_dict_name.c_str()));
XgCenterMessageBoxW(hwnd, szText, XgLoadStringDx2(IDS_APPNAME), MB_ICONINFORMATION);
} else {
// 失敗メッセージ。
StringCchPrintfW(szText, _countof(szText), XgLoadStringDx1(IDS_UPDATEDICTFAIL),
PathFindFileNameW(xg_dict_name.c_str()));
XgCenterMessageBoxW(hwnd, szText, nullptr, MB_ICONERROR);
}
::SetCursor(hOldCursor);
}
break;
}
}

Expand Down
87 changes: 0 additions & 87 deletions XG_HiddenDialog.hpp

This file was deleted.

11 changes: 7 additions & 4 deletions lang/en_US.rc
Original file line number Diff line number Diff line change
Expand Up @@ -1572,14 +1572,15 @@ FONT 9, "Tahoma"
PUSHBUTTON "&Theme...", psh5, 115, 175, 105, 20
}

IDD_HIDDEN DIALOG 0, 0, 275, 170
IDD_HIDDEN DIALOG 0, 0, 275, 220
CAPTION "Advanced"
STYLE DS_CENTER | DS_MODALFRAME | WS_POPUPWINDOW | WS_CAPTION
FONT 9, "Tahoma"
{
PUSHBUTTON "&Add the current pattern to PAT.txt", psh1, 40, 10, 185, 40
PUSHBUTTON "&Remove the current pattern from PAT.txt", psh2, 40, 60, 185, 40
PUSHBUTTON "&Open app folder", psh3, 40, 110, 185, 40
PUSHBUTTON "&Add the current pattern to PAT.txt", psh1, 40, 10, 185, 40, BS_MULTILINE
PUSHBUTTON "&Remove the current pattern from PAT.txt", psh2, 40, 60, 185, 40, BS_MULTILINE
PUSHBUTTON "&Open app folder", psh3, 40, 110, 185, 40, BS_MULTILINE
PUSHBUTTON "&Update the current dictionary using the clues of the current problem", psh4, 40, 160, 185, 40, BS_MULTILINE
}

//////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1828,6 +1829,8 @@ STRINGTABLE
IDS_FILENAME, "Filename"
IDS_DISPLAYNAME, "Display Name"
IDS_UPDATEDTIME, "Updated Time"
IDS_UPDATEDICTOK, "Successfully wrote to dictionary file ""%s""."
IDS_UPDATEDICTFAIL, "Failed to write to dictionary file ""%s"". The folder may not be writable."
IDS_TT_NEW, "New crossword"
IDS_TT_GENERATE, "Generate crossword"
IDS_TT_OPEN, "Open crossword"
Expand Down
11 changes: 7 additions & 4 deletions lang/ja_JP.rc
Original file line number Diff line number Diff line change
Expand Up @@ -1576,14 +1576,15 @@ FONT 9, "MS UI Gothic"
PUSHBUTTON "テーマ(&T)...", psh5, 115, 175, 105, 20
}

IDD_HIDDEN DIALOG 0, 0, 275, 170
IDD_HIDDEN DIALOG 0, 0, 275, 220
CAPTION "上級者向け"
STYLE DS_CENTER | DS_MODALFRAME | WS_POPUPWINDOW | WS_CAPTION
FONT 9, "MS UI Gothic"
{
PUSHBUTTON "現在の黒マスパターンをPAT.txtに追加する(&A)", psh1, 40, 10, 185, 40
PUSHBUTTON "現在の黒マスパターンをPAT.txtから削除する(&D)", psh2, 40, 60, 185, 40
PUSHBUTTON "アプリのフォルダを開く(&O)", psh3, 40, 110, 185, 40
PUSHBUTTON "現在の黒マスパターンをPAT.txtに追加する(&A)", psh1, 40, 10, 185, 40, BS_MULTILINE
PUSHBUTTON "現在の黒マスパターンをPAT.txtから削除する(&D)", psh2, 40, 60, 185, 40, BS_MULTILINE
PUSHBUTTON "アプリのフォルダを開く(&O)", psh3, 40, 110, 185, 40, BS_MULTILINE
PUSHBUTTON "現在の問題のカギを使って現在の辞書を更新する(&U)", psh4, 40, 160, 185, 40, BS_MULTILINE
}

//////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1832,6 +1833,8 @@ STRINGTABLE
IDS_FILENAME, "ファイル名"
IDS_DISPLAYNAME, "表示名"
IDS_UPDATEDTIME, "更新日時"
IDS_UPDATEDICTOK, "辞書ファイル「%s」への書き込みに成功しました。"
IDS_UPDATEDICTFAIL, "辞書ファイル「%s」への書き込みに失敗しました。書き込めないフォルダの可能性があります。"
IDS_TT_NEW, "新規作成"
IDS_TT_GENERATE, "問題を自動生成する"
IDS_TT_OPEN, "問題を開く"
Expand Down
2 changes: 2 additions & 0 deletions resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@
#define IDS_FILENAME 332
#define IDS_DISPLAYNAME 333
#define IDS_UPDATEDTIME 334
#define IDS_UPDATEDICTOK 335
#define IDS_UPDATEDICTFAIL 336
#define IDS_TT_NEW 10100
#define IDS_TT_GENERATE 10101
#define IDS_TT_OPEN 10102
Expand Down

0 comments on commit 5c9f45d

Please sign in to comment.