Compare commits

..

No commits in common. "master" and "2.6" have entirely different histories.
master ... 2.6

70 changed files with 1846 additions and 2849 deletions

18
.gitmodules vendored
View File

@ -1,24 +1,18 @@
[submodule "lib/stdex"] [submodule "lib/stdex"]
path = lib/stdex path = lib/stdex
url = https://git.amebis.si/Amebis/stdex.git url = https://github.com/Amebis/stdex.git
[submodule "lib/WinStd"] [submodule "lib/WinStd"]
path = lib/WinStd path = lib/WinStd
url = https://git.amebis.si/Amebis/WinStd.git url = https://github.com/Amebis/WinStd.git
[submodule "lib/wxExtend"] [submodule "lib/wxExtend"]
path = lib/wxExtend path = lib/wxExtend
url = https://git.amebis.si/Amebis/wxExtend.git url = https://github.com/Amebis/wxExtend.git
[submodule "MSI/MSIBuild"] [submodule "MSI/MSIBuild"]
path = MSI/MSIBuild path = MSI/MSIBuild
url = https://git.amebis.si/Amebis/MSIBuild.git url = https://github.com/Amebis/MSIBuild.git
[submodule "MSI/MSICA"] [submodule "MSI/MSICA"]
path = MSI/MSICA path = MSI/MSICA
url = https://git.amebis.si/Amebis/MSICA.git url = https://github.com/Amebis/MSICA.git
[submodule "Updater"] [submodule "Updater"]
path = Updater path = Updater
url = https://git.amebis.si/Amebis/Updater.git url = https://github.com/Amebis/Updater.git
[submodule "lib/oatpp"]
path = lib/oatpp
url = https://github.com/oatpp/oatpp.git
[submodule "lib/oatpp-swagger"]
path = lib/oatpp-swagger
url = https://github.com/oatpp/oatpp-swagger.git

@ -1 +1 @@
Subproject commit b8364dea81f39b321d726317a9dcbf6b13a455e0 Subproject commit 9eeff699e50ab9c837bb261ec979cb0a45eca813

@ -1 +1 @@
Subproject commit f989fdc827d8fa731977d1771097cf038a50e145 Subproject commit 51e1196c19cffdad0ae38ea902dd0bf66c21a2eb

Binary file not shown.

BIN
Makefile

Binary file not shown.

View File

@ -1,4 +1,4 @@
# ZRCola # ZRCola
A Microsoft Windows application for composing texts using a wide range of Slavic (and general) letters from or beyond Unicode. A Microsoft Windows application for composing texts using a wide range of Slavic (and general) letters from or beyond Unicode.
@ -56,10 +56,6 @@ Use Microsoft NMAKE to build the project. The resulting files can be found in ou
The `/ls` flag can be appended to the commands above to reduce NMAKE's verbosity. You can combine multiple targets (e.g. nmake Unregister Clean). Please, see NMAKE reference for further reading. The `/ls` flag can be appended to the commands above to reduce NMAKE's verbosity. You can combine multiple targets (e.g. nmake Unregister Clean). Please, see NMAKE reference for further reading.
## Building and installing ZRCola webservice
ZRCola is also available as a Linux web-service. See [ZRColaWS/README.md](ZRColaWS/README.md) for instructions.
## Translating ZRCola ## Translating ZRCola
Instructions how to translate ZRCola to your language can be found [here](LOCALIZATION.md). Instructions how to translate ZRCola to your language can be found [here](LOCALIZATION.md).

@ -1 +1 @@
Subproject commit a98ef46c5d38dec796dcb78f48240c17f743e3af Subproject commit 76124075fce359419f5e53bbb816c0c815cd8724

Binary file not shown.

View File

@ -46,7 +46,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <zrcola/idrec.h> #include <stdex/idrec>
#if defined(__WXMSW__) #if defined(__WXMSW__)
#include <Msi.h> #include <Msi.h>

View File

@ -82,7 +82,7 @@ bool ZRColaApp::OnInit()
ZRCola::recordid_t id; ZRCola::recordid_t id;
if (!stdex::idrec::read_id(dat, id, size)) break; if (!stdex::idrec::read_id(dat, id, size)) break;
if (id == ZRCola::translation_rec::id()) { if (id == ZRCola::translation_rec::id) {
dat >> ZRCola::translation_rec(m_t_db); dat >> ZRCola::translation_rec(m_t_db);
if (dat.good()) { if (dat.good()) {
has_translation_data = true; has_translation_data = true;
@ -90,61 +90,61 @@ bool ZRColaApp::OnInit()
wxFAIL_MSG(wxT("Error reading translation data from ZRCola.zrcdb.")); wxFAIL_MSG(wxT("Error reading translation data from ZRCola.zrcdb."));
m_t_db.clear(); m_t_db.clear();
} }
} else if (id == ZRCola::transet_rec::id()) { } else if (id == ZRCola::transet_rec::id) {
dat >> ZRCola::transet_rec(m_ts_db); dat >> ZRCola::transet_rec(m_ts_db);
if (!dat.good()) { if (!dat.good()) {
wxFAIL_MSG(wxT("Error reading translation set data from ZRCola.zrcdb.")); wxFAIL_MSG(wxT("Error reading translation set data from ZRCola.zrcdb."));
m_ts_db.clear(); m_ts_db.clear();
} }
} else if (id == ZRCola::transeq_rec::id()) { } else if (id == ZRCola::transeq_rec::id) {
dat >> ZRCola::transeq_rec(m_tsq_db); dat >> ZRCola::transeq_rec(m_tsq_db);
if (!dat.good()) { if (!dat.good()) {
wxFAIL_MSG(wxT("Error reading translation sequence data from ZRCola.zrcdb.")); wxFAIL_MSG(wxT("Error reading translation sequence data from ZRCola.zrcdb."));
m_tsq_db.clear(); m_tsq_db.clear();
} }
} else if (id == ZRCola::langchar_rec::id()) { } else if (id == ZRCola::langchar_rec::id) {
dat >> ZRCola::langchar_rec(m_lc_db); dat >> ZRCola::langchar_rec(m_lc_db);
if (!dat.good()) { if (!dat.good()) {
wxFAIL_MSG(wxT("Error reading language character data from ZRCola.zrcdb.")); wxFAIL_MSG(wxT("Error reading language character data from ZRCola.zrcdb."));
m_lc_db.clear(); m_lc_db.clear();
} }
} else if (id == ZRCola::language_rec::id()) { } else if (id == ZRCola::language_rec::id) {
dat >> ZRCola::language_rec(m_lang_db); dat >> ZRCola::language_rec(m_lang_db);
if (!dat.good()) { if (!dat.good()) {
wxFAIL_MSG(wxT("Error reading language character data from ZRCola.zrcdb.")); wxFAIL_MSG(wxT("Error reading language character data from ZRCola.zrcdb."));
m_lang_db.clear(); m_lang_db.clear();
} }
} else if (id == ZRCola::keyseq_rec::id()) { } else if (id == ZRCola::keyseq_rec::id) {
dat >> ZRCola::keyseq_rec(m_ks_db); dat >> ZRCola::keyseq_rec(m_ks_db);
if (!dat.good()) { if (!dat.good()) {
wxFAIL_MSG(wxT("Error reading key sequences data from ZRCola.zrcdb.")); wxFAIL_MSG(wxT("Error reading key sequences data from ZRCola.zrcdb."));
m_ks_db.clear(); m_ks_db.clear();
} }
} else if (id == ZRCola::character_rec::id()) { } else if (id == ZRCola::character_rec::id) {
dat >> ZRCola::character_rec(m_chr_db); dat >> ZRCola::character_rec(m_chr_db);
if (!dat.good()) { if (!dat.good()) {
wxFAIL_MSG(wxT("Error reading character data from ZRCola.zrcdb.")); wxFAIL_MSG(wxT("Error reading character data from ZRCola.zrcdb."));
m_chr_db.clear(); m_chr_db.clear();
} }
} else if (id == ZRCola::chrcat_rec::id()) { } else if (id == ZRCola::chrcat_rec::id) {
dat >> ZRCola::chrcat_rec(m_cc_db); dat >> ZRCola::chrcat_rec(m_cc_db);
if (!dat.good()) { if (!dat.good()) {
wxFAIL_MSG(wxT("Error reading character category data from ZRCola.zrcdb.")); wxFAIL_MSG(wxT("Error reading character category data from ZRCola.zrcdb."));
m_cc_db.clear(); m_cc_db.clear();
} }
} else if (id == ZRCola::chrtag_rec::id()) { } else if (id == ZRCola::chrtag_rec::id) {
dat >> ZRCola::chrtag_rec(m_ct_db); dat >> ZRCola::chrtag_rec(m_ct_db);
if (!dat.good()) { if (!dat.good()) {
wxFAIL_MSG(wxT("Error reading character tag data from ZRCola.zrcdb.")); wxFAIL_MSG(wxT("Error reading character tag data from ZRCola.zrcdb."));
m_ct_db.clear(); m_ct_db.clear();
} }
} else if (id == ZRCola::tagname_rec::id()) { } else if (id == ZRCola::tagname_rec::id) {
dat >> ZRCola::tagname_rec(m_tn_db); dat >> ZRCola::tagname_rec(m_tn_db);
if (!dat.good()) { if (!dat.good()) {
wxFAIL_MSG(wxT("Error reading tag name data from ZRCola.zrcdb.")); wxFAIL_MSG(wxT("Error reading tag name data from ZRCola.zrcdb."));
m_tn_db.clear(); m_tn_db.clear();
} }
} else if (id == ZRCola::highlight_rec::id()) { } else if (id == ZRCola::highlight_rec::id) {
dat >> ZRCola::highlight_rec(m_h_db); dat >> ZRCola::highlight_rec(m_h_db);
if (!dat.good()) { if (!dat.good()) {
wxFAIL_MSG(wxT("Error reading highlight data from ZRCola.zrcdb.")); wxFAIL_MSG(wxT("Error reading highlight data from ZRCola.zrcdb."));

View File

@ -82,7 +82,7 @@ public:
protected: protected:
#ifdef __WXMSW__ #ifdef __WXMSW__
winstd::event m_running; ///< Global Win32 event to determine if another instance of ZRCola is already running winstd::win_handle<NULL> m_running; ///< Global Win32 event to determine if another instance of ZRCola is already running
#endif #endif
}; };

View File

@ -148,11 +148,11 @@ void wxZRColaCharacterCatalogPanel::Update()
wxArrayShort(reinterpret_cast<const short*>(cg.chrshow()), reinterpret_cast<const short*>(cg.chrshow_end()))); wxArrayShort(reinterpret_cast<const short*>(cg.chrshow()), reinterpret_cast<const short*>(cg.chrshow_end())));
} else { } else {
// Select frequently used characters only. // Select frequently used characters only.
const auto *src = cg.chrlst(); const wchar_t *src = cg.chrlst();
const uint16_t *shown = cg.chrshow(); const unsigned __int16 *shown = cg.chrshow();
wxArrayString chars; wxArrayString chars;
for (size_t i = 0, i_end = cg.chrlst_len(), j = 0; i < i_end; j++) { for (size_t i = 0, i_end = cg.chrlst_len(), j = 0; i < i_end; j++) {
for (uint16_t k = 0, mask = shown[j]; k < 16 && i < i_end; k++, mask >>= 1) { for (unsigned __int16 k = 0, mask = shown[j]; k < 16 && i < i_end; k++, mask >>= 1) {
size_t len = wcsnlen(src + i, i_end - i); size_t len = wcsnlen(src + i, i_end - i);
if (mask & 1) if (mask & 1)
chars.Add(wxString(src + i, len)); chars.Add(wxString(src + i, len));

View File

@ -110,7 +110,7 @@ wxString wxZRColaCharGrid::GetToolTipText(int idx)
const auto &chr = m_chars[idx]; const auto &chr = m_chars[idx];
// See if this character has a key sequence registered. // See if this character has a key sequence registered.
std::unique_ptr<ZRCola::keyseq_db::keyseq> ks((ZRCola::keyseq_db::keyseq*)new char[sizeof(ZRCola::keyseq_db::keyseq) + sizeof(ZRCola::char_t)*chr.length()]); std::unique_ptr<ZRCola::keyseq_db::keyseq> ks((ZRCola::keyseq_db::keyseq*)new char[sizeof(ZRCola::keyseq_db::keyseq) + sizeof(wchar_t)*chr.length()]);
ks->ZRCola::keyseq_db::keyseq::keyseq(NULL, 0, chr.data(), chr.length()); ks->ZRCola::keyseq_db::keyseq::keyseq(NULL, 0, chr.data(), chr.length());
ZRCola::keyseq_db::indexKey::size_type start; ZRCola::keyseq_db::indexKey::size_type start;
if (app->m_ks_db.idxChr.find(*ks, start)) { if (app->m_ks_db.idxChr.find(*ks, start)) {

View File

@ -13,7 +13,7 @@
wxIMPLEMENT_DYNAMIC_CLASS(wxZRColaUTF16CharValidator, wxValidator); wxIMPLEMENT_DYNAMIC_CLASS(wxZRColaUTF16CharValidator, wxValidator);
wxZRColaUTF16CharValidator::wxZRColaUTF16CharValidator(ZRCola::char_t *val) : wxZRColaUTF16CharValidator::wxZRColaUTF16CharValidator(wchar_t *val) :
m_val(val), m_val(val),
wxValidator() wxValidator()
{ {
@ -58,11 +58,11 @@ bool wxZRColaUTF16CharValidator::TransferFromWindow()
} }
bool wxZRColaUTF16CharValidator::Parse(const wxString &val_in, size_t i_start, size_t i_end, wxTextCtrl *ctrl, wxWindow *parent, ZRCola::char_t *val_out) bool wxZRColaUTF16CharValidator::Parse(const wxString &val_in, size_t i_start, size_t i_end, wxTextCtrl *ctrl, wxWindow *parent, wchar_t *val_out)
{ {
const wxStringCharType *buf = val_in; const wxStringCharType *buf = val_in;
ZRCola::char_t chr = 0; wchar_t chr = 0;
for (size_t i = i_start;;) { for (size_t i = i_start;;) {
if (i >= i_end) { if (i >= i_end) {
// End of Unicode found. // End of Unicode found.
@ -156,7 +156,7 @@ bool wxZRColaUnicodeDumpValidator::Parse(const wxString &val_in, size_t i_start,
wxString str; wxString str;
for (size_t i = i_start;;) { for (size_t i = i_start;;) {
const wxStringCharType *buf_next; const wxStringCharType *buf_next;
ZRCola::char_t chr; wchar_t chr;
if ((buf_next = wmemchr(buf + i, L'+', i_end - i)) != NULL) { if ((buf_next = wmemchr(buf + i, L'+', i_end - i)) != NULL) {
// Unicode dump separator found. // Unicode dump separator found.
if (!wxZRColaUTF16CharValidator::Parse(val_in, i, buf_next - buf, ctrl, parent, &chr)) if (!wxZRColaUTF16CharValidator::Parse(val_in, i, buf_next - buf, ctrl, parent, &chr))
@ -242,7 +242,7 @@ void wxZRColaCharSelect::OnIdle(wxIdleEvent& event)
m_gridPreview->SetCellValue(0, 0, m_char); m_gridPreview->SetCellValue(0, 0, m_char);
std::unique_ptr<ZRCola::character_db::character> ch((ZRCola::character_db::character*)new char[sizeof(ZRCola::character_db::character) + sizeof(ZRCola::char_t)*m_char.length()]); std::unique_ptr<ZRCola::character_db::character> ch((ZRCola::character_db::character*)new char[sizeof(ZRCola::character_db::character) + sizeof(wchar_t)*m_char.length()]);
ch->ZRCola::character_db::character::character(m_char.data(), m_char.length()); ch->ZRCola::character_db::character::character(m_char.data(), m_char.length());
ZRCola::character_db::indexChr::size_type ch_start; ZRCola::character_db::indexChr::size_type ch_start;
if (app->m_chr_db.idxChr.find(*ch, ch_start)) { if (app->m_chr_db.idxChr.find(*ch, ch_start)) {
@ -251,7 +251,7 @@ void wxZRColaCharSelect::OnIdle(wxIdleEvent& event)
m_description->SetValue(wxString(chr.desc(), chr.desc_len())); m_description->SetValue(wxString(chr.desc(), chr.desc_len()));
{ {
// See if this character has a key sequence registered. // See if this character has a key sequence registered.
std::unique_ptr<ZRCola::keyseq_db::keyseq> ks((ZRCola::keyseq_db::keyseq*)new char[sizeof(ZRCola::keyseq_db::keyseq) + sizeof(ZRCola::char_t)*m_char.length()]); std::unique_ptr<ZRCola::keyseq_db::keyseq> ks((ZRCola::keyseq_db::keyseq*)new char[sizeof(ZRCola::keyseq_db::keyseq) + sizeof(wchar_t)*m_char.length()]);
ks->ZRCola::keyseq_db::keyseq::keyseq(NULL, 0, m_char.data(), m_char.length()); ks->ZRCola::keyseq_db::keyseq::keyseq(NULL, 0, m_char.data(), m_char.length());
ZRCola::keyseq_db::indexKey::size_type ks_start; ZRCola::keyseq_db::indexKey::size_type ks_start;
if (app->m_ks_db.idxChr.find(*ks, ks_start)) { if (app->m_ks_db.idxChr.find(*ks, ks_start)) {
@ -284,7 +284,7 @@ void wxZRColaCharSelect::OnIdle(wxIdleEvent& event)
// Find character tags. // Find character tags.
std::list<std::wstring> tag_names; std::list<std::wstring> tag_names;
std::unique_ptr<ZRCola::chrtag_db::chrtag> ct((ZRCola::chrtag_db::chrtag*)new char[sizeof(ZRCola::chrtag_db::chrtag) + sizeof(ZRCola::char_t)*m_char.length()]); std::unique_ptr<ZRCola::chrtag_db::chrtag> ct((ZRCola::chrtag_db::chrtag*)new char[sizeof(ZRCola::chrtag_db::chrtag) + sizeof(wchar_t)*m_char.length()]);
ct->ZRCola::chrtag_db::chrtag::chrtag(m_char.data(), m_char.length()); ct->ZRCola::chrtag_db::chrtag::chrtag(m_char.data(), m_char.length());
ZRCola::chrtag_db::indexChr::size_type ct_start, ct_end; ZRCola::chrtag_db::indexChr::size_type ct_start, ct_end;
if (app->m_ct_db.idxChr.find(*ct, ct_start, ct_end)) { if (app->m_ct_db.idxChr.find(*ct, ct_start, ct_end)) {
@ -301,7 +301,7 @@ void wxZRColaCharSelect::OnIdle(wxIdleEvent& event)
// Add name to the list. // Add name to the list.
tag_names.push_back(std::wstring(tn.name(), tn.name_end())); tag_names.push_back(std::wstring(tn.name(), tn.name_end()));
break; break;
} else if (ZRCola::tagname_db::tagname::CompareName(m_locale, name->data(), (uint16_t)name->length(), tn.name(), tn.name_len()) == 0) } else if (ZRCola::tagname_db::tagname::CompareName(m_locale, name->data(), (unsigned __int16)name->length(), tn.name(), tn.name_len()) == 0)
// Name is already on the list. // Name is already on the list.
break; break;
} }
@ -718,7 +718,7 @@ wxThread::ExitCode wxZRColaCharSelect::SearchThread::Entry()
{ {
// Search by tags: Get tags with given names. Then, get characters of found tags. // Search by tags: Get tags with given names. Then, get characters of found tags.
std::map<ZRCola::tagid_t, uint16_t> hits_tag; std::map<ZRCola::tagid_t, unsigned __int16> hits_tag;
if (!app->m_tn_db.Search(m_search.c_str(), m_parent->m_locale, hits_tag, TestDestroyS, this)) return (wxThread::ExitCode)1; if (!app->m_tn_db.Search(m_search.c_str(), m_parent->m_locale, hits_tag, TestDestroyS, this)) return (wxThread::ExitCode)1;
if (!app->m_ct_db.Search(hits_tag, app->m_chr_db, m_cats, hits, TestDestroyS, this)) return (wxThread::ExitCode)1; if (!app->m_ct_db.Search(hits_tag, app->m_chr_db, m_cats, hits, TestDestroyS, this)) return (wxThread::ExitCode)1;
} }
@ -766,14 +766,14 @@ wxThread::ExitCode wxZRColaCharSelect::SearchThread::Entry()
int __cdecl wxZRColaCharSelect::SearchThread::CompareHits(const void *a, const void *b) int __cdecl wxZRColaCharSelect::SearchThread::CompareHits(const void *a, const void *b)
{ {
const std::pair<ZRCola::charrank_t, ZRCola::char_t> *_a = (const std::pair<ZRCola::charrank_t, ZRCola::char_t>*)a; const std::pair<ZRCola::charrank_t, wchar_t> *_a = (const std::pair<ZRCola::charrank_t, wchar_t>*)a;
const std::pair<ZRCola::charrank_t, ZRCola::char_t> *_b = (const std::pair<ZRCola::charrank_t, ZRCola::char_t>*)b; const std::pair<ZRCola::charrank_t, wchar_t> *_b = (const std::pair<ZRCola::charrank_t, wchar_t>*)b;
if (_a->first > _b->first) return -1; if (_a->first > _b->first) return -1;
if (_a->first < _b->first) return 1; else if (_a->first < _b->first) return 1;
if (_a->second < _b->second) return -1; if (_a->second < _b->second) return -1;
if (_a->second > _b->second) return 1; else if (_a->second > _b->second) return 1;
return 0; return 0;
} }
@ -843,7 +843,7 @@ bool wxPersistentZRColaCharSelect::Restore()
for (wxStringTokenizer tok(str, wxT("|")); tok.HasMoreTokens(); ) { for (wxStringTokenizer tok(str, wxT("|")); tok.HasMoreTokens(); ) {
wxString chr; wxString chr;
for (wxStringTokenizer tok_chr(tok.GetNextToken(), wxT("+")); tok_chr.HasMoreTokens(); ) for (wxStringTokenizer tok_chr(tok.GetNextToken(), wxT("+")); tok_chr.HasMoreTokens(); )
chr += (ZRCola::char_t)_tcstoul(tok_chr.GetNextToken().c_str(), NULL, 16); chr += (wchar_t)_tcstoul(tok_chr.GetNextToken().c_str(), NULL, 16);
val.Add(chr); val.Add(chr);
} }
wnd->m_gridRecent->SetCharacters(val); wnd->m_gridRecent->SetCharacters(val);

View File

@ -40,7 +40,7 @@ public:
/// ///
/// Construct the validator with a value to store data /// Construct the validator with a value to store data
/// ///
wxZRColaUTF16CharValidator(ZRCola::char_t *val = NULL); wxZRColaUTF16CharValidator(wchar_t *val = NULL);
/// ///
/// Copies this validator /// Copies this validator

View File

@ -49,7 +49,7 @@ void wxZRColaComposerPanel::RestoreFromStateFile()
wxFFile file(fileName, wxT("rb")); wxFFile file(fileName, wxT("rb"));
if (file.IsOpened()) { if (file.IsOpened()) {
// Load source text. // Load source text.
uint64_t n; unsigned __int64 n;
file.Read(&n, sizeof(n)); file.Read(&n, sizeof(n));
if (!file.Error()) { if (!file.Error()) {
wxString source; wxString source;
@ -98,11 +98,9 @@ void wxZRColaComposerPanel::SynchronizePanels()
// ZRCola decompose first, then re-compose. // ZRCola decompose first, then re-compose.
app->m_t_db.TranslateInv(app->m_mainWnd->m_composition_id, dst.data(), dst.size(), dst2, &map); app->m_t_db.TranslateInv(app->m_mainWnd->m_composition_id, dst.data(), dst.size(), dst2, &map);
m_mapping.push_back(std::move(map)); m_mapping.push_back(std::move(map));
map.clear();
app->m_t_db.Translate(app->m_mainWnd->m_composition_id, dst2.data(), dst2.size(), dst, &map); app->m_t_db.Translate(app->m_mainWnd->m_composition_id, dst2.data(), dst2.size(), dst, &map);
m_mapping.push_back(std::move(map)); m_mapping.push_back(std::move(map));
map.clear();
} }
// Other translations // Other translations
@ -111,9 +109,7 @@ void wxZRColaComposerPanel::SynchronizePanels()
for (auto s = sets_begin; s != sets_end; ++s) { for (auto s = sets_begin; s != sets_end; ++s) {
app->m_t_db.Translate(*s, dst.data(), dst.size(), dst2, &map); app->m_t_db.Translate(*s, dst.data(), dst.size(), dst2, &map);
m_mapping.push_back(std::move(map)); m_mapping.push_back(std::move(map));
map.clear();
dst = std::move(dst2); dst = std::move(dst2);
dst2.clear();
} }
m_source->GetSelection(&m_selSource.first, &m_selSource.second); m_source->GetSelection(&m_selSource.first, &m_selSource.second);
@ -148,24 +144,18 @@ void wxZRColaComposerPanel::SynchronizePanels()
for (auto s = sets_end; (s--) != sets_begin;) { for (auto s = sets_end; (s--) != sets_begin;) {
app->m_t_db.TranslateInv(*s, dst.data(), dst.size(), dst2, &map); app->m_t_db.TranslateInv(*s, dst.data(), dst.size(), dst2, &map);
dst = std::move(dst2); dst = std::move(dst2);
dst2.clear();
for (auto& m : map) map.invert();
m.invert();
m_mapping.push_back(std::move(map)); m_mapping.push_back(std::move(map));
map.clear();
} }
if (app->m_mainWnd->m_composition) { if (app->m_mainWnd->m_composition) {
// ZRCola decompose. // ZRCola decompose.
app->m_t_db.TranslateInv(app->m_mainWnd->m_composition_id, dst.data(), dst.size(), &app->m_lc_db, app->m_mainWnd->m_settings->m_lang, dst2, &map); app->m_t_db.TranslateInv(app->m_mainWnd->m_composition_id, dst.data(), dst.size(), &app->m_lc_db, app->m_mainWnd->m_settings->m_lang, dst2, &map);
dst = std::move(dst2); dst = std::move(dst2);
dst2.clear();
for (auto& m : map) map.invert();
m.invert();
m_mapping.push_back(std::move(map)); m_mapping.push_back(std::move(map));
map.clear();
} }
m_destination->GetSelection(&m_selDestination.first, &m_selDestination.second); m_destination->GetSelection(&m_selDestination.first, &m_selDestination.second);
@ -205,16 +195,16 @@ void wxZRColaComposerPanel::OnSourcePaint(wxPaintEvent& event)
m_selSource.second = to; m_selSource.second = to;
m_sourceHex->SetSelection( m_sourceHex->SetSelection(
m_selSourceHex.first = (long)stdex::src2dst<size_t>(m_mappingSourceHex, from), m_selSourceHex.first = (long)m_mappingSourceHex.to_dst(from),
m_selSourceHex.second = (long)stdex::src2dst<size_t>(m_mappingSourceHex, to )); m_selSourceHex.second = (long)m_mappingSourceHex.to_dst(to ));
m_destination->SetSelection( m_destination->SetSelection(
m_selDestination.first = (long)MapToDestination(from), m_selDestination.first = (long)MapToDestination(from),
m_selDestination.second = (long)MapToDestination(to )); m_selDestination.second = (long)MapToDestination(to ));
m_destinationHex->SetSelection( m_destinationHex->SetSelection(
m_selDestinationHex.first = (long)stdex::src2dst<size_t>(m_mappingDestinationHex, m_selDestination.first ), m_selDestinationHex.first = (long)m_mappingDestinationHex.to_dst(m_selDestination.first ),
m_selDestinationHex.second = (long)stdex::src2dst<size_t>(m_mappingDestinationHex, m_selDestination.second)); m_selDestinationHex.second = (long)m_mappingDestinationHex.to_dst(m_selDestination.second));
} }
} }
@ -232,16 +222,16 @@ void wxZRColaComposerPanel::OnSourceHexPaint(wxPaintEvent& event)
m_selSourceHex.second = to; m_selSourceHex.second = to;
m_source->SetSelection( m_source->SetSelection(
m_selSource.first = (long)stdex::dst2src<size_t>(m_mappingSourceHex, from), m_selSource.first = (long)m_mappingSourceHex.to_src(from),
m_selSource.second = (long)stdex::dst2src<size_t>(m_mappingSourceHex, to )); m_selSource.second = (long)m_mappingSourceHex.to_src(to ));
m_destination->SetSelection( m_destination->SetSelection(
m_selDestination.first = (long)MapToDestination(m_selSource.first ), m_selDestination.first = (long)MapToDestination(m_selSource.first ),
m_selDestination.second = (long)MapToDestination(m_selSource.second)); m_selDestination.second = (long)MapToDestination(m_selSource.second));
m_destinationHex->SetSelection( m_destinationHex->SetSelection(
m_selDestinationHex.first = (long)stdex::src2dst<size_t>(m_mappingDestinationHex, m_selDestination.first ), m_selDestinationHex.first = (long)m_mappingDestinationHex.to_dst(m_selDestination.first ),
m_selDestinationHex.second = (long)stdex::src2dst<size_t>(m_mappingDestinationHex, m_selDestination.second)); m_selDestinationHex.second = (long)m_mappingDestinationHex.to_dst(m_selDestination.second));
} }
} }
@ -278,16 +268,16 @@ void wxZRColaComposerPanel::OnDestinationPaint(wxPaintEvent& event)
m_selDestination.second = to; m_selDestination.second = to;
m_destinationHex->SetSelection( m_destinationHex->SetSelection(
m_selDestinationHex.first = (long)stdex::src2dst<size_t>(m_mappingDestinationHex, from), m_selDestinationHex.first = (long)m_mappingDestinationHex.to_dst(from),
m_selDestinationHex.second = (long)stdex::src2dst<size_t>(m_mappingDestinationHex, to )); m_selDestinationHex.second = (long)m_mappingDestinationHex.to_dst(to ));
m_source->SetSelection( m_source->SetSelection(
m_selSource.first = (long)MapToSource(from), m_selSource.first = (long)MapToSource(from),
m_selSource.second = (long)MapToSource(to )); m_selSource.second = (long)MapToSource(to ));
m_sourceHex->SetSelection( m_sourceHex->SetSelection(
m_selSourceHex.first = (long)stdex::src2dst<size_t>(m_mappingSourceHex, m_selSource.first ), m_selSourceHex.first = (long)m_mappingSourceHex.to_dst(m_selSource.first ),
m_selSourceHex.second = (long)stdex::src2dst<size_t>(m_mappingSourceHex, m_selSource.second)); m_selSourceHex.second = (long)m_mappingSourceHex.to_dst(m_selSource.second));
} }
} }
@ -305,16 +295,16 @@ void wxZRColaComposerPanel::OnDestinationHexPaint(wxPaintEvent& event)
m_selDestinationHex.second = to; m_selDestinationHex.second = to;
m_destination->SetSelection( m_destination->SetSelection(
m_selDestination.first = (long)stdex::dst2src<size_t>(m_mappingDestinationHex, from), m_selDestination.first = (long)m_mappingDestinationHex.to_src(from),
m_selDestination.second = (long)stdex::dst2src<size_t>(m_mappingDestinationHex, to )); m_selDestination.second = (long)m_mappingDestinationHex.to_src(to ));
m_source->SetSelection( m_source->SetSelection(
m_selSource.first = (long)MapToSource(m_selDestination.first ), m_selSource.first = (long)MapToSource(m_selDestination.first ),
m_selSource.second = (long)MapToSource(m_selDestination.second)); m_selSource.second = (long)MapToSource(m_selDestination.second));
m_sourceHex->SetSelection( m_sourceHex->SetSelection(
m_selSourceHex.first = (long)stdex::src2dst<size_t>(m_mappingSourceHex, m_selSource.first ), m_selSourceHex.first = (long)m_mappingSourceHex.to_dst(m_selSource.first ),
m_selSourceHex.second = (long)stdex::src2dst<size_t>(m_mappingSourceHex, m_selSource.second)); m_selSourceHex.second = (long)m_mappingSourceHex.to_dst(m_selSource.second));
} }
} }
@ -418,8 +408,8 @@ void wxZRColaComposerPanel::SetHexValue(wxTextCtrl *wnd, std::pair<long, long> &
wnd->SetValue(hex); wnd->SetValue(hex);
wnd->SetSelection( wnd->SetSelection(
range.first = (long)stdex::src2dst<size_t>(mapping, from), range.first = (long)mapping.to_dst(from),
range.second = (long)stdex::src2dst<size_t>(mapping, to )); range.second = (long)mapping.to_dst(to ));
} }

View File

@ -97,7 +97,7 @@ protected:
inline size_t wxZRColaComposerPanel::MapToDestination(_In_ size_t src) const inline size_t wxZRColaComposerPanel::MapToDestination(_In_ size_t src) const
{ {
for (auto m = m_mapping.cbegin(), m_end = m_mapping.cend(); m != m_end; ++m) for (auto m = m_mapping.cbegin(), m_end = m_mapping.cend(); m != m_end; ++m)
src = stdex::src2dst(*m, src); src = m->to_dst(src);
return src; return src;
} }
@ -106,7 +106,7 @@ inline size_t wxZRColaComposerPanel::MapToDestination(_In_ size_t src) const
inline size_t wxZRColaComposerPanel::MapToSource(_In_ size_t dst) const inline size_t wxZRColaComposerPanel::MapToSource(_In_ size_t dst) const
{ {
for (auto m = m_mapping.crbegin(), m_end = m_mapping.crend(); m != m_end; ++m) for (auto m = m_mapping.crbegin(), m_end = m_mapping.crend(); m != m_end; ++m)
dst = stdex::dst2src(*m, dst); dst = m->to_src(dst);
return dst; return dst;
} }

View File

@ -42,7 +42,7 @@ void ZRCola::DBSource::character_bank::build_related()
ZRCola::DBSource::character_bank::build_related_worker::build_related_worker(_In_ const character_bank *cb, _In_ iterator from, _In_ iterator to) : ZRCola::DBSource::character_bank::build_related_worker::build_related_worker(_In_ const character_bank *cb, _In_ iterator from, _In_ iterator to) :
winstd::thread((HANDLE)_beginthreadex(NULL, 0, process, this, CREATE_SUSPENDED, NULL)), win_handle<INVALID_HANDLE_VALUE>((HANDLE)_beginthreadex(NULL, 0, process, this, CREATE_SUSPENDED, NULL)),
m_heap(HeapCreate(0, 0, 0)), m_heap(HeapCreate(0, 0, 0)),
m_cb(cb), m_cb(cb),
m_from(from), m_from(from),
@ -186,7 +186,7 @@ void ZRCola::DBSource::character_desc_idx::add_keywords(const set<wstring> &term
} }
void ZRCola::DBSource::character_desc_idx::save(ZRCola::textindex<wchar_t, wchar_t, uint32_t> &idx) const void ZRCola::DBSource::character_desc_idx::save(ZRCola::textindex<wchar_t, wchar_t, unsigned __int32> &idx) const
{ {
idx .clear(); idx .clear();
idx.keys .clear(); idx.keys .clear();
@ -205,7 +205,7 @@ void ZRCola::DBSource::character_desc_idx::save(ZRCola::textindex<wchar_t, wchar
// Convert the index. // Convert the index.
for (const_iterator i = cbegin(), i_end = cend(); i != i_end; ++i) { for (const_iterator i = cbegin(), i_end = cend(); i != i_end; ++i) {
ZRCola::mappair_t<uint32_t> p = { idx.keys.size(), idx.values.size() }; ZRCola::mappair_t<unsigned __int32> p = { idx.keys.size(), idx.values.size() };
idx.push_back(p); idx.push_back(p);
idx.keys.insert(idx.keys.end(), i->first.cbegin(), i->first.cend()); idx.keys.insert(idx.keys.end(), i->first.cbegin(), i->first.cend());
idx.values.insert(idx.values.end(), i->second.cbegin(), i->second.cend()); idx.values.insert(idx.values.end(), i->second.cbegin(), i->second.cend());
@ -670,7 +670,7 @@ bool ZRCola::DBSource::GetTagNames(const winstd::com_obj<ADOField>& f, LCID lcid
// Add name to the list. // Add name to the list.
names.push_back(std::move(name)); names.push_back(std::move(name));
break; break;
} else if (ZRCola::tagname_db::tagname::CompareName(lcid, n->data(), (uint16_t)n->length(), name.data(), (uint16_t)name.length()) == CSTR_EQUAL) { } else if (ZRCola::tagname_db::tagname::CompareName(lcid, n->data(), (unsigned __int16)n->length(), name.data(), (unsigned __int16)name.length()) == CSTR_EQUAL) {
// Name is already on the list. // Name is already on the list.
break; break;
} }

View File

@ -222,11 +222,11 @@ namespace ZRCola {
/// ///
class chrgrp { class chrgrp {
public: public:
short grp; ///< Character group ID short grp; ///< Character group ID
short rank; ///< Rank short rank; ///< Rank
std::wstring name; ///< Name std::wstring name; ///< Name
std::vector<wchar_t> chars; ///< Characters (zero-delimited) std::vector<wchar_t> chars; ///< Characters (zero-delimited)
std::vector<uint16_t> show; ///< Bit vector if particular character from \c chars is displayed initially std::vector<unsigned __int16> show; ///< Bit vector if particular character from \c chars is displayed initially
inline chrgrp() : grp(0), rank(0) {} inline chrgrp() : grp(0), rank(0) {}
}; };
@ -275,7 +275,7 @@ namespace ZRCola {
void build_related(); void build_related();
protected: protected:
class build_related_worker : public winstd::thread class build_related_worker : public winstd::win_handle<INVALID_HANDLE_VALUE>
{ {
public: public:
build_related_worker(_In_ const character_bank *cb, _In_ iterator from, _In_ iterator to); build_related_worker(_In_ const character_bank *cb, _In_ iterator from, _In_ iterator to);
@ -308,12 +308,19 @@ namespace ZRCola {
/// ///
/// Character description index key comparator /// Character description index key comparator
/// ///
struct character_desc_idx_less struct character_desc_idx_less : public std::binary_function<std::wstring, std::wstring, bool>
{ {
inline bool operator()(const std::wstring& _Left, const std::wstring& _Right) const inline bool operator()(const std::wstring& _Left, const std::wstring& _Right) const
{ {
auto &coll = std::use_facet<std::collate<wchar_t>>(std::locale()); size_t
return coll.compare(&*_Left.cbegin(), &*_Left.cend(), &*_Right.cbegin(), &*_Right.cend()) < 0; _Left_len = _Left .size(),
_Right_len = _Right.size();
int r = _wcsncoll(_Left.c_str(), _Right.c_str(), std::min<size_t>(_Left_len, _Right_len));
if (r != 0 ) return r < 0;
else if (_Left_len < _Right_len) return true;
return false;
} }
}; };
@ -333,7 +340,7 @@ namespace ZRCola {
add_keywords(terms, chr, sub); add_keywords(terms, chr, sub);
} }
void save(ZRCola::textindex<wchar_t, wchar_t, uint32_t> &idx) const; void save(ZRCola::textindex<wchar_t, wchar_t, unsigned __int32> &idx) const;
protected: protected:
inline void add_keyword(const std::wstring &term, const std::wstring &chr) inline void add_keyword(const std::wstring &term, const std::wstring &chr)
@ -913,16 +920,16 @@ namespace ZRCola {
inline ZRCola::translation_db& operator<<(_Inout_ ZRCola::translation_db &db, _In_ const ZRCola::DBSource::translation &rec) inline ZRCola::translation_db& operator<<(_Inout_ ZRCola::translation_db &db, _In_ const ZRCola::DBSource::translation &rec)
{ {
uint32_t idx = db.data.size(); unsigned __int32 idx = db.data.size();
db.data.push_back((uint16_t)rec.set); db.data.push_back((unsigned __int16)rec.set);
db.data.push_back((uint16_t)rec.dst.rank); db.data.push_back((unsigned __int16)rec.dst.rank);
db.data.push_back((uint16_t)rec.src.rank); db.data.push_back((unsigned __int16)rec.src.rank);
std::wstring::size_type n = rec.dst.str.length(); std::wstring::size_type n = rec.dst.str.length();
wxASSERT_MSG(n <= 0xffff, wxT("destination overflow")); wxASSERT_MSG(n <= 0xffff, wxT("destination overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
n += rec.src.str.length(); n += rec.src.str.length();
wxASSERT_MSG(n <= 0xffff, wxT("source overflow")); wxASSERT_MSG(n <= 0xffff, wxT("source overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
db.data.insert(db.data.end(), rec.dst.str.cbegin(), rec.dst.str.cend()); db.data.insert(db.data.end(), rec.dst.str.cbegin(), rec.dst.str.cend());
db.data.insert(db.data.end(), rec.src.str.cbegin(), rec.src.str.cend()); db.data.insert(db.data.end(), rec.src.str.cbegin(), rec.src.str.cend());
db.idxSrc.push_back(idx); db.idxSrc.push_back(idx);
@ -934,14 +941,14 @@ inline ZRCola::translation_db& operator<<(_Inout_ ZRCola::translation_db &db, _I
inline ZRCola::transet_db& operator<<(_Inout_ ZRCola::transet_db &db, _In_ const ZRCola::DBSource::transet &rec) inline ZRCola::transet_db& operator<<(_Inout_ ZRCola::transet_db &db, _In_ const ZRCola::DBSource::transet &rec)
{ {
uint32_t idx = db.data.size(); unsigned __int32 idx = db.data.size();
db.data.push_back((uint16_t)rec.set); db.data.push_back((unsigned __int16)rec.set);
std::wstring::size_type n = rec.src.length(); std::wstring::size_type n = rec.src.length();
wxASSERT_MSG(n <= 0xffff, wxT("translation set source name overflow")); wxASSERT_MSG(n <= 0xffff, wxT("translation set source name overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
n += rec.dst.length(); n += rec.dst.length();
wxASSERT_MSG(n <= 0xffff, wxT("translation set destination name overflow")); wxASSERT_MSG(n <= 0xffff, wxT("translation set destination name overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
db.data.insert(db.data.end(), rec.src.cbegin(), rec.src.cend()); db.data.insert(db.data.end(), rec.src.cbegin(), rec.src.cend());
db.data.insert(db.data.end(), rec.dst.cbegin(), rec.dst.cend()); db.data.insert(db.data.end(), rec.dst.cbegin(), rec.dst.cend());
db.idxTranSet.push_back(idx); db.idxTranSet.push_back(idx);
@ -952,18 +959,18 @@ inline ZRCola::transet_db& operator<<(_Inout_ ZRCola::transet_db &db, _In_ const
inline ZRCola::transeq_db& operator<<(_Inout_ ZRCola::transeq_db &db, _In_ const ZRCola::DBSource::transeq &rec) inline ZRCola::transeq_db& operator<<(_Inout_ ZRCola::transeq_db &db, _In_ const ZRCola::DBSource::transeq &rec)
{ {
uint32_t idx = db.data.size(); unsigned __int32 idx = db.data.size();
db.data.push_back((uint16_t)rec.seq); db.data.push_back((unsigned __int16)rec.seq);
db.data.push_back((uint16_t)rec.rank); db.data.push_back((unsigned __int16)rec.rank);
std::wstring::size_type n = rec.name.length(); std::wstring::size_type n = rec.name.length();
wxASSERT_MSG(n <= 0xffff, wxT("translation sequence name overflow")); wxASSERT_MSG(n <= 0xffff, wxT("translation sequence name overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
n += rec.sets.size(); n += rec.sets.size();
wxASSERT_MSG(n <= 0xffff, wxT("translation sequence sets overflow")); wxASSERT_MSG(n <= 0xffff, wxT("translation sequence sets overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
db.data.insert(db.data.end(), rec.name.cbegin(), rec.name.cend()); db.data.insert(db.data.end(), rec.name.cbegin(), rec.name.cend());
for (auto s = rec.sets.cbegin(), s_end = rec.sets.cend(); s != s_end; ++s) for (auto s = rec.sets.cbegin(), s_end = rec.sets.cend(); s != s_end; ++s)
db.data.push_back((uint16_t)*s); db.data.push_back((unsigned __int16)*s);
db.idxTranSeq.push_back(idx); db.idxTranSeq.push_back(idx);
db.idxRank .push_back(idx); db.idxRank .push_back(idx);
@ -973,13 +980,13 @@ inline ZRCola::transeq_db& operator<<(_Inout_ ZRCola::transeq_db &db, _In_ const
inline ZRCola::keyseq_db& operator<<(_Inout_ ZRCola::keyseq_db &db, _In_ const ZRCola::DBSource::keyseq &rec) inline ZRCola::keyseq_db& operator<<(_Inout_ ZRCola::keyseq_db &db, _In_ const ZRCola::DBSource::keyseq &rec)
{ {
uint32_t idx = db.data.size(); unsigned __int32 idx = db.data.size();
std::wstring::size_type n = rec.chr.length(); std::wstring::size_type n = rec.chr.length();
wxASSERT_MSG(n <= 0xffff, wxT("character overflow")); wxASSERT_MSG(n <= 0xffff, wxT("character overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
n += rec.seq.size() * sizeof(ZRCola::keyseq_db::keyseq::key_t) / sizeof(wchar_t); n += rec.seq.size() * sizeof(ZRCola::keyseq_db::keyseq::key_t) / sizeof(wchar_t);
wxASSERT_MSG(n <= 0xffff, wxT("key sequence overflow")); wxASSERT_MSG(n <= 0xffff, wxT("key sequence overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
db.data.insert(db.data.end(), rec.chr.cbegin(), rec.chr.cend()); db.data.insert(db.data.end(), rec.chr.cbegin(), rec.chr.cend());
for (auto kc = rec.seq.cbegin(), kc_end = rec.seq.cend(); kc != kc_end; ++kc) { for (auto kc = rec.seq.cbegin(), kc_end = rec.seq.cend(); kc != kc_end; ++kc) {
db.data.push_back(kc->key); db.data.push_back(kc->key);
@ -997,11 +1004,11 @@ inline ZRCola::keyseq_db& operator<<(_Inout_ ZRCola::keyseq_db &db, _In_ const Z
inline ZRCola::language_db& operator<<(_Inout_ ZRCola::language_db &db, _In_ const ZRCola::DBSource::language &rec) inline ZRCola::language_db& operator<<(_Inout_ ZRCola::language_db &db, _In_ const ZRCola::DBSource::language &rec)
{ {
uint32_t idx = db.data.size(); unsigned __int32 idx = db.data.size();
db.data.insert(db.data.end(), reinterpret_cast<const uint16_t*>(&rec.lang), reinterpret_cast<const uint16_t*>(&rec.lang + 1)); db.data.insert(db.data.end(), reinterpret_cast<const unsigned __int16*>(&rec.lang), reinterpret_cast<const unsigned __int16*>(&rec.lang + 1));
std::wstring::size_type n = rec.name.length(); std::wstring::size_type n = rec.name.length();
wxASSERT_MSG(n <= 0xffff, wxT("language name overflow")); wxASSERT_MSG(n <= 0xffff, wxT("language name overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
db.data.insert(db.data.end(), rec.name.cbegin(), rec.name.cend()); db.data.insert(db.data.end(), rec.name.cbegin(), rec.name.cend());
db.idxLang.push_back(idx); db.idxLang.push_back(idx);
@ -1011,11 +1018,11 @@ inline ZRCola::language_db& operator<<(_Inout_ ZRCola::language_db &db, _In_ con
inline ZRCola::langchar_db& operator<<(_Inout_ ZRCola::langchar_db &db, _In_ const ZRCola::DBSource::langchar &rec) inline ZRCola::langchar_db& operator<<(_Inout_ ZRCola::langchar_db &db, _In_ const ZRCola::DBSource::langchar &rec)
{ {
uint32_t idx = db.data.size(); unsigned __int32 idx = db.data.size();
db.data.insert(db.data.end(), reinterpret_cast<const uint16_t*>(&rec.lang), reinterpret_cast<const uint16_t*>(&rec.lang + 1)); db.data.insert(db.data.end(), reinterpret_cast<const unsigned __int16*>(&rec.lang), reinterpret_cast<const unsigned __int16*>(&rec.lang + 1));
std::wstring::size_type n = rec.chr.length(); std::wstring::size_type n = rec.chr.length();
wxASSERT_MSG(n <= 0xffff, wxT("character overflow")); wxASSERT_MSG(n <= 0xffff, wxT("character overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
db.data.insert(db.data.end(), rec.chr.cbegin(), rec.chr.cend()); db.data.insert(db.data.end(), rec.chr.cbegin(), rec.chr.cend());
db.idxChr .push_back(idx); db.idxChr .push_back(idx);
#ifdef ZRCOLA_LANGCHAR_LANG_IDX #ifdef ZRCOLA_LANGCHAR_LANG_IDX
@ -1028,15 +1035,15 @@ inline ZRCola::langchar_db& operator<<(_Inout_ ZRCola::langchar_db &db, _In_ con
inline ZRCola::chrgrp_db& operator<<(_Inout_ ZRCola::chrgrp_db &db, _In_ const ZRCola::DBSource::chrgrp &rec) inline ZRCola::chrgrp_db& operator<<(_Inout_ ZRCola::chrgrp_db &db, _In_ const ZRCola::DBSource::chrgrp &rec)
{ {
uint32_t idx = db.data.size(); unsigned __int32 idx = db.data.size();
db.data.push_back((uint16_t)rec.grp); db.data.push_back((unsigned __int16)rec.grp);
db.data.push_back((uint16_t)rec.rank); db.data.push_back((unsigned __int16)rec.rank);
std::wstring::size_type n = rec.name.length(); std::wstring::size_type n = rec.name.length();
wxASSERT_MSG(n <= 0xffff, wxT("character group name overflow")); wxASSERT_MSG(n <= 0xffff, wxT("character group name overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
n += rec.chars.size(); n += rec.chars.size();
wxASSERT_MSG(n <= 0xffff, wxT("character group characters overflow")); wxASSERT_MSG(n <= 0xffff, wxT("character group characters overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
db.data.insert(db.data.end(), rec.name .cbegin(), rec.name .cend()); db.data.insert(db.data.end(), rec.name .cbegin(), rec.name .cend());
db.data.insert(db.data.end(), rec.chars.cbegin(), rec.chars.cend()); db.data.insert(db.data.end(), rec.chars.cbegin(), rec.chars.cend());
db.data.insert(db.data.end(), rec.show .cbegin(), rec.show .cend()); db.data.insert(db.data.end(), rec.show .cbegin(), rec.show .cend());
@ -1048,17 +1055,17 @@ inline ZRCola::chrgrp_db& operator<<(_Inout_ ZRCola::chrgrp_db &db, _In_ const Z
inline ZRCola::character_db& operator<<(_Inout_ ZRCola::character_db &db, _In_ const ZRCola::DBSource::character &rec) inline ZRCola::character_db& operator<<(_Inout_ ZRCola::character_db &db, _In_ const ZRCola::DBSource::character &rec)
{ {
uint32_t idx = db.data.size(); unsigned __int32 idx = db.data.size();
db.data.insert(db.data.end(), reinterpret_cast<const uint16_t*>(&rec.second.cat), reinterpret_cast<const uint16_t*>(&rec.second.cat + 1)); db.data.insert(db.data.end(), reinterpret_cast<const unsigned __int16*>(&rec.second.cat), reinterpret_cast<const unsigned __int16*>(&rec.second.cat + 1));
std::wstring::size_type n = rec.first.length(); std::wstring::size_type n = rec.first.length();
wxASSERT_MSG(n <= 0xffff, wxT("character overflow")); wxASSERT_MSG(n <= 0xffff, wxT("character overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
n += rec.second.desc.length(); n += rec.second.desc.length();
wxASSERT_MSG(n <= 0xffff, wxT("character description overflow")); wxASSERT_MSG(n <= 0xffff, wxT("character description overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
n += rec.second.rel.size(); n += rec.second.rel.size();
wxASSERT_MSG(n <= 0xffff, wxT("related characters overflow")); wxASSERT_MSG(n <= 0xffff, wxT("related characters overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
db.data.insert(db.data.end(), rec.first .cbegin(), rec.first .cend()); db.data.insert(db.data.end(), rec.first .cbegin(), rec.first .cend());
db.data.insert(db.data.end(), rec.second.desc.cbegin(), rec.second.desc.cend()); db.data.insert(db.data.end(), rec.second.desc.cbegin(), rec.second.desc.cend());
db.data.insert(db.data.end(), rec.second.rel .cbegin(), rec.second.rel .cend()); db.data.insert(db.data.end(), rec.second.rel .cbegin(), rec.second.rel .cend());
@ -1070,12 +1077,12 @@ inline ZRCola::character_db& operator<<(_Inout_ ZRCola::character_db &db, _In_ c
inline ZRCola::chrcat_db& operator<<(_Inout_ ZRCola::chrcat_db &db, _In_ const ZRCola::DBSource::chrcat &rec) inline ZRCola::chrcat_db& operator<<(_Inout_ ZRCola::chrcat_db &db, _In_ const ZRCola::DBSource::chrcat &rec)
{ {
uint32_t idx = db.data.size(); unsigned __int32 idx = db.data.size();
db.data.insert(db.data.end(), reinterpret_cast<const uint16_t*>(&rec.cat), reinterpret_cast<const uint16_t*>(&rec.cat + 1)); db.data.insert(db.data.end(), reinterpret_cast<const unsigned __int16*>(&rec.cat), reinterpret_cast<const unsigned __int16*>(&rec.cat + 1));
db.data.push_back((uint16_t)rec.rank); db.data.push_back((unsigned __int16)rec.rank);
std::wstring::size_type n = rec.name.length(); std::wstring::size_type n = rec.name.length();
wxASSERT_MSG(n <= 0xffff, wxT("character category name overflow")); wxASSERT_MSG(n <= 0xffff, wxT("character category name overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
db.data.insert(db.data.end(), rec.name.cbegin(), rec.name.cend()); db.data.insert(db.data.end(), rec.name.cbegin(), rec.name.cend());
db.idxChrCat.push_back(idx); db.idxChrCat.push_back(idx);
db.idxRank .push_back(idx); db.idxRank .push_back(idx);
@ -1086,11 +1093,11 @@ inline ZRCola::chrcat_db& operator<<(_Inout_ ZRCola::chrcat_db &db, _In_ const Z
inline ZRCola::chrtag_db& operator<<(_Inout_ ZRCola::chrtag_db &db, _In_ const ZRCola::DBSource::chrtag &rec) inline ZRCola::chrtag_db& operator<<(_Inout_ ZRCola::chrtag_db &db, _In_ const ZRCola::DBSource::chrtag &rec)
{ {
uint32_t idx = db.data.size(); unsigned __int32 idx = db.data.size();
db.data.push_back((uint16_t)rec.tag); db.data.push_back((unsigned __int16)rec.tag);
std::wstring::size_type n = rec.chr.length(); std::wstring::size_type n = rec.chr.length();
wxASSERT_MSG(n <= 0xffff, wxT("character overflow")); wxASSERT_MSG(n <= 0xffff, wxT("character overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
db.data.insert(db.data.end(), rec.chr.cbegin(), rec.chr.cend()); db.data.insert(db.data.end(), rec.chr.cbegin(), rec.chr.cend());
db.idxChr.push_back(idx); db.idxChr.push_back(idx);
db.idxTag.push_back(idx); db.idxTag.push_back(idx);
@ -1103,13 +1110,13 @@ inline ZRCola::tagname_db& operator<<(_Inout_ ZRCola::tagname_db &db, _In_ const
{ {
for (auto ln = rec.names.cbegin(), ln_end = rec.names.cend(); ln != ln_end; ++ln) { for (auto ln = rec.names.cbegin(), ln_end = rec.names.cend(); ln != ln_end; ++ln) {
for (auto nm = ln->second.cbegin(), nm_end = ln->second.cend(); nm != nm_end; ++nm) { for (auto nm = ln->second.cbegin(), nm_end = ln->second.cend(); nm != nm_end; ++nm) {
uint32_t idx = db.data.size(); unsigned __int32 idx = db.data.size();
db.data.push_back((uint16_t)rec.tag); db.data.push_back((unsigned __int16)rec.tag);
db.data.push_back(LOWORD(ln->first)); db.data.push_back(LOWORD(ln->first));
db.data.push_back(HIWORD(ln->first)); db.data.push_back(HIWORD(ln->first));
std::wstring::size_type n = nm->length(); std::wstring::size_type n = nm->length();
wxASSERT_MSG(n <= 0xffff, wxT("tag name overflow")); wxASSERT_MSG(n <= 0xffff, wxT("tag name overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
db.data.insert(db.data.end(), nm->cbegin(), nm->cend()); db.data.insert(db.data.end(), nm->cbegin(), nm->cend());
db.idxName.push_back(idx); db.idxName.push_back(idx);
db.idxTag .push_back(idx); db.idxTag .push_back(idx);
@ -1122,11 +1129,11 @@ inline ZRCola::tagname_db& operator<<(_Inout_ ZRCola::tagname_db &db, _In_ const
inline ZRCola::highlight_db& operator<<(_Inout_ ZRCola::highlight_db &db, _In_ const ZRCola::DBSource::highlight &rec) inline ZRCola::highlight_db& operator<<(_Inout_ ZRCola::highlight_db &db, _In_ const ZRCola::DBSource::highlight &rec)
{ {
uint32_t idx = db.data.size(); unsigned __int32 idx = db.data.size();
db.data.push_back((uint16_t)rec.set); db.data.push_back((unsigned __int16)rec.set);
std::wstring::size_type n = rec.chr.length(); std::wstring::size_type n = rec.chr.length();
wxASSERT_MSG(n <= 0xffff, wxT("character overflow")); wxASSERT_MSG(n <= 0xffff, wxT("character overflow"));
db.data.push_back((uint16_t)n); db.data.push_back((unsigned __int16)n);
db.data.insert(db.data.end(), rec.chr.cbegin(), rec.chr.cend()); db.data.insert(db.data.end(), rec.chr.cbegin(), rec.chr.cend());
db.idxChr.push_back(idx); db.idxChr.push_back(idx);

View File

@ -981,10 +981,10 @@ int _tmain(int argc, _TCHAR *argv[])
<< "\"Content-Transfer-Encoding: 8bit\\n\"" << endl << "\"Content-Transfer-Encoding: 8bit\\n\"" << endl
<< "\"X-Generator: ZRColaCompile\\n\"" << endl; << "\"X-Generator: ZRColaCompile\\n\"" << endl;
charset_encoder<wchar_t, char> conv(stdex::wchar_t_charset, charset_id::utf8); wstring_convert<codecvt_utf8<wchar_t>> conv;
for (auto p = pot.cbegin(); p != pot.cend(); ++p) { for (auto p = pot.cbegin(); p != pot.cend(); ++p) {
// Convert UTF-16 to UTF-8 and escape. // Convert UTF-16 to UTF-8 and escape.
string t(conv.convert(*p)), u; string t(conv.to_bytes(*p)), u;
for (size_t i = 0, n = t.size(); i < n; i++) { for (size_t i = 0, n = t.size(); i < n; i++) {
char c = t[i]; char c = t[i];
switch (c) { switch (c) {

View File

@ -20,7 +20,7 @@
#include <wx/intl.h> #include <wx/intl.h>
#pragma warning(pop) #pragma warning(pop)
#include <zrcola/idrec.h> #include <stdex/idrec>
#include <WinStd/Common.h> #include <WinStd/Common.h>

3
ZRColaWS/.gitignore vendored
View File

@ -1,3 +0,0 @@
/*.d
/*.o
/zrcolaws

View File

@ -1,91 +0,0 @@
CPPFLAGS := $(CPPFLAGS) -I../lib/libZRCola/include -I../lib/stdex/include -I../lib/oatpp-swagger/src -I../lib/oatpp/src
LDFLAGS := $(LDFLAGS) -L../lib/libZRCola/lib -L../lib/oatpp-swagger/build/src -L../lib/oatpp/build/src
LDLIBS := $(LDLIBS) -lZRCola -loatpp-swagger -loatpp -lstdc++
SRCS := zrcolaws.cpp
include ../include/props.mak
.PHONY: all
all: zrcolaws
zrcolaws: \
../lib/oatpp/build/src/liboatpp.a \
../lib/oatpp-swagger/build/src/liboatpp-swagger.a \
../lib/libZRCola/lib/libZRCola.a \
$(OBJS)
$(CC) $(LDFLAGS) $(OBJS) -o $@ $(LDLIBS)
../lib/oatpp/build/src/liboatpp.a: ../lib/oatpp/build/Makefile
$(MAKE) $(MFLAGS) -C ../lib/oatpp/build
../lib/oatpp/build/Makefile: ../lib/oatpp/CMakeLists.txt
cmake -D OATPP_INSTALL=OFF -D OATPP_BUILD_TESTS=OFF -D OATPP_LINK_TEST_LIBRARY=OFF -S ../lib/oatpp -B ../lib/oatpp/build
../lib/oatpp-swagger/build/src/liboatpp-swagger.a: ../lib/oatpp-swagger/build/Makefile
$(MAKE) $(MFLAGS) -C ../lib/oatpp-swagger/build
../lib/oatpp-swagger/build/Makefile: ../lib/oatpp-swagger/CMakeLists.txt
cmake -D OATPP_INSTALL=OFF -D OATPP_BUILD_TESTS=OFF -D OATPP_MODULES_LOCATION=CUSTOM -D OATPP_DIR_SRC=${CURDIR}/../lib/oatpp/src -D OATPP_DIR_LIB=${CURDIR}/../lib/oatpp/build/src -S ../lib/oatpp-swagger -B ../lib/oatpp-swagger/build
../lib/libZRCola/lib/libZRCola.a:
$(MAKE) $(MFLAGS) -C ../lib/libZRCola/build
.PHONY: install
install: zrcolaws ../output/data/ZRCola.zrcdb
install -d $(PREFIX)/bin/
install -m 755 zrcolaws $(PREFIX)/bin/
install -d $(PREFIX)/share/zrcola/
install -m 644 ../output/data/ZRCola.zrcdb $(PREFIX)/share/zrcola/
install -d $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/favicon-16x16.png $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/favicon-32x32.png $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/index.html $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/oauth2-redirect.html $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/swagger-ui-bundle.js $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/swagger-ui-bundle.js.map $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/swagger-ui-es-bundle-core.js $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/swagger-ui-es-bundle-core.js.map $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/swagger-ui-es-bundle.js $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/swagger-ui-es-bundle.js.map $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/swagger-ui-standalone-preset.js $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/swagger-ui-standalone-preset.js.map $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/swagger-ui.css $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/swagger-ui.css.map $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/swagger-ui.js $(PREFIX)/share/zrcola/res/
install -m 644 ../lib/oatpp-swagger/res/swagger-ui.js.map $(PREFIX)/share/zrcola/res/
install -d $(PREFIX)/etc/sysconfig/
{ echo '#!/bin/sh'; \
echo ''; \
echo '# Interface to listen on. Default: localhost'; \
echo '#HOST=0.0.0.0'; \
echo ''; \
echo '# Port to listen on. Default: 54591'; \
echo '#PORT=54591'; \
echo ''; \
echo '# Additional zrcolaws command line options'; \
echo '#OPTIONS=-4'; \
} > $(PREFIX)/etc/sysconfig/zrcolaws
install -d $(PREFIX)/lib/systemd/system/
{ echo '[Unit]'; \
echo 'Description=ZRCola Web Service'; \
echo 'After=network.target'; \
echo ''; \
echo '[Service]'; \
echo 'Environment="HOST=localhost" "PORT=54591"'; \
echo 'EnvironmentFile=-$(PREFIX)/etc/sysconfig/zrcolaws'; \
echo 'DynamicUser=yes'; \
echo 'ExecStart=/usr/local/bin/zrcolaws --host $$HOST --port $$PORT $$OPTIONS'; \
echo 'Type=exec'; \
echo 'Restart=always'; \
echo ''; \
echo '[Install]'; \
echo 'WantedBy=multi-user.target'; \
echo 'RequiredBy=network.target'; \
} > $(PREFIX)/lib/systemd/system/zrcolaws.service
.PHONY: clean
clean:
-rm -r *.{d,o} zrcolaws
include ../include/targets.mak
-include $(DEPS)

View File

@ -1,23 +0,0 @@
# ZRCola Web Service
## Building and Installation
1. Install prerequisites: `sudo dnf install git cmake gcc gcc-c++ make libatomic`
2. Clone this Git repository: `git clone --recursive https://github.com/Amebis/ZRCola.git`
3. Build ZRColaWS: `make -C ZRCola/ZRColaWS`
4. Install ZRColaWS: `sudo make -C ZRCola/ZRColaWS install`
5. Configure ZRColaWS by editing _/usr/local/etc/sysconfig/zrcolaws_
6. Start ZRColaWS: `sudo systemctl start zrcolaws.service`
7. Configure ZRColaWS for auto-start: `sudo systemctl enable zrcolaws.service`
8. Remember to open webservice port in firewall, should the service require external access.
## Usage
Web service API documentation is available at _http://host:port/swagger/ui_.

View File

@ -1,73 +0,0 @@
/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2022 Amebis
*/
#pragma once
#include "../include/version.h"
#include "controller.hpp"
#include <oatpp-swagger/Model.hpp>
#include <oatpp-swagger/Resources.hpp>
#include <oatpp/core/base/CommandLineArguments.hpp>
#include <oatpp/core/macro/component.hpp>
#include <oatpp/network/Server.hpp>
#include <oatpp/network/tcp/server/ConnectionProvider.hpp>
#include <oatpp/parser/json/mapping/ObjectMapper.hpp>
#include <oatpp/web/server/HttpConnectionHandler.hpp>
class AppComponent
{
protected:
oatpp::network::Address m_address;
public:
AppComponent(const oatpp::network::Address& address) : m_address(address) {}
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([this] {
return oatpp::network::tcp::server::ConnectionProvider::createShared({m_address.host, m_address.port, m_address.family});
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
return oatpp::web::server::HttpRouter::createShared();
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
return oatpp::web::server::HttpConnectionHandler::createShared(router);
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
auto serializerConfig = oatpp::parser::json::mapping::Serializer::Config::createShared();
serializerConfig->escapeFlags &= ~oatpp::parser::json::Utils::FLAG_ESCAPE_UTF8CHAR;
return oatpp::parser::json::mapping::ObjectMapper::createShared(
serializerConfig,
oatpp::parser::json::mapping::Deserializer::Config::createShared());
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::Server>, server)([] {
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler);
return oatpp::network::Server::createShared(connectionProvider, connectionHandler);
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::swagger::DocumentInfo>, swaggerDocumentInfo)([] {
oatpp::swagger::DocumentInfo::Builder builder;
builder
.setTitle("ZRCola Web Service")
.setDescription(
"ZRCola is an input system designed mainly, although not exclusively, for linguistic use. "
"It allows the user to combine basic letters with any diacritic marks and insert the resulting complex characters into the texts with ease.")
.setVersion(PRODUCT_VERSION_STR)
.setContactName("ZRCola")
.setContactUrl("https://zrcola.zrc-sazu.si/en/")
.setLicenseName("GNU General Public License, Version 3")
.setLicenseUrl("https://www.gnu.org/licenses/gpl-3.0.en.html");
return builder.build();
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::swagger::Resources>, swaggerResources)([] {
return oatpp::swagger::Resources::loadResources(PREFIX "/share/zrcola/res");
}());
};

View File

@ -1,243 +0,0 @@
/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2022 Amebis
*/
#pragma once
#include "../include/version.h"
#include "dto.hpp"
#include "iconverter.hpp"
#include "zrcolaws.hpp"
#include <zrcola/translate.h>
#include <oatpp/core/macro/codegen.hpp>
#include <oatpp/core/macro/component.hpp>
#include <oatpp/web/server/api/ApiController.hpp>
#include OATPP_CODEGEN_BEGIN(ApiController)
class Controller : public oatpp::web::server::api::ApiController
{
public:
Controller(const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& defaultObjectMapper, const oatpp::String &routerPrefix = nullptr) :
oatpp::web::server::api::ApiController(defaultObjectMapper, routerPrefix)
{}
ADD_CORS(getAbout)
ENDPOINT_INFO(getAbout) {
info->summary = "Returns service information";
info->addResponse<Object<dto::About>>(Status::CODE_200, "application/json");
}
ENDPOINT("GET", "/about", getAbout)
{
auto dto = dto::About::createShared();
dto->vendor = PRODUCT_CFG_VENDOR;
dto->application = PRODUCT_CFG_APPLICATION;
dto->version = PRODUCT_VERSION_STR;
return createDtoResponse(Status::CODE_200, dto);
}
ADD_CORS(getTranset)
ENDPOINT_INFO(getTranset) {
info->summary = "Lists supported translation sets";
info->description = "Each translation set describes a set of replacements that are performed to translate text from one script or encoding to another. ";
info->addResponse<oatpp::Vector<oatpp::Object<dto::TranSet>>>(Status::CODE_200, "application/json");
}
ENDPOINT("GET", "/transet", getTranset)
{
try {
utf16toutf8 c;
auto result = oatpp::Vector<oatpp::Object<dto::TranSet>>::createShared();
auto dto = dto::TranSet::createShared();
dto->set = ZRCOLA_TRANSETID_DEFAULT;
dto->src = "ZRCola Decomposed";
dto->dst = "ZRCola Composed";
result->push_back(dto);
for (size_t i = 0, n = ts_db.idxTranSet.size(); i < n; i++) {
const auto &ts = ts_db.idxTranSet[i];
dto = dto::TranSet::createShared();
dto->set = ts.set;
dto->src = c.convert(ts.src(), ts.src_len());
dto->dst = c.convert(ts.dst(), ts.dst_len());
result->push_back(dto);
}
dto = dto::TranSet::createShared();
dto->set = ZRCOLA_TRANSETID_UNICODE;
dto->src = "ZRCola Decomposed";
dto->dst = "Unicode";
result->push_back(dto);
return createDtoResponse(Status::CODE_200, result);
} catch (std::exception &ex) {
OATPP_LOGE(__FUNCTION__, "%s: %s", typeid(ex).name(), ex.what());
return ResponseFactory::createResponse(Status::CODE_500, ex.what());
}
}
ADD_CORS(getLanguage)
ENDPOINT_INFO(getLanguage) {
info->summary = "Lists supported languages";
info->description = "Each language describes a set of special characters that are specific to that language (e.g. č, š, ž in Slovenian, or ä, ö, ü in German).";
info->addResponse<oatpp::Vector<oatpp::Object<dto::Language>>>(Status::CODE_200, "application/json");
}
ENDPOINT("GET", "/language", getLanguage)
{
try {
utf16toutf8 c;
auto result = oatpp::Vector<oatpp::Object<dto::Language>>::createShared();
for (size_t i = 0, n = lang_db.idxLang.size(); i < n; i++) {
const auto &lang = lang_db.idxLang[i];
auto dto = dto::Language::createShared();
dto->lang = std::string(&lang.lang.data[0], strnlen(lang.lang.data, std::size(lang.lang.data)));
dto->name = c.convert(lang.name(), lang.name_len());
result->push_back(dto);
}
return createDtoResponse(Status::CODE_200, result);
} catch (std::exception &ex) {
OATPP_LOGE(__FUNCTION__, "%s: %s", typeid(ex).name(), ex.what());
return ResponseFactory::createResponse(Status::CODE_500, ex.what());
}
}
ADD_CORS(postTranslate)
ENDPOINT_INFO(postTranslate) {
info->summary = "Translate text";
info->description =
"Performs any number of supported translations (see /transet) on a given input text in a sequence. "
"Together with the output text, it also returns character index mapping between input and output texts.";
auto transet = oatpp::Vector<UInt16>::createShared();
transet->push_back(ZRCOLA_TRANSETID_DEFAULT);
auto dto = dto::TranslateIn::createShared();
dto->transet = transet;
dto->text = "To je test.";
info->addConsumes<Object<dto::TranslateIn>>("application/json")
.addExample("Perform ZRCola composition", dto);
info->addResponse<Object<dto::TranslateOut>>(Status::CODE_200, "application/json");
}
ENDPOINT("POST", "/translate", postTranslate, BODY_DTO(Object<dto::TranslateIn>, input))
{
try {
utf8toutf16 cIn;
std::u16string dst, dst2;
if (input->text)
dst = cIn.convert(*input->text);
size_t src_len = dst.size();
std::vector<ZRCola::mapping_vector> mapping;
if (input->transet) {
ZRCola::mapping_vector map;
const auto ts_end = input->transet->cend();
for (auto ts = input->transet->cbegin(); ts != ts_end; ++ts) {
switch (*ts) {
case ZRCOLA_TRANSETID_DEFAULT:
case ZRCOLA_TRANSETID_UNICODE:
// Decompose first, then re-compose.
t_db.TranslateInv(*ts, dst.data(), dst.size(), dst2, &map);
mapping.push_back(std::move(map));
map.clear();
t_db.Translate(*ts, dst2.data(), dst2.size(), dst, &map);
mapping.push_back(std::move(map));
map.clear();
break;
default:
t_db.Translate(*ts, dst.data(), dst.size(), dst2, &map);
mapping.push_back(std::move(map));
map.clear();
dst = std::move(dst2);
dst2.clear();
}
}
}
utf16toutf8 cOut;
auto dto = dto::TranslateOut::createShared();
dto->text = cOut.convert(dst);
auto map = oatpp::Vector<oatpp::UInt32>::createShared();
auto m_end = mapping.cend();
for (size_t i = 0; i < src_len; ++i) {
auto j = i;
for (auto m = mapping.cbegin(); m != m_end; ++m)
j = m->to_dst(j);
map->push_back(j);
}
dto->map = map;
return createDtoResponse(Status::CODE_200, dto);
} catch (std::exception &ex) {
OATPP_LOGE(__FUNCTION__, "%s: %s", typeid(ex).name(), ex.what());
return ResponseFactory::createResponse(Status::CODE_500, ex.what());
}
}
ADD_CORS(postTranslateInv)
ENDPOINT_INFO(postTranslateInv) {
info->summary = "Inverse translate text";
info->description =
"Performs any number of supported translations (see /transet) on a given input text in a sequence in reverse. "
"Together with the output text, it also returns character index mapping between input and output texts.";
auto transet = oatpp::Vector<UInt16>::createShared();
transet->push_back(ZRCOLA_TRANSETID_DEFAULT);
auto dto = dto::TranslateIn::createShared();
dto->transet = transet;
dto->text = "T  ťᵉⓢṭ.";
dto->lang = "slv";
info->addConsumes<Object<dto::TranslateIn>>("application/json")
.addExample("Perform ZRCola decomposition", dto);
info->addResponse<Object<dto::TranslateOut>>(Status::CODE_200, "application/json");
}
ENDPOINT("POST", "/translateInv", postTranslateInv, BODY_DTO(Object<dto::TranslateIn>, input))
{
try {
utf8toutf16 cIn;
std::u16string dst, dst2;
ZRCola::langid_t lang;
if (input->text)
dst = cIn.convert(*input->text);
lang = input->lang->c_str();
size_t src_len = dst.size();
std::vector<ZRCola::mapping_vector> mapping;
if (input->transet) {
ZRCola::mapping_vector map;
const auto ts_begin = input->transet->cbegin();
for (auto ts = input->transet->cend(); (ts--) != ts_begin; ) {
switch (*ts) {
case ZRCOLA_TRANSETID_DEFAULT:
case ZRCOLA_TRANSETID_UNICODE:
t_db.TranslateInv(*ts, dst.data(), dst.size(), &lc_db, lang, dst2, &map);
dst = std::move(dst2);
dst2.clear();
map.invert();
mapping.push_back(std::move(map));
map.clear();
break;
default:
t_db.TranslateInv(*ts, dst.data(), dst.size(), dst2, &map);
dst = std::move(dst2);
dst2.clear();
map.invert();
mapping.push_back(std::move(map));
map.clear();
}
}
}
utf16toutf8 cOut;
auto dto = dto::TranslateOut::createShared();
dto->text = cOut.convert(dst);
auto map = oatpp::Vector<oatpp::UInt32>::createShared();
auto m_end = mapping.crend();
for (size_t i = 0; i < src_len; ++i) {
auto j = i;
for (auto m = mapping.crbegin(); m != m_end; ++m)
j = m->to_src(j);
map->push_back(j);
}
dto->map = map;
return createDtoResponse(Status::CODE_200, dto);
} catch (std::exception &ex) {
OATPP_LOGE(__FUNCTION__, "%s: %s", typeid(ex).name(), ex.what());
return ResponseFactory::createResponse(Status::CODE_500, ex.what());
}
}
};
#include OATPP_CODEGEN_END(ApiController)

View File

@ -1,90 +0,0 @@
/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2022 Amebis
*/
#pragma once
#include <oatpp/core/data/mapping/type/Object.hpp>
#include <oatpp/core/macro/codegen.hpp>
#include <oatpp/core/Types.hpp>
#include OATPP_CODEGEN_BEGIN(DTO)
namespace dto {
class About : public oatpp::DTO
{
DTO_INIT(About, DTO)
DTO_FIELD_INFO(vendor) { info->description = "Application vendor"; }
DTO_FIELD(String, vendor);
DTO_FIELD_INFO(application) { info->description = "Application name"; }
DTO_FIELD(String, application);
DTO_FIELD_INFO(version) { info->description = "Application version"; }
DTO_FIELD(String, version);
};
class TranSet : public oatpp::DTO
{
DTO_INIT(TranSet, DTO)
DTO_FIELD_INFO(set) { info->description = "Translation set ID"; }
DTO_FIELD(UInt16, set);
DTO_FIELD_INFO(src) { info->description = "Input transcript name in English"; }
DTO_FIELD(String, src);
DTO_FIELD_INFO(dst) { info->description = "Output transcript name in English"; }
DTO_FIELD(String, dst);
};
class Language : public oatpp::DTO
{
DTO_INIT(Language, DTO)
DTO_FIELD_INFO(lang) { info->description = "Language ID"; }
DTO_FIELD(String, lang);
DTO_FIELD_INFO(name) { info->description = "Language name in English"; }
DTO_FIELD(String, name);
};
class TranslateIn : public oatpp::DTO
{
DTO_INIT(TranslateIn, DTO)
DTO_FIELD_INFO(transet) {
info->description = "Array of one or multiple translation set IDs to perform translation on the text. When inverse translating, the translation sets are read in reverse order listed in this array. Use /transet to get IDs of all supported translation sets.";
info->required = true;
}
DTO_FIELD(Vector<UInt16>, transet);
DTO_FIELD_INFO(text) {
info->description = "Text to be translated";
info->required = true;
}
DTO_FIELD(String, text);
DTO_FIELD_INFO(lang) {
info->description = "Language ID of the text to be translated. This is used on inverse translating to skip decomposing language-specific common characters (e.g. č, š, ž in Slovenian, or ä, ö, ü in German). Use /language to get IDs of all supported languages.";
}
DTO_FIELD(String, lang) = "slv";
};
class TranslateOut : public oatpp::DTO
{
DTO_INIT(TranslateOut, DTO)
DTO_FIELD_INFO(text) { info->description = "Translated text"; }
DTO_FIELD(String, text);
DTO_FIELD_INFO(map) { info->description = "Character index mapping between input and translated text. The map[i] value represents the index of the beginning of a character (in translated text) that translated from the character (in the input text) beginning at index i. All input indexes are measured in characters after input string is translated to UTF-16, and all output indexes are measured in characters before output string is translated from UTF-16."; }
DTO_FIELD(Vector<UInt32>, map);
};
}
#include OATPP_CODEGEN_END(DTO)

View File

@ -1,75 +0,0 @@
/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2022 Amebis
*/
#pragma once
#include <stdex/compat.hpp>
#include <iconv.h>
#include <cstring>
#include <stdexcept>
#include <string>
inline static std::runtime_error errno_error(_In_z_ const char *file, _In_ int line, _In_z_ const char *func)
{
int _errno = errno;
return std::runtime_error(
std::string(file) + ":" + std::to_string(line) +
std::string(func) + " error " + std::to_string(_errno) + ": " +
std::strerror(_errno));
}
template <typename T_from, typename T_to>
class iconverter
{
public:
iconverter(_In_z_ const char* from, _In_z_ const char* to)
{
m_handle = iconv_open(to, from);
if (m_handle == (iconv_t)-1)
throw errno_error(__FILE__, __LINE__, __FUNCTION__);
}
~iconverter()
{
iconv_close(m_handle);
}
std::basic_string<T_to> convert(_In_z_count_(count) const T_from* input, _In_ size_t count) const
{
T_to buf[0x100];
std::basic_string<T_to> result;
size_t inSize = sizeof(T_from) * count;
do {
T_to* output = &buf[0];
size_t outSize = sizeof(buf);
errno = 0;
iconv(m_handle, (char**)&input, &inSize, (char**)&output, &outSize);
if (errno)
throw errno_error(__FILE__, __LINE__, __FUNCTION__);
result.insert(result.end(), buf, (T_to*)((char*)buf + sizeof(buf) - outSize));
} while (inSize);
return result;
}
std::basic_string<T_to> convert(_In_ const std::basic_string<T_from>& input)
{
return convert(input.c_str(), input.length());
}
protected:
iconv_t m_handle;
};
class utf16toutf8 : public iconverter<char16_t, char>
{
public:
utf16toutf8() : iconverter("UTF-16LE", "UTF-8") {}
};
class utf8toutf16 : public iconverter<char, char16_t>
{
public:
utf8toutf16() : iconverter("UTF-8", "UTF-16LE") {}
};

View File

@ -1,27 +0,0 @@
/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2022 Amebis
*/
#pragma once
#include <oatpp/core/base/Environment.hpp>
#include <iostream>
#include <mutex>
class StdLogger : public oatpp::base::Logger
{
private:
std::mutex m_lock;
public:
StdLogger() {}
void log(v_uint32 priority, const std::string& tag, const std::string& message) override
{
if (!isLogPriorityEnabled(priority))
return;
std::lock_guard<std::mutex> guard(m_lock);
(priority < oatpp::base::Logger::PRIORITY_W ? std::cout : std::cerr) << tag << ": " << message << std::endl;
}
};

View File

@ -1,176 +0,0 @@
/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2022 Amebis
*/
#include "appcomponent.hpp"
#include "controller.hpp"
#include "stdlogger.hpp"
#include "zrcolaws.hpp"
#include <oatpp-swagger/Controller.hpp>
#include <oatpp/core/base/CommandLineArguments.hpp>
#include <oatpp/network/Server.hpp>
#include <signal.h>
#include <fstream>
#include <iostream>
using namespace std;
using namespace ZRCola;
translation_db t_db;
transet_db ts_db;
// transeq_db tsq_db;
langchar_db lc_db;
language_db lang_db;
// character_db chr_db;
// chrcat_db cc_db;
// chrtag_db ct_db;
// tagname_db tn_db;
// highlight_db h_db;
static void load_database()
{
fstream dat(PREFIX "/share/zrcola/ZRCola.zrcdb", ios_base::in | ios_base::binary);
if (!dat.good())
throw runtime_error(PREFIX "/share/zrcola/ZRCola.zrcdb not found or cannot be opened.");
if (!stdex::idrec::find<recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN>(dat, ZRCOLA_DB_ID, sizeof(recordid_t)))
throw runtime_error(PREFIX "/share/zrcola/ZRCola.zrcdb is not a valid ZRCola database.");
recordsize_t size;
dat.read((char*)&size, sizeof(recordsize_t));
if (dat.good()) {
bool has_translation_data = false;
for (;;) {
recordid_t id;
if (!stdex::idrec::read_id(dat, id, size)) break;
if (id == translation_rec::id()) {
dat >> translation_rec(t_db);
if (dat.good()) {
has_translation_data = true;
} else {
OATPP_LOGE(__FUNCTION__, "Error reading translation data from " PREFIX "/share/zrcola/ZRCola.zrcdb.");
t_db.clear();
}
} else if (id == transet_rec::id()) {
dat >> transet_rec(ts_db);
if (!dat.good()) {
OATPP_LOGE(__FUNCTION__, "Error reading translation set data from " PREFIX "/share/zrcola/ZRCola.zrcdb.");
ts_db.clear();
}
// } else if (id == transeq_rec::id()) {
// dat >> transeq_rec(tsq_db);
// if (!dat.good()) {
// OATPP_LOGE(__FUNCTION__, "Error reading translation sequence data from " PREFIX "/share/zrcola/ZRCola.zrcdb.");
// tsq_db.clear();
// }
} else if (id == langchar_rec::id()) {
dat >> langchar_rec(lc_db);
if (!dat.good()) {
OATPP_LOGE(__FUNCTION__, "Error reading language character data from " PREFIX "/share/zrcola/ZRCola.zrcdb.");
lc_db.clear();
}
} else if (id == language_rec::id()) {
dat >> language_rec(lang_db);
if (!dat.good()) {
OATPP_LOGE(__FUNCTION__, "Error reading language character data from " PREFIX "/share/zrcola/ZRCola.zrcdb.");
lang_db.clear();
}
// } else if (id == character_rec::id()) {
// dat >> character_rec(chr_db);
// if (!dat.good()) {
// OATPP_LOGE(__FUNCTION__, "Error reading character data from " PREFIX "/share/zrcola/ZRCola.zrcdb.");
// chr_db.clear();
// }
// } else if (id == chrcat_rec::id()) {
// dat >> chrcat_rec(cc_db);
// if (!dat.good()) {
// OATPP_LOGE(__FUNCTION__, "Error reading character category data from " PREFIX "/share/zrcola/ZRCola.zrcdb.");
// cc_db.clear();
// }
// } else if (id == chrtag_rec::id()) {
// dat >> chrtag_rec(ct_db);
// if (!dat.good()) {
// OATPP_LOGE(__FUNCTION__, "Error reading character tag data from " PREFIX "/share/zrcola/ZRCola.zrcdb.");
// ct_db.clear();
// }
// } else if (id == tagname_rec::id()) {
// dat >> tagname_rec(tn_db);
// if (!dat.good()) {
// OATPP_LOGE(__FUNCTION__, "Error reading tag name data from " PREFIX "/share/zrcola/ZRCola.zrcdb.");
// tn_db.clear();
// }
// } else if (id == highlight_rec::id()) {
// dat >> highlight_rec(h_db);
// if (!dat.good()) {
// OATPP_LOGE(__FUNCTION__, "Error reading highlight data from " PREFIX "/share/zrcola/ZRCola.zrcdb.");
// h_db.clear();
// }
} else
stdex::idrec::ignore<recordsize_t, ZRCOLA_RECORD_ALIGN>(dat);
}
if (!has_translation_data)
throw runtime_error(PREFIX "/share/zrcola/ZRCola.zrcdb has no translation data.");
}
}
static void sig_handler(int s)
{
OATPP_LOGD(__FUNCTION__, "Caught signal %d", s);
OATPP_COMPONENT(std::shared_ptr<oatpp::network::Server>, server);
server->stop();
}
int main(int argc, const char* argv[])
{
auto logger = std::make_shared<StdLogger>();
oatpp::base::Environment::init(logger);
try {
{
oatpp::base::CommandLineArguments cmdArgs(argc, argv);
if (cmdArgs.hasArgument("-?") || cmdArgs.hasArgument("--help")) {
cerr << "ZRColaWS " << PRODUCT_VERSION_STR << " Copyright © 2022 Amebis" << endl;
cerr << endl;
cerr << argv[0] << " [--host <interface name>] [--port <port number>] [-4|-6]" << endl;
return 1;
}
load_database();
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = sig_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
oatpp::String host = cmdArgs.getNamedArgumentValue("--host", "localhost");
v_uint16 port = oatpp::utils::conversion::strToInt32(cmdArgs.getNamedArgumentValue("--port", "54591"));
oatpp::network::Address::Family family = oatpp::network::Address::UNSPEC;
if (cmdArgs.hasArgument("-4"))
family = oatpp::network::Address::IP_4;
else if (cmdArgs.hasArgument("-6"))
family = oatpp::network::Address::IP_6;
AppComponent components({host, port, family});
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper);
auto controller = std::make_shared<Controller>(objectMapper);
router->addController(controller);
auto swaggerController = oatpp::swagger::Controller::createShared(controller->getEndpoints());
router->addController(swaggerController);
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);
OATPP_LOGI(__FUNCTION__, "Server " PRODUCT_VERSION_STR " starting on %s:%s",
connectionProvider->getProperty("host").getData(), connectionProvider->getProperty("port").getData());
OATPP_COMPONENT(std::shared_ptr<oatpp::network::Server>, server);
server->run();
OATPP_LOGI(__FUNCTION__, "Server stopped");
}
oatpp::base::Environment::destroy();
} catch (exception &ex) {
OATPP_LOGE(__FUNCTION__, "%s: %s", typeid(ex).name(), ex.what());
return 1;
}
return 0;
}

View File

@ -1,24 +0,0 @@
/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2022 Amebis
*/
#pragma once
#include "../include/version.h"
#include <zrcola/idrec.h>
#ifndef PREFIX
#define PREFIX "/usr/local"
#endif
extern ZRCola::translation_db t_db;
extern ZRCola::transet_db ts_db;
// extern ZRCola::transeq_db tsq_db;
extern ZRCola::langchar_db lc_db;
extern ZRCola::language_db lang_db;
// extern ZRCola::character_db chr_db;
// extern ZRCola::chrcat_db cc_db;
// extern ZRCola::chrtag_db ct_db;
// extern ZRCola::tagname_db tn_db;
// extern ZRCola::highlight_db h_db;

1
include/.gitignore vendored
View File

@ -1 +1,2 @@
/UpdaterKeypair.txt
/UpdaterKeyPrivate.bin /UpdaterKeyPrivate.bin

View File

@ -31,7 +31,6 @@
<DisableSpecificWarnings>4100;4505</DisableSpecificWarnings> <DisableSpecificWarnings>4100;4505</DisableSpecificWarnings>
<SupportJustMyCode>false</SupportJustMyCode> <SupportJustMyCode>false</SupportJustMyCode>
<EnablePREfast>true</EnablePREfast> <EnablePREfast>true</EnablePREfast>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>

View File

@ -1,17 +0,0 @@
ifeq ($(PREFIX),)
PREFIX := /usr/local
endif
CPPFLAGS := $(CPPFLAGS) -MMD -MP -DPREFIX='"$(PREFIX)"'
ifeq ($(CFG),Debug)
CPPFLAGS := $(CPPFLAGS) -D_DEBUG
CFLAGS := $(CFLAGS) -Og -g
CXXFLAGS := $(CXXFLAGS) -Og -g
else
CPPFLAGS := $(CPPFLAGS) -DNDEBUG
CFLAGS := $(CFLAGS) -O3 -fdata-sections -ffunction-sections
CXXFLAGS := $(CXXFLAGS) -O3 -fdata-sections -ffunction-sections
LDFLAGS := $(LDFLAGS) -Wl,--gc-sections
endif
OBJS := $(SRCS:%=%.o)
DEPS := $(OBJS:.o=.d)

View File

@ -1,5 +0,0 @@
%.h.gch: %.h
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -x c++-header -o $@ -c $<
%.cpp.o: %.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<

View File

@ -9,7 +9,7 @@
// Product version as a single DWORD // Product version as a single DWORD
// Note: Used for version comparison within C/C++ code. // Note: Used for version comparison within C/C++ code.
// //
#define PRODUCT_VERSION 0x02070100 #define PRODUCT_VERSION 0x02060000
// //
// Product version by components // Product version by components
@ -18,27 +18,27 @@
// separately. // separately.
// //
#define PRODUCT_VERSION_MAJ 2 #define PRODUCT_VERSION_MAJ 2
#define PRODUCT_VERSION_MIN 7 #define PRODUCT_VERSION_MIN 6
#define PRODUCT_VERSION_REV 1 #define PRODUCT_VERSION_REV 0
#define PRODUCT_VERSION_BUILD 0 #define PRODUCT_VERSION_BUILD 0
// //
// Human readable product version and build year for UI // Human readable product version and build year for UI
// //
#define PRODUCT_VERSION_STR "2.7.1" #define PRODUCT_VERSION_STR "2.6"
#define PRODUCT_BUILD_YEAR_STR "2024" #define PRODUCT_BUILD_YEAR_STR "2022"
// //
// Numerical version presentation for ProductVersion propery in // Numerical version presentation for ProductVersion propery in
// MSI packages (syntax: N.N[.N[.N]]) // MSI packages (syntax: N.N[.N[.N]])
// //
#define PRODUCT_VERSION_INST "2.7.1" #define PRODUCT_VERSION_INST "2.6"
// //
// The product code for ProductCode property in MSI packages // The product code for ProductCode property in MSI packages
// Replace with new on every version change, regardless how minor it is. // Replace with new on every version change, regardless how minor it is.
// //
#define PRODUCT_VERSION_GUID "{9423BEC3-3159-4130-8C3E-48D5DE24D48A}" #define PRODUCT_VERSION_GUID "{B7743708-2694-4BA7-8FC4-0797C071C4F8}"
// //
// The product vendor and application name for configuration keeping. // The product vendor and application name for configuration keeping.

@ -1 +1 @@
Subproject commit 6dead076a2e48e7f561c6e72e027c48ffcbb07be Subproject commit 328646b2d9d7100afe9d2d0a25e2c656241bb25b

View File

@ -1,33 +0,0 @@
CPPFLAGS := $(CPPFLAGS) -I../../stdex/include
SRCS := \
../src/character.cpp \
../src/common.cpp \
../src/highlight.cpp \
../src/language.cpp \
../src/mapping.cpp \
../src/pch.cpp \
../src/tag.cpp \
../src/translate.cpp
include ../../../include/props.mak
.PHONY: all
all: ../lib/libZRCola.a
../lib/libZRCola.a: ../src/pch.h.gch $(OBJS)
$(AR) $(ARFLAGS) $@ $(OBJS)
.PHONY: test
test: ../test/test
../test/test
../test/test: ../lib/libZRCola.a
$(CXX) $(CPPFLAGS) -I../../stdex/include -I../include $(CXXFLAGS) -L../lib -o $@ ../test/test.cpp -lstdc++ -lZRCola
.PHONY: clean
clean:
-rm -r ../src/*.{d,gch,o} ../lib/libZRCola.a ../test/*.d ../test/test
include ../../../include/targets.mak
-include $(DEPS)
-include ../test/test.d

View File

@ -66,6 +66,7 @@
<ClCompile Include="..\src\common.cpp" /> <ClCompile Include="..\src\common.cpp" />
<ClCompile Include="..\src\highlight.cpp" /> <ClCompile Include="..\src\highlight.cpp" />
<ClCompile Include="..\src\language.cpp" /> <ClCompile Include="..\src\language.cpp" />
<ClCompile Include="..\src\mapping.cpp" />
<ClCompile Include="..\src\pch.cpp"> <ClCompile Include="..\src\pch.cpp">
<PrecompiledHeader>Create</PrecompiledHeader> <PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile> </ClCompile>

View File

@ -14,6 +14,9 @@
<ClCompile Include="..\src\pch.cpp"> <ClCompile Include="..\src\pch.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\mapping.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\translate.cpp"> <ClCompile Include="..\src\translate.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>

View File

@ -7,15 +7,15 @@
#include "common.h" #include "common.h"
#include <stdex/idrec>
#include <assert.h> #include <assert.h>
#include <algorithm> #include <algorithm>
#include <istream> #include <istream>
#include <locale>
#include <map> #include <map>
#include <ostream> #include <ostream>
#include <vector>
#include <set> #include <set>
#include <string> #include <string>
#include <vector>
#pragma warning(push) #pragma warning(push)
#pragma warning(disable: 4200) #pragma warning(disable: 4200)
@ -29,16 +29,11 @@ namespace ZRCola {
/// ///
typedef double charrank_t; typedef double charrank_t;
inline bool ispua(_In_ char_t c) inline bool ispua(_In_ wchar_t c)
{ {
return u'\ue000' <= c && c <= u'\uf8ff'; return L'\ue000' <= c && c <= L'\uf8ff';
} }
#ifndef _WIN32
size_t wcslen(_In_z_ const char_t* str);
size_t wcsnlen(_In_z_count_(count) const char_t* str, _In_ size_t count);
#endif
#pragma pack(push) #pragma pack(push)
#pragma pack(2) #pragma pack(2)
/// ///
@ -182,13 +177,13 @@ namespace ZRCola {
/// ///
struct character { struct character {
public: public:
chrcatid_t cat; ///> Character category ID chrcatid_t cat; ///> Character category ID
protected: protected:
uint16_t chr_to; ///< Character end in \c data unsigned __int16 chr_to; ///< Character end in \c data
uint16_t desc_to; ///< Character description end in \c data unsigned __int16 desc_to; ///< Character description end in \c data
uint16_t rel_to; ///< Related characters end in \c data unsigned __int16 rel_to; ///< Related characters end in \c data
char_t data[]; ///< Character, character description wchar_t data[]; ///< Character, character description
private: private:
inline character(_In_ const character &other); inline character(_In_ const character &other);
@ -207,47 +202,47 @@ namespace ZRCola {
/// \param[in] rel_len Number of UTF-16 characters in \p rel (including zero delimiters) /// \param[in] rel_len Number of UTF-16 characters in \p rel (including zero delimiters)
/// ///
inline character( inline character(
_In_opt_z_count_(chr_len) const char_t *chr = NULL, _In_opt_z_count_(chr_len) const wchar_t *chr = NULL,
_In_opt_ size_t chr_len = 0, _In_opt_ size_t chr_len = 0,
_In_opt_ chrcatid_t cat = chrcatid_t::blank, _In_opt_ chrcatid_t cat = chrcatid_t::blank,
_In_opt_z_count_(desc_len) const char_t *desc = NULL, _In_opt_z_count_(desc_len) const wchar_t *desc = NULL,
_In_opt_ size_t desc_len = 0, _In_opt_ size_t desc_len = 0,
_In_opt_z_count_(rel_len) const char_t *rel = NULL, _In_opt_z_count_(rel_len) const wchar_t *rel = NULL,
_In_opt_ size_t rel_len = 0) _In_opt_ size_t rel_len = 0)
{ {
this->cat = cat; this->cat = cat;
this->chr_to = static_cast<uint16_t>(chr_len); this->chr_to = static_cast<unsigned __int16>(chr_len);
if (chr && chr_len) memcpy(this->data, chr, sizeof(char_t)*chr_len); if (chr && chr_len) memcpy(this->data, chr, sizeof(wchar_t)*chr_len);
this->desc_to = static_cast<uint16_t>(this->chr_to + desc_len); this->desc_to = static_cast<unsigned __int16>(this->chr_to + desc_len);
if (desc && desc_len) memcpy(this->data + this->chr_to, desc, sizeof(char_t)*desc_len); if (desc && desc_len) memcpy(this->data + this->chr_to, desc, sizeof(wchar_t)*desc_len);
this->rel_to = static_cast<uint16_t>(this->desc_to + rel_len); this->rel_to = static_cast<unsigned __int16>(this->desc_to + rel_len);
if (rel && rel_len) memcpy(this->data + this->desc_to, rel, sizeof(char_t)*rel_len); if (rel && rel_len) memcpy(this->data + this->desc_to, rel, sizeof(wchar_t)*rel_len);
} }
inline const char_t* chr () const { return data; }; inline const wchar_t* chr () const { return data; };
inline char_t* chr () { return data; }; inline wchar_t* chr () { return data; };
inline const char_t* chr_end() const { return data + chr_to; }; inline const wchar_t* chr_end() const { return data + chr_to; };
inline char_t* chr_end() { return data + chr_to; }; inline wchar_t* chr_end() { return data + chr_to; };
inline uint16_t chr_len() const { return chr_to; }; inline unsigned __int16 chr_len() const { return chr_to; };
inline const char_t* desc () const { return data + chr_to; }; inline const wchar_t* desc () const { return data + chr_to; };
inline char_t* desc () { return data + chr_to; }; inline wchar_t* desc () { return data + chr_to; };
inline const char_t* desc_end() const { return data + desc_to; }; inline const wchar_t* desc_end() const { return data + desc_to; };
inline char_t* desc_end() { return data + desc_to; }; inline wchar_t* desc_end() { return data + desc_to; };
inline uint16_t desc_len() const { return desc_to - chr_to; }; inline unsigned __int16 desc_len() const { return desc_to - chr_to; };
inline const char_t* rel () const { return data + desc_to; }; inline const wchar_t* rel () const { return data + desc_to; };
inline char_t* rel () { return data + desc_to; }; inline wchar_t* rel () { return data + desc_to; };
inline const char_t* rel_end() const { return data + rel_to; }; inline const wchar_t* rel_end() const { return data + rel_to; };
inline char_t* rel_end() { return data + rel_to; }; inline wchar_t* rel_end() { return data + rel_to; };
inline uint16_t rel_len() const { return rel_to - desc_to; }; inline unsigned __int16 rel_len() const { return rel_to - desc_to; };
}; };
#pragma pack(pop) #pragma pack(pop)
/// ///
/// Character index /// Character index
/// ///
class indexChr : public index<uint16_t, uint32_t, character> class indexChr : public index<unsigned __int16, unsigned __int32, character>
{ {
public: public:
/// ///
@ -255,7 +250,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexChr(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, character>(h) {} indexChr(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, character>(h) {}
/// ///
/// Compares two characters by ID (for searching) /// Compares two characters by ID (for searching)
@ -277,9 +272,9 @@ namespace ZRCola {
} }
} idxChr; ///< Character index } idxChr; ///< Character index
textindex<char_t, char_t, uint32_t> idxDsc; ///< Description index textindex<wchar_t, wchar_t, unsigned __int32> idxDsc; ///< Description index
textindex<char_t, char_t, uint32_t> idxDscSub; ///< Description index (sub-terms) textindex<wchar_t, wchar_t, unsigned __int32> idxDscSub; ///< Description index (sub-terms)
std::vector<uint16_t> data; ///< Character data std::vector<unsigned __int16> data; ///< Character data
public: public:
/// ///
@ -308,7 +303,7 @@ namespace ZRCola {
/// \param[in ] fn_abort Pointer to function to periodically test for search cancellation /// \param[in ] fn_abort Pointer to function to periodically test for search cancellation
/// \param[in ] cookie Cookie for \p fn_abort call /// \param[in ] cookie Cookie for \p fn_abort call
/// ///
bool Search(_In_z_ const char_t *str, _In_ const std::set<chrcatid_t> &cats, _Inout_ std::map<string_t, charrank_t> &hits, _Inout_ std::map<string_t, charrank_t> &hits_sub, _In_opt_ bool (__cdecl *fn_abort)(void *cookie) = NULL, _In_opt_ void *cookie = NULL) const; bool Search(_In_z_ const wchar_t *str, _In_ const std::set<chrcatid_t> &cats, _Inout_ std::map<std::wstring, charrank_t> &hits, _Inout_ std::map<std::wstring, charrank_t> &hits_sub, _In_opt_ bool (__cdecl *fn_abort)(void *cookie) = NULL, _In_opt_ void *cookie = NULL) const;
/// ///
/// Get character category /// Get character category
@ -320,99 +315,20 @@ namespace ZRCola {
/// - Character category if character found /// - Character category if character found
/// - `ZRCola::chrcatid_t::blank` otherwise /// - `ZRCola::chrcatid_t::blank` otherwise
/// ///
inline chrcatid_t GetCharCat(_In_z_count_(len) const char_t *chr, _In_ const size_t len) const inline chrcatid_t GetCharCat(_In_z_count_(len) const wchar_t *chr, _In_ const size_t len) const
{ {
assert(len <= 0xffff); assert(len <= 0xffff);
std::unique_ptr<character> c((character*)new char[sizeof(character) + sizeof(char_t)*len]); std::unique_ptr<character> c((character*)new char[sizeof(character) + sizeof(wchar_t)*len]);
new (c.get()) character(chr, len); c->character::character(chr, len);
indexChr::size_type start; indexChr::size_type start;
return idxChr.find(*c, start) ? idxChr[start].cat : chrcatid_t::blank; return idxChr.find(*c, start) ? idxChr[start].cat : chrcatid_t::blank;
} }
///
/// Writes character database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Character database
///
/// \returns The stream \p stream
///
friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const character_db &db)
{
// Write character index.
if (stream.fail()) return stream;
stream << db.idxChr;
// Write description index.
if (!stream.good()) return stream;
stream << db.idxDsc;
// Write sub-term description index.
if (!stream.good()) return stream;
stream << db.idxDscSub;
// 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;
uint32_t count = (uint32_t)data_count;
stream.write((const char*)&count, sizeof(count));
// Write data.
if (stream.fail()) return stream;
stream.write((const char*)db.data.data(), sizeof(uint16_t)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads character database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Character database
///
/// \returns The stream \p stream
///
friend std::istream& operator >>(_In_ std::istream& stream, _Out_ character_db& db)
{
// Read character index.
stream >> db.idxChr;
if (!stream.good()) return stream;
// Read description index.
stream >> db.idxDsc;
if (!stream.good()) return stream;
// Read sub-term description index.
stream >> db.idxDscSub;
if (!stream.good()) return stream;
// Read data count.
uint32_t 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(uint16_t) * static_cast<std::streamsize>(count));
}
else
db.data.clear();
return stream;
}
}; };
typedef stdex::idrec::record<character_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> character_rec;
/// ///
/// Character category database /// Character category database
/// ///
@ -425,12 +341,12 @@ namespace ZRCola {
/// ///
struct chrcat { struct chrcat {
public: public:
chrcatid_t cat; ///< Character category ID chrcatid_t cat; ///< Character category ID
uint16_t rank; ///< Character category rank unsigned __int16 rank; ///< Character category rank
protected: protected:
uint16_t name_to; ///< Character category name end in \c data unsigned __int16 name_to; ///< Character category name end in \c data
char_t data[]; ///< Character category name wchar_t data[]; ///< Character category name
private: private:
inline chrcat(_In_ const chrcat &other); inline chrcat(_In_ const chrcat &other);
@ -446,29 +362,29 @@ namespace ZRCola {
/// \param[in] name_len Number of UTF-16 characters in \p name /// \param[in] name_len Number of UTF-16 characters in \p name
/// ///
inline chrcat( inline chrcat(
_In_opt_ chrcatid_t cat = chrcatid_t::blank, _In_opt_ chrcatid_t cat = chrcatid_t::blank,
_In_opt_ uint16_t rank = 0, _In_opt_ unsigned __int16 rank = 0,
_In_opt_z_count_(name_len) const char_t *name = NULL, _In_opt_z_count_(name_len) const wchar_t *name = NULL,
_In_opt_ size_t name_len = 0) _In_opt_ size_t name_len = 0)
{ {
this->cat = cat; this->cat = cat;
this->rank = rank; this->rank = rank;
this->name_to = static_cast<uint16_t>(name_len); this->name_to = static_cast<unsigned __int16>(name_len);
if (name && name_len) memcpy(this->data, name, sizeof(char_t)*name_len); if (name && name_len) memcpy(this->data, name, sizeof(wchar_t)*name_len);
} }
inline const char_t* name () const { return data; }; inline const wchar_t* name () const { return data; };
inline char_t* name () { return data; }; inline wchar_t* name () { return data; };
inline const char_t* name_end() const { return data + name_to; }; inline const wchar_t* name_end() const { return data + name_to; };
inline char_t* name_end() { return data + name_to; }; inline wchar_t* name_end() { return data + name_to; };
inline uint16_t name_len() const { return name_to; }; inline unsigned __int16 name_len() const { return name_to; };
}; };
#pragma pack(pop) #pragma pack(pop)
/// ///
/// Character category index /// Character category index
/// ///
class indexChrCat : public index<uint16_t, uint32_t, chrcat> class indexChrCat : public index<unsigned __int16, unsigned __int32, chrcat>
{ {
public: public:
/// ///
@ -476,7 +392,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexChrCat(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, chrcat>(h) {} indexChrCat(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, chrcat>(h) {}
/// ///
/// Compares two character categories by ID (for searching) /// Compares two character categories by ID (for searching)
@ -491,8 +407,8 @@ namespace ZRCola {
/// ///
virtual int compare(_In_ const chrcat &a, _In_ const chrcat &b) const virtual int compare(_In_ const chrcat &a, _In_ const chrcat &b) const
{ {
if (a.cat < b.cat) return -1; if (a.cat < b.cat) return -1;
if (a.cat > b.cat) return 1; else if (a.cat > b.cat) return 1;
return 0; return 0;
} }
@ -501,7 +417,7 @@ namespace ZRCola {
/// ///
/// Rank index /// Rank index
/// ///
class indexRank : public index<uint16_t, uint32_t, chrcat> class indexRank : public index<unsigned __int16, unsigned __int32, chrcat>
{ {
public: public:
/// ///
@ -509,7 +425,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexRank(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, chrcat>(h) {} indexRank(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, chrcat>(h) {}
/// ///
/// Compares two character categories by ID (for searching) /// Compares two character categories by ID (for searching)
@ -524,8 +440,8 @@ namespace ZRCola {
/// ///
virtual int compare(_In_ const chrcat &a, _In_ const chrcat &b) const virtual int compare(_In_ const chrcat &a, _In_ const chrcat &b) const
{ {
if (a.rank < b.rank) return -1; if (a.rank < b.rank) return -1;
if (a.rank > b.rank) return +1; else if (a.rank > b.rank) return +1;
return 0; return 0;
} }
@ -546,12 +462,19 @@ namespace ZRCola {
if (a.rank < b.rank) return -1; if (a.rank < b.rank) return -1;
else if (a.rank > b.rank) return +1; else if (a.rank > b.rank) return +1;
auto &coll = std::use_facet<std::collate<char_t>>(std::locale()); unsigned __int16
return coll.compare(a.name(), a.name_end(), b.name(), b.name_end()); a_name_len = a.name_len(),
b_name_len = b.name_len();
int r = _wcsncoll(a.name(), b.name(), std::min<unsigned __int16>(a_name_len, b_name_len));
if (r != 0) return r;
if (a_name_len < b_name_len) return -1;
else if (a_name_len > b_name_len) return +1;
return 0;
} }
} idxRank; ///< Rank index } idxRank; ///< Rank index
std::vector<uint16_t> data; ///< Character category data std::vector<unsigned __int16> data; ///< Character category data
public: public:
/// ///
@ -568,81 +491,168 @@ namespace ZRCola {
idxRank .clear(); idxRank .clear();
data .clear(); data .clear();
} }
///
/// Writes character category database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Character category database
///
/// \returns The stream \p stream
///
friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const chrcat_db& db)
{
// Write character category index.
if (stream.fail()) return stream;
stream << db.idxChrCat;
// Write rank index.
if (stream.fail()) return stream;
stream << db.idxRank;
// 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;
uint32_t count = (uint32_t)data_count;
stream.write((const char*)&count, sizeof(count));
// Write data.
if (stream.fail()) return stream;
stream.write((const char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads character category database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Character category database
///
/// \returns The stream \p stream
///
friend std::istream& operator >>(_In_ std::istream& stream, _Out_ chrcat_db& db)
{
// Read character category index.
stream >> db.idxChrCat;
if (!stream.good()) return stream;
// Read rank index.
stream >> db.idxRank;
if (!stream.good()) return stream;
// Read data count.
uint32_t 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(uint16_t) * static_cast<std::streamsize>(count));
}
else
db.data.clear();
return stream;
}
}; };
typedef stdex::idrec::record<chrcat_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> chrcat_rec;
}; };
const ZRCola::recordid_t ZRCola::character_rec::id = *(ZRCola::recordid_t*)"CHR";
const ZRCola::recordid_t ZRCola::chrcat_rec ::id = *(ZRCola::recordid_t*)"CCT";
///
/// Reads character database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Character database
///
/// \returns The stream \p stream
///
inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::character_db &db)
{
// Read character index.
stream >> db.idxChr;
if (!stream.good()) return stream;
// Read description index.
stream >> db.idxDsc;
if (!stream.good()) return stream;
// Read sub-term description index.
stream >> db.idxDscSub;
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)*static_cast<std::streamsize>(count));
} else
db.data.clear();
return stream;
}
///
/// Writes character database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Character database
///
/// \returns The stream \p stream
///
inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::character_db &db)
{
// Write character index.
if (stream.fail()) return stream;
stream << db.idxChr;
// Write description index.
if (!stream.good()) return stream;
stream << db.idxDsc;
// Write sub-term description index.
if (!stream.good()) return stream;
stream << db.idxDscSub;
// 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)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Writes character category database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Character category database
///
/// \returns The stream \p stream
///
inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::chrcat_db &db)
{
// Write character category index.
if (stream.fail()) return stream;
stream << db.idxChrCat;
// Write rank index.
if (stream.fail()) return stream;
stream << db.idxRank;
// 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)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads character category database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Character category database
///
/// \returns The stream \p stream
///
inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::chrcat_db &db)
{
// Read character category index.
stream >> db.idxChrCat;
if (!stream.good()) return stream;
// Read rank index.
stream >> db.idxRank;
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)*static_cast<std::streamsize>(count));
} else
db.data.clear();
return stream;
}
#pragma warning(pop) #pragma warning(pop)

View File

@ -9,14 +9,8 @@
#define _WINSOCKAPI_ // Prevent inclusion of winsock.h in windows.h. #define _WINSOCKAPI_ // Prevent inclusion of winsock.h in windows.h.
#include <Windows.h> #include <Windows.h>
#endif #endif
#include <stdex/compat.hpp> #include <sal.h>
#include <stdex/mapping.hpp>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <istream> #include <istream>
#include <memory>
#include <ostream> #include <ostream>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -35,39 +29,12 @@
/// ///
/// Database IDs /// Database IDs
/// ///
#define ZRCOLA_DB_ID 0x43525a // "ZRC" #define ZRCOLA_DB_ID (*(ZRCola::recordid_t*)"ZRC")
#ifdef __GNUC__
#ifdef __i386__
#define __cdecl __attribute__((__cdecl__))
#else
#define __cdecl
#endif
#endif
namespace ZRCola { namespace ZRCola {
typedef uint32_t recordid_t; typedef unsigned __int32 recordid_t;
typedef uint32_t recordsize_t; typedef unsigned __int32 recordsize_t;
///
/// ZRCola database character type
///
#ifdef _WIN32
typedef wchar_t char_t;
#else
typedef char16_t char_t;
#endif
///
/// ZRCola database string type
///
#ifdef _WIN32
typedef std::wstring string_t;
#else
typedef std::u16string string_t;
#endif
#pragma pack(push) #pragma pack(push)
@ -93,7 +60,7 @@ namespace ZRCola {
struct langid_t { struct langid_t {
char data[4]; char data[4];
inline langid_t& operator=(_In_ const langid_t &src) inline langid_t& operator=(const langid_t &src)
{ {
data[0] = src.data[0]; data[0] = src.data[0];
data[1] = src.data[1]; data[1] = src.data[1];
@ -102,7 +69,7 @@ namespace ZRCola {
return *this; return *this;
} }
inline langid_t& operator=(_In_z_ const char *src) inline langid_t& operator=(const char *src)
{ {
data[3] = ( data[3] = (
data[2] = ( data[2] = (
@ -243,14 +210,9 @@ namespace ZRCola {
/// ///
/// Memory index /// Memory index
/// ///
template <class T_data, class T_idx = uint32_t, class T_el = T_data> template <class T_data, class T_idx = unsigned __int32, class T_el = T_data>
class index : public std::vector<T_idx> class index : public std::vector<T_idx>
{ {
typedef std::vector<T_idx> base_t;
public:
typedef size_t size_type;
protected: protected:
std::vector<T_data> &host; ///< Reference to host data std::vector<T_data> &host; ///< Reference to host data
@ -272,7 +234,7 @@ namespace ZRCola {
/// ///
inline const T_el& at(size_type pos) const inline const T_el& at(size_type pos) const
{ {
return *reinterpret_cast<const T_el*>(&host[base_t::at(pos)]); return *reinterpret_cast<const T_el*>(&host[std::vector<T_idx>::at(pos)]);
} }
@ -285,7 +247,7 @@ namespace ZRCola {
/// ///
inline T_el& at(size_type pos) inline T_el& at(size_type pos)
{ {
return *reinterpret_cast<T_el*>(&host[base_t::at(pos)]); return *reinterpret_cast<T_el*>(&host[std::vector<T_idx>::at(pos)]);
} }
@ -298,7 +260,7 @@ namespace ZRCola {
/// ///
inline const T_el& operator[](size_type pos) const inline const T_el& operator[](size_type pos) const
{ {
return *reinterpret_cast<const T_el*>(&host[base_t::operator[](pos)]); return *reinterpret_cast<const T_el*>(&host[std::vector<T_idx>::operator[](pos)]);
} }
@ -311,7 +273,7 @@ namespace ZRCola {
/// ///
inline T_el& operator[](size_type pos) inline T_el& operator[](size_type pos)
{ {
return *reinterpret_cast<T_el*>(&host[base_t::operator[](pos)]); return *reinterpret_cast<T_el*>(&host[std::vector<T_idx>::operator[](pos)]);
} }
@ -320,7 +282,7 @@ namespace ZRCola {
/// ///
inline void sort() inline void sort()
{ {
qsort_s(base_t::data(), base_t::size(), sizeof(T_idx), compare_s, this); qsort_s(data(), size(), sizeof(T_idx), compare_s, this);
} }
@ -371,21 +333,21 @@ namespace ZRCola {
bool find(_In_ const T_el &el, _Out_ size_type &start, _Out_ size_type &end) const bool find(_In_ const T_el &el, _Out_ size_type &start, _Out_ size_type &end) const
{ {
// Start with the full search area. // Start with the full search area.
for (start = 0, end = base_t::size(); start < end; ) { for (start = 0, end = size(); start < end; ) {
auto m = (start + end) / 2; size_type m = (start + end) / 2;
int r = compare(el, at(m)); int r = compare(el, at(m));
if (r < 0) end = m; if (r < 0) end = m;
else if (r > 0) start = m + 1; else if (r > 0) start = m + 1;
else { else {
// Narrow the search area on the left to start at the first element in the run. // Narrow the search area on the left to start at the first element in the run.
for (auto end2 = m; start < end2;) { for (size_type end2 = m; start < end2;) {
auto m2 = (start + end2) / 2; size_type m2 = (start + end2) / 2;
if (compare(el, at(m2)) <= 0) end2 = m2; else start = m2 + 1; if (compare(el, at(m2)) <= 0) end2 = m2; else start = m2 + 1;
} }
// Narrow the search area on the right to end at the first element not in the run. // Narrow the search area on the right to end at the first element not in the run.
for (auto start2 = m + 1; start2 < end;) { for (size_type start2 = m + 1; start2 < end;) {
auto m2 = (start2 + end) / 2; size_type m2 = (start2 + end) / 2;
if (0 <= compare(el, at(m2))) start2 = m2 + 1; else end = m2; if (0 <= compare(el, at(m2))) start2 = m2 + 1; else end = m2;
} }
@ -410,14 +372,14 @@ namespace ZRCola {
{ {
// Start with the full search area. // Start with the full search area.
size_t end; size_t end;
for (start = 0, end = base_t::size(); start < end; ) { for (start = 0, end = size(); start < end; ) {
auto m = (start + end) / 2; size_type m = (start + end) / 2;
int r = compare(el, at(m)); int r = compare(el, at(m));
if (r < 0) end = m; if (r < 0) end = m;
else if (r > 0) start = m + 1; else if (r > 0) start = m + 1;
else { else {
// Narrow the search area on the left to start at the first element in the run. // Narrow the search area on the left to start at the first element in the run.
for (auto end2 = m; start < end2;) { for (size_type end2 = m; start < end2;) {
m = (start + end2) / 2; m = (start + end2) / 2;
if (compare(el, at(m)) <= 0) end2 = m; else start = m + 1; if (compare(el, at(m)) <= 0) end2 = m; else start = m + 1;
} }
@ -429,68 +391,6 @@ namespace ZRCola {
return false; return false;
} }
///
/// Writes index to a stream
///
/// \param[in] stream Output stream
/// \param[in] idx Index
///
/// \returns The stream \p stream
///
friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const index& idx)
{
// Write index count.
auto idx_count = idx.size();
#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
// 4G check
if (idx_count > 0xffffffff) {
stream.setstate(std::ios_base::failbit);
return stream;
}
#endif
if (stream.fail()) return stream;
uint32_t count = (uint32_t)idx_count;
stream.write((const char*)&count, sizeof(count));
// Write index data.
if (stream.fail()) return stream;
stream.write((const char*)idx.data(), sizeof(T_idx) * static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads index from a stream
///
/// \param[in] stream Input stream
/// \param[out] idx Index
///
/// \returns The stream \p stream
///
friend std::istream& operator >>(_In_ std::istream& stream, _Out_ index& idx)
{
uint32_t count;
// Read index count.
stream.read((char*)&count, sizeof(count));
if (!stream.good()) {
idx.clear();
return stream;
}
if (count) {
// Read index data.
idx.resize(count);
stream.read((char*)idx.data(), sizeof(T_idx) * static_cast<std::streamsize>(count));
}
else
idx.clear();
return stream;
}
private: private:
static int __cdecl compare_s(void *p, const void *a, const void *b) static int __cdecl compare_s(void *p, const void *a, const void *b)
{ {
@ -506,13 +406,11 @@ namespace ZRCola {
/// ///
/// Memory text index /// Memory text index
/// ///
template <class T_key, class T_val, class T_idx = uint32_t> template <class T_key, class T_val, class T_idx = unsigned __int32>
class textindex : public std::vector< mappair_t<T_idx> > class textindex : public std::vector< mappair_t<T_idx> >
{ {
typedef std::vector< mappair_t<T_idx> > base_t;
public: public:
typedef size_t size_type; typedef std::vector< mappair_t<T_idx> > base_t;
std::vector<T_key> keys; ///< Key data std::vector<T_key> keys; ///< Key data
std::vector<T_val> values; ///< Index values std::vector<T_val> values; ///< Index values
@ -548,15 +446,15 @@ namespace ZRCola {
/// ///
_Success_(return) bool find(_In_count_(key_len) const T_key *key, _In_ size_t key_len, _Out_ const T_val **val, _Out_ size_t *val_len) const _Success_(return) bool find(_In_count_(key_len) const T_key *key, _In_ size_t key_len, _Out_ const T_val **val, _Out_ size_t *val_len) const
{ {
for (size_type start = 0, end = base_t::size(); start < end; ) { for (size_type start = 0, end = size(); start < end; ) {
auto m = (start + end) / 2; size_type m = (start + end) / 2;
int r = compare(key, key_len, m); int r = compare(key, key_len, m);
if (r < 0) end = m; if (r < 0) end = m;
else if (r > 0) start = m + 1; else if (r > 0) start = m + 1;
else { else {
// Get values at position m. // Get values at position m.
start = base_t::at(m ).idx_val; start = base_t::at(m ).idx_val;
*val_len = (m < base_t::size() ? base_t::at(m + 1).idx_val : values.size()) - start; *val_len = (m < size() ? base_t::at(m + 1).idx_val : values.size()) - start;
*val = &values.at(start); *val = &values.at(start);
return true; return true;
} }
@ -565,145 +463,15 @@ namespace ZRCola {
return false; return false;
} }
///
/// Writes text index to a stream
///
/// \param[in] stream Output stream
/// \param[in] idx Text index
///
/// \returns The stream \p stream
///
friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const textindex& idx)
{
uint32_t count;
// Write index count.
auto idx_count = idx.size();
#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
// 4G check
if (idx_count > 0xffffffff) {
stream.setstate(std::ios_base::failbit);
return stream;
}
#endif
if (stream.fail()) return stream;
count = (uint32_t)idx_count;
stream.write((const char*)&count, sizeof(count));
// Write index data.
if (stream.fail()) return stream;
auto idx_data = idx.data();
stream.write((const char*)idx_data, sizeof(*idx_data) * static_cast<std::streamsize>(count));
// Write key count.
auto key_count = idx.keys.size();
#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
// 4G check
if (idx_count > 0xffffffff) {
stream.setstate(std::ios_base::failbit);
return stream;
}
#endif
if (stream.fail()) return stream;
count = (uint32_t)key_count;
stream.write((const char*)&count, sizeof(count));
// Write key data.
if (stream.fail()) return stream;
auto idx_keys_data = idx.keys.data();
stream.write((const char*)idx_keys_data, sizeof(*idx_keys_data) * static_cast<std::streamsize>(count));
// Write value count.
auto value_count = idx.values.size();
#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
// 4G check
if (idx_count > 0xffffffff) {
stream.setstate(std::ios_base::failbit);
return stream;
}
#endif
if (stream.fail()) return stream;
count = (uint32_t)value_count;
stream.write((const char*)&count, sizeof(count));
// Write value data.
if (stream.fail()) return stream;
auto idx_values_data = idx.values.data();
stream.write((const char*)idx_values_data, sizeof(*idx_values_data) * static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads text index from a stream
///
/// \param[in] stream Input stream
/// \param[out] idx Text index
///
/// \returns The stream \p stream
///
friend std::istream& operator >>(_In_ std::istream& stream, _Out_ textindex& idx)
{
uint32_t count;
// Read text index count.
stream.read((char*)&count, sizeof(count));
if (!stream.good()) {
idx.clear();
return stream;
}
if (count) {
// Read text index.
idx.resize(count);
auto p = idx.data();
stream.read((char*)p, sizeof(*p) * static_cast<std::streamsize>(count));
if (!stream.good()) return stream;
}
else
idx.clear();
// Read keys count.
stream.read((char*)&count, sizeof(count));
if (!stream.good()) return stream;
if (count) {
// Read keys.
idx.keys.resize(count);
auto p = idx.keys.data();
stream.read((char*)p, sizeof(*p) * static_cast<std::streamsize>(count));
if (!stream.good()) return stream;
}
else
idx.keys.clear();
// Read value count.
stream.read((char*)&count, sizeof(count));
if (!stream.good()) return stream;
if (count) {
// Read values.
idx.values.resize(count);
auto p = idx.values.data();
stream.read((char*)p, sizeof(*p) * static_cast<std::streamsize>(count));
}
else
idx.values.clear();
return stream;
}
protected: protected:
inline int compare(_In_count_(key_len) const T_key *key, _In_ size_t key_len, size_type pos) const inline int compare(_In_count_(key_len) const T_key *key, _In_ size_t key_len, size_type pos) const
{ {
// Get key at position pos. // Get key at position pos.
auto pos_next = pos + 1; size_type pos_next = pos + 1;
size_t size_t
start = base_t::at(pos ).idx_key, start = base_t::at(pos ).idx_key,
key2_len = (pos_next < base_t::size() ? base_t::at(pos_next).idx_key : keys.size()) - start; key2_len = (pos_next < size() ? base_t::at(pos_next).idx_key : keys.size()) - start;
auto key2 = &keys.at(start); std::vector<T_key>::const_pointer key2 = &keys.at(start);
// Compare keys. // Compare keys.
int r = memcmp(key, key2, sizeof(T_key)*std::min<size_t>(key_len, key2_len)); int r = memcmp(key, key2, sizeof(T_key)*std::min<size_t>(key_len, key2_len));
@ -715,15 +483,57 @@ namespace ZRCola {
} }
}; };
/// ///
/// Source-destination index transformation mapping /// Source-destination index transformation mapping
/// ///
using mapping = stdex::mapping<size_t>; class __declspec(novtable) mapping {
public:
size_t src; ///< Character index in source string
size_t dst; ///< Character index in destination string
inline mapping() : src(0), dst(0) {};
inline mapping(_In_ size_t s, _In_ size_t d) : src(s), dst(d) {}
///
/// Reverses source and destination indexes
///
inline void invert() { size_t tmp = src; src = dst; dst = tmp; }
};
/// ///
/// A vector for destination-source index transformation mapping /// A vector for destination-source index transformation mapping
/// ///
using mapping_vector = std::vector<mapping>; class mapping_vector : public std::vector<mapping> {
public:
///
/// Transforms character index of destination to source
///
/// \param[in] decmp Character index in destination string
///
/// \returns Character index in source string
///
size_t to_src(_In_ size_t dst) const;
///
/// Transforms source index to destination index
///
/// \param[in] cmp Character index in source string
///
/// \returns Character index in destination string
///
size_t to_dst(_In_ size_t src) const;
///
/// Reverses source and destination indexes
///
inline void invert()
{
for (iterator i = begin(), iEnd = end(); i != iEnd; ++i)
i->invert();
}
};
/// ///
/// Binary compares two strings /// Binary compares two strings
@ -742,7 +552,16 @@ namespace ZRCola {
/// The function does not treat \\0 characters as terminators for performance reasons. /// The function does not treat \\0 characters as terminators for performance reasons.
/// Therefore \p count_a and \p count_b must represent exact string lengths. /// Therefore \p count_a and \p count_b must represent exact string lengths.
/// ///
int CompareString(_In_ const char_t* str_a, _In_ size_t count_a, _In_ const char_t* str_b, _In_ size_t count_b); inline int CompareString(_In_ const wchar_t *str_a, _In_ size_t count_a, _In_ const wchar_t *str_b, _In_ size_t count_b)
{
for (size_t i = 0; ; i++) {
if (i >= count_a && i >= count_b) return 0;
else if (i >= count_a && i < count_b) return -1;
else if (i < count_a && i >= count_b) return +1;
else if (str_a[i] < str_b[i]) return -1;
else if (str_a[i] > str_b[i]) return +1;
}
}
/// ///
/// Generates and returns Unicode representation of the string using hexadecimal codes. /// Generates and returns Unicode representation of the string using hexadecimal codes.
@ -751,7 +570,21 @@ namespace ZRCola {
/// \param[in] count Number of characters in string \p str /// \param[in] count Number of characters in string \p str
/// \param[in] sep Separator /// \param[in] sep Separator
/// ///
std::string GetUnicodeDumpA(_In_z_count_(count) const char_t* str, _In_ size_t count, _In_z_ const char* sep = "+"); inline std::string GetUnicodeDumpA(_In_ const wchar_t *str, _In_ size_t count, _In_z_ const char *sep = "+")
{
std::string out;
size_t dump_len_max = strlen(sep) + 4 + 1;
char *dump;
std::unique_ptr<char> dump_obj(dump = new char[dump_len_max]);
if (count && str[0]) {
size_t i = 0;
out.insert(out.end(), dump, dump + _snprintf(dump, dump_len_max, "%04X", str[i++]));
while (i < count && str[i])
out.insert(out.end(), dump, dump + _snprintf(dump, dump_len_max, "%s%04X", sep, str[i++]));
}
return out;
}
/// ///
/// Generates and returns Unicode representation of the string using hexadecimal codes. /// Generates and returns Unicode representation of the string using hexadecimal codes.
@ -760,7 +593,21 @@ namespace ZRCola {
/// \param[in] count Number of characters in string \p str /// \param[in] count Number of characters in string \p str
/// \param[in] sep Separator /// \param[in] sep Separator
/// ///
std::wstring GetUnicodeDumpW(_In_z_count_(count) const char_t* str, _In_ size_t count, _In_z_ const wchar_t* sep = L"+"); inline std::wstring GetUnicodeDumpW(_In_ const wchar_t *str, _In_ size_t count, _In_z_ const wchar_t *sep = L"+")
{
std::wstring out;
size_t dump_len_max = wcslen(sep) + 4 + 1;
wchar_t *dump;
std::unique_ptr<wchar_t> dump_obj(dump = new wchar_t[dump_len_max]);
if (count && str[0]) {
size_t i = 0;
out.insert(out.end(), dump, dump + _snwprintf(dump, dump_len_max, L"%04X", str[i++]));
while (i < count && str[i])
out.insert(out.end(), dump, dump + _snwprintf(dump, dump_len_max, L"%s%04X", sep, str[i++]));
}
return out;
}
#ifdef _UNICODE #ifdef _UNICODE
#define GetUnicodeDump GetUnicodeDumpW #define GetUnicodeDump GetUnicodeDumpW
@ -769,4 +616,190 @@ namespace ZRCola {
#endif #endif
}; };
///
/// Writes index to a stream
///
/// \param[in] stream Output stream
/// \param[in] idx Index
///
/// \returns The stream \p stream
///
template <class T_data, class T_idx, class T_el>
inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::index<T_data, T_idx, T_el> &idx)
{
// Write index count.
auto idx_count = idx.size();
#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
// 4G check
if (idx_count > 0xffffffff) {
stream.setstate(std::ios_base::failbit);
return stream;
}
#endif
if (stream.fail()) return stream;
unsigned __int32 count = (unsigned __int32)idx_count;
stream.write((const char*)&count, sizeof(count));
// Write index data.
if (stream.fail()) return stream;
stream.write((const char*)idx.data(), sizeof(T_idx)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads index from a stream
///
/// \param[in] stream Input stream
/// \param[out] idx Index
///
/// \returns The stream \p stream
///
template <class T_data, class T_idx, class T_el>
inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::index<T_data, T_idx, T_el> &idx)
{
unsigned __int32 count;
// Read index count.
stream.read((char*)&count, sizeof(count));
if (!stream.good()) {
idx.clear();
return stream;
}
if (count) {
// Read index data.
idx.resize(count);
stream.read((char*)idx.data(), sizeof(T_idx)*static_cast<std::streamsize>(count));
} else
idx.clear();
return stream;
}
///
/// Writes text index to a stream
///
/// \param[in] stream Output stream
/// \param[in] idx Text index
///
/// \returns The stream \p stream
///
template <class T_key, class T_val, class T_idx>
inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::textindex<T_key, T_val, T_idx> &idx)
{
unsigned __int32 count;
// Write index count.
auto idx_count = idx.size();
#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
// 4G check
if (idx_count > 0xffffffff) {
stream.setstate(std::ios_base::failbit);
return stream;
}
#endif
if (stream.fail()) return stream;
count = (unsigned __int32)idx_count;
stream.write((const char*)&count, sizeof(count));
// Write index data.
if (stream.fail()) return stream;
stream.write((const char*)idx.data(), sizeof(ZRCola::textindex<T_key, T_val, T_idx>::value_type)*static_cast<std::streamsize>(count));
// Write key count.
auto key_count = idx.keys.size();
#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
// 4G check
if (idx_count > 0xffffffff) {
stream.setstate(std::ios_base::failbit);
return stream;
}
#endif
if (stream.fail()) return stream;
count = (unsigned __int32)key_count;
stream.write((const char*)&count, sizeof(count));
// Write key data.
if (stream.fail()) return stream;
stream.write((const char*)idx.keys.data(), sizeof(std::vector<T_key>::value_type)*static_cast<std::streamsize>(count));
// Write value count.
auto value_count = idx.values.size();
#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
// 4G check
if (idx_count > 0xffffffff) {
stream.setstate(std::ios_base::failbit);
return stream;
}
#endif
if (stream.fail()) return stream;
count = (unsigned __int32)value_count;
stream.write((const char*)&count, sizeof(count));
// Write value data.
if (stream.fail()) return stream;
stream.write((const char*)idx.values.data(), sizeof(std::vector<T_val>::value_type)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads text index from a stream
///
/// \param[in] stream Input stream
/// \param[out] idx Text index
///
/// \returns The stream \p stream
///
template <class T_key, class T_val, class T_idx>
inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::textindex<T_key, T_val, T_idx> &idx)
{
unsigned __int32 count;
// Read text index count.
stream.read((char*)&count, sizeof(count));
if (!stream.good()) {
idx.clear();
return stream;
}
if (count) {
// Read text index.
idx.resize(count);
stream.read((char*)idx.data(), sizeof(ZRCola::textindex<T_key, T_val, T_idx>::value_type)*static_cast<std::streamsize>(count));
if (!stream.good()) return stream;
} else
idx.clear();
// Read keys count.
stream.read((char*)&count, sizeof(count));
if (!stream.good()) return stream;
if (count) {
// Read keys.
idx.keys.resize(count);
stream.read((char*)idx.keys.data(), sizeof(std::vector<T_key>::value_type)*static_cast<std::streamsize>(count));
if (!stream.good()) return stream;
} else
idx.keys.clear();
// Read value count.
stream.read((char*)&count, sizeof(count));
if (!stream.good()) return stream;
if (count) {
// Read values.
idx.values.resize(count);
stream.read((char*)idx.values.data(), sizeof(std::vector<T_val>::value_type)*static_cast<std::streamsize>(count));
} else
idx.values.clear();
return stream;
}
#pragma warning(pop) #pragma warning(pop)

View File

@ -7,6 +7,8 @@
#include "common.h" #include "common.h"
#include <stdex/idrec>
#include <functional> #include <functional>
#pragma warning(push) #pragma warning(push)
@ -27,7 +29,7 @@ namespace ZRCola {
/// ///
/// Highlight set ID /// Highlight set ID
/// ///
typedef uint16_t hlghtsetid_t; typedef unsigned __int16 hlghtsetid_t;
/// ///
/// Highlight database /// Highlight database
@ -41,11 +43,11 @@ namespace ZRCola {
/// ///
struct highlight { struct highlight {
public: public:
hlghtsetid_t set; ///< Highlight set ID hlghtsetid_t set; ///< Highlight set ID
protected: protected:
uint16_t chr_to; ///< Character end in \c data unsigned __int16 chr_to; ///< Character end in \c data
char_t data[]; ///< Character wchar_t data[]; ///< Character
private: private:
inline highlight(_In_ const highlight &other); inline highlight(_In_ const highlight &other);
@ -61,21 +63,21 @@ namespace ZRCola {
/// ///
inline highlight( inline highlight(
_In_opt_ hlghtsetid_t set = 0, _In_opt_ hlghtsetid_t set = 0,
_In_opt_z_count_(chr_len) const char_t *chr = NULL, _In_opt_z_count_(chr_len) const wchar_t *chr = NULL,
_In_opt_ size_t chr_len = 0) _In_opt_ size_t chr_len = 0)
{ {
this->set = set; this->set = set;
this->chr_to = static_cast<uint16_t>(chr_len); this->chr_to = static_cast<unsigned __int16>(chr_len);
if (chr && chr_len) memcpy(this->data, chr, sizeof(char_t)*chr_len); if (chr && chr_len) memcpy(this->data, chr, sizeof(wchar_t)*chr_len);
} }
inline const char_t* chr () const { return data; }; inline const wchar_t* chr () const { return data; };
inline char_t* chr () { return data; }; inline wchar_t* chr () { return data; };
inline const char_t* chr_end() const { return data + chr_to; }; inline const wchar_t* chr_end() const { return data + chr_to; };
inline char_t* chr_end() { return data + chr_to; }; inline wchar_t* chr_end() { return data + chr_to; };
inline uint16_t chr_len() const { return chr_to; }; inline unsigned __int16 chr_len() const { return chr_to; };
inline char_t chr_at(_In_ size_t i) const inline wchar_t chr_at(_In_ size_t i) const
{ {
return i < chr_to ? data[i] : 0; return i < chr_to ? data[i] : 0;
} }
@ -85,7 +87,7 @@ namespace ZRCola {
/// ///
/// Highlight index /// Highlight index
/// ///
class indexChr : public index<uint16_t, uint32_t, highlight> class indexChr : public index<unsigned __int16, unsigned __int32, highlight>
{ {
public: public:
/// ///
@ -93,7 +95,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexChr(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, highlight>(h) {} indexChr(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, highlight>(h) {}
/// ///
/// Compares two highlights by string (for searching) /// Compares two highlights by string (for searching)
@ -111,8 +113,8 @@ namespace ZRCola {
int r = ZRCola::CompareString(a.chr(), a.chr_len(), b.chr(), b.chr_len()); int r = ZRCola::CompareString(a.chr(), a.chr_len(), b.chr(), b.chr_len());
if (r != 0) return r; if (r != 0) return r;
if (a.set < b.set) return -1; if (a.set < b.set) return -1;
if (a.set > b.set) return +1; else if (a.set > b.set) return +1;
return 0; return 0;
} }
@ -136,7 +138,7 @@ namespace ZRCola {
} idxChr; ///< Highlight index } idxChr; ///< Highlight index
std::vector<uint16_t> data; ///< Highlight data std::vector<unsigned __int16> data; ///< Highlight data
public: public:
/// ///
@ -160,73 +162,79 @@ namespace ZRCola {
/// \param[in] inputMax Length of the input string in characters. Can be (size_t)-1 if \p input is zero terminated. /// \param[in] inputMax Length of the input string in characters. Can be (size_t)-1 if \p input is zero terminated.
/// \param[in] callback Function to be called on highlight switch /// \param[in] callback Function to be called on highlight switch
/// ///
void Highlight(_In_z_count_(inputMax) const char_t* input, _In_ size_t inputMax, _In_ std::function<void (hlghtsetid_t set, size_t start, size_t end)> callback) const; void Highlight(_In_z_count_(inputMax) const wchar_t* input, _In_ size_t inputMax, _In_ std::function<void (hlghtsetid_t set, size_t start, size_t end)> callback) const;
///
/// Writes highlight database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Highlight database
///
/// \returns The stream \p stream
///
friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const highlight_db& db)
{
// Write highlight index.
if (stream.fail()) return stream;
stream << db.idxChr;
// 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;
uint32_t count = (uint32_t)data_count;
stream.write((const char*)&count, sizeof(count));
// Write data.
if (stream.fail()) return stream;
stream.write((const char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads highlight database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Highlight database
///
/// \returns The stream \p stream
///
friend std::istream& operator >>(_In_ std::istream& stream, _Out_ highlight_db& db)
{
// Read highlight index.
stream >> db.idxChr;
if (!stream.good()) return stream;
// Read data count.
uint32_t 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(uint16_t) * static_cast<std::streamsize>(count));
}
else
db.data.clear();
return stream;
}
}; };
typedef stdex::idrec::record<highlight_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> highlight_rec;
}; };
const ZRCola::recordid_t ZRCola::highlight_rec::id = *(ZRCola::recordid_t*)"HGH";
///
/// Writes highlight database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Highlight database
///
/// \returns The stream \p stream
///
inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::highlight_db &db)
{
// Write highlight index.
if (stream.fail()) return stream;
stream << db.idxChr;
// 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)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads highlight database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Highlight database
///
/// \returns The stream \p stream
///
inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::highlight_db &db)
{
// Read highlight index.
stream >> db.idxChr;
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)*static_cast<std::streamsize>(count));
} else
db.data.clear();
return stream;
}
#pragma warning(pop) #pragma warning(pop)

View File

@ -1,27 +0,0 @@
/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2022 Amebis
*/
#pragma once
#include "character.h"
#include "highlight.h"
#include "language.h"
#include "tag.h"
#include "translate.h"
#include <stdex/idrec.hpp>
namespace ZRCola {
typedef stdex::idrec::record<character_db, recordid_t, 0x524843 /*"CHR"*/, recordsize_t, ZRCOLA_RECORD_ALIGN> character_rec;
typedef stdex::idrec::record<chrcat_db, recordid_t, 0x544343 /*"CCT"*/, recordsize_t, ZRCOLA_RECORD_ALIGN> chrcat_rec;
typedef stdex::idrec::record<highlight_db, recordid_t, 0x484748 /*"HGH"*/, recordsize_t, ZRCOLA_RECORD_ALIGN> highlight_rec;
typedef stdex::idrec::record<langchar_db, recordid_t, 0x432d4c /*"L-C"*/, recordsize_t, ZRCOLA_RECORD_ALIGN> langchar_rec;
typedef stdex::idrec::record<language_db, recordid_t, 0x474e4c /*"LNG"*/, recordsize_t, ZRCOLA_RECORD_ALIGN> language_rec;
typedef stdex::idrec::record<chrtag_db, recordid_t, 0x542d43 /*"C-T"*/, recordsize_t, ZRCOLA_RECORD_ALIGN> chrtag_rec;
typedef stdex::idrec::record<tagname_db, recordid_t, 0x4e4754 /*"TGN"*/, recordsize_t, ZRCOLA_RECORD_ALIGN> tagname_rec;
typedef stdex::idrec::record<translation_db, recordid_t, 0x4e5254 /*"TRN"*/, recordsize_t, ZRCOLA_RECORD_ALIGN> translation_rec;
typedef stdex::idrec::record<transet_db, recordid_t, 0x455354 /*"TSE"*/, recordsize_t, ZRCOLA_RECORD_ALIGN> transet_rec;
typedef stdex::idrec::record<transeq_db, recordid_t, 0x515354 /*"TSQ"*/, recordsize_t, ZRCOLA_RECORD_ALIGN> transeq_rec;
}

View File

@ -7,6 +7,7 @@
#include "common.h" #include "common.h"
#include <stdex/idrec>
#include <istream> #include <istream>
#include <ostream> #include <ostream>
#include <vector> #include <vector>
@ -31,11 +32,11 @@ namespace ZRCola {
/// ///
struct langchar { struct langchar {
public: public:
langid_t lang; ///< Language ID langid_t lang; ///< Language ID
protected: protected:
uint16_t chr_to; ///< Character end in \c data unsigned __int16 chr_to; ///< Character end in \c data
char_t data[]; ///< Character wchar_t data[]; ///< Character
private: private:
inline langchar(_In_ const langchar &other); inline langchar(_In_ const langchar &other);
@ -51,26 +52,26 @@ namespace ZRCola {
/// ///
inline langchar( inline langchar(
_In_opt_ langid_t lang = langid_t::blank, _In_opt_ langid_t lang = langid_t::blank,
_In_opt_z_count_(chr_len) const char_t *chr = NULL, _In_opt_z_count_(chr_len) const wchar_t *chr = NULL,
_In_opt_ size_t chr_len = 0) _In_opt_ size_t chr_len = 0)
{ {
this->lang = lang; this->lang = lang;
this->chr_to = static_cast<uint16_t>(chr_len); this->chr_to = static_cast<unsigned __int16>(chr_len);
if (chr && chr_len) memcpy(this->data, chr, sizeof(char_t)*chr_len); if (chr && chr_len) memcpy(this->data, chr, sizeof(wchar_t)*chr_len);
} }
inline const char_t* chr () const { return data; }; inline const wchar_t* chr () const { return data; };
inline char_t* chr () { return data; }; inline wchar_t* chr () { return data; };
inline const char_t* chr_end() const { return data + chr_to; }; inline const wchar_t* chr_end() const { return data + chr_to; };
inline char_t* chr_end() { return data + chr_to; }; inline wchar_t* chr_end() { return data + chr_to; };
inline uint16_t chr_len() const { return chr_to; }; inline unsigned __int16 chr_len() const { return chr_to; };
}; };
#pragma pack(pop) #pragma pack(pop)
/// ///
/// Character index /// Character index
/// ///
class indexChr : public index<uint16_t, uint32_t, langchar> class indexChr : public index<unsigned __int16, unsigned __int32, langchar>
{ {
public: public:
/// ///
@ -78,7 +79,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexChr(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, langchar>(h) {} indexChr(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, langchar>(h) {}
/// ///
/// Compares two characters by ID (for searching) /// Compares two characters by ID (for searching)
@ -96,8 +97,8 @@ namespace ZRCola {
int r = ZRCola::CompareString(a.chr(), a.chr_len(), b.chr(), b.chr_len()); int r = ZRCola::CompareString(a.chr(), a.chr_len(), b.chr(), b.chr_len());
if (r != 0) return r; if (r != 0) return r;
if (a.lang < b.lang) return -1; if (a.lang < b.lang) return -1;
if (a.lang > b.lang) return 1; else if (a.lang > b.lang) return 1;
return 0; return 0;
} }
@ -108,7 +109,7 @@ namespace ZRCola {
/// ///
/// Language Index /// Language Index
/// ///
class indexLang : public index<uint16_t, uint32_t, langchar> class indexLang : public index<unsigned __int16, unsigned __int32, langchar>
{ {
public: public:
/// ///
@ -116,7 +117,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexLang(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, langchar>(h) {} indexLang(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, langchar>(h) {}
/// ///
/// Compares two languages by ID (for searching) /// Compares two languages by ID (for searching)
@ -131,8 +132,8 @@ namespace ZRCola {
/// ///
virtual int compare(_In_ const langchar &a, _In_ const langchar &b) const virtual int compare(_In_ const langchar &a, _In_ const langchar &b) const
{ {
if (a.lang < b.lang) return -1; if (a.lang < b.lang) return -1;
if (a.lang > b.lang) return 1; else if (a.lang > b.lang) return 1;
int r = ZRCola::CompareString(a.chr, a.chr_len(), b.chr(), b.chr_len()); int r = ZRCola::CompareString(a.chr, a.chr_len(), b.chr(), b.chr_len());
if (r != 0) return r; if (r != 0) return r;
@ -142,7 +143,7 @@ namespace ZRCola {
} idxLang; ///< Language index } idxLang; ///< Language index
#endif #endif
std::vector<uint16_t> data; ///< Character data std::vector<unsigned __int16> data; ///< Character data
public: public:
/// ///
@ -176,88 +177,13 @@ namespace ZRCola {
/// \returns /// \returns
/// - \c true when character is used in language /// - \c true when character is used in language
/// - \c false otherwise /// - \c false otherwise
bool IsLocalCharacter(_In_ const char_t *chr, _In_ const char_t *chr_end, _In_ langid_t lang) const; bool IsLocalCharacter(_In_ const wchar_t *chr, _In_ const wchar_t *chr_end, _In_ langid_t lang) const;
///
/// Writes language character database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Language character database
///
/// \returns The stream \p stream
///
friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const langchar_db& db)
{
// Write character index.
if (stream.fail()) return stream;
stream << db.idxChr;
#ifdef ZRCOLA_LANGCHAR_LANG_IDX
// Write language index.
if (stream.fail()) return stream;
stream << db.idxLang;
#endif
// 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;
uint32_t count = (uint32_t)data_count;
stream.write((const char*)&count, sizeof(count));
// Write data.
if (stream.fail()) return stream;
stream.write((const char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads language character database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Language character database
///
/// \returns The stream \p stream
///
friend std::istream& operator >>(_In_ std::istream& stream, _Out_ langchar_db& db)
{
// Read character index.
stream >> db.idxChr;
if (!stream.good()) return stream;
#ifdef ZRCOLA_LANGCHAR_LANG_IDX
// Read language index.
stream >> db.idxLang;
if (!stream.good()) return stream;
#endif
// Read data count.
uint32_t 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(uint16_t) * static_cast<std::streamsize>(count));
}
else
db.data.clear();
return stream;
}
}; };
typedef stdex::idrec::record<langchar_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> langchar_rec;
/// ///
/// Language database /// Language database
/// ///
@ -270,11 +196,11 @@ namespace ZRCola {
/// ///
struct language { struct language {
public: public:
langid_t lang; ///< Language ID langid_t lang; ///< Language ID
protected: protected:
uint16_t name_to; ///< Language name end in \c data unsigned __int16 name_to; ///< Language name end in \c data
char_t data[]; ///< Language name wchar_t data[]; ///< Language name
private: private:
inline language(_In_ const language &other); inline language(_In_ const language &other);
@ -290,26 +216,26 @@ namespace ZRCola {
/// ///
inline language( inline language(
_In_opt_ langid_t lang = langid_t::blank, _In_opt_ langid_t lang = langid_t::blank,
_In_opt_z_count_(name_len) const char_t *name = NULL, _In_opt_z_count_(name_len) const wchar_t *name = NULL,
_In_opt_ size_t name_len = 0) _In_opt_ size_t name_len = 0)
{ {
this->lang = lang; this->lang = lang;
this->name_to = static_cast<uint16_t>(name_len); this->name_to = static_cast<unsigned __int16>(name_len);
if (name && name_len) memcpy(this->data, name, sizeof(char_t)*name_len); if (name && name_len) memcpy(this->data, name, sizeof(wchar_t)*name_len);
} }
inline const char_t* name () const { return data; }; inline const wchar_t* name () const { return data; };
inline char_t* name () { return data; }; inline wchar_t* name () { return data; };
inline const char_t* name_end() const { return data + name_to; }; inline const wchar_t* name_end() const { return data + name_to; };
inline char_t* name_end() { return data + name_to; }; inline wchar_t* name_end() { return data + name_to; };
inline uint16_t name_len() const { return name_to; }; inline unsigned __int16 name_len() const { return name_to; };
}; };
#pragma pack(pop) #pragma pack(pop)
/// ///
/// Language index /// Language index
/// ///
class indexLang : public index<uint16_t, uint32_t, language> class indexLang : public index<unsigned __int16, unsigned __int32, language>
{ {
public: public:
/// ///
@ -317,7 +243,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexLang(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, language>(h) {} indexLang(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, language>(h) {}
/// ///
/// Compares two languages by ID (for searching) /// Compares two languages by ID (for searching)
@ -332,14 +258,14 @@ namespace ZRCola {
/// ///
virtual int compare(_In_ const language &a, _In_ const language &b) const virtual int compare(_In_ const language &a, _In_ const language &b) const
{ {
if (a.lang < b.lang) return -1; if (a.lang < b.lang) return -1;
if (a.lang > b.lang) return 1; else if (a.lang > b.lang) return 1;
return 0; return 0;
} }
} idxLang; ///< Language index } idxLang; ///< Language index
std::vector<uint16_t> data; ///< Language data std::vector<unsigned __int16> data; ///< Language data
public: public:
/// ///
@ -355,73 +281,156 @@ namespace ZRCola {
idxLang.clear(); idxLang.clear();
data .clear(); data .clear();
} }
///
/// Writes language database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Language database
///
/// \returns The stream \p stream
///
friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const language_db& db)
{
// Write language index.
if (stream.fail()) return stream;
stream << db.idxLang;
// 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;
uint32_t count = (uint32_t)data_count;
stream.write((const char*)&count, sizeof(count));
// Write data.
if (stream.fail()) return stream;
stream.write((const char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads language database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Language database
///
/// \returns The stream \p stream
///
friend std::istream& operator >>(_In_ std::istream& stream, _Out_ language_db& db)
{
// Read language index.
stream >> db.idxLang;
if (!stream.good()) return stream;
// Read data count.
uint32_t 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(uint16_t) * static_cast<std::streamsize>(count));
}
else
db.data.clear();
return stream;
}
}; };
typedef stdex::idrec::record<language_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> language_rec;
}; };
const ZRCola::recordid_t ZRCola::langchar_rec::id = *(ZRCola::recordid_t*)"L-C";
const ZRCola::recordid_t ZRCola::language_rec::id = *(ZRCola::recordid_t*)"LNG";
///
/// Writes language character database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Language character database
///
/// \returns The stream \p stream
///
inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::langchar_db &db)
{
// Write character index.
if (stream.fail()) return stream;
stream << db.idxChr;
#ifdef ZRCOLA_LANGCHAR_LANG_IDX
// Write language index.
if (stream.fail()) return stream;
stream << db.idxLang;
#endif
// 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)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads language character database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Language character database
///
/// \returns The stream \p stream
///
inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::langchar_db &db)
{
// Read character index.
stream >> db.idxChr;
if (!stream.good()) return stream;
#ifdef ZRCOLA_LANGCHAR_LANG_IDX
// Read language index.
stream >> db.idxLang;
if (!stream.good()) return stream;
#endif
// 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)*static_cast<std::streamsize>(count));
} else
db.data.clear();
return stream;
}
///
/// Writes language database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Language database
///
/// \returns The stream \p stream
///
inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::language_db &db)
{
// Write language index.
if (stream.fail()) return stream;
stream << db.idxLang;
// 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)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads language database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Language database
///
/// \returns The stream \p stream
///
inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::language_db &db)
{
// Read language index.
stream >> db.idxLang;
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)*static_cast<std::streamsize>(count));
} else
db.data.clear();
return stream;
}
#pragma warning(pop) #pragma warning(pop)

View File

@ -7,6 +7,7 @@
#include "common.h" #include "common.h"
#include <stdex/idrec>
#include <assert.h> #include <assert.h>
#include <istream> #include <istream>
#include <ostream> #include <ostream>
@ -20,7 +21,7 @@
namespace ZRCola { namespace ZRCola {
typedef uint16_t tagid_t; typedef unsigned __int16 tagid_t;
/// ///
/// Character Tag Database /// Character Tag Database
@ -34,11 +35,11 @@ namespace ZRCola {
/// ///
struct chrtag { struct chrtag {
public: public:
tagid_t tag; ///< Tag ID tagid_t tag; ///< Tag ID
protected: protected:
uint16_t chr_to; ///< Character end in \c data unsigned __int16 chr_to; ///< Character end in \c data
char_t data[]; ///< Character wchar_t data[]; ///< Character
private: private:
inline chrtag(_In_ const chrtag &other); inline chrtag(_In_ const chrtag &other);
@ -53,27 +54,27 @@ namespace ZRCola {
/// \param[in] tag Tag /// \param[in] tag Tag
/// ///
inline chrtag( inline chrtag(
_In_opt_z_count_(chr_len) const char_t *chr = NULL, _In_opt_z_count_(chr_len) const wchar_t *chr = NULL,
_In_opt_ size_t chr_len = 0, _In_opt_ size_t chr_len = 0,
_In_opt_ tagid_t tag = 0) _In_opt_ tagid_t tag = 0)
{ {
this->tag = tag; this->tag = tag;
this->chr_to = static_cast<uint16_t>(chr_len); this->chr_to = static_cast<unsigned __int16>(chr_len);
if (chr && chr_len) memcpy(this->data, chr, sizeof(char_t)*chr_len); if (chr && chr_len) memcpy(this->data, chr, sizeof(wchar_t)*chr_len);
} }
inline const char_t* chr () const { return data; }; inline const wchar_t* chr () const { return data; };
inline char_t* chr () { return data; }; inline wchar_t* chr () { return data; };
inline const char_t* chr_end() const { return data + chr_to; }; inline const wchar_t* chr_end() const { return data + chr_to; };
inline char_t* chr_end() { return data + chr_to; }; inline wchar_t* chr_end() { return data + chr_to; };
inline uint16_t chr_len() const { return chr_to; }; inline unsigned __int16 chr_len() const { return chr_to; };
}; };
#pragma pack(pop) #pragma pack(pop)
/// ///
/// Character Index /// Character Index
/// ///
class indexChr : public index<uint16_t, uint32_t, chrtag> class indexChr : public index<unsigned __int16, unsigned __int32, chrtag>
{ {
public: public:
/// ///
@ -81,7 +82,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexChr(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, chrtag>(h) {} indexChr(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, chrtag>(h) {}
/// ///
/// Compares two character tags by character (for searching) /// Compares two character tags by character (for searching)
@ -129,7 +130,7 @@ namespace ZRCola {
/// ///
/// Tag Index /// Tag Index
/// ///
class indexTag : public index<uint16_t, uint32_t, chrtag> class indexTag : public index<unsigned __int16, unsigned __int32, chrtag>
{ {
public: public:
/// ///
@ -137,7 +138,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexTag(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, chrtag>(h) {} indexTag(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, chrtag>(h) {}
/// ///
/// Compares two character tags by tag (for searching) /// Compares two character tags by tag (for searching)
@ -152,8 +153,8 @@ namespace ZRCola {
/// ///
virtual int compare(_In_ const chrtag &a, _In_ const chrtag &b) const virtual int compare(_In_ const chrtag &a, _In_ const chrtag &b) const
{ {
if (a.tag < b.tag) return -1; if (a.tag < b.tag) return -1;
if (a.tag > b.tag) return 1; else if (a.tag > b.tag) return 1;
return 0; return 0;
} }
@ -181,7 +182,7 @@ namespace ZRCola {
} }
} idxTag; ///< Tag index } idxTag; ///< Tag index
std::vector<uint16_t> data; ///< Character tags data std::vector<unsigned __int16> data; ///< Character tags data
public: public:
/// ///
@ -209,84 +210,13 @@ namespace ZRCola {
/// \param[in ] fn_abort Pointer to function to periodically test for search cancellation /// \param[in ] fn_abort Pointer to function to periodically test for search cancellation
/// \param[in ] cookie Cookie for \p fn_abort call /// \param[in ] cookie Cookie for \p fn_abort call
/// ///
bool Search(_In_ const std::map<tagid_t, uint16_t> &tags, _In_ const character_db &ch_db, _In_ const std::set<chrcatid_t> &cats, _Inout_ std::map<string_t, charrank_t> &hits, _In_opt_ bool (__cdecl *fn_abort)(void *cookie) = NULL, _In_opt_ void *cookie = NULL) const; bool Search(_In_ const std::map<tagid_t, unsigned __int16> &tags, _In_ const character_db &ch_db, _In_ const std::set<chrcatid_t> &cats, _Inout_ std::map<std::wstring, charrank_t> &hits, _In_opt_ bool (__cdecl *fn_abort)(void *cookie) = NULL, _In_opt_ void *cookie = NULL) const;
///
/// Writes character tag database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Character tag database
///
/// \returns The stream \p stream
///
friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const chrtag_db& db)
{
// Write character index.
if (stream.fail()) return stream;
stream << db.idxChr;
// Write tag index.
if (stream.fail()) return stream;
stream << db.idxTag;
// 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;
uint32_t count = (uint32_t)data_count;
stream.write((const char*)&count, sizeof(count));
// Write data.
if (stream.fail()) return stream;
stream.write((const char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads character tag database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Character tag database
///
/// \returns The stream \p stream
///
friend std::istream& operator >>(_In_ std::istream& stream, _Out_ chrtag_db& db)
{
// Read character index.
stream >> db.idxChr;
if (!stream.good()) return stream;
// Read tag index.
stream >> db.idxTag;
if (!stream.good()) return stream;
// Read data count.
uint32_t 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(uint16_t) * static_cast<std::streamsize>(count));
}
else
db.data.clear();
return stream;
}
}; };
typedef stdex::idrec::record<chrtag_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> chrtag_rec;
/// ///
/// Tag name database /// Tag name database
/// ///
@ -299,12 +229,12 @@ namespace ZRCola {
/// ///
struct tagname { struct tagname {
public: public:
tagid_t tag; ///< Tag ID tagid_t tag; ///< Tag ID
uint32_t locale; ///< Locale ID LCID locale; ///< Locale ID
protected: protected:
uint16_t name_to; ///< Tag name end in \c data unsigned __int16 name_to; ///< Tag name end in \c data
char_t data[]; ///< Tag name wchar_t data[]; ///< Tag name
private: private:
inline tagname(_In_ const tagname &other); inline tagname(_In_ const tagname &other);
@ -320,22 +250,22 @@ namespace ZRCola {
/// \param[in] name_len Number of UTF-16 characters in \p name /// \param[in] name_len Number of UTF-16 characters in \p name
/// ///
inline tagname( inline tagname(
_In_opt_ tagid_t tag = 0, _In_opt_ tagid_t tag = 0,
_In_opt_ uint32_t locale = 0, _In_opt_ LCID locale = MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), SORT_DEFAULT),
_In_opt_z_count_(name_len) const char_t *name = NULL, _In_opt_z_count_(name_len) const wchar_t *name = NULL,
_In_opt_ size_t name_len = 0) _In_opt_ size_t name_len = 0)
{ {
this->tag = tag; this->tag = tag;
this->locale = locale; this->locale = locale;
this->name_to = static_cast<uint16_t>(name_len); this->name_to = static_cast<unsigned __int16>(name_len);
if (name && name_len) memcpy(this->data, name, sizeof(char_t)*name_len); if (name && name_len) memcpy(this->data, name, sizeof(wchar_t)*name_len);
} }
inline const char_t* name () const { return data; }; inline const wchar_t* name () const { return data; };
inline char_t* name () { return data; }; inline wchar_t* name () { return data; };
inline const char_t* name_end() const { return data + name_to; }; inline const wchar_t* name_end() const { return data + name_to; };
inline char_t* name_end() { return data + name_to; }; inline wchar_t* name_end() { return data + name_to; };
inline uint16_t name_len() const { return name_to; }; inline unsigned __int16 name_len() const { return name_to; };
/// ///
/// Compares two names /// Compares two names
@ -355,26 +285,14 @@ namespace ZRCola {
/// The function does not treat \\0 characters as terminators for performance reasons. /// The function does not treat \\0 characters as terminators for performance reasons.
/// Therefore \p count_a and \p count_b must represent exact string lengths. /// Therefore \p count_a and \p count_b must represent exact string lengths.
/// ///
static inline int CompareName(_In_ uint32_t locale, _In_z_count_(count_a) const char_t *str_a, _In_ uint16_t count_a, _In_z_count_(count_b) const char_t *str_b, _In_ uint16_t count_b) static inline int CompareName(LCID locale, const wchar_t *str_a, unsigned __int16 count_a, const wchar_t *str_b, unsigned __int16 count_b)
{ {
#ifdef _WIN32
switch (::CompareString(locale, SORT_STRINGSORT | NORM_IGNORECASE, str_a, count_a, str_b, count_b)) { switch (::CompareString(locale, SORT_STRINGSORT | NORM_IGNORECASE, str_a, count_a, str_b, count_b)) {
case CSTR_LESS_THAN : return -1; case CSTR_LESS_THAN : return -1;
case CSTR_EQUAL : return 0; case CSTR_EQUAL : return 0;
case CSTR_GREATER_THAN: return 1; case CSTR_GREATER_THAN: return 1;
default : assert(0); return -1; default : assert(0); return -1;
} }
#else
assert(0); // TODO: 1. Should honour locale. 2. Should use ICU for lowercase conversion. 3. Should be UTF-16-aware.
string_t
a(str_a, count_a),
b(str_b, count_b);
auto tolower = [](char_t c){ return std::towlower(c); };
std::transform(a.begin(), a.end(), a.begin(), tolower);
std::transform(b.begin(), b.end(), b.begin(), tolower);
auto &coll = std::use_facet<std::collate<char_t>>(std::locale());
return coll.compare(&*a.cbegin(), &*a.cend(), &*b.cbegin(), &*b.cend());
#endif
} }
}; };
#pragma pack(pop) #pragma pack(pop)
@ -382,7 +300,7 @@ namespace ZRCola {
/// ///
/// Name index /// Name index
/// ///
class indexName : public index<uint16_t, uint32_t, tagname> class indexName : public index<unsigned __int16, unsigned __int32, tagname>
{ {
public: public:
/// ///
@ -391,7 +309,7 @@ namespace ZRCola {
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// \param[in] locale Locale used to perform tag name comparison /// \param[in] locale Locale used to perform tag name comparison
/// ///
indexName(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, tagname>(h) {} indexName(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, tagname>(h) {}
/// ///
/// Compares two tag names by locale and name (for searching) /// Compares two tag names by locale and name (for searching)
@ -406,8 +324,8 @@ namespace ZRCola {
/// ///
virtual int compare(_In_ const tagname &a, _In_ const tagname &b) const virtual int compare(_In_ const tagname &a, _In_ const tagname &b) const
{ {
if (a.locale < b.locale) return -1; if (a.locale < b.locale) return -1;
if (a.locale > b.locale) return 1; else if (a.locale > b.locale) return 1;
int r = tagname::CompareName(a.locale, a.name(), a.name_len(), b.name(), b.name_len()); int r = tagname::CompareName(a.locale, a.name(), a.name_len(), b.name(), b.name_len());
if (r != 0) return r; if (r != 0) return r;
@ -444,7 +362,7 @@ namespace ZRCola {
/// ///
/// Tag index /// Tag index
/// ///
class indexTag : public index<uint16_t, uint32_t, tagname> class indexTag : public index<unsigned __int16, unsigned __int32, tagname>
{ {
public: public:
/// ///
@ -453,7 +371,7 @@ namespace ZRCola {
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// \param[in] locale Locale used to perform tag name comparison /// \param[in] locale Locale used to perform tag name comparison
/// ///
indexTag(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, tagname>(h) {} indexTag(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, tagname>(h) {}
/// ///
/// Compares two tag names by tag (for searching) /// Compares two tag names by tag (for searching)
@ -468,17 +386,17 @@ namespace ZRCola {
/// ///
virtual int compare(_In_ const tagname &a, _In_ const tagname &b) const virtual int compare(_In_ const tagname &a, _In_ const tagname &b) const
{ {
if (a.locale < b.locale) return -1; if (a.locale < b.locale) return -1;
if (a.locale > b.locale) return 1; else if (a.locale > b.locale) return 1;
if (a.tag < b.tag) return -1; if (a.tag < b.tag) return -1;
if (a.tag > b.tag) return 1; else if (a.tag > b.tag) return 1;
return 0; return 0;
} }
} idxTag; ///< Tag index } idxTag; ///< Tag index
std::vector<uint16_t> data; ///< Tag data std::vector<unsigned __int16> data; ///< Tag data
public: public:
/// ///
@ -505,82 +423,161 @@ namespace ZRCola {
/// \param[in ] fn_abort Pointer to function to periodically test for search cancellation /// \param[in ] fn_abort Pointer to function to periodically test for search cancellation
/// \param[in ] cookie Cookie for \p fn_abort call /// \param[in ] cookie Cookie for \p fn_abort call
/// ///
bool Search(_In_z_ const char_t *str, _In_ uint32_t locale, _Inout_ std::map<tagid_t, uint16_t> &hits, _In_opt_ bool (__cdecl *fn_abort)(void *cookie) = NULL, _In_opt_ void *cookie = NULL) const; bool Search(_In_z_ const wchar_t *str, _In_ LCID locale, _Inout_ std::map<tagid_t, unsigned __int16> &hits, _In_opt_ bool (__cdecl *fn_abort)(void *cookie) = NULL, _In_opt_ void *cookie = NULL) const;
///
/// Writes tag database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Tag database
///
/// \returns The stream \p stream
///
friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const tagname_db& db)
{
// Write name index.
if (stream.fail()) return stream;
stream << db.idxName;
// Write tag index.
if (stream.fail()) return stream;
stream << db.idxTag;
// 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;
uint32_t count = (uint32_t)data_count;
stream.write((const char*)&count, sizeof(count));
// Write data.
if (stream.fail()) return stream;
stream.write((const char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads tag database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Tag database
///
/// \returns The stream \p stream
///
friend std::istream& operator >>(_In_ std::istream& stream, _Out_ tagname_db& db)
{
// Read name index.
stream >> db.idxName;
if (!stream.good()) return stream;
// Read tag index.
stream >> db.idxTag;
if (!stream.good()) return stream;
// Read data count.
uint32_t 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(uint16_t) * static_cast<std::streamsize>(count));
}
else
db.data.clear();
return stream;
}
}; };
typedef stdex::idrec::record<tagname_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> tagname_rec;
}; };
const ZRCola::recordid_t ZRCola::chrtag_rec ::id = *(ZRCola::recordid_t*)"C-T";
const ZRCola::recordid_t ZRCola::tagname_rec::id = *(ZRCola::recordid_t*)"TGN";
///
/// Writes character tag database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Character tag database
///
/// \returns The stream \p stream
///
inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::chrtag_db &db)
{
// Write character index.
if (stream.fail()) return stream;
stream << db.idxChr;
// Write tag index.
if (stream.fail()) return stream;
stream << db.idxTag;
// 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)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads character tag database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Character tag database
///
/// \returns The stream \p stream
///
inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::chrtag_db &db)
{
// Read character index.
stream >> db.idxChr;
if (!stream.good()) return stream;
// Read tag index.
stream >> db.idxTag;
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)*static_cast<std::streamsize>(count));
} else
db.data.clear();
return stream;
}
///
/// Writes tag database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Tag database
///
/// \returns The stream \p stream
///
inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::tagname_db &db)
{
// Write name index.
if (stream.fail()) return stream;
stream << db.idxName;
// Write tag index.
if (stream.fail()) return stream;
stream << db.idxTag;
// 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)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads tag database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Tag database
///
/// \returns The stream \p stream
///
inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::tagname_db &db)
{
// Read name index.
stream >> db.idxName;
if (!stream.good()) return stream;
// Read tag index.
stream >> db.idxTag;
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)*static_cast<std::streamsize>(count));
} else
db.data.clear();
return stream;
}
#pragma warning(pop) #pragma warning(pop)

View File

@ -8,10 +8,7 @@
#include "common.h" #include "common.h"
#include "language.h" #include "language.h"
namespace ZRCola { #include <stdex/idrec>
class translation_db;
}
#include <algorithm> #include <algorithm>
#include <istream> #include <istream>
#include <ostream> #include <ostream>
@ -48,12 +45,12 @@ namespace ZRCola {
/// ///
/// Translation set ID /// Translation set ID
/// ///
typedef uint16_t transetid_t; typedef unsigned __int16 transetid_t;
/// ///
/// Translation sequence ID /// Translation sequence ID
/// ///
typedef uint16_t transeqid_t; typedef unsigned __int16 transeqid_t;
/// ///
/// Translation database /// Translation database
@ -67,14 +64,14 @@ namespace ZRCola {
/// ///
struct translation { struct translation {
public: public:
transetid_t set; ///< Translation set ID transetid_t set; ///< Translation set ID
uint16_t dst_rank; ///< Destination character rank unsigned __int16 dst_rank; ///< Destination character rank
uint16_t src_rank; ///< Source character rank unsigned __int16 src_rank; ///< Source character rank
protected: protected:
uint16_t dst_to; ///< Destination character end in \c data unsigned __int16 dst_to; ///< Destination character end in \c data
uint16_t src_to; ///< Source string end in \c data unsigned __int16 src_to; ///< Source string end in \c data
char_t data[]; ///< Destination string and source character wchar_t data[]; ///< Destination string and source character
private: private:
inline translation(_In_ const translation &other); inline translation(_In_ const translation &other);
@ -93,41 +90,41 @@ namespace ZRCola {
/// \param[in] src_len Number of UTF-16 characters in \p src /// \param[in] src_len Number of UTF-16 characters in \p src
/// ///
inline translation( inline translation(
_In_opt_ transetid_t set = 0, _In_opt_ transetid_t set = 0,
_In_opt_ uint16_t dst_rank = 0, _In_opt_ unsigned __int16 dst_rank = 0,
_In_opt_z_count_(dst_len) const char_t *dst = NULL, _In_opt_z_count_(dst_len) const wchar_t *dst = NULL,
_In_opt_ size_t dst_len = 0, _In_opt_ size_t dst_len = 0,
_In_opt_ uint16_t src_rank = 0, _In_opt_ unsigned __int16 src_rank = 0,
_In_opt_z_count_(src_len) const char_t *src = NULL, _In_opt_z_count_(src_len) const wchar_t *src = NULL,
_In_opt_ size_t src_len = 0) _In_opt_ size_t src_len = 0)
{ {
this->set = set; this->set = set;
this->dst_rank = dst_rank; this->dst_rank = dst_rank;
this->src_rank = src_rank; this->src_rank = src_rank;
this->dst_to = static_cast<uint16_t>(dst_len); this->dst_to = static_cast<unsigned __int16>(dst_len);
if (dst && dst_len) memcpy(this->data, dst, sizeof(char_t)*dst_len); if (dst && dst_len) memcpy(this->data, dst, sizeof(wchar_t)*dst_len);
this->src_to = static_cast<uint16_t>(this->dst_to + src_len); this->src_to = static_cast<unsigned __int16>(this->dst_to + src_len);
if (src && src_len) memcpy(this->data + this->dst_to, src, sizeof(char_t)*src_len); if (src && src_len) memcpy(this->data + this->dst_to, src, sizeof(wchar_t)*src_len);
} }
inline const char_t* dst () const { return data; }; inline const wchar_t* dst () const { return data; };
inline char_t* dst () { return data; }; inline wchar_t* dst () { return data; };
inline const char_t* dst_end() const { return data + dst_to; }; inline const wchar_t* dst_end() const { return data + dst_to; };
inline char_t* dst_end() { return data + dst_to; }; inline wchar_t* dst_end() { return data + dst_to; };
inline uint16_t dst_len() const { return dst_to; }; inline unsigned __int16 dst_len() const { return dst_to; };
inline char_t dst_at(_In_ size_t i) const inline wchar_t dst_at(_In_ size_t i) const
{ {
return i < dst_to ? data[i] : 0; return i < dst_to ? data[i] : 0;
} }
inline const char_t* src () const { return data + dst_to; }; inline const wchar_t* src () const { return data + dst_to; };
inline char_t* src () { return data + dst_to; }; inline wchar_t* src () { return data + dst_to; };
inline const char_t* src_end() const { return data + src_to; }; inline const wchar_t* src_end() const { return data + src_to; };
inline char_t* src_end() { return data + src_to; }; inline wchar_t* src_end() { return data + src_to; };
inline uint16_t src_len() const { return src_to - dst_to; }; inline unsigned __int16 src_len() const { return src_to - dst_to; };
inline char_t src_at(_In_ size_t i) const inline wchar_t src_at(_In_ size_t i) const
{ {
size_t ii = i + dst_to; // absolute index size_t ii = i + dst_to; // absolute index
return ii < src_to ? data[ii] : 0; return ii < src_to ? data[ii] : 0;
@ -138,7 +135,7 @@ namespace ZRCola {
/// ///
/// Translation index /// Translation index
/// ///
class indexSrc : public index<uint16_t, uint32_t, translation> class indexSrc : public index<unsigned __int16, unsigned __int32, translation>
{ {
public: public:
/// ///
@ -146,7 +143,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexSrc(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, translation>(h) {} indexSrc(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, translation>(h) {}
/// ///
/// Compares two transformations by string (for searching) /// Compares two transformations by string (for searching)
@ -161,8 +158,8 @@ namespace ZRCola {
/// ///
virtual int compare(_In_ const translation &a, _In_ const translation &b) const virtual int compare(_In_ const translation &a, _In_ const translation &b) const
{ {
if (a.set < b.set) return -1; if (a.set < b.set) return -1;
if (a.set > b.set) return +1; else if (a.set > b.set) return +1;
int r = ZRCola::CompareString(a.src(), a.src_len(), b.src(), b.src_len()); int r = ZRCola::CompareString(a.src(), a.src_len(), b.src(), b.src_len());
if (r != 0) return r; if (r != 0) return r;
@ -203,7 +200,7 @@ namespace ZRCola {
/// ///
/// Inverse translation index /// Inverse translation index
/// ///
class indexDst : public index<uint16_t, uint32_t, translation> class indexDst : public index<unsigned __int16, unsigned __int32, translation>
{ {
public: public:
/// ///
@ -211,7 +208,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexDst(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, translation>(h) {} indexDst(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, translation>(h) {}
/// ///
/// Compares two transformations by character (for searching) /// Compares two transformations by character (for searching)
@ -226,8 +223,8 @@ namespace ZRCola {
/// ///
virtual int compare(_In_ const translation &a, _In_ const translation &b) const virtual int compare(_In_ const translation &a, _In_ const translation &b) const
{ {
if (a.set < b.set) return -1; if (a.set < b.set) return -1;
if (a.set > b.set) return +1; else if (a.set > b.set) return +1;
int r = ZRCola::CompareString(a.dst(), a.dst_len(), b.dst(), b.dst_len()); int r = ZRCola::CompareString(a.dst(), a.dst_len(), b.dst(), b.dst_len());
if (r != 0) return r; if (r != 0) return r;
@ -265,7 +262,7 @@ namespace ZRCola {
} idxDst; ///< Inverse translation index } idxDst; ///< Inverse translation index
std::vector<uint16_t> data; ///< Transformation data std::vector<unsigned __int16> data; ///< Transformation data
public: public:
/// ///
@ -292,7 +289,7 @@ namespace ZRCola {
/// \param[out] output Output string (UTF-16) /// \param[out] output Output string (UTF-16)
/// \param[out] map The vector of source to destination index mappings (optional) /// \param[out] map The vector of source to destination index mappings (optional)
/// ///
void Translate(_In_ transetid_t set, _In_z_count_(inputMax) const char_t* input, _In_ size_t inputMax, _Out_ string_t &output, _Out_opt_ std::vector<mapping>* map = NULL) const; void Translate(_In_ transetid_t set, _In_z_count_(inputMax) const wchar_t* input, _In_ size_t inputMax, _Out_ std::wstring &output, _Out_opt_ std::vector<mapping>* map = NULL) const;
/// ///
/// Inverse translates string /// Inverse translates string
@ -303,7 +300,7 @@ namespace ZRCola {
/// \param[out] output Output string (UTF-16) /// \param[out] output Output string (UTF-16)
/// \param[out] map The vector of source to destination index mappings (optional) /// \param[out] map The vector of source to destination index mappings (optional)
/// ///
inline void TranslateInv(_In_ transetid_t set, _In_z_count_(inputMax) const char_t* input, _In_ size_t inputMax, _Out_ string_t &output, _Out_opt_ std::vector<mapping>* map = NULL) const inline void TranslateInv(_In_ transetid_t set, _In_z_count_(inputMax) const wchar_t* input, _In_ size_t inputMax, _Out_ std::wstring &output, _Out_opt_ std::vector<mapping>* map = NULL) const
{ {
TranslateInv(set, input, inputMax, NULL, langid_t::blank, output, map); TranslateInv(set, input, inputMax, NULL, langid_t::blank, output, map);
} }
@ -319,84 +316,13 @@ namespace ZRCola {
/// \param[out] output Output string (UTF-16) /// \param[out] output Output string (UTF-16)
/// \param[out] map The vector of source to destination index mappings (optional) /// \param[out] map The vector of source to destination index mappings (optional)
/// ///
void TranslateInv(_In_ transetid_t set, _In_z_count_(inputMax) const char_t* input, _In_ size_t inputMax, _In_opt_ const langchar_db *lc_db, _In_opt_ langid_t lang, _Out_ string_t &output, _Out_opt_ std::vector<mapping>* map = NULL) const; void TranslateInv(_In_ transetid_t set, _In_z_count_(inputMax) const wchar_t* input, _In_ size_t inputMax, _In_opt_ const langchar_db *lc_db, _In_opt_ langid_t lang, _Out_ std::wstring &output, _Out_opt_ std::vector<mapping>* map = NULL) const;
///
/// Writes translation database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Translation database
///
/// \returns The stream \p stream
///
friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::translation_db& db)
{
// Write translation index.
if (stream.fail()) return stream;
stream << db.idxSrc;
// Write inverse translation index.
if (stream.fail()) return stream;
stream << db.idxDst;
// 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;
uint32_t count = (uint32_t)data_count;
stream.write((const char*)&count, sizeof(count));
// Write data.
if (stream.fail()) return stream;
stream.write((const char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads translation database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Translation database
///
/// \returns The stream \p stream
///
friend std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::translation_db& db)
{
// Read translation index.
stream >> db.idxSrc;
if (!stream.good()) return stream;
// Read inverse translation index.
stream >> db.idxDst;
if (!stream.good()) return stream;
// Read data count.
uint32_t 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(uint16_t) * static_cast<std::streamsize>(count));
}
else
db.data.clear();
return stream;
}
}; };
typedef stdex::idrec::record<translation_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> translation_rec;
/// ///
/// Translation set database /// Translation set database
/// ///
@ -409,12 +335,12 @@ namespace ZRCola {
/// ///
struct transet { struct transet {
public: public:
transetid_t set; ///< Translation set ID transetid_t set; ///< Translation set ID
protected: protected:
uint16_t src_to; ///< Source name end in \c data unsigned __int16 src_to; ///< Source name end in \c data
uint16_t dst_to; ///< Destination name end in \c data unsigned __int16 dst_to; ///< Sestination name end in \c data
char_t data[]; ///< Source and destination names wchar_t data[]; ///< Source and destination names
private: private:
inline transet(_In_ const transet &other); inline transet(_In_ const transet &other);
@ -432,36 +358,36 @@ namespace ZRCola {
/// ///
inline transet( inline transet(
_In_opt_ transetid_t set = 0, _In_opt_ transetid_t set = 0,
_In_opt_z_count_(src_len) const char_t *src = NULL, _In_opt_z_count_(src_len) const wchar_t *src = NULL,
_In_opt_ size_t src_len = 0, _In_opt_ size_t src_len = 0,
_In_opt_z_count_(dst_len) const char_t *dst = NULL, _In_opt_z_count_(dst_len) const wchar_t *dst = NULL,
_In_opt_ size_t dst_len = 0) _In_opt_ size_t dst_len = 0)
{ {
this->set = set; this->set = set;
this->src_to = static_cast<uint16_t>(src_len); this->src_to = static_cast<unsigned __int16>(src_len);
if (src && src_len) memcpy(this->data, src, sizeof(char_t)*src_len); if (src && src_len) memcpy(this->data, src, sizeof(wchar_t)*src_len);
this->dst_to = static_cast<uint16_t>(this->src_to + dst_len); this->dst_to = static_cast<unsigned __int16>(this->src_to + dst_len);
if (dst && dst_len) memcpy(this->data + this->src_to, dst, sizeof(char_t)*dst_len); if (dst && dst_len) memcpy(this->data + this->src_to, dst, sizeof(wchar_t)*dst_len);
} }
inline const char_t* src () const { return data; }; inline const wchar_t* src () const { return data; };
inline char_t* src () { return data; }; inline wchar_t* src () { return data; };
inline const char_t* src_end() const { return data + src_to; }; inline const wchar_t* src_end() const { return data + src_to; };
inline char_t* src_end() { return data + src_to; }; inline wchar_t* src_end() { return data + src_to; };
inline uint16_t src_len() const { return src_to; }; inline unsigned __int16 src_len() const { return src_to; };
inline const char_t* dst () const { return data + src_to; }; inline const wchar_t* dst () const { return data + src_to; };
inline char_t* dst () { return data + src_to; }; inline wchar_t* dst () { return data + src_to; };
inline const char_t* dst_end() const { return data + dst_to; }; inline const wchar_t* dst_end() const { return data + dst_to; };
inline char_t* dst_end() { return data + dst_to; }; inline wchar_t* dst_end() { return data + dst_to; };
inline uint16_t dst_len() const { return dst_to - src_to; }; inline unsigned __int16 dst_len() const { return dst_to - src_to; };
}; };
#pragma pack(pop) #pragma pack(pop)
/// ///
/// Translation set index /// Translation set index
/// ///
class indexTranSet : public index<uint16_t, uint32_t, transet> class indexTranSet : public index<unsigned __int16, unsigned __int32, transet>
{ {
public: public:
/// ///
@ -469,7 +395,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexTranSet(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, transet>(h) {} indexTranSet(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, transet>(h) {}
/// ///
/// Compares two translation sets by ID (for searching) /// Compares two translation sets by ID (for searching)
@ -484,14 +410,14 @@ namespace ZRCola {
/// ///
virtual int compare(_In_ const transet &a, _In_ const transet &b) const virtual int compare(_In_ const transet &a, _In_ const transet &b) const
{ {
if (a.set < b.set) return -1; if (a.set < b.set) return -1;
if (a.set > b.set) return 1; else if (a.set > b.set) return 1;
return 0; return 0;
} }
} idxTranSet; ///< Translation set index } idxTranSet; ///< Translation set index
std::vector<uint16_t> data; ///< Translation set data std::vector<unsigned __int16> data; ///< Translation set data
public: public:
/// ///
@ -507,75 +433,12 @@ namespace ZRCola {
idxTranSet.clear(); idxTranSet.clear();
data .clear(); data .clear();
} }
///
/// Writes translation set database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Translation set database
///
/// \returns The stream \p stream
///
friend 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;
uint32_t count = (uint32_t)data_count;
stream.write((const char*)&count, sizeof(count));
// Write data.
if (stream.fail()) return stream;
stream.write((const char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(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
///
friend 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.
uint32_t 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(uint16_t) * static_cast<std::streamsize>(count));
}
else
db.data.clear();
return stream;
}
}; };
typedef stdex::idrec::record<transet_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> transet_rec;
/// ///
/// Translation sequence database /// Translation sequence database
/// ///
@ -588,13 +451,13 @@ namespace ZRCola {
/// ///
struct transeq { struct transeq {
public: public:
transeqid_t seq; ///< Translation sequence ID transeqid_t seq; ///< Translation sequence ID
uint16_t rank; ///< Translation sequence rank unsigned __int16 rank; ///< Translation sequence rank
protected: protected:
uint16_t name_to; ///< Translation sequence name end in \c data unsigned __int16 name_to; ///< Translation sequence name end in \c data
uint16_t sets_to; ///< Translation sequence sets end in \c data unsigned __int16 sets_to; ///< Translation sequence sets end in \c data
char_t data[]; ///< Translation sequence name and sets wchar_t data[]; ///< Translation sequence name and sets
private: private:
inline transeq(_In_ const transeq &other); inline transeq(_In_ const transeq &other);
@ -608,43 +471,43 @@ namespace ZRCola {
/// \param[in] rank Translation sequence rank /// \param[in] rank Translation sequence rank
/// \param[in] name Translation sequence source /// \param[in] name Translation sequence source
/// \param[in] name_len Number of UTF-16 characters in \p src /// \param[in] name_len Number of UTF-16 characters in \p src
/// \param[in] sets Translation sequence destination /// \param[in] sets Translation sequence destination
/// \param[in] sets_len Number of UTF-16 characters in \p sets /// \param[in] sets_len Number of UTF-16 characters in \p sets
/// ///
inline transeq( inline transeq(
_In_opt_ transeqid_t seq = 0, _In_opt_ transeqid_t seq = 0,
_In_opt_ uint16_t rank = 0, _In_opt_ unsigned __int16 rank = 0,
_In_opt_z_count_(name_len) const char_t *name = NULL, _In_opt_z_count_(name_len) const wchar_t *name = NULL,
_In_opt_ size_t name_len = 0, _In_opt_ size_t name_len = 0,
_In_opt_count_ (sets_len) const transetid_t *sets = NULL, _In_opt_count_ (sets_len) const transetid_t *sets = NULL,
_In_opt_ size_t sets_len = 0) _In_opt_ size_t sets_len = 0)
{ {
this->seq = seq; this->seq = seq;
this->rank = rank; this->rank = rank;
this->name_to = static_cast<uint16_t>(name_len); this->name_to = static_cast<unsigned __int16>(name_len);
if (name && name_len) memcpy(this->data, name, sizeof(char_t)*name_len); if (name && name_len) memcpy(this->data, name, sizeof(wchar_t)*name_len);
this->sets_to = static_cast<uint16_t>(this->name_to + sets_len); this->sets_to = static_cast<unsigned __int16>(this->name_to + sets_len);
if (sets && sets_len) memcpy(this->data + this->name_to, sets, sizeof(transetid_t)*sets_len); if (sets && sets_len) memcpy(this->data + this->name_to, sets, sizeof(transetid_t)*sets_len);
} }
inline const char_t* name () const { return data; }; inline const wchar_t* name () const { return data; };
inline char_t* name () { return data; }; inline wchar_t* name () { return data; };
inline const char_t* name_end() const { return data + name_to; }; inline const wchar_t* name_end() const { return data + name_to; };
inline char_t* name_end() { return data + name_to; }; inline wchar_t* name_end() { return data + name_to; };
inline uint16_t name_len() const { return name_to; }; inline unsigned __int16 name_len() const { return name_to; };
inline const transetid_t* sets () const { return reinterpret_cast<const transetid_t*>(data + name_to); }; inline const transetid_t* sets () const { return reinterpret_cast<const transetid_t*>(data + name_to); };
inline transetid_t* sets () { return reinterpret_cast< transetid_t*>(data + name_to); }; inline transetid_t* sets () { return reinterpret_cast< transetid_t*>(data + name_to); };
inline const transetid_t* sets_end() const { return reinterpret_cast<const transetid_t*>(data + sets_to); }; inline const transetid_t* sets_end() const { return reinterpret_cast<const transetid_t*>(data + sets_to); };
inline transetid_t* sets_end() { return reinterpret_cast< transetid_t*>(data + sets_to); }; inline transetid_t* sets_end() { return reinterpret_cast< transetid_t*>(data + sets_to); };
inline uint16_t sets_len() const { return sets_to - name_to; }; inline unsigned __int16 sets_len() const { return sets_to - name_to; };
}; };
#pragma pack(pop) #pragma pack(pop)
/// ///
/// Translation sequence index /// Translation sequence index
/// ///
class indexTranSeq : public index<uint16_t, uint32_t, transeq> class indexTranSeq : public index<unsigned __int16, unsigned __int32, transeq>
{ {
public: public:
/// ///
@ -652,7 +515,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexTranSeq(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, transeq>(h) {} indexTranSeq(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, transeq>(h) {}
/// ///
/// Compares two translation sequences by ID (for searching) /// Compares two translation sequences by ID (for searching)
@ -667,8 +530,8 @@ namespace ZRCola {
/// ///
virtual int compare(_In_ const transeq &a, _In_ const transeq &b) const virtual int compare(_In_ const transeq &a, _In_ const transeq &b) const
{ {
if (a.seq < b.seq) return -1; if (a.seq < b.seq) return -1;
if (a.seq > b.seq) return 1; else if (a.seq > b.seq) return 1;
return 0; return 0;
} }
@ -677,7 +540,7 @@ namespace ZRCola {
/// ///
/// Rank index /// Rank index
/// ///
class indexRank : public index<uint16_t, uint32_t, transeq> class indexRank : public index<unsigned __int16, unsigned __int32, transeq>
{ {
public: public:
/// ///
@ -685,7 +548,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexRank(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, transeq>(h) {} indexRank(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, transeq>(h) {}
/// ///
/// Compares two translation sets by rank (for searching) /// Compares two translation sets by rank (for searching)
@ -700,8 +563,8 @@ namespace ZRCola {
/// ///
virtual int compare(_In_ const transeq &a, _In_ const transeq &b) const virtual int compare(_In_ const transeq &a, _In_ const transeq &b) const
{ {
if (a.rank < b.rank) return -1; if (a.rank < b.rank) return -1;
if (a.rank > b.rank) return +1; else if (a.rank > b.rank) return +1;
return 0; return 0;
} }
@ -722,12 +585,19 @@ namespace ZRCola {
if (a.rank < b.rank) return -1; if (a.rank < b.rank) return -1;
else if (a.rank > b.rank) return +1; else if (a.rank > b.rank) return +1;
auto &coll = std::use_facet<std::collate<char_t>>(std::locale()); unsigned __int16
return coll.compare(a.name(), a.name_end(), b.name(), b.name_end()); a_name_len = a.name_len(),
b_name_len = b.name_len();
int r = _wcsncoll(a.name(), b.name(), std::min<unsigned __int16>(a_name_len, b_name_len));
if (r != 0) return r;
if (a_name_len < b_name_len) return -1;
else if (a_name_len > b_name_len) return +1;
return 0;
} }
} idxRank; ///< Rank index } idxRank; ///< Rank index
std::vector<uint16_t> data; ///< Translation sequence data std::vector<unsigned __int16> data; ///< Translation sequence data
public: public:
/// ///
@ -744,81 +614,226 @@ namespace ZRCola {
idxRank .clear(); idxRank .clear();
data .clear(); data .clear();
} }
///
/// Writes translation sequence database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Translation sequence database
///
/// \returns The stream \p stream
///
friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::transeq_db& db)
{
// Write translation sequence index.
if (stream.fail()) return stream;
stream << db.idxTranSeq;
// Write rank index.
if (stream.fail()) return stream;
stream << db.idxRank;
// 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;
uint32_t count = (uint32_t)data_count;
stream.write((const char*)&count, sizeof(count));
// Write data.
if (stream.fail()) return stream;
stream.write((const char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads translation sequence database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Translation sequence database
///
/// \returns The stream \p stream
///
friend std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::transeq_db& db)
{
// Read translation sequence index.
stream >> db.idxTranSeq;
if (!stream.good()) return stream;
// Read rank index.
stream >> db.idxRank;
if (!stream.good()) return stream;
// Read data count.
uint32_t 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(uint16_t) * static_cast<std::streamsize>(count));
}
else
db.data.clear();
return stream;
}
}; };
typedef stdex::idrec::record<transeq_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> transeq_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";
const ZRCola::recordid_t ZRCola::transeq_rec ::id = *(ZRCola::recordid_t*)"TSQ";
///
/// Writes translation database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Translation database
///
/// \returns The stream \p stream
///
inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::translation_db &db)
{
// Write translation index.
if (stream.fail()) return stream;
stream << db.idxSrc;
// Write inverse translation index.
if (stream.fail()) return stream;
stream << db.idxDst;
// 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)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads translation database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Translation database
///
/// \returns The stream \p stream
///
inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::translation_db &db)
{
// Read translation index.
stream >> db.idxSrc;
if (!stream.good()) return stream;
// Read inverse translation index.
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)*static_cast<std::streamsize>(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)*static_cast<std::streamsize>(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.
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)*static_cast<std::streamsize>(count));
} else
db.data.clear();
return stream;
}
///
/// Writes translation sequence database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Translation sequence database
///
/// \returns The stream \p stream
///
inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::transeq_db &db)
{
// Write translation sequence index.
if (stream.fail()) return stream;
stream << db.idxTranSeq;
// Write rank index.
if (stream.fail()) return stream;
stream << db.idxRank;
// 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)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads translation sequence database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Translation sequence database
///
/// \returns The stream \p stream
///
inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::transeq_db &db)
{
// Read translation sequence index.
stream >> db.idxTranSeq;
if (!stream.good()) return stream;
// Read rank index.
stream >> db.idxRank;
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)*static_cast<std::streamsize>(count));
} else
db.data.clear();
return stream;
}
#pragma warning(pop) #pragma warning(pop)

View File

@ -1 +0,0 @@
/libZRCola.a

View File

@ -1,3 +0,0 @@
/*.d
/*.gch
/*.o

View File

@ -9,29 +9,7 @@
const ZRCola::chrcatid_t ZRCola::chrcatid_t::blank = {}; const ZRCola::chrcatid_t ZRCola::chrcatid_t::blank = {};
#ifndef _WIN32 bool ZRCola::character_db::Search(_In_z_ const wchar_t *str, _In_ const std::set<chrcatid_t> &cats, _Inout_ std::map<std::wstring, charrank_t> &hits, _Inout_ std::map<std::wstring, charrank_t> &hits_sub, _In_opt_ bool (__cdecl *fn_abort)(void *cookie), _In_opt_ void *cookie) const
_Use_decl_annotations_
size_t ZRCola::wcslen(const char_t *str)
{
for (size_t i = 0; ; ++i)
if (!str[i])
return i;
}
_Use_decl_annotations_
size_t ZRCola::wcsnlen(const char_t *str, size_t count)
{
for (size_t i = 0; ; ++i)
if (i >= count || !str[i])
return i;
}
#endif
_Use_decl_annotations_
bool ZRCola::character_db::Search(const char_t *str, const std::set<chrcatid_t> &cats, std::map<string_t, charrank_t> &hits, std::map<string_t, charrank_t> &hits_sub, bool (__cdecl *fn_abort)(void *cookie), void *cookie) const
{ {
assert(str); assert(str);
@ -49,14 +27,14 @@ bool ZRCola::character_db::Search(const char_t *str, const std::set<chrcatid_t>
} }
// Get term. // Get term.
string_t term; std::wstring term;
if (*str == u'"') { if (*str == L'"') {
const auto *str_end = ++str; const wchar_t *str_end = ++str;
for (;;) { for (;;) {
if (*str_end == 0) { if (*str_end == 0) {
term.assign(str, str_end); term.assign(str, str_end);
break; break;
} else if (*str_end == u'"') { } else if (*str_end == L'"') {
term.assign(str, str_end); term.assign(str, str_end);
str_end++; str_end++;
break; break;
@ -65,7 +43,7 @@ bool ZRCola::character_db::Search(const char_t *str, const std::set<chrcatid_t>
} }
str = str_end; str = str_end;
} else { } else {
const auto *str_end = str + 1; const wchar_t *str_end = str + 1;
for (; *str_end && !iswspace(*str_end); str_end++); for (; *str_end && !iswspace(*str_end); str_end++);
term.assign(str, str_end); term.assign(str, str_end);
str = str_end; str = str_end;
@ -79,7 +57,7 @@ bool ZRCola::character_db::Search(const char_t *str, const std::set<chrcatid_t>
if (fn_abort && fn_abort(cookie)) return false; if (fn_abort && fn_abort(cookie)) return false;
const char_t *val; const wchar_t *val;
size_t val_len; size_t val_len;
if (idxDsc.find(term.c_str(), term.size(), &val, &val_len)) { if (idxDsc.find(term.c_str(), term.size(), &val, &val_len)) {
@ -88,7 +66,7 @@ bool ZRCola::character_db::Search(const char_t *str, const std::set<chrcatid_t>
if (fn_abort && fn_abort(cookie)) return false; if (fn_abort && fn_abort(cookie)) return false;
j = wcsnlen(val + i, val_len - i); j = wcsnlen(val + i, val_len - i);
if (cats.find(GetCharCat(val + i, j)) != cats.end()) { if (cats.find(GetCharCat(val + i, j)) != cats.end()) {
string_t c(val + i, j); std::wstring c(val + i, j);
auto idx = hits.find(c); auto idx = hits.find(c);
if (idx == hits.end()) { if (idx == hits.end()) {
// New character. // New character.
@ -107,7 +85,7 @@ bool ZRCola::character_db::Search(const char_t *str, const std::set<chrcatid_t>
if (fn_abort && fn_abort(cookie)) return false; if (fn_abort && fn_abort(cookie)) return false;
j = wcsnlen(val + i, val_len - i); j = wcsnlen(val + i, val_len - i);
if (cats.find(GetCharCat(val + i, j)) != cats.end()) { if (cats.find(GetCharCat(val + i, j)) != cats.end()) {
string_t c(val + i, j); std::wstring c(val + i, j);
auto idx = hits_sub.find(c); auto idx = hits_sub.find(c);
if (idx == hits_sub.end()) { if (idx == hits_sub.end()) {
// New character. // New character.

View File

@ -7,78 +7,3 @@
const ZRCola::langid_t ZRCola::langid_t::blank = {}; const ZRCola::langid_t ZRCola::langid_t::blank = {};
_Use_decl_annotations_
int ZRCola::CompareString(const char_t* str_a, size_t count_a, const char_t* str_b, size_t count_b)
{
for (size_t i = 0; ; i++) {
if (i >= count_a && i >= count_b) return 0;
else if (i >= count_a && i < count_b) return -1;
else if (i < count_a && i >= count_b) return +1;
else if (str_a[i] < str_b[i]) return -1;
else if (str_a[i] > str_b[i]) return +1;
}
}
_Use_decl_annotations_
inline std::string ZRCola::GetUnicodeDumpA(const char_t* str, size_t count, const char* sep)
{
std::string out;
size_t sep_len = strlen(sep);
size_t dump_len_max = sep_len + 4 + 1;
char* dump;
std::unique_ptr<char[]> dump_obj(dump = new char[dump_len_max]);
if (count && str[0]) {
size_t i = 0;
static const char error[] = "????";
int n = snprintf(dump, dump_len_max, "%04X", str[i++]);
if (n >= 0)
out.insert(out.end(), dump, dump + n);
else
out.insert(out.end(), error, error + std::size(error) - 1);
while (i < count && str[i]) {
n = snprintf(dump, dump_len_max, "%s%04X", sep, str[i++]);
if (n >= 0)
out.insert(out.end(), dump, dump + n);
else {
out.insert(out.end(), sep, sep + sep_len);
out.insert(out.end(), error, error + std::size(error) - 1);
}
}
}
return out;
}
_Use_decl_annotations_
std::wstring ZRCola::GetUnicodeDumpW(const char_t* str, size_t count, const wchar_t* sep)
{
std::wstring out;
size_t sep_len = ::wcslen(sep);
size_t dump_len_max = sep_len + 4 + 1;
wchar_t* dump;
std::unique_ptr<wchar_t[]> dump_obj(dump = new wchar_t[dump_len_max]);
if (count && str[0]) {
size_t i = 0;
static const wchar_t error[] = L"????";
int n = swprintf(dump, dump_len_max, L"%04X", str[i++]);
if (n >= 0)
out.insert(out.end(), dump, dump + n);
else
out.insert(out.end(), error, error + std::size(error) - 1);
while (i < count && str[i]) {
n = swprintf(dump, dump_len_max, L"%s%04X", sep, str[i++]);
if (n >= 0)
out.insert(out.end(), dump, dump + n);
else {
out.insert(out.end(), sep, sep + sep_len);
out.insert(out.end(), error, error + std::size(error) - 1);
}
}
}
return out;
}

View File

@ -6,7 +6,7 @@
#include "pch.h" #include "pch.h"
_Use_decl_annotations_ _Use_decl_annotations_
void ZRCola::highlight_db::Highlight(const char_t* input, size_t inputMax, std::function<void (hlghtsetid_t set, size_t start, size_t end)> callback) const void ZRCola::highlight_db::Highlight(const wchar_t* input, size_t inputMax, std::function<void (hlghtsetid_t set, size_t start, size_t end)> callback) const
{ {
size_t start = 0; size_t start = 0;
hlghtsetid_t set = ZRCOLA_HLGHTSETID_DEFAULT; hlghtsetid_t set = ZRCOLA_HLGHTSETID_DEFAULT;
@ -15,7 +15,7 @@ void ZRCola::highlight_db::Highlight(const char_t* input, size_t inputMax, std::
// Find the longest matching highlight at i-th character. // Find the longest matching highlight at i-th character.
size_t l_match = (size_t)-1; size_t l_match = (size_t)-1;
for (size_t l = 0, r = idxChr.size(), ii = i, j = 0; ii < inputMax && l < r; ii++, j++) { for (size_t l = 0, r = idxChr.size(), ii = i, j = 0; ii < inputMax && l < r; ii++, j++) {
auto c = input[ii]; wchar_t c = input[ii];
while (l < r) { while (l < r) {
// Test the highlight in the middle of the search area. // Test the highlight in the middle of the search area.
size_t m = (l + r) / 2; size_t m = (l + r) / 2;
@ -23,7 +23,7 @@ void ZRCola::highlight_db::Highlight(const char_t* input, size_t inputMax, std::
// Get the j-th character of the highlight. // Get the j-th character of the highlight.
// All highlights that get short on characters are lexically ordered before. // All highlights that get short on characters are lexically ordered before.
// Thus the j-th character is considered 0. // Thus the j-th character is considered 0.
auto s = idxChr[m].chr_at(j); wchar_t s = idxChr[m].chr_at(j);
// Do the bisection test. // Do the bisection test.
if (c < s) r = m; if (c < s) r = m;

View File

@ -57,12 +57,12 @@ void ZRCola::LangConvert(_In_ LANGID lang_win, _Inout_ ZRCola::langid_t &lang)
#endif #endif
bool ZRCola::langchar_db::IsLocalCharacter(_In_ const char_t *chr, _In_ const char_t *chr_end, _In_ ZRCola::langid_t lang) const bool ZRCola::langchar_db::IsLocalCharacter(_In_ const wchar_t *chr, _In_ const wchar_t *chr_end, _In_ ZRCola::langid_t lang) const
{ {
size_t n = chr_end - chr; size_t n = chr_end - chr;
assert(n <= 0xffff); assert(n <= 0xffff);
std::unique_ptr<langchar> lc((langchar*)new char[sizeof(langchar) + sizeof(char_t)*n]); std::unique_ptr<langchar> lc((langchar*)new char[sizeof(langchar) + sizeof(wchar_t)*n]);
new (lc.get()) langchar(lang, chr, n); lc->langchar::langchar(lang, chr, n);
indexChr::size_type start; indexChr::size_type start;
return idxChr.find(*lc, start); return idxChr.find(*lc, start);
} }

View File

@ -0,0 +1,68 @@
/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2015-2022 Amebis
*/
#include "pch.h"
size_t ZRCola::mapping_vector::to_src(_In_ size_t dst) const
{
if (empty()) {
// One-to-one mapping.
return dst;
}
for (size_type l = 0, r = size();;) {
if (l < r) {
size_type m = (l + r) / 2;
const mapping &el = (*this)[m];
if ( dst < el.dst) r = m;
else if (el.dst < dst) l = m + 1;
else {
// An exact match found.
return el.src;
}
} else if (l) {
// We found a map interval.
const mapping &el = (*this)[l - 1];
return el.src + (dst - el.dst);
} else {
// The destination character index is left of the first transformation.
const mapping &el = (*this)[0];
return std::min<size_t>(dst, el.src);
}
}
}
size_t ZRCola::mapping_vector::to_dst(_In_ size_t src) const
{
if (empty()) {
// One-to-one mapping.
return src;
}
for (size_type l = 0, r = size();;) {
if (l < r) {
size_type m = (l + r) / 2;
const mapping &el = (*this)[m];
if ( src < el.src) r = m;
else if (el.src < src) l = m + 1;
else {
// An exact match found.
return el.dst;
}
} else if (l) {
// We found a map interval.
const mapping &el = (*this)[l - 1];
return el.dst + (src - el.src);
} else {
// The source character index is left of the first transformation.
const mapping &el = (*this)[0];
return std::min<size_t>(src, el.dst);
}
}
}

View File

@ -3,8 +3,7 @@
Copyright © 2015-2022 Amebis Copyright © 2015-2022 Amebis
*/ */
#ifndef __PCH_H__ #pragma once
#define __PCH_H__
#include "../../../include/version.h" #include "../../../include/version.h"
@ -18,5 +17,3 @@
#include <algorithm> #include <algorithm>
#include <cwctype> #include <cwctype>
#endif

View File

@ -6,7 +6,7 @@
#include "pch.h" #include "pch.h"
bool ZRCola::chrtag_db::Search(_In_ const std::map<tagid_t, uint16_t> &tags, _In_ const character_db &ch_db, _In_ const std::set<chrcatid_t> &cats, _Inout_ std::map<string_t, charrank_t> &hits, _In_opt_ bool (__cdecl *fn_abort)(void *cookie), _In_opt_ void *cookie) const bool ZRCola::chrtag_db::Search(_In_ const std::map<tagid_t, unsigned __int16> &tags, _In_ const character_db &ch_db, _In_ const std::set<chrcatid_t> &cats, _Inout_ std::map<std::wstring, charrank_t> &hits, _In_opt_ bool (__cdecl *fn_abort)(void *cookie), _In_opt_ void *cookie) const
{ {
for (auto tag = tags.cbegin(), tag_end = tags.cend(); tag != tag_end; ++tag) { for (auto tag = tags.cbegin(), tag_end = tags.cend(); tag != tag_end; ++tag) {
if (fn_abort && fn_abort(cookie)) return false; if (fn_abort && fn_abort(cookie)) return false;
@ -17,9 +17,9 @@ bool ZRCola::chrtag_db::Search(_In_ const std::map<tagid_t, uint16_t> &tags, _In
for (size_t i = start; i < end; i++) { for (size_t i = start; i < end; i++) {
if (fn_abort && fn_abort(cookie)) return false; if (fn_abort && fn_abort(cookie)) return false;
const chrtag &ct = idxTag[i]; const chrtag &ct = idxTag[i];
uint16_t len = ct.chr_len(); unsigned __int16 len = ct.chr_len();
if (cats.find(ch_db.GetCharCat(ct.chr(), len)) != cats.end()) { if (cats.find(ch_db.GetCharCat(ct.chr(), len)) != cats.end()) {
string_t chr(ct.chr(), len); std::wstring chr(ct.chr(), len);
auto idx = hits.find(chr); auto idx = hits.find(chr);
if (idx == hits.end()) { if (idx == hits.end()) {
// New character. // New character.
@ -37,7 +37,7 @@ bool ZRCola::chrtag_db::Search(_In_ const std::map<tagid_t, uint16_t> &tags, _In
} }
bool ZRCola::tagname_db::Search(_In_z_ const char_t *str, _In_ uint32_t locale, _Inout_ std::map<tagid_t, uint16_t> &hits, _In_opt_ bool (__cdecl *fn_abort)(void *cookie), _In_opt_ void *cookie) const bool ZRCola::tagname_db::Search(_In_z_ const wchar_t *str, _In_ LCID locale, _Inout_ std::map<tagid_t, unsigned __int16> &hits, _In_opt_ bool (__cdecl *fn_abort)(void *cookie), _In_opt_ void *cookie) const
{ {
assert(str); assert(str);
@ -55,14 +55,14 @@ bool ZRCola::tagname_db::Search(_In_z_ const char_t *str, _In_ uint32_t locale,
} }
// Get name. // Get name.
string_t name; std::wstring name;
if (*str == u'"') { if (*str == L'"') {
const auto *str_end = ++str; const wchar_t *str_end = ++str;
for (;;) { for (;;) {
if (*str_end == 0) { if (*str_end == 0) {
name.assign(str, str_end); name.assign(str, str_end);
break; break;
} else if (*str_end == u'"') { } else if (*str_end == L'"') {
name.assign(str, str_end); name.assign(str, str_end);
str_end++; str_end++;
break; break;
@ -71,7 +71,7 @@ bool ZRCola::tagname_db::Search(_In_z_ const char_t *str, _In_ uint32_t locale,
} }
str = str_end; str = str_end;
} else { } else {
const auto *str_end = str + 1; const wchar_t *str_end = str + 1;
for (; *str_end && !iswspace(*str_end); str_end++); for (; *str_end && !iswspace(*str_end); str_end++);
name.assign(str, str_end); name.assign(str, str_end);
str = str_end; str = str_end;
@ -81,8 +81,8 @@ bool ZRCola::tagname_db::Search(_In_z_ const char_t *str, _In_ uint32_t locale,
if (fn_abort && fn_abort(cookie)) return false; if (fn_abort && fn_abort(cookie)) return false;
// Find the name. // Find the name.
std::unique_ptr<tagname> tn(reinterpret_cast<tagname*>(new char[sizeof(tagname) + sizeof(char_t)*name.length()])); std::unique_ptr<tagname> tn(reinterpret_cast<tagname*>(new char[sizeof(tagname) + sizeof(wchar_t)*name.length()]));
new (tn.get()) tagname(0, locale, name.data(), name.length()); tn->tagname::tagname(0, locale, name.data(), name.length());
size_t start, end; size_t start, end;
if (idxName.find(*tn, start, end)) { if (idxName.find(*tn, start, end)) {
// The name was found. // The name was found.
@ -92,7 +92,7 @@ bool ZRCola::tagname_db::Search(_In_z_ const char_t *str, _In_ uint32_t locale,
auto idx = hits.find(val.tag); auto idx = hits.find(val.tag);
if (idx == hits.end()) { if (idx == hits.end()) {
// New tag. // New tag.
hits.insert(std::make_pair(val.tag, (uint16_t)1)); hits.insert(std::make_pair(val.tag, (unsigned __int16)1));
} else { } else {
// Increase count for existing tag. // Increase count for existing tag.
idx->second++; idx->second++;

View File

@ -6,7 +6,7 @@
#include "pch.h" #include "pch.h"
void ZRCola::translation_db::Translate(_In_ transetid_t set, _In_z_count_(inputMax) const char_t* input, _In_ size_t inputMax, _Out_ string_t &output, _Out_opt_ std::vector<mapping>* map) const void ZRCola::translation_db::Translate(_In_ transetid_t set, _In_z_count_(inputMax) const wchar_t* input, _In_ size_t inputMax, _Out_ std::wstring &output, _Out_opt_ std::vector<mapping>* map) const
{ {
assert(input || inputMax == 0); assert(input || inputMax == 0);
@ -28,7 +28,7 @@ void ZRCola::translation_db::Translate(_In_ transetid_t set, _In_z_count_(inputM
// Find the longest matching translation at i-th character. // Find the longest matching translation at i-th character.
size_t l_match = (size_t)-1; size_t l_match = (size_t)-1;
for (size_t l = l_set, r = r_set, ii = i, j = 0; ii < inputMax && l < r; ii++, j++) { for (size_t l = l_set, r = r_set, ii = i, j = 0; ii < inputMax && l < r; ii++, j++) {
auto c = input[ii]; wchar_t c = input[ii];
while (l < r) { while (l < r) {
// Test the translation in the middle of the search area. // Test the translation in the middle of the search area.
size_t m = (l + r) / 2; size_t m = (l + r) / 2;
@ -36,7 +36,7 @@ void ZRCola::translation_db::Translate(_In_ transetid_t set, _In_z_count_(inputM
// Get the j-th character of the translation. // Get the j-th character of the translation.
// All translations that get short on characters are lexically ordered before. // All translations that get short on characters are lexically ordered before.
// Thus the j-th character is considered 0. // Thus the j-th character is considered 0.
auto s = idxSrc[m].src_at(j); wchar_t s = idxSrc[m].src_at(j);
// Do the bisection test. // Do the bisection test.
if (c < s) r = m; if (c < s) r = m;
@ -84,7 +84,7 @@ void ZRCola::translation_db::Translate(_In_ transetid_t set, _In_z_count_(inputM
} }
void ZRCola::translation_db::TranslateInv(_In_ transetid_t set, _In_z_count_(inputMax) const char_t* input, _In_ size_t inputMax, _In_opt_ const langchar_db *lc_db, _In_opt_ langid_t lang, _Out_ string_t &output, _Out_opt_ std::vector<mapping>* map) const void ZRCola::translation_db::TranslateInv(_In_ transetid_t set, _In_z_count_(inputMax) const wchar_t* input, _In_ size_t inputMax, _In_opt_ const langchar_db *lc_db, _In_opt_ langid_t lang, _Out_ std::wstring &output, _Out_opt_ std::vector<mapping>* map) const
{ {
assert(input || inputMax == 0); assert(input || inputMax == 0);
@ -106,7 +106,7 @@ void ZRCola::translation_db::TranslateInv(_In_ transetid_t set, _In_z_count_(inp
// Find the longest matching inverse translation at i-th character. // Find the longest matching inverse translation at i-th character.
size_t l_match = (size_t)-1; size_t l_match = (size_t)-1;
for (size_t l = l_set, r = r_set, ii = i, j = 0; ii < inputMax && l < r; ii++, j++) { for (size_t l = l_set, r = r_set, ii = i, j = 0; ii < inputMax && l < r; ii++, j++) {
auto c = input[ii]; wchar_t c = input[ii];
while (l < r) { while (l < r) {
// Test the inverse translation in the middle of the search area. // Test the inverse translation in the middle of the search area.
size_t m = (l + r) / 2; size_t m = (l + r) / 2;
@ -114,7 +114,7 @@ void ZRCola::translation_db::TranslateInv(_In_ transetid_t set, _In_z_count_(inp
// Get the j-th character of the inverse translation. // Get the j-th character of the inverse translation.
// All inverse translations that get short on characters are lexically ordered before. // All inverse translations that get short on characters are lexically ordered before.
// Thus the j-th character is considered 0. // Thus the j-th character is considered 0.
auto s = idxDst[m].dst_at(j); wchar_t s = idxDst[m].dst_at(j);
// Do the bisection test. // Do the bisection test.
if (c < s) r = m; if (c < s) r = m;
@ -147,7 +147,7 @@ void ZRCola::translation_db::TranslateInv(_In_ transetid_t set, _In_z_count_(inp
if (l_match < r_set) { if (l_match < r_set) {
// The saved inverse translation was an exact match. // The saved inverse translation was an exact match.
const translation &trans = idxDst[l_match]; const translation &trans = idxDst[l_match];
if (trans.src_len() && trans.src()[0] != u'#' && (!lc_db || !lc_db->IsLocalCharacter(trans.dst(), trans.dst_end(), lang))) { if (trans.src_len() && trans.src()[0] != L'#' && (!lc_db || !lc_db->IsLocalCharacter(trans.dst(), trans.dst_end(), lang))) {
// Append source sequence. // Append source sequence.
output.append(trans.src(), trans.src_end()); output.append(trans.src(), trans.src_end());
i += trans.dst_len(); i += trans.dst_len();

View File

@ -1,2 +0,0 @@
/*.d
/test

View File

@ -1,129 +0,0 @@
#include <zrcola/idrec.h>
#include <fstream>
#include <iostream>
#include <typeinfo>
using namespace std;
using namespace ZRCola;
translation_db t_db;
transet_db ts_db;
transeq_db tsq_db;
langchar_db lc_db;
language_db lang_db;
character_db chr_db;
chrcat_db cc_db;
chrtag_db ct_db;
tagname_db tn_db;
highlight_db h_db;
static void load_database()
{
fstream dat("../../../output/data/ZRCola.zrcdb", ios_base::in | ios_base::binary);
if (!dat.good())
throw runtime_error("ZRCola.zrcdb not found or cannot be opened.");
if (!stdex::idrec::find<recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN>(dat, ZRCOLA_DB_ID, sizeof(recordid_t)))
throw runtime_error("ZRCola.zrcdb is not a valid ZRCola database.");
recordsize_t size;
dat.read((char*)&size, sizeof(recordsize_t));
if (dat.good()) {
bool has_translation_data = false;
for (;;) {
recordid_t id;
if (!stdex::idrec::read_id(dat, id, size)) break;
if (id == translation_rec::id()) {
dat >> translation_rec(t_db);
if (dat.good()) {
has_translation_data = true;
} else {
cerr << "Error reading translation data from ZRCola.zrcdb.\n";
t_db.clear();
}
} else if (id == transet_rec::id()) {
dat >> transet_rec(ts_db);
if (!dat.good()) {
cerr << "Error reading translation set data from ZRCola.zrcdb.\n";
ts_db.clear();
}
} else if (id == transeq_rec::id()) {
dat >> transeq_rec(tsq_db);
if (!dat.good()) {
cerr << "Error reading translation sequence data from ZRCola.zrcdb.\n";
tsq_db.clear();
}
} else if (id == langchar_rec::id()) {
dat >> langchar_rec(lc_db);
if (!dat.good()) {
cerr << "Error reading language character data from ZRCola.zrcdb.\n";
lc_db.clear();
}
} else if (id == language_rec::id()) {
dat >> language_rec(lang_db);
if (!dat.good()) {
cerr << "Error reading language character data from ZRCola.zrcdb.\n";
lang_db.clear();
}
} else if (id == character_rec::id()) {
dat >> character_rec(chr_db);
if (!dat.good()) {
cerr << "Error reading character data from ZRCola.zrcdb.\n";
chr_db.clear();
}
} else if (id == chrcat_rec::id()) {
dat >> chrcat_rec(cc_db);
if (!dat.good()) {
cerr << "Error reading character category data from ZRCola.zrcdb.\n";
cc_db.clear();
}
} else if (id == chrtag_rec::id()) {
dat >> chrtag_rec(ct_db);
if (!dat.good()) {
cerr << "Error reading character tag data from ZRCola.zrcdb.\n";
ct_db.clear();
}
} else if (id == tagname_rec::id()) {
dat >> tagname_rec(tn_db);
if (!dat.good()) {
cerr << "Error reading tag name data from ZRCola.zrcdb.\n";
tn_db.clear();
}
} else if (id == highlight_rec::id()) {
dat >> highlight_rec(h_db);
if (!dat.good()) {
cerr << "Error reading highlight data from ZRCola.zrcdb.\n";
h_db.clear();
}
} else
stdex::idrec::ignore<recordsize_t, ZRCOLA_RECORD_ALIGN>(dat);
}
if (!has_translation_data)
throw runtime_error("ZRCola.zrcdb has no translation data.");
}
}
int main()
{
try {
load_database();
u16string output;
vector<mapping> map;
t_db.Translate(ZRCOLA_TRANSETID_DEFAULT, u"", -1, output, &map);
if (!output.empty()) throw runtime_error("Empty string translated to nonempty output.");
if (!map.empty()) throw runtime_error("Empty string translation produced non-empty map.");
t_db.Translate(ZRCOLA_TRANSETID_DEFAULT, u"To je test.", -1, output, &map);
if (output != u"T  ťᵉⓢṭ.") throw runtime_error("Unexpected translation.");
cout << "Passed\n";
return 0;
} catch (exception &ex) {
cerr << typeid(ex).name() << ": " << ex.what() << endl;
return 1;
}
}

View File

@ -7,11 +7,10 @@
#include <zrcola/common.h> #include <zrcola/common.h>
#include <stdex/idrec.hpp> #include <stdex/idrec>
#include <assert.h> #include <assert.h>
#include <algorithm> #include <algorithm>
#include <istream> #include <istream>
#include <locale>
#include <ostream> #include <ostream>
#include <vector> #include <vector>
@ -25,7 +24,7 @@ namespace ZRCola {
/// ///
/// Character group ID /// Character group ID
/// ///
typedef uint16_t chrgrpid_t; typedef unsigned __int16 chrgrpid_t;
/// ///
@ -40,13 +39,13 @@ namespace ZRCola {
/// ///
struct chrgrp { struct chrgrp {
public: public:
chrgrpid_t grp; ///< Character group ID chrgrpid_t grp; ///< Character group ID
uint16_t rank; ///< Character group rank unsigned __int16 rank; ///< Character group rank
protected: protected:
uint16_t name_to; ///< Character group name end in \c data unsigned __int16 name_to; ///< Character group name end in \c data
uint16_t chrlst_to; ///< Character list end in \c data unsigned __int16 chrlst_to; ///< Character list end in \c data
char_t data[]; ///< Character group name, character list, bit vector if particular character is displayed initially wchar_t data[]; ///< Character group name, character list, bit vector if particular character is displayed initially
public: public:
/// ///
@ -61,49 +60,49 @@ namespace ZRCola {
/// \param[in] chrshow Binary vector which particular character is displayed initially /// \param[in] chrshow Binary vector which particular character is displayed initially
/// ///
inline chrgrp( inline chrgrp(
_In_opt_ chrgrpid_t grp = 0, _In_opt_ chrgrpid_t grp = 0,
_In_opt_ uint16_t rank = 0, _In_opt_ unsigned __int16 rank = 0,
_In_opt_z_count_(name_len) const char_t *name = NULL, _In_opt_z_count_(name_len) const wchar_t *name = NULL,
_In_opt_ size_t name_len = 0, _In_opt_ size_t name_len = 0,
_In_opt_z_count_(chrlst_len) const char_t *chrlst = NULL, _In_opt_z_count_(chrlst_len) const wchar_t *chrlst = NULL,
_In_opt_ size_t chrlst_len = 0, _In_opt_ size_t chrlst_len = 0,
_In_opt_count_x_((chrlst_len + 15)/16) const uint16_t *chrshow = NULL) _In_opt_count_x_((chrlst_len + 15)/16) const unsigned __int16 *chrshow = NULL)
{ {
this->grp = grp; this->grp = grp;
this->rank = rank; this->rank = rank;
this->name_to = static_cast<uint16_t>(name_len); this->name_to = static_cast<unsigned __int16>(name_len);
if (name && name_len) memcpy(this->data, name, sizeof(char_t)*name_len); if (name && name_len) memcpy(this->data, name, sizeof(wchar_t)*name_len);
this->chrlst_to = static_cast<uint16_t>(this->name_to + chrlst_len); this->chrlst_to = static_cast<unsigned __int16>(this->name_to + chrlst_len);
if (chrlst && chrshow && chrlst_len) { if (chrlst && chrshow && chrlst_len) {
memcpy(this->data + this->name_to, chrlst, sizeof(char_t)*chrlst_len); memcpy(this->data + this->name_to, chrlst, sizeof(wchar_t)*chrlst_len);
memcpy(this->data + this->chrlst_to, chrshow, (chrlst_len + sizeof(*data)*8 - 1)/8); memcpy(this->data + this->chrlst_to, chrshow, (chrlst_len + sizeof(*data)*8 - 1)/8);
} }
} }
inline const char_t* name () const { return data; }; inline const wchar_t* name () const { return data; };
inline char_t* name () { return data; }; inline wchar_t* name () { return data; };
inline const char_t* name_end() const { return data + name_to; }; inline const wchar_t* name_end() const { return data + name_to; };
inline char_t* name_end() { return data + name_to; }; inline wchar_t* name_end() { return data + name_to; };
inline uint16_t name_len() const { return name_to; }; inline unsigned __int16 name_len() const { return name_to; };
inline const char_t* chrlst () const { return data + name_to; }; inline const wchar_t* chrlst () const { return data + name_to; };
inline char_t* chrlst () { return data + name_to; }; inline wchar_t* chrlst () { return data + name_to; };
inline const char_t* chrlst_end() const { return data + chrlst_to; }; inline const wchar_t* chrlst_end() const { return data + chrlst_to; };
inline char_t* chrlst_end() { return data + chrlst_to; }; inline wchar_t* chrlst_end() { return data + chrlst_to; };
inline uint16_t chrlst_len() const { return chrlst_to - name_to; }; inline unsigned __int16 chrlst_len() const { return chrlst_to - name_to; };
inline const uint16_t* chrshow () const { return reinterpret_cast<const uint16_t*>(data + chrlst_to ); }; inline const unsigned __int16* chrshow () const { return reinterpret_cast<const unsigned __int16*>(data + chrlst_to ); };
inline uint16_t* chrshow () { return reinterpret_cast< uint16_t*>(data + chrlst_to ); }; inline unsigned __int16* chrshow () { return reinterpret_cast< unsigned __int16*>(data + chrlst_to ); };
inline const uint16_t* chrshow_end() const { return reinterpret_cast<const uint16_t*>(data + chrlst_to + chrshow_len()); }; inline const unsigned __int16* chrshow_end() const { return reinterpret_cast<const unsigned __int16*>(data + chrlst_to + chrshow_len()); };
inline uint16_t* chrshow_end() { return reinterpret_cast< uint16_t*>(data + chrlst_to + chrshow_len()); }; inline unsigned __int16* chrshow_end() { return reinterpret_cast< unsigned __int16*>(data + chrlst_to + chrshow_len()); };
inline uint16_t chrshow_len() const { return (chrlst_len() + sizeof(*data)*8 - 1)/(sizeof(*data)*8); }; inline unsigned __int16 chrshow_len() const { return (chrlst_len() + sizeof(*data)*8 - 1)/(sizeof(*data)*8); };
}; };
#pragma pack(pop) #pragma pack(pop)
/// ///
/// Rank index /// Rank index
/// ///
class indexRank : public index<uint16_t, uint32_t, chrgrp> class indexRank : public index<unsigned __int16, unsigned __int32, chrgrp>
{ {
public: public:
/// ///
@ -111,7 +110,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexRank(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, chrgrp>(h) {} indexRank(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, chrgrp>(h) {}
/// ///
/// Compares two character groups by rank (for searching) /// Compares two character groups by rank (for searching)
@ -126,8 +125,8 @@ namespace ZRCola {
/// ///
virtual int compare(_In_ const chrgrp &a, _In_ const chrgrp &b) const virtual int compare(_In_ const chrgrp &a, _In_ const chrgrp &b) const
{ {
if (a.rank < b.rank) return -1; if (a.rank < b.rank) return -1;
if (a.rank > b.rank) return +1; else if (a.rank > b.rank) return +1;
return 0; return 0;
} }
@ -148,88 +147,97 @@ namespace ZRCola {
if (a.rank < b.rank) return -1; if (a.rank < b.rank) return -1;
else if (a.rank > b.rank) return +1; else if (a.rank > b.rank) return +1;
auto &coll = std::use_facet<std::collate<char_t>>(std::locale()); unsigned __int16
return coll.compare(a.name(), a.name_end(), b.name(), b.name_end()); a_name_len = a.name_len(),
b_name_len = b.name_len();
int r = _wcsncoll(a.name(), b.name(), std::min<unsigned __int16>(a_name_len, b_name_len));
if (r != 0) return r;
if (a_name_len < b_name_len) return -1;
else if (a_name_len > b_name_len) return +1;
return 0;
} }
} idxRank; ///< Rank index } idxRank; ///< Rank index
std::vector<uint16_t> data; ///< Character groups data std::vector<unsigned __int16> data; ///< Character groups data
public: public:
/// ///
/// Constructs the database /// Constructs the database
/// ///
inline chrgrp_db() : idxRank(data) {} inline chrgrp_db() : idxRank(data) {}
///
/// Writes character group database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Character group database
///
/// \returns The stream \p stream
///
friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const chrgrp_db& db)
{
// Write rank index.
if (stream.fail()) return stream;
stream << db.idxRank;
// 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;
uint32_t count = (uint32_t)data_count;
stream.write((const char*)&count, sizeof(count));
// Write data.
if (stream.fail()) return stream;
stream.write((const char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads character group database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Character group database
///
/// \returns The stream \p stream
///
friend std::istream& operator >>(_In_ std::istream& stream, _Out_ chrgrp_db& db)
{
// Read rank index.
stream >> db.idxRank;
if (!stream.good()) return stream;
// Read data count.
uint32_t 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(uint16_t) * static_cast<std::streamsize>(count));
}
else
db.data.clear();
return stream;
}
}; };
typedef stdex::idrec::record<chrgrp_db, recordid_t, 0x524743 /*"CGR"*/, recordsize_t, ZRCOLA_RECORD_ALIGN> chrgrp_rec; typedef stdex::idrec::record<chrgrp_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> chrgrp_rec;
}; };
const ZRCola::recordid_t ZRCola::chrgrp_rec::id = *(ZRCola::recordid_t*)"CGR";
///
/// Writes character group database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Character group database
///
/// \returns The stream \p stream
///
inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::chrgrp_db &db)
{
// Write rank index.
if (stream.fail()) return stream;
stream << db.idxRank;
// 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)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads character group database from a stream
///
/// \param[in ] stream Input stream
/// \param[out] db Character group database
///
/// \returns The stream \p stream
///
inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::chrgrp_db &db)
{
// Read rank index.
stream >> db.idxRank;
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)*static_cast<std::streamsize>(count));
} else
db.data.clear();
return stream;
}
#pragma warning(pop) #pragma warning(pop)

View File

@ -7,7 +7,7 @@
#include <zrcola/common.h> #include <zrcola/common.h>
#include <stdex/idrec.hpp> #include <stdex/idrec>
#include <wxex/common.h> #include <wxex/common.h>
#pragma warning(push) #pragma warning(push)
@ -39,20 +39,20 @@ namespace ZRCola {
struct keyseq { struct keyseq {
public: public:
enum modifiers_t { enum modifiers_t {
SHIFT = 1<<0, ///< SHIFT key was pressed SHIFT = 1<<0, ///< SHIFT key was pressed
CTRL = 1<<1, ///< CTRL key was pressed CTRL = 1<<1, ///< CTRL key was pressed
ALT = 1<<2, ///< ALT key was pressed ALT = 1<<2, ///< ALT key was pressed
}; };
struct key_t { struct key_t {
char_t key; ///< Key wchar_t key; ///< Key
uint16_t modifiers; ///< Modifiers (bitwise combination of SHIFT, CTRL and ALT) unsigned __int16 modifiers; ///< Modifiers (bitwise combination of SHIFT, CTRL and ALT)
}; };
protected: protected:
uint16_t chr_to; ///< Character end in \c data unsigned __int16 chr_to; ///< Character end in \c data
uint16_t seq_to; ///< Key sequence end in \c data unsigned __int16 seq_to; ///< Key sequence end in \c data
char_t data[]; ///< Character and key sequence wchar_t data[]; ///< Character and key sequence
public: public:
/// ///
@ -64,28 +64,28 @@ namespace ZRCola {
/// \param[in] chr_len Number of UTF-16 characters in \p chr /// \param[in] chr_len Number of UTF-16 characters in \p chr
/// ///
inline keyseq( inline keyseq(
_In_opt_count_(seq_count) const key_t *seq = NULL, _In_opt_count_(seq_count) const key_t *seq = NULL,
_In_opt_ size_t seq_count = 0, _In_opt_ size_t seq_count = 0,
_In_opt_z_count_(chr_len) const char_t *chr = NULL, _In_opt_z_count_(chr_len) const wchar_t *chr = NULL,
_In_opt_ size_t chr_len = 0) _In_opt_ size_t chr_len = 0)
{ {
this->chr_to = static_cast<uint16_t>(chr_len); this->chr_to = static_cast<unsigned __int16>(chr_len);
if (chr && chr_len) memcpy(this->data, chr, sizeof(char_t)*chr_len); if (chr && chr_len) memcpy(this->data, chr, sizeof(wchar_t)*chr_len);
this->seq_to = static_cast<uint16_t>(this->chr_to + seq_count * sizeof(key_t) / sizeof(*data)); this->seq_to = static_cast<unsigned __int16>(this->chr_to + seq_count * sizeof(key_t) / sizeof(*data));
if (seq && seq_count) memcpy(this->data + this->chr_to, seq, sizeof(key_t)*seq_count); if (seq && seq_count) memcpy(this->data + this->chr_to, seq, sizeof(key_t)*seq_count);
} }
inline const char_t* chr () const { return data; }; inline const wchar_t* chr () const { return data; };
inline char_t* chr () { return data; }; inline wchar_t* chr () { return data; };
inline const char_t* chr_end() const { return data + chr_to; }; inline const wchar_t* chr_end() const { return data + chr_to; };
inline char_t* chr_end() { return data + chr_to; }; inline wchar_t* chr_end() { return data + chr_to; };
inline uint16_t chr_len() const { return chr_to; }; inline unsigned __int16 chr_len() const { return chr_to; };
inline const key_t* seq () const { return reinterpret_cast<const key_t*>(data + chr_to); }; inline const key_t* seq () const { return reinterpret_cast<const key_t*>(data + chr_to); };
inline key_t* seq () { return reinterpret_cast< key_t*>(data + chr_to); }; inline key_t* seq () { return reinterpret_cast< key_t*>(data + chr_to); };
inline const key_t* seq_end() const { return reinterpret_cast<const key_t*>(data + seq_to); }; inline const key_t* seq_end() const { return reinterpret_cast<const key_t*>(data + seq_to); };
inline key_t* seq_end() { return reinterpret_cast< key_t*>(data + seq_to); }; inline key_t* seq_end() { return reinterpret_cast< key_t*>(data + seq_to); };
inline uint16_t seq_len() const { return (seq_to - chr_to) * sizeof(*data) / sizeof(key_t); }; inline unsigned __int16 seq_len() const { return (seq_to - chr_to) * sizeof(*data) / sizeof(key_t); };
/// ///
/// Compares two key sequences /// Compares two key sequences
@ -118,7 +118,7 @@ namespace ZRCola {
/// ///
/// Character index /// Character index
/// ///
class indexChr : public index<uint16_t, uint32_t, keyseq> class indexChr : public index<unsigned __int16, unsigned __int32, keyseq>
{ {
public: public:
/// ///
@ -126,7 +126,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexChr(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, keyseq>(h) {} indexChr(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, keyseq>(h) {}
/// ///
/// Compares two key sequences by character (for searching) /// Compares two key sequences by character (for searching)
@ -174,7 +174,7 @@ namespace ZRCola {
/// ///
/// Key index /// Key index
/// ///
class indexKey : public index<uint16_t, uint32_t, keyseq> class indexKey : public index<unsigned __int16, unsigned __int32, keyseq>
{ {
public: public:
/// ///
@ -182,7 +182,7 @@ namespace ZRCola {
/// ///
/// \param[in] h Reference to vector holding the data /// \param[in] h Reference to vector holding the data
/// ///
indexKey(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, keyseq>(h) {} indexKey(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, keyseq>(h) {}
/// ///
/// Compares two key sequences by key (for searching) /// Compares two key sequences by key (for searching)
@ -226,7 +226,7 @@ namespace ZRCola {
} }
} idxKey; ///< Key index } idxKey; ///< Key index
std::vector<uint16_t> data; ///< Key sequences data std::vector<unsigned __int16> data; ///< Key sequences data
public: public:
/// ///
@ -270,84 +270,86 @@ namespace ZRCola {
wxString str; wxString str;
return GetSequenceAsText(seq, seq_len, str) ? str : wxEmptyString; return GetSequenceAsText(seq, seq_len, str) ? str : wxEmptyString;
} }
///
/// Writes key sequence database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Key sequence database
///
/// \returns The stream \p stream
///
friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const keyseq_db& db)
{
// Write character index.
if (stream.fail()) return stream;
stream << db.idxChr;
// Write key index.
if (stream.fail()) return stream;
stream << db.idxKey;
// 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;
uint32_t count = (uint32_t)data_count;
stream.write((const char*)&count, sizeof(count));
// Write data.
if (stream.fail()) return stream;
stream.write((const char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads key sequence database from a stream
///
/// \param[in] stream Input stream
/// \param[out] db Key sequence database
///
/// \returns The stream \p stream
///
friend std::istream& operator >>(_In_ std::istream& stream, _Out_ keyseq_db& db)
{
// Read character index.
stream >> db.idxChr;
if (!stream.good()) return stream;
// Read key index.
stream >> db.idxKey;
if (!stream.good()) return stream;
// Read data count.
uint32_t 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(uint16_t) * static_cast<std::streamsize>(count));
}
else
db.data.clear();
return stream;
}
}; };
typedef stdex::idrec::record<keyseq_db, recordid_t, 0x59454b /*"KEY"*/, recordsize_t, ZRCOLA_RECORD_ALIGN> keyseq_rec; typedef stdex::idrec::record<keyseq_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> keyseq_rec;
}; };
const ZRCola::recordid_t ZRCola::keyseq_rec::id = *(ZRCola::recordid_t*)"KEY";
///
/// Writes key sequence database to a stream
///
/// \param[in] stream Output stream
/// \param[in] db Key sequence database
///
/// \returns The stream \p stream
///
inline std::ostream& operator <<(_In_ std::ostream& stream, _In_ const ZRCola::keyseq_db &db)
{
// Write character index.
if (stream.fail()) return stream;
stream << db.idxChr;
// Write key index.
if (stream.fail()) return stream;
stream << db.idxKey;
// 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)*static_cast<std::streamsize>(count));
return stream;
}
///
/// Reads key sequence database from a stream
///
/// \param[in] stream Input stream
/// \param[out] db Key sequence database
///
/// \returns The stream \p stream
///
inline std::istream& operator >>(_In_ std::istream& stream, _Out_ ZRCola::keyseq_db &db)
{
// Read character index.
stream >> db.idxChr;
if (!stream.good()) return stream;
// Read key index.
stream >> db.idxKey;
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)*static_cast<std::streamsize>(count));
} else
db.data.clear();
return stream;
}
#pragma warning(pop) #pragma warning(pop)

@ -1 +0,0 @@
Subproject commit 14ca5e55c8a7c8265b090e1704463c7ab42ca2ee

@ -1 +0,0 @@
Subproject commit ed5251c580e2e98beb50d818bcea8ddc91419d8c

@ -1 +1 @@
Subproject commit c1616b032e9597b072de6fae634ef242a6a67b1d Subproject commit 1fbff95bd7fecf80f958c15ab7d0eecdbe35e4cb

@ -1 +1 @@
Subproject commit 79ec08365068ab6e03b06caef13de0ce6b06fcd5 Subproject commit e3a59d1118053ed48dc15b83d17125da87c434dd

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.