From acbae76737cae2781bf177745fa3ebb54a317d59 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 30 Mar 2017 10:57:31 +0200 Subject: [PATCH] Clean-up --- ZRCola/zrcolachrcatpnl.cpp | 30 +-- ZRCola/zrcolachrslct.cpp | 32 +-- ZRCola/zrcolasettings.cpp | 20 +- ZRColaCompile/main.cpp | 50 ++--- lib/libZRCola/include/zrcola/character.h | 35 ++-- lib/libZRCola/include/zrcola/language.h | 48 ++--- lib/libZRCola/include/zrcola/tag.h | 4 +- lib/libZRCola/include/zrcola/translate.h | 197 +++++++++++++++++-- lib/libZRCola/src/language.cpp | 2 +- lib/libZRCola/src/translate.cpp | 32 +-- lib/libZRColaUI/include/zrcolaui/chargroup.h | 22 ++- 11 files changed, 324 insertions(+), 148 deletions(-) diff --git a/ZRCola/zrcolachrcatpnl.cpp b/ZRCola/zrcolachrcatpnl.cpp index 94f4d3a..1d93ac3 100644 --- a/ZRCola/zrcolachrcatpnl.cpp +++ b/ZRCola/zrcolachrcatpnl.cpp @@ -42,8 +42,8 @@ wxZRColaCharacterCatalogPanel::wxZRColaCharacterCatalogPanel(wxWindow* parent) : dat >> rec; if (!dat.good()) { wxFAIL_MSG(wxT("Error reading character group data from ZRCola.zrcdb.")); - m_cg_db.idxRnk.clear(); - m_cg_db.data .clear(); + m_cg_db.idxRank.clear(); + m_cg_db.data .clear(); } } else wxFAIL_MSG(wxT("ZRCola.zrcdb has no character group data.")); @@ -52,16 +52,16 @@ wxZRColaCharacterCatalogPanel::wxZRColaCharacterCatalogPanel(wxWindow* parent) : wxFAIL_MSG(wxT("ZRCola.zrcdb is not a valid ZRCola database.")); } - if (!m_cg_db.idxRnk.empty()) { + if (!m_cg_db.idxRank.empty()) { // Populate character group list. - for (size_t i = 0, n = m_cg_db.idxRnk.size(); i < n; i++) { - const ZRCola::chrgrp_db::chrgrp &cg = m_cg_db.idxRnk[i]; + for (size_t i = 0, n = m_cg_db.idxRank.size(); i < n; i++) { + const ZRCola::chrgrp_db::chrgrp &cg = m_cg_db.idxRank[i]; wxString label(cg.name(), cg.name_len()), label_tran2(wxGetTranslation(label, wxT("ZRCola-zrcdb"))); m_choice->Insert(label_tran2, i); } - m_cg_id = m_cg_db.idxRnk[0].id; + m_cg_id = m_cg_db.idxRank[0].grp; m_choice->Select(0); Update(); @@ -83,10 +83,10 @@ wxZRColaCharacterCatalogPanel::~wxZRColaCharacterCatalogPanel() void wxZRColaCharacterCatalogPanel::OnChoice(wxCommandEvent& event) { - const ZRCola::chrgrp_db::chrgrp &cg = m_cg_db.idxRnk[event.GetSelection()]; + const ZRCola::chrgrp_db::chrgrp &cg = m_cg_db.idxRank[event.GetSelection()]; - if (m_cg_id != cg.id) { - m_cg_id = cg.id; + if (m_cg_id != cg.grp) { + m_cg_id = cg.grp; Update(); } @@ -151,7 +151,7 @@ void wxZRColaCharacterCatalogPanel::OnFocusSource(wxCommandEvent& event) void wxZRColaCharacterCatalogPanel::Update() { - const ZRCola::chrgrp_db::chrgrp &cg = m_cg_db.idxRnk[m_choice->GetSelection()]; + const ZRCola::chrgrp_db::chrgrp &cg = m_cg_db.idxRank[m_choice->GetSelection()]; if (m_show_all->GetValue()) { m_grid->SetCharacters( @@ -210,11 +210,11 @@ bool wxPersistentZRColaCharacterCatalogPanel::Restore() // Restore selected character group. int cg_id; if (RestoreValue(wxT("charGroup"), &cg_id)) { - for (size_t i = 0, n = wnd->m_cg_db.idxRnk.size(); i < n; i++) { - const ZRCola::chrgrp_db::chrgrp &cg = wnd->m_cg_db.idxRnk[i]; - if (cg.id == cg_id) { - if (wnd->m_cg_id != cg.id) { - wnd->m_cg_id = cg.id; + for (size_t i = 0, n = wnd->m_cg_db.idxRank.size(); i < n; i++) { + const ZRCola::chrgrp_db::chrgrp &cg = wnd->m_cg_db.idxRank[i]; + if (cg.grp == cg_id) { + if (wnd->m_cg_id != cg.grp) { + wnd->m_cg_id = cg.grp; wnd->m_choice->Select(i); update = true; } diff --git a/ZRCola/zrcolachrslct.cpp b/ZRCola/zrcolachrslct.cpp index f7e2d3c..362e241 100644 --- a/ZRCola/zrcolachrslct.cpp +++ b/ZRCola/zrcolachrslct.cpp @@ -217,11 +217,11 @@ wxZRColaCharSelect::wxZRColaCharSelect(wxWindow* parent) : // Fill categories. auto app = dynamic_cast(wxTheApp); - for (size_t i = 0, n = app->m_cc_db.idxRnk.size(); i < n; i++) { - const auto &cc = app->m_cc_db.idxRnk[i]; + for (size_t i = 0, n = app->m_cc_db.idxRank.size(); i < n; i++) { + const auto &cc = app->m_cc_db.idxRank[i]; int idx = m_categories->Insert(wxGetTranslation(wxString(cc.name(), cc.name_len()), wxT("ZRCola-zrcdb")), i); m_categories->Check(idx); - m_ccOrder.insert(std::make_pair(cc.id, idx)); + m_ccOrder.insert(std::make_pair(cc.cat, idx)); } ResetResults(); @@ -277,7 +277,7 @@ void wxZRColaCharSelect::OnIdle(wxIdleEvent& event) } { char cc[sizeof(ZRCola::chrcat_db::chrcat)] = {}; - ((ZRCola::chrcat_db::chrcat*)cc)->id = chr.cat; + ((ZRCola::chrcat_db::chrcat*)cc)->cat= chr.cat; size_t start; // Update character category. if (app->m_cc_db.idxChrCat.find(*((ZRCola::chrcat_db::chrcat*)cc), start)) { @@ -359,10 +359,10 @@ void wxZRColaCharSelect::OnIdle(wxIdleEvent& event) m_searchThread->m_search.assign(val.c_str(), val.Length()); // Select categories. - for (size_t i = 0, n = app->m_cc_db.idxRnk.size(); i < n; i++) { - const auto &cc = app->m_cc_db.idxRnk[i]; + for (size_t i = 0, n = app->m_cc_db.idxRank.size(); i < n; i++) { + const auto &cc = app->m_cc_db.idxRank[i]; if (m_categories->IsChecked(i)) - m_searchThread->m_cats.insert(cc.id); + m_searchThread->m_cats.insert(cc.cat); } if (m_searchThread->Run() != wxTHREAD_NO_ERROR) { @@ -407,7 +407,7 @@ void wxZRColaCharSelect::OnCategoriesAll(wxHyperlinkEvent& event) event.StopPropagation(); auto app = dynamic_cast(wxTheApp); - for (size_t i = 0, n = app->m_cc_db.idxRnk.size(); i < n; i++) + for (size_t i = 0, n = app->m_cc_db.idxRank.size(); i < n; i++) m_categories->Check(i, true); m_searchChanged = true; @@ -419,7 +419,7 @@ void wxZRColaCharSelect::OnCategoriesNone(wxHyperlinkEvent& event) event.StopPropagation(); auto app = dynamic_cast(wxTheApp); - for (size_t i = 0, n = app->m_cc_db.idxRnk.size(); i < n; i++) + for (size_t i = 0, n = app->m_cc_db.idxRank.size(); i < n; i++) m_categories->Check(i, false); m_searchChanged = true; @@ -431,7 +431,7 @@ void wxZRColaCharSelect::OnCategoriesInvert(wxHyperlinkEvent& event) event.StopPropagation(); auto app = dynamic_cast(wxTheApp); - for (size_t i = 0, n = app->m_cc_db.idxRnk.size(); i < n; i++) + for (size_t i = 0, n = app->m_cc_db.idxRank.size(); i < n; i++) m_categories->Check(i, !m_categories->IsChecked(i)); m_searchChanged = true; @@ -817,10 +817,10 @@ void wxPersistentZRColaCharSelect::Save() const } SaveValue(wxT("recentChars2"), val); - for (size_t i = 0, n = app->m_cc_db.idxRnk.size(); i < n; i++) { - const auto &cc = app->m_cc_db.idxRnk[i]; + for (size_t i = 0, n = app->m_cc_db.idxRank.size(); i < n; i++) { + const auto &cc = app->m_cc_db.idxRank[i]; wxString name(wxT("category")); - name.Append(cc.id.data, _countof(cc.id.data)); + name.Append(cc.cat.data, _countof(cc.cat.data)); SaveValue(name, wnd->m_categories->IsChecked(i)); } @@ -867,10 +867,10 @@ bool wxPersistentZRColaCharSelect::Restore() wnd->m_gridRecent->SetCharacters(val); } - for (size_t i = 0, n = app->m_cc_db.idxRnk.size(); i < n; i++) { - const auto &cc = app->m_cc_db.idxRnk[i]; + for (size_t i = 0, n = app->m_cc_db.idxRank.size(); i < n; i++) { + const auto &cc = app->m_cc_db.idxRank[i]; wxString name(wxT("category")); - name.Append(cc.id.data, _countof(cc.id.data)); + name.Append(cc.cat.data, _countof(cc.cat.data)); bool val; if (RestoreValue(name, &val)) wnd->m_categories->Check(i, val); diff --git a/ZRCola/zrcolasettings.cpp b/ZRCola/zrcolasettings.cpp index 5121a8a..0e86032 100644 --- a/ZRCola/zrcolasettings.cpp +++ b/ZRCola/zrcolasettings.cpp @@ -31,8 +31,8 @@ wxZRColaSettings::wxZRColaSettings(wxWindow* parent) : { auto app = dynamic_cast(wxTheApp); m_languages->Clear(); - for (size_t i = 0, n = app->m_lang_db.idxLng.size(); i < n; i++) { - const auto &lang = app->m_lang_db.idxLng[i]; + for (size_t i = 0, n = app->m_lang_db.idxLang.size(); i < n; i++) { + const auto &lang = app->m_lang_db.idxLang[i]; wxString label(lang.name(), lang.name_len()), label_tran(wxGetTranslation(label, wxT("ZRCola-zrcdb"))); @@ -59,9 +59,9 @@ void wxZRColaSettings::OnInitDialog(wxInitDialogEvent& event) auto app = dynamic_cast(wxTheApp); char l[sizeof(ZRCola::language_db::language)] = {}; - ((ZRCola::language_db::language*)l)->id = m_lang; + ((ZRCola::language_db::language*)l)->lang = m_lang; ZRCola::language_db::indexLang::size_type start; - m_languages->Select(app->m_lang_db.idxLng.find(*(ZRCola::language_db::language*)l, start) ? start : -1); + m_languages->Select(app->m_lang_db.idxLang.find(*(ZRCola::language_db::language*)l, start) ? start : -1); } @@ -122,10 +122,10 @@ void wxZRColaSettings::OnApplyButtonClick(wxCommandEvent& event) m_lang_auto = false; auto app = dynamic_cast(wxTheApp); - const auto &lang = app->m_lang_db.idxLng[m_languages->GetSelection()]; + const auto &lang = app->m_lang_db.idxLang[m_languages->GetSelection()]; - if (m_lang != lang.id) { - m_lang = lang.id; + if (m_lang != lang.lang) { + m_lang = lang.lang; // Notify destination text something changed and should re-inverse translate. wxCommandEvent event2(wxEVT_COMMAND_TEXT_UPDATED); @@ -179,9 +179,9 @@ bool wxPersistentZRColaSettings::Restore() } else if (RestoreValue(wxT("lang"), &lang) && lang.Length() == 3) { // The language was read from configuration. wnd->m_lang = lang.c_str(); - } else if (!app->m_lang_db.idxLng.empty()) { - const auto &lang = app->m_lang_db.idxLng[0]; - wnd->m_lang = lang.id; + } else if (!app->m_lang_db.idxLang.empty()) { + const auto &lang = app->m_lang_db.idxLang[0]; + wnd->m_lang = lang.lang; } else wnd->m_lang = ZRCola::langid_t::blank; diff --git a/ZRColaCompile/main.cpp b/ZRColaCompile/main.cpp index f65b1d4..3a90229 100644 --- a/ZRColaCompile/main.cpp +++ b/ZRColaCompile/main.cpp @@ -225,9 +225,9 @@ int _tmain(int argc, _TCHAR *argv[]) ZRCola::translation_db db; // Preallocate memory. - db.idxTrans .reserve(count); - db.idxTransInv.reserve(count); - db.data .reserve(count*5); + db.idxSrc.reserve(count); + db.idxDst.reserve(count); + db.data .reserve(count*5); // Parse translations and build index and data. for (auto t = db_temp2.cbegin(), t_end = db_temp2.cend(); t != t_end; ++t) { @@ -247,14 +247,14 @@ int _tmain(int argc, _TCHAR *argv[]) db.data.push_back((unsigned __int16)n); db.data.insert(db.data.end(), t->first .cbegin(), t->first .cend()); db.data.insert(db.data.end(), d->second.str.cbegin(), d->second.str.cend()); - db.idxTrans .push_back(idx); - db.idxTransInv.push_back(idx); + db.idxSrc.push_back(idx); + db.idxDst.push_back(idx); } } // Sort indices. - db.idxTrans .sort(); - db.idxTransInv.sort(); + db.idxSrc.sort(); + db.idxDst.sort(); // Write translations to file. dst << ZRCola::translation_rec(db); @@ -351,8 +351,8 @@ int _tmain(int argc, _TCHAR *argv[]) ZRCola::language_db db; // Preallocate memory. - db.idxLng.reserve(count); - db.data .reserve(count*4); + db.idxLang.reserve(count); + db.data .reserve(count*4); // Parse languages and build index and data. for (; !ZRCola::DBSource::IsEOF(rs); rs->MoveNext()) { @@ -368,13 +368,13 @@ int _tmain(int argc, _TCHAR *argv[]) wxASSERT_MSG(n <= 0xffff, wxT("language name overflow")); db.data.push_back((unsigned __int16)n); db.data.insert(db.data.end(), lang.name.cbegin(), lang.name.cend()); - db.idxLng.push_back(idx); + db.idxLang.push_back(idx); } else has_errors = true; } // Sort indices. - db.idxLng.sort(); + db.idxLang.sort(); // Write languages to file. dst << ZRCola::language_rec(db); @@ -398,11 +398,11 @@ int _tmain(int argc, _TCHAR *argv[]) ZRCola::langchar_db db; // Preallocate memory. - db.idxChr.reserve(count); + db.idxChr .reserve(count); #ifdef ZRCOLA_LANGCHAR_LANG_IDX - db.idxLng.reserve(count); + db.idxLang.reserve(count); #endif - db.data .reserve(count*4); + db.data .reserve(count*4); // Parse language characters and build index and data. for (; !ZRCola::DBSource::IsEOF(rs); rs->MoveNext()) { @@ -415,9 +415,9 @@ int _tmain(int argc, _TCHAR *argv[]) wxASSERT_MSG(n <= 0xffff, wxT("character overflow")); db.data.push_back((unsigned __int16)n); db.data.insert(db.data.end(), lc.chr.cbegin(), lc.chr.cend()); - db.idxChr.push_back(idx); + db.idxChr .push_back(idx); #ifdef ZRCOLA_LANGCHAR_LANG_IDX - db.idxLng.push_back(idx); + db.idxLang.push_back(idx); #endif } else has_errors = true; @@ -426,7 +426,7 @@ int _tmain(int argc, _TCHAR *argv[]) // Sort indices. db.idxChr .sort(); #ifdef ZRCOLA_LANGCHAR_LANG_IDX - db.idxLng.sort(); + db.idxLang.sort(); #endif // Write language characters to file. @@ -451,8 +451,8 @@ int _tmain(int argc, _TCHAR *argv[]) ZRCola::chrgrp_db db; // Preallocate memory. - db.idxRnk.reserve(count); - db.data .reserve(count*4); + db.idxRank.reserve(count); + db.data .reserve(count*4); // Parse character groups and build index and data. for (; !ZRCola::DBSource::IsEOF(rs); rs->MoveNext()) { @@ -481,13 +481,13 @@ int _tmain(int argc, _TCHAR *argv[]) db.data.insert(db.data.end(), cg.name .cbegin(), cg.name .cend()); db.data.insert(db.data.end(), cg.chars.cbegin(), cg.chars.cend()); db.data.insert(db.data.end(), cg.show .cbegin(), cg.show .cend()); - db.idxRnk.push_back(idx); + db.idxRank.push_back(idx); } else has_errors = true; } // Sort indices. - db.idxRnk.sort(); + db.idxRank.sort(); // Write character groups to file. dst << ZRCola::chrgrp_rec(db); @@ -589,7 +589,7 @@ int _tmain(int argc, _TCHAR *argv[]) // Preallocate memory. db.idxChrCat.reserve(count); - db.idxRnk .reserve(count); + db.idxRank .reserve(count); db.data .reserve(count*4); // Parse character categories and build index and data. @@ -614,14 +614,14 @@ int _tmain(int argc, _TCHAR *argv[]) db.data.push_back((unsigned __int16)n); db.data.insert(db.data.end(), cc.name.cbegin(), cc.name.cend()); db.idxChrCat.push_back(idx); - db.idxRnk .push_back(idx); + db.idxRank .push_back(idx); } else has_errors = true; } // Sort indices. db.idxChrCat.sort(); - db.idxRnk .sort(); + db.idxRank .sort(); // Write character categories to file. dst << ZRCola::chrcat_rec(db); @@ -668,7 +668,7 @@ int _tmain(int argc, _TCHAR *argv[]) } // Sort indices. - db.idxChr .sort(); + db.idxChr.sort(); db.idxTag.sort(); // Write characters tags to file. diff --git a/lib/libZRCola/include/zrcola/character.h b/lib/libZRCola/include/zrcola/character.h index 7ae3d0d..658607f 100644 --- a/lib/libZRCola/include/zrcola/character.h +++ b/lib/libZRCola/include/zrcola/character.h @@ -184,7 +184,7 @@ namespace ZRCola { /// struct character { public: - chrcatid_t cat; ///> Category ID + chrcatid_t cat; ///> Character category ID protected: unsigned __int16 chr_to; ///< Character end in \c data @@ -249,7 +249,7 @@ namespace ZRCola { /// /// Character index /// - class indexChar : public index + class indexChr : public index { public: /// @@ -257,7 +257,7 @@ namespace ZRCola { /// /// \param[in] h Reference to vector holding the data /// - indexChar(_In_ std::vector &h) : index(h) {} + indexChr(_In_ std::vector &h) : index(h) {} /// /// Compares two characters by ID (for searching) @@ -327,7 +327,7 @@ namespace ZRCola { assert(len <= 0xffff); std::unique_ptr c((character*)new char[sizeof(character) + sizeof(wchar_t)*len]); c->character::character(chr, len); - indexChar::size_type start; + indexChr::size_type start; return idxChr.find(*c, start) ? idxChr[start].cat : chrcatid_t::blank; } }; @@ -348,7 +348,7 @@ namespace ZRCola { /// struct chrcat { public: - chrcatid_t id; ///< Character category ID + chrcatid_t cat; ///< Character category ID unsigned __int16 rank; ///< Character category rank protected: @@ -363,18 +363,18 @@ namespace ZRCola { /// /// Constructs the character category /// - /// \param[in] id Character category ID + /// \param[in] cat Character category ID /// \param[in] rank Character category rank /// \param[in] name Character category name /// \param[in] name_len Number of UTF-16 characters in \p name /// inline chrcat( - _In_opt_ chrcatid_t id = chrcatid_t::blank, + _In_opt_ chrcatid_t cat = chrcatid_t::blank, _In_opt_ unsigned __int16 rank = 0, _In_opt_z_count_(name_len) const wchar_t *name = NULL, _In_opt_ size_t name_len = 0) { - this->id = id; + this->cat = cat; this->rank = rank; this->name_to = static_cast(name_len); if (name_len) memcpy(this->data, name, sizeof(wchar_t)*name_len); @@ -414,11 +414,12 @@ namespace ZRCola { /// virtual int compare(_In_ const chrcat &a, _In_ const chrcat &b) const { - if (a.id < b.id) return -1; - else if (a.id > b.id) return 1; - else return 0; + if (a.cat < b.cat) return -1; + else if (a.cat > b.cat) return 1; + + return 0; } - } idxChrCat; ///< Character category index + } idxChrCat; ///< Character category index /// /// Rank index @@ -478,7 +479,7 @@ namespace ZRCola { return 0; } - } idxRnk; ///< Rank index + } idxRank; ///< Rank index std::vector data; ///< Character category data @@ -486,7 +487,7 @@ namespace ZRCola { /// /// Constructs the database /// - inline chrcat_db() : idxChrCat(data), idxRnk(data) {} + inline chrcat_db() : idxChrCat(data), idxRank(data) {} /// /// Clears the database @@ -494,7 +495,7 @@ namespace ZRCola { inline void clear() { idxChrCat.clear(); - idxRnk .clear(); + idxRank .clear(); data .clear(); } }; @@ -605,7 +606,7 @@ inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::c // Write rank index. if (stream.fail()) return stream; - stream << db.idxRnk; + stream << db.idxRank; // Write data count. auto data_count = db.data.size(); @@ -643,7 +644,7 @@ inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::chrcat if (!stream.good()) return stream; // Read rank index. - stream >> db.idxRnk; + stream >> db.idxRank; if (!stream.good()) return stream; // Read data count. diff --git a/lib/libZRCola/include/zrcola/language.h b/lib/libZRCola/include/zrcola/language.h index af62044..0b6d9a8 100644 --- a/lib/libZRCola/include/zrcola/language.h +++ b/lib/libZRCola/include/zrcola/language.h @@ -85,7 +85,7 @@ namespace ZRCola { /// /// Character index /// - class indexChar : public index + class indexChr : public index { public: /// @@ -93,7 +93,7 @@ namespace ZRCola { /// /// \param[in] h Reference to vector holding the data /// - indexChar(_In_ std::vector &h) : index(h) {} + indexChr(_In_ std::vector &h) : index(h) {} /// /// Compares two characters by ID (for searching) @@ -121,9 +121,9 @@ namespace ZRCola { #ifdef ZRCOLA_LANGCHAR_LANG_IDX /// - /// Character Language Index + /// Language Index /// - class indexCharLang : public index + class indexLang : public index { public: /// @@ -131,7 +131,7 @@ namespace ZRCola { /// /// \param[in] h Reference to vector holding the data /// - indexCharLang(_In_ std::vector &h) : index(h) {} + indexLang(_In_ std::vector &h) : index(h) {} /// /// Compares two languages by ID (for searching) @@ -154,7 +154,7 @@ namespace ZRCola { return 0; } - } idxLng; ///< Character language index + } idxLang; ///< Language index #endif std::vector data; ///< Character data @@ -165,7 +165,7 @@ namespace ZRCola { /// inline langchar_db() : idxChr(data) #ifdef ZRCOLA_LANGCHAR_LANG_IDX - , idxLng(data) + , idxLang(data) #endif {} @@ -174,11 +174,11 @@ namespace ZRCola { /// inline void clear() { - idxChr.clear(); + idxChr .clear(); #ifdef ZRCOLA_LANGCHAR_LANG_IDX - idxLng.clear(); + idxLang.clear(); #endif - data .clear(); + data .clear(); } /// @@ -210,7 +210,7 @@ namespace ZRCola { /// struct language { public: - langid_t id; ///< Language ID + langid_t lang; ///< Language ID protected: unsigned __int16 name_to; ///< Language name end in \c data @@ -224,16 +224,16 @@ namespace ZRCola { /// /// Constructs the language /// - /// \param[in] id Language ID + /// \param[in] lang Language ID /// \param[in] name Language name /// \param[in] name_len Number of UTF-16 characters in \p name /// inline language( - _In_opt_ langid_t id = langid_t::blank, + _In_opt_ langid_t lang = langid_t::blank, _In_opt_z_count_(name_len) const wchar_t *name = NULL, _In_opt_ size_t name_len = 0) { - this->id = id; + this->lang = lang; this->name_to = static_cast(name_len); if (name_len) memcpy(this->data, name, sizeof(wchar_t)*name_len); } @@ -272,12 +272,12 @@ namespace ZRCola { /// virtual int compare(_In_ const language &a, _In_ const language &b) const { - if (a.id < b.id) return -1; - else if (a.id > b.id) return 1; + if (a.lang < b.lang) return -1; + else if (a.lang > b.lang) return 1; return 0; } - } idxLng; ///< Language index + } idxLang; ///< Language index std::vector data; ///< Language data @@ -285,15 +285,15 @@ namespace ZRCola { /// /// Constructs the database /// - inline language_db() : idxLng(data) {} + inline language_db() : idxLang(data) {} /// /// Clears the database /// inline void clear() { - idxLng.clear(); - data .clear(); + idxLang.clear(); + data .clear(); } }; @@ -323,7 +323,7 @@ inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::l #ifdef ZRCOLA_LANGCHAR_LANG_IDX // Write language index. if (stream.fail()) return stream; - stream << db.idxLng; + stream << db.idxLang; #endif // Write data count. @@ -363,7 +363,7 @@ inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::langch #ifdef ZRCOLA_LANGCHAR_LANG_IDX // Read language index. - stream >> db.idxLng; + stream >> db.idxLang; if (!stream.good()) return stream; #endif @@ -395,7 +395,7 @@ inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::l { // Write language index. if (stream.fail()) return stream; - stream << db.idxLng; + stream << db.idxLang; // Write data count. auto data_count = db.data.size(); @@ -429,7 +429,7 @@ inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::l inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::language_db &db) { // Read language index. - stream >> db.idxLng; + stream >> db.idxLang; if (!stream.good()) return stream; // Read data count. diff --git a/lib/libZRCola/include/zrcola/tag.h b/lib/libZRCola/include/zrcola/tag.h index c2c6ed1..0d25775 100644 --- a/lib/libZRCola/include/zrcola/tag.h +++ b/lib/libZRCola/include/zrcola/tag.h @@ -87,7 +87,7 @@ namespace ZRCola { /// /// Character Index /// - class indexChar : public index + class indexChr : public index { public: /// @@ -95,7 +95,7 @@ namespace ZRCola { /// /// \param[in] h Reference to vector holding the data /// - indexChar(_In_ std::vector &h) : index(h) {} + indexChr(_In_ std::vector &h) : index(h) {} /// /// Compares two character tags by character (for searching) diff --git a/lib/libZRCola/include/zrcola/translate.h b/lib/libZRCola/include/zrcola/translate.h index 0116c67..a4a4ae6 100644 --- a/lib/libZRCola/include/zrcola/translate.h +++ b/lib/libZRCola/include/zrcola/translate.h @@ -123,7 +123,7 @@ namespace ZRCola { /// /// Translation index /// - class indexTrans : public index + class indexSrc : public index { public: /// @@ -131,7 +131,7 @@ namespace ZRCola { /// /// \param[in] h Reference to vector holding the data /// - indexTrans(_In_ std::vector &h) : index(h) {} + indexSrc(_In_ std::vector &h) : index(h) {} /// /// Compares two transformations by string (for searching) @@ -182,13 +182,13 @@ namespace ZRCola { return 0; } - } idxTrans; ///< Translation index + } idxSrc; ///< Translation index /// /// Inverse translation index /// - class indexTransInv : public index + class indexDst : public index { public: /// @@ -196,7 +196,7 @@ namespace ZRCola { /// /// \param[in] h Reference to vector holding the data /// - indexTransInv(_In_ std::vector &h) : index(h) {} + indexDst(_In_ std::vector &h) : index(h) {} /// /// Compares two transformations by character (for searching) @@ -247,7 +247,7 @@ namespace ZRCola { return 0; } - } idxTransInv; ///< Inverse translation index + } idxDst; ///< Inverse translation index std::vector data; ///< Transformation data @@ -256,16 +256,16 @@ namespace ZRCola { /// /// Constructs the database /// - inline translation_db() : idxTrans(data), idxTransInv(data) {} + inline translation_db() : idxSrc(data), idxDst(data) {} /// /// Clears the database /// inline void clear() { - idxTrans .clear(); - idxTransInv.clear(); - data .clear(); + idxSrc.clear(); + idxDst.clear(); + data .clear(); } /// @@ -309,10 +309,114 @@ namespace ZRCola { typedef ZRCOLA_API stdex::idrec::record translation_rec; + + + /// + /// Translation set database + /// + class ZRCOLA_API transet_db { + public: +#pragma pack(push) +#pragma pack(2) + /// + /// Translation set data + /// + struct transet { + public: + transetid_t set; ///< Translation set ID + + protected: + unsigned __int16 name_to; ///< Translation set name end in \c data + wchar_t data[]; ///< Translation set name + + private: + inline transet(_In_ const transet &other); + inline transet& operator=(_In_ const transet &other); + + public: + /// + /// Constructs the translation set + /// + /// \param[in] set Translation set ID + /// \param[in] name Translation set name + /// \param[in] name_len Number of UTF-16 characters in \p name + /// + inline transet( + _In_opt_ transetid_t set = 0, + _In_opt_z_count_(name_len) const wchar_t *name = NULL, + _In_opt_ size_t name_len = 0) + { + this->set = set; + this->name_to = static_cast(name_len); + if (name_len) memcpy(this->data, name, sizeof(wchar_t)*name_len); + } + + inline const wchar_t* name () const { return data; }; + inline wchar_t* name () { return data; }; + inline const wchar_t* name_end() const { return data + name_to; }; + inline wchar_t* name_end() { return data + name_to; }; + inline unsigned __int16 name_len() const { return name_to; }; + }; +#pragma pack(pop) + + /// + /// Translation set index + /// + class indexTranSet : public index + { + public: + /// + /// Constructs the index + /// + /// \param[in] h Reference to vector holding the data + /// + indexTranSet(_In_ std::vector &h) : index(h) {} + + /// + /// Compares two translation sets by ID (for searching) + /// + /// \param[in] a Pointer to first element + /// \param[in] b Pointer to second element + /// + /// \returns + /// - <0 when a < b + /// - =0 when a == b + /// - >0 when a > b + /// + virtual int compare(_In_ const transet &a, _In_ const transet &b) const + { + if (a.set < b.set) return -1; + else if (a.set > b.set) return 1; + + return 0; + } + } idxTranSet; ///< Translation set index + + std::vector data; ///< Translation set data + + public: + /// + /// Constructs the database + /// + inline transet_db() : idxTranSet(data) {} + + /// + /// Clears the database + /// + inline void clear() + { + idxTranSet.clear(); + data .clear(); + } + }; + + + typedef ZRCOLA_API stdex::idrec::record transet_rec; }; const ZRCola::recordid_t ZRCola::translation_rec::id = *(ZRCola::recordid_t*)"TRN"; +const ZRCola::recordid_t ZRCola::transet_rec ::id = *(ZRCola::recordid_t*)"TSE"; /// @@ -327,11 +431,11 @@ inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::t { // Write translation index. if (stream.fail()) return stream; - stream << db.idxTrans; + stream << db.idxSrc; // Write inverse translation index. if (stream.fail()) return stream; - stream << db.idxTransInv; + stream << db.idxDst; // Write data count. auto data_count = db.data.size(); @@ -365,11 +469,76 @@ inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::t inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::translation_db &db) { // Read translation index. - stream >> db.idxTrans; + stream >> db.idxSrc; if (!stream.good()) return stream; // Read inverse translation index. - stream >> db.idxTransInv; + stream >> db.idxDst; + if (!stream.good()) return stream; + + // Read data count. + unsigned __int32 count; + stream.read((char*)&count, sizeof(count)); + if (!stream.good()) return stream; + + if (count) { + // Read data. + db.data.resize(count); + stream.read((char*)db.data.data(), sizeof(unsigned __int16)*count); + } else + db.data.clear(); + + return stream; +} + + +/// +/// Writes translation set database to a stream +/// +/// \param[in] stream Output stream +/// \param[in] db Translation set database +/// +/// \returns The stream \p stream +/// +inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::transet_db &db) +{ + // Write translation set index. + if (stream.fail()) return stream; + stream << db.idxTranSet; + + // Write data count. + auto data_count = db.data.size(); +#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__) + // 4G check + if (data_count > 0xffffffff) { + stream.setstate(std::ios_base::failbit); + return stream; + } +#endif + if (stream.fail()) return stream; + unsigned __int32 count = (unsigned __int32)data_count; + stream.write((const char*)&count, sizeof(count)); + + // Write data. + if (stream.fail()) return stream; + stream.write((const char*)db.data.data(), sizeof(unsigned __int16)*count); + + return stream; +} + + +/// +/// Reads translation set database from a stream +/// +/// \param[in ] stream Input stream +/// \param[out] db Translation set database +/// +/// \returns The stream \p stream +/// +inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::transet_db &db) +{ + // Read translation set index. + stream >> db.idxTranSet; if (!stream.good()) return stream; // Read data count. diff --git a/lib/libZRCola/src/language.cpp b/lib/libZRCola/src/language.cpp index 34249af..eca3414 100644 --- a/lib/libZRCola/src/language.cpp +++ b/lib/libZRCola/src/language.cpp @@ -77,6 +77,6 @@ bool ZRCola::langchar_db::IsLocalCharacter(_In_ const wchar_t *chr, _In_ const w assert(n <= 0xffff); std::unique_ptr lc((langchar*)new char[sizeof(langchar) + sizeof(wchar_t)*n]); lc->langchar::langchar(lang, chr, n); - indexChar::size_type start; + indexChr::size_type start; return idxChr.find(*lc, start); } diff --git a/lib/libZRCola/src/translate.cpp b/lib/libZRCola/src/translate.cpp index fce595d..94b0c8a 100644 --- a/lib/libZRCola/src/translate.cpp +++ b/lib/libZRCola/src/translate.cpp @@ -34,9 +34,9 @@ void ZRCola::translation_db::Translate(_In_ transetid_t set, _In_z_count_(inputM map->clear(); // Limit search to the given set first. - indexTrans::size_type l_set, r_set; - idxTrans.find(translation(set ), l_set); - idxTrans.find(translation(set + 1), r_set); + indexSrc::size_type l_set, r_set; + idxSrc.find(translation(set ), l_set); + idxSrc.find(translation(set + 1), r_set); for (size_t i = 0; i < inputMax;) { // Find the longest matching translation at i-th character. @@ -50,7 +50,7 @@ void ZRCola::translation_db::Translate(_In_ transetid_t set, _In_z_count_(inputM // Get the j-th character of the translation. // All translations that get short on characters are lexically ordered before. // Thus the j-th character is considered 0. - const translation &trans = idxTrans[m]; + const translation &trans = idxSrc[m]; wchar_t s = trans.src_at(j); // Do the bisection test. @@ -62,7 +62,7 @@ void ZRCola::translation_db::Translate(_In_ transetid_t set, _In_z_count_(inputM // Narrow the search area on the left to start at the first translation in the run. for (size_t rr = m; l < rr;) { size_t m = (l + rr) / 2; - const translation &trans = idxTrans[m]; + const translation &trans = idxSrc[m]; wchar_t s = trans.src_at(j); if (c <= s) rr = m; else l = m + 1; } @@ -70,12 +70,12 @@ void ZRCola::translation_db::Translate(_In_ transetid_t set, _In_z_count_(inputM // Narrow the search area on the right to end at the first translation not in the run. for (size_t ll = m + 1; ll < r;) { size_t m = (ll + r) / 2; - const translation &trans = idxTrans[m]; + const translation &trans = idxSrc[m]; wchar_t s = trans.src_at(j); if (s <= c) ll = m + 1; else r = m; } - const translation &trans = idxTrans[l]; + const translation &trans = idxSrc[l]; if (j + 1 == trans.src_len()) { // The first translation of the run was a match (thus far). Save it. l_match = l; @@ -88,7 +88,7 @@ void ZRCola::translation_db::Translate(_In_ transetid_t set, _In_z_count_(inputM if (l_match < r_set) { // The saved translation was an exact match. - const translation &trans = idxTrans[l_match]; + const translation &trans = idxSrc[l_match]; output.append(trans.dst(), trans.dst_end()); i += trans.src_len(); if (trans.src_len() != trans.dst_len() && map) { @@ -118,9 +118,9 @@ void ZRCola::translation_db::TranslateInv(_In_ transetid_t set, _In_z_count_(inp map->clear(); // Limit search to the given set first. - indexTransInv::size_type l_set, r_set; - idxTransInv.find(translation(set ), l_set); - idxTransInv.find(translation(set + 1), r_set); + indexDst::size_type l_set, r_set; + idxDst.find(translation(set ), l_set); + idxDst.find(translation(set + 1), r_set); for (size_t i = 0; i < inputMax;) { // Find the longest matching inverse translation at i-th character. @@ -134,7 +134,7 @@ void ZRCola::translation_db::TranslateInv(_In_ transetid_t set, _In_z_count_(inp // Get the j-th character of the inverse translation. // All inverse translations that get short on characters are lexically ordered before. // Thus the j-th character is considered 0. - const translation &trans = idxTransInv[m]; + const translation &trans = idxDst[m]; wchar_t s = trans.dst_at(j); // Do the bisection test. @@ -146,7 +146,7 @@ void ZRCola::translation_db::TranslateInv(_In_ transetid_t set, _In_z_count_(inp // Narrow the search area on the left to start at the first inverse translation in the run. for (size_t rr = m; l < rr;) { size_t m = (l + rr) / 2; - const translation &trans = idxTransInv[m]; + const translation &trans = idxDst[m]; wchar_t s = trans.dst_at(j); if (c <= s) rr = m; else l = m + 1; } @@ -154,12 +154,12 @@ void ZRCola::translation_db::TranslateInv(_In_ transetid_t set, _In_z_count_(inp // Narrow the search area on the right to end at the first inverse translation not in the run. for (size_t ll = m + 1; ll < r;) { size_t m = (ll + r) / 2; - const translation &trans = idxTransInv[m]; + const translation &trans = idxDst[m]; wchar_t s = trans.dst_at(j); if (s <= c) ll = m + 1; else r = m; } - const translation &trans = idxTransInv[l]; + const translation &trans = idxDst[l]; if (j + 1 == trans.dst_len()) { // The first inverse translation of the run was a match (thus far). Save it. l_match = l; @@ -172,7 +172,7 @@ void ZRCola::translation_db::TranslateInv(_In_ transetid_t set, _In_z_count_(inp if (l_match < r_set) { // The saved inverse translation was an exact match. - const translation &trans = idxTransInv[l_match]; + const translation &trans = idxDst[l_match]; if (trans.src_len() && trans.src()[0] != L'#' && (!lc_db || !lc_db->IsLocalCharacter(trans.dst(), trans.dst_end(), lang))) { // Append source sequence. output.append(trans.src(), trans.src_end()); diff --git a/lib/libZRColaUI/include/zrcolaui/chargroup.h b/lib/libZRColaUI/include/zrcolaui/chargroup.h index 5ae6f6f..a0276d8 100644 --- a/lib/libZRColaUI/include/zrcolaui/chargroup.h +++ b/lib/libZRColaUI/include/zrcolaui/chargroup.h @@ -35,6 +35,12 @@ namespace ZRCola { + /// + /// Character group ID + /// + typedef unsigned __int16 chrgrpid_t; + + /// /// Character group database /// @@ -47,7 +53,7 @@ namespace ZRCola { /// struct chrgrp { public: - unsigned __int16 id; ///< Character group ID + chrgrpid_t grp; ///< Character group ID unsigned __int16 rank; ///< Character group rank protected: @@ -59,7 +65,7 @@ namespace ZRCola { /// /// Constructs the character group /// - /// \param[in] id Character group ID + /// \param[in] grp Character group ID /// \param[in] rank Character group rank /// \param[in] name Character group name /// \param[in] name_len Number of UTF-16 characters in \p name @@ -68,7 +74,7 @@ namespace ZRCola { /// \param[in] chrshow Binary vector which particular character is displayed initially /// inline chrgrp( - _In_opt_ unsigned __int16 id = 0, + _In_opt_ chrgrpid_t grp = 0, _In_opt_ unsigned __int16 rank = 0, _In_opt_z_count_(name_len) const wchar_t *name = NULL, _In_opt_ size_t name_len = 0, @@ -76,7 +82,7 @@ namespace ZRCola { _In_opt_ size_t chrlst_len = 0, _In_opt_count_x_((chrlst_len + 15)/16) const unsigned __int16 *chrshow = NULL) { - this->id = id; + this->grp = grp; this->rank = rank; this->name_to = static_cast(name_len); if (name_len) memcpy(this->data, name, sizeof(wchar_t)*name_len); @@ -165,7 +171,7 @@ namespace ZRCola { return 0; } - } idxRnk; ///< Rank index + } idxRank; ///< Rank index std::vector data; ///< Character groups data @@ -173,7 +179,7 @@ namespace ZRCola { /// /// Constructs the database /// - inline chrgrp_db() : idxRnk(data) {} + inline chrgrp_db() : idxRank(data) {} }; @@ -196,7 +202,7 @@ inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::c { // Write rank index. if (stream.fail()) return stream; - stream << db.idxRnk; + stream << db.idxRank; // Write data count. auto data_count = db.data.size(); @@ -230,7 +236,7 @@ inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::c inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::chrgrp_db &db) { // Read rank index. - stream >> db.idxRnk; + stream >> db.idxRank; if (!stream.good()) return stream; // Read data count.