From ad79961c33152ccdcee8d83dc2a8cbf6257f0265 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 20 Dec 2021 19:49:29 +0100 Subject: [PATCH] Match integer datatypes with Access DB Integer in Access is short in C/C++. Signed-off-by: Simon Rozman --- ZRColaCompile/dbsource.cpp | 26 +++++++++--------- ZRColaCompile/dbsource.h | 56 ++++++++++++++------------------------ ZRColaCompile/main.cpp | 20 +++++++------- 3 files changed, 44 insertions(+), 58 deletions(-) diff --git a/ZRColaCompile/dbsource.cpp b/ZRColaCompile/dbsource.cpp index a6113f4..c67a37e 100644 --- a/ZRColaCompile/dbsource.cpp +++ b/ZRColaCompile/dbsource.cpp @@ -305,7 +305,7 @@ bool ZRCola::DBSource::Open(LPCTSTR filename) com_obj params; wxVERIFY(SUCCEEDED(m_comTranslation->get_Parameters(¶ms))); wxASSERT_MSG(!m_pTranslation1, wxT("ADO command parameter already created")); - wxVERIFY(SUCCEEDED(m_comTranslation->CreateParameter(bstr(L"@Script"), adInteger, adParamInput, 0, variant(DISP_E_PARAMNOTFOUND, VT_ERROR), &m_pTranslation1))); + wxVERIFY(SUCCEEDED(m_comTranslation->CreateParameter(bstr(L"@Script"), adSmallInt, adParamInput, 0, variant(DISP_E_PARAMNOTFOUND, VT_ERROR), &m_pTranslation1))); wxVERIFY(SUCCEEDED(params->Append(m_pTranslation1))); } @@ -322,7 +322,7 @@ bool ZRCola::DBSource::Open(LPCTSTR filename) com_obj params; wxVERIFY(SUCCEEDED(m_comTranslationSets->get_Parameters(¶ms))); wxASSERT_MSG(!m_pTranslationSets1, wxT("ADO command parameter already created")); - wxVERIFY(SUCCEEDED(m_comTranslationSets->CreateParameter(bstr(L"@ID"), adInteger, adParamInput, 0, variant(DISP_E_PARAMNOTFOUND, VT_ERROR), &m_pTranslationSets1))); + wxVERIFY(SUCCEEDED(m_comTranslationSets->CreateParameter(bstr(L"@ID"), adSmallInt, adParamInput, 0, variant(DISP_E_PARAMNOTFOUND, VT_ERROR), &m_pTranslationSets1))); wxVERIFY(SUCCEEDED(params->Append(m_pTranslationSets1))); } @@ -386,15 +386,15 @@ bool ZRCola::DBSource::GetValue(const com_obj& f, bool& val) const } -bool ZRCola::DBSource::GetValue(const com_obj& f, int& val) const +bool ZRCola::DBSource::GetValue(const com_obj& f, short& val) const { wxASSERT_MSG(f, wxT("field is empty")); variant v; wxVERIFY(SUCCEEDED(f->get_Value(&v))); - wxCHECK(SUCCEEDED(v.change_type(VT_I4)), false); + wxCHECK(SUCCEEDED(v.change_type(VT_I2)), false); - val = V_I4(&v); + val = V_I2(&v); return true; } @@ -737,7 +737,7 @@ bool ZRCola::DBSource::SelectTranslations(com_obj &rs) const } -bool ZRCola::DBSource::SelectTranslations(int set, winstd::com_obj& rs) const +bool ZRCola::DBSource::SelectTranslations(short set, winstd::com_obj& rs) const { // Create a new recordset. rs.free(); @@ -923,7 +923,7 @@ bool ZRCola::DBSource::GetTranslationSeq(const com_obj& rs, ZRCola wxVERIFY(SUCCEEDED(flds2->get_Item(variant(L"Script"), &f_set))); size_t n = 0; for (VARIANT_BOOL eof = VARIANT_TRUE; SUCCEEDED(rs_chars->get_EOF(&eof)) && !eof; rs_chars->MoveNext(), n++) { - int set; + short set; wxCHECK(GetValue(f_set, set), false); ts.sets.push_back(set); } @@ -967,21 +967,21 @@ bool ZRCola::DBSource::GetKeySequence(const com_obj& rs, ZRCola::D wxCHECK(GetUnicodeString(f, ks.chr), false); } - int modifiers; + short modifiers; { com_obj f; wxVERIFY(SUCCEEDED(flds->get_Item(variant(L"Modifiers"), &f))); wxCHECK(GetValue(f, modifiers), false); } - int keycode1; + short keycode1; { com_obj f; wxVERIFY(SUCCEEDED(flds->get_Item(variant(L"KeyCodePre"), &f))); wxCHECK(GetValue(f, keycode1), false); } - int keycode; + short keycode; { com_obj f; wxVERIFY(SUCCEEDED(flds->get_Item(variant(L"KeyCode"), &f))); @@ -999,18 +999,18 @@ bool ZRCola::DBSource::GetKeySequence(const com_obj& rs, ZRCola::D if (keycode1) { // First key in the sequence is complete. keyseq::keycode kc1 = { - keyseq::keycode::translate_slen(static_cast(keycode1 & 0xffff)), + keyseq::keycode::translate_slen(static_cast(keycode1)), (modifiers & 0x100) != 0, (modifiers & 0x200) != 0, (modifiers & 0x400) != 0 }; ks.seq.push_back(kc1); - keyseq::keycode kc2 = { keyseq::keycode::translate_slen(static_cast(keycode & 0xffff)), shift }; + keyseq::keycode kc2 = { keyseq::keycode::translate_slen(static_cast(keycode)), shift }; ks.seq.push_back(kc2); } else { // First key in the sequence is only modifier(s). keyseq::keycode kc1 = { - keyseq::keycode::translate_slen(static_cast(keycode & 0xffff)), + keyseq::keycode::translate_slen(static_cast(keycode)), shift || (modifiers & 0x100) != 0, (modifiers & 0x200) != 0, (modifiers & 0x400) != 0 }; diff --git a/ZRColaCompile/dbsource.h b/ZRColaCompile/dbsource.h index 054a04d..80c490d 100644 --- a/ZRColaCompile/dbsource.h +++ b/ZRColaCompile/dbsource.h @@ -47,7 +47,7 @@ namespace ZRCola { /// class charseq { public: - int rank; ///< Sequence rank + short rank; ///< Sequence rank std::wstring str; ///< Sequence string inline charseq() : @@ -55,19 +55,19 @@ namespace ZRCola { { } - inline charseq(_In_ int _rank, _In_z_ const wchar_t *_str) : + inline charseq(_In_ short _rank, _In_z_ const wchar_t *_str) : rank(_rank), str (_str) { } - inline charseq(_In_ int _rank, _In_ const std::wstring &_str) : + inline charseq(_In_ short _rank, _In_ const std::wstring &_str) : rank(_rank), str (_str) { } - inline charseq(_In_ int _rank, _Inout_ std::wstring &&_str) : + inline charseq(_In_ short _rank, _Inout_ std::wstring &&_str) : rank(_rank), str (std::move(_str)) { @@ -113,12 +113,12 @@ namespace ZRCola { /// class translation { public: - int set; ///< Translation set ID + short set; ///< Translation set ID charseq src; ///< Source sequence std::string norm; ///< Normalization footprint charseq dst; ///< Destination sequence - inline translation() : set((int)ZRCOLA_TRANSETID_DEFAULT) {} + inline translation() : set((short)ZRCOLA_TRANSETID_DEFAULT) {} }; @@ -127,11 +127,11 @@ namespace ZRCola { /// class transet { public: - int set; ///< ID + short set; ///< ID std::wstring src; ///< Source name std::wstring dst; ///< Destination name - inline transet() : set((int)ZRCOLA_TRANSETID_DEFAULT) {} + inline transet() : set((short)ZRCOLA_TRANSETID_DEFAULT) {} }; @@ -140,10 +140,10 @@ namespace ZRCola { /// class transeq { public: - int seq; ///< ID - int rank; ///< Rank - std::wstring name; ///< Name - std::vector sets; ///< Sets + short seq; ///< ID + short rank; ///< Rank + std::wstring name; ///< Name + std::vector sets; ///< Sets inline transeq() : seq(0), @@ -222,8 +222,8 @@ namespace ZRCola { /// class chrgrp { public: - int grp; ///< Character group ID - int rank; ///< Rank + short grp; ///< Character group ID + short rank; ///< Rank std::wstring name; ///< Name std::vector chars; ///< Characters (zero-delimited) std::vector show; ///< Bit vector if particular character from \c chars is displayed initially @@ -373,7 +373,7 @@ namespace ZRCola { class chrcat { public: ZRCola::chrcatid_t cat; ///> Category ID - int rank; ///< Rank + short rank; ///< Rank std::wstring name; ///< Name inline chrcat() : cat(ZRCola::chrcatid_t::blank), rank(0) {} @@ -386,7 +386,7 @@ namespace ZRCola { class chrtag { public: std::wstring chr; ///> Character - int tag; ///< Tag ID + short tag; ///< Tag ID inline chrtag() : tag(0) {} }; @@ -397,7 +397,7 @@ namespace ZRCola { /// class tagname { public: - int tag; ///< Tag ID + short tag; ///< Tag ID std::map > names; ///< Names inline tagname() : tag(0) {} @@ -486,7 +486,7 @@ namespace ZRCola { /// - true when successful /// - false otherwise /// - bool GetValue(const winstd::com_obj& f, int& val) const; + bool GetValue(const winstd::com_obj& f, short& val) const; /// /// Gets string from ZRCola.zrc database @@ -628,7 +628,7 @@ namespace ZRCola { /// - true when query succeeds /// - false otherwise /// - bool SelectTranslations(int set, winstd::com_obj& rs) const; + bool SelectTranslations(short set, winstd::com_obj& rs) const; /// /// Returns translation data @@ -894,11 +894,8 @@ namespace ZRCola { inline ZRCola::translation_db& operator<<(_Inout_ ZRCola::translation_db &db, _In_ const ZRCola::DBSource::translation &rec) { unsigned __int32 idx = db.data.size(); - wxASSERT_MSG((int)0xffff0000 <= rec.set && rec.set <= (int)0x0000ffff, wxT("translation set id out of bounds")); db.data.push_back((unsigned __int16)rec.set); - wxASSERT_MSG((int)0xffff8000 <= rec.dst.rank && rec.dst.rank <= (int)0x00007fff, wxT("destination character rank out of bounds")); db.data.push_back((unsigned __int16)rec.dst.rank); - wxASSERT_MSG((int)0xffff8000 <= rec.src.rank && rec.src.rank <= (int)0x00007fff, wxT("source character rank out of bounds")); db.data.push_back((unsigned __int16)rec.src.rank); std::wstring::size_type n = rec.dst.str.length(); wxASSERT_MSG(n <= 0xffff, wxT("destination overflow")); @@ -918,7 +915,6 @@ 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) { unsigned __int32 idx = db.data.size(); - wxASSERT_MSG((int)0xffff0000 <= rec.set && rec.set <= (int)0x0000ffff, wxT("translation set id out of bounds")); db.data.push_back((unsigned __int16)rec.set); std::wstring::size_type n = rec.src.length(); wxASSERT_MSG(n <= 0xffff, wxT("translation set source name overflow")); @@ -937,9 +933,7 @@ 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) { unsigned __int32 idx = db.data.size(); - wxASSERT_MSG((int)0xffff8000 <= rec.seq && rec.seq <= (int)0x00007fff, wxT("translation sequence id out of bounds")); db.data.push_back((unsigned __int16)rec.seq); - wxASSERT_MSG((int)0xffff8000 <= rec.rank && rec.rank <= (int)0x00007fff, wxT("translation rank id out of bounds")); db.data.push_back((unsigned __int16)rec.rank); std::wstring::size_type n = rec.name.length(); wxASSERT_MSG(n <= 0xffff, wxT("translation sequence name overflow")); @@ -948,11 +942,8 @@ inline ZRCola::transeq_db& operator<<(_Inout_ ZRCola::transeq_db &db, _In_ const wxASSERT_MSG(n <= 0xffff, wxT("translation sequence sets overflow")); db.data.push_back((unsigned __int16)n); 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) { - int val = *s; - wxASSERT_MSG(val <= 0xffff, wxT("translation sequence ID overflow")); - db.data.push_back((unsigned __int16)val); - } + for (auto s = rec.sets.cbegin(), s_end = rec.sets.cend(); s != s_end; ++s) + db.data.push_back((unsigned __int16)*s); db.idxTranSeq.push_back(idx); db.idxRank .push_back(idx); @@ -1018,9 +1009,7 @@ 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) { unsigned __int32 idx = db.data.size(); - wxASSERT_MSG((int)0xffff8000 <= rec.grp && rec.grp <= (int)0x00007fff, wxT("character group ID out of bounds")); db.data.push_back((unsigned __int16)rec.grp); - wxASSERT_MSG((int)0xffff8000 <= rec.rank && rec.rank <= (int)0x00007fff, wxT("character group rank out of bounds")); db.data.push_back((unsigned __int16)rec.rank); std::wstring::size_type n = rec.name.length(); wxASSERT_MSG(n <= 0xffff, wxT("character group name overflow")); @@ -1063,7 +1052,6 @@ inline ZRCola::chrcat_db& operator<<(_Inout_ ZRCola::chrcat_db &db, _In_ const Z { unsigned __int32 idx = db.data.size(); db.data.insert(db.data.end(), reinterpret_cast(&rec.cat), reinterpret_cast(&rec.cat + 1)); - wxASSERT_MSG((int)0xffff8000 <= rec.rank && rec.rank <= (int)0x00007fff, wxT("character category rank out of bounds")); db.data.push_back((unsigned __int16)rec.rank); std::wstring::size_type n = rec.name.length(); wxASSERT_MSG(n <= 0xffff, wxT("character category name overflow")); @@ -1079,7 +1067,6 @@ 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) { unsigned __int32 idx = db.data.size(); - wxASSERT_MSG((int)0xffff8000 <= rec.tag && rec.tag <= (int)0x00007fff, wxT("tag out of bounds")); db.data.push_back((unsigned __int16)rec.tag); std::wstring::size_type n = rec.chr.length(); wxASSERT_MSG(n <= 0xffff, wxT("character overflow")); @@ -1097,7 +1084,6 @@ 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 nm = ln->second.cbegin(), nm_end = ln->second.cend(); nm != nm_end; ++nm) { unsigned __int32 idx = db.data.size(); - wxASSERT_MSG((int)0xffff8000 <= rec.tag && rec.tag <= (int)0x00007fff, wxT("tag out of bounds")); db.data.push_back((unsigned __int16)rec.tag); db.data.push_back(LOWORD(ln->first)); db.data.push_back(HIWORD(ln->first)); diff --git a/ZRColaCompile/main.cpp b/ZRColaCompile/main.cpp index abb7309..fa18694 100644 --- a/ZRColaCompile/main.cpp +++ b/ZRColaCompile/main.cpp @@ -16,8 +16,8 @@ using namespace winstd; class com_translation { public: - int rank_src; ///< Source sequence rank - int rank_dst; ///< Destination character rank + short rank_src; ///< Source sequence rank + short rank_dst; ///< Destination character rank string norm; ///< Normalization footprint inline com_translation() : @@ -26,20 +26,20 @@ public: { } - inline com_translation(int _rank_src, int _rank_dst) : + inline com_translation(short _rank_src, short _rank_dst) : rank_src(_rank_src), rank_dst(_rank_dst) { } - inline com_translation(int _rank_src, int _rank_dst, const char *_norm) : + inline com_translation(short _rank_src, short _rank_dst, const char *_norm) : rank_src(_rank_src), rank_dst(_rank_dst), norm (_norm ) { } - inline com_translation(int _rank_src, int _rank_dst, string &&_norm) : + inline com_translation(short _rank_src, short _rank_dst, string &&_norm) : rank_src( _rank_src ), rank_dst( _rank_dst ), norm (std::move(_norm )) @@ -365,8 +365,8 @@ int _tmain(int argc, _TCHAR *argv[]) translation_db::mapped_type::mapped_type ct(d1->second.rank_src + r->rank, d1->second.rank_dst); auto hit = t2->second.find(r->str); if (hit != t2->second.end()) { - hit->second.rank_src = std::min(hit->second.rank_src, ct.rank_src); - hit->second.rank_dst = std::max(hit->second.rank_dst, ct.rank_dst); + hit->second.rank_src = std::min(hit->second.rank_src, ct.rank_src); + hit->second.rank_dst = std::max(hit->second.rank_dst, ct.rank_dst); } else t2->second.insert(pair(r->str, std::move(ct))); } @@ -384,7 +384,7 @@ int _tmain(int argc, _TCHAR *argv[]) // Add translation to index and data. trans.dst.str = t->first; for (auto d = t->second.cbegin(), d_end = t->second.cend(); d != d_end; ++d) { - trans.set = (int)ZRCOLA_TRANSETID_DEFAULT; + trans.set = (short)ZRCOLA_TRANSETID_DEFAULT; trans.dst.rank = d->second.rank_dst; trans.src.rank = d->second.rank_src; trans.src.str = d->first; @@ -398,7 +398,7 @@ int _tmain(int argc, _TCHAR *argv[]) break; } if (!has_pua) { - trans.set = (int)ZRCOLA_TRANSETID_UNICODE; + trans.set = (short)ZRCOLA_TRANSETID_UNICODE; db_trans << trans; } } @@ -428,7 +428,7 @@ int _tmain(int argc, _TCHAR *argv[]) // Read translation set from the database. ZRCola::DBSource::transet ts; if (src.GetTranslationSet(rs, ts)) { - if (ts.set <= (int)ZRCOLA_TRANSETID_DEFAULT) + if (ts.set <= (short)ZRCOLA_TRANSETID_DEFAULT) continue; if (build_pot) {