Character tagging support added
This commit is contained in:
parent
a45115d5e7
commit
f53779dbad
@ -539,6 +539,50 @@ bool ZRCola::DBSource::GetChrCat(const com_obj<ADOField>& f, chrcatid_t& cc) con
|
||||
}
|
||||
|
||||
|
||||
bool ZRCola::DBSource::GetTagNames(const winstd::com_obj<ADOField>& f, LCID lcid, list<wstring>& names) const
|
||||
{
|
||||
wxASSERT_MSG(f, wxT("field is empty"));
|
||||
|
||||
variant v;
|
||||
wxVERIFY(SUCCEEDED(f->get_Value(&v)));
|
||||
wxCHECK(SUCCEEDED(v.change_type(VT_BSTR)), false);
|
||||
|
||||
// Parse the field. Must be "name, name, name..." sequence.
|
||||
names.clear();
|
||||
for (UINT i = 0, n = ::SysStringLen(V_BSTR(&v)); i < n && V_BSTR(&v)[i];) {
|
||||
if (iswspace(V_BSTR(&v)[i])) {
|
||||
// Skip leading white space.
|
||||
i++; continue;
|
||||
}
|
||||
|
||||
// Parse name.
|
||||
UINT j = i, j_end = i;
|
||||
for (; i < n && V_BSTR(&v)[i]; i++) {
|
||||
if (V_BSTR(&v)[i] == L',' || V_BSTR(&v)[i] == L';') {
|
||||
// Delimiter found.
|
||||
i++; break;
|
||||
} else if (!iswspace(V_BSTR(&v)[i])) {
|
||||
// Remember last non-white space character.
|
||||
j_end = i + 1;
|
||||
}
|
||||
}
|
||||
wstring name(V_BSTR(&v) + j, V_BSTR(&v) + j_end);
|
||||
for (auto n = names.cbegin(), n_end = names.cend(); ; ++n) {
|
||||
if (n == n_end) {
|
||||
// Add name to the list.
|
||||
names.push_back(std::move(name));
|
||||
break;
|
||||
} 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.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool ZRCola::DBSource::SelectTranslations(com_obj<ADORecordset> &rs) const
|
||||
{
|
||||
// Create a new recordset.
|
||||
@ -979,3 +1023,115 @@ bool ZRCola::DBSource::GetCharacterCategory(const com_obj<ADORecordset>& rs, chr
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool ZRCola::DBSource::SelectCharacterTags(winstd::com_obj<ADORecordset>& rs) const
|
||||
{
|
||||
// Create a new recordset.
|
||||
rs.free();
|
||||
wxCHECK(SUCCEEDED(::CoCreateInstance(CLSID_CADORecordset, NULL, CLSCTX_ALL, IID_IADORecordset, (LPVOID*)&rs)), false);
|
||||
|
||||
// Open it.
|
||||
if (FAILED(rs->Open(variant(
|
||||
L"SELECT DISTINCT [znak], [oznaka] "
|
||||
L"FROM [VRS_CharTags] "
|
||||
L"ORDER BY [znak], [oznaka]"), variant(m_db), adOpenStatic, adLockReadOnly, adCmdText)))
|
||||
{
|
||||
_ftprintf(stderr, wxT("%s: error ZCC0130: Error loading character tags from database. Please make sure the file is ZRCola.zrc compatible.\n"), m_filename.c_str());
|
||||
LogErrors();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool ZRCola::DBSource::GetCharacterTag(const winstd::com_obj<ADORecordset>& rs, chrtag& ct) const
|
||||
{
|
||||
wxASSERT_MSG(rs, wxT("recordset is empty"));
|
||||
|
||||
com_obj<ADOFields> flds;
|
||||
wxVERIFY(SUCCEEDED(rs->get_Fields(&flds)));
|
||||
wstring id;
|
||||
|
||||
{
|
||||
com_obj<ADOField> f;
|
||||
wxVERIFY(SUCCEEDED(flds->get_Item(variant(L"znak"), &f)));
|
||||
wxCHECK(GetUnicodeCharacter(f, ct.chr), false);
|
||||
}
|
||||
|
||||
{
|
||||
com_obj<ADOField> f;
|
||||
wxVERIFY(SUCCEEDED(flds->get_Item(variant(L"oznaka"), &f)));
|
||||
wxCHECK(GetValue(f, ct.tag), false);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool ZRCola::DBSource::SelectTagNames(winstd::com_obj<ADORecordset>& rs) const
|
||||
{
|
||||
// Create a new recordset.
|
||||
rs.free();
|
||||
wxCHECK(SUCCEEDED(::CoCreateInstance(CLSID_CADORecordset, NULL, CLSCTX_ALL, IID_IADORecordset, (LPVOID*)&rs)), false);
|
||||
|
||||
// Open it.
|
||||
if (FAILED(rs->Open(variant(
|
||||
L"SELECT DISTINCT [oznaka], [opis_en], [opis_sl], [opis_ru] "
|
||||
L"FROM [VRS_Tags] "
|
||||
L"ORDER BY [oznaka]"), variant(m_db), adOpenStatic, adLockReadOnly, adCmdText)))
|
||||
{
|
||||
_ftprintf(stderr, wxT("%s: error ZCC0130: Error loading tags from database. Please make sure the file is ZRCola.zrc compatible.\n"), m_filename.c_str());
|
||||
LogErrors();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool ZRCola::DBSource::GetTagName(const winstd::com_obj<ADORecordset>& rs, tagname& tn) const
|
||||
{
|
||||
wxASSERT_MSG(rs, wxT("recordset is empty"));
|
||||
|
||||
com_obj<ADOFields> flds;
|
||||
wxVERIFY(SUCCEEDED(rs->get_Fields(&flds)));
|
||||
wstring id;
|
||||
tn.names.clear();
|
||||
|
||||
{
|
||||
com_obj<ADOField> f;
|
||||
wxVERIFY(SUCCEEDED(flds->get_Item(variant(L"oznaka"), &f)));
|
||||
wxCHECK(GetValue(f, tn.tag), false);
|
||||
}
|
||||
|
||||
{
|
||||
com_obj<ADOField> f;
|
||||
wxVERIFY(SUCCEEDED(flds->get_Item(variant(L"opis_en"), &f)));
|
||||
LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), SORT_DEFAULT);
|
||||
list<wstring> names;
|
||||
wxCHECK(GetTagNames(f, lcid, names), false);
|
||||
tn.names.insert(std::move(pair<LCID, list<wstring> >(lcid, std::move(names))));
|
||||
}
|
||||
|
||||
{
|
||||
com_obj<ADOField> f;
|
||||
wxVERIFY(SUCCEEDED(flds->get_Item(variant(L"opis_sl"), &f)));
|
||||
LCID lcid = MAKELCID(MAKELANGID(LANG_SLOVENIAN, SUBLANG_DEFAULT), SORT_DEFAULT);
|
||||
list<wstring> names;
|
||||
wxCHECK(GetTagNames(f, lcid, names), false);
|
||||
tn.names.insert(std::move(pair<LCID, list<wstring> >(lcid, std::move(names))));
|
||||
}
|
||||
|
||||
{
|
||||
com_obj<ADOField> f;
|
||||
wxVERIFY(SUCCEEDED(flds->get_Item(variant(L"opis_ru"), &f)));
|
||||
LCID lcid = MAKELCID(MAKELANGID(LANG_RUSSIAN, SUBLANG_DEFAULT), SORT_DEFAULT);
|
||||
list<wstring> names;
|
||||
wxCHECK(GetTagNames(f, lcid, names), false);
|
||||
tn.names.insert(std::move(pair<LCID, list<wstring> >(lcid, std::move(names))));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -26,6 +26,7 @@
|
||||
#include <WinStd/Win.h>
|
||||
|
||||
#include <adoint.h>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
@ -311,11 +312,30 @@ namespace ZRCola {
|
||||
};
|
||||
|
||||
|
||||
///
|
||||
/// Character tag
|
||||
///
|
||||
class chrtag {
|
||||
public:
|
||||
wchar_t chr; ///> Character
|
||||
int tag; ///< Tag ID
|
||||
};
|
||||
|
||||
|
||||
///
|
||||
/// Tag name
|
||||
///
|
||||
class tagname {
|
||||
public:
|
||||
int tag; ///< Tag ID
|
||||
std::map<LCID, std::list<std::wstring> > names; ///< Names
|
||||
};
|
||||
|
||||
|
||||
public:
|
||||
DBSource();
|
||||
virtual ~DBSource();
|
||||
|
||||
|
||||
///
|
||||
/// Opens the database
|
||||
///
|
||||
@ -327,13 +347,11 @@ namespace ZRCola {
|
||||
///
|
||||
bool Open(LPCTSTR filename);
|
||||
|
||||
|
||||
///
|
||||
/// Logs errors in database connections
|
||||
///
|
||||
void LogErrors() const;
|
||||
|
||||
|
||||
///
|
||||
/// Is recordset at end
|
||||
///
|
||||
@ -349,7 +367,6 @@ namespace ZRCola {
|
||||
return FAILED(rs->get_EOF(&eof)) || eof ? true : false;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Gets number of records in a recordset
|
||||
///
|
||||
@ -363,7 +380,6 @@ namespace ZRCola {
|
||||
return SUCCEEDED(rs->get_RecordCount(&count)) ? count : (size_t)-1;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Splits string to individual keywords
|
||||
///
|
||||
@ -376,7 +392,6 @@ namespace ZRCola {
|
||||
///
|
||||
static bool GetKeywords(const wchar_t *str, std::vector< std::wstring > &keywords);
|
||||
|
||||
|
||||
///
|
||||
/// Gets boolean from ZRCola.zrc database
|
||||
///
|
||||
@ -389,7 +404,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool GetValue(const winstd::com_obj<ADOField>& f, bool& val) const;
|
||||
|
||||
|
||||
///
|
||||
/// Gets integer from ZRCola.zrc database
|
||||
///
|
||||
@ -402,7 +416,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool GetValue(const winstd::com_obj<ADOField>& f, int& val) const;
|
||||
|
||||
|
||||
///
|
||||
/// Gets string from ZRCola.zrc database
|
||||
///
|
||||
@ -415,7 +428,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool GetValue(const winstd::com_obj<ADOField>& f, std::wstring& val) const;
|
||||
|
||||
|
||||
///
|
||||
/// Gets encoded Unicode character from ZRCola.zrc database
|
||||
///
|
||||
@ -428,7 +440,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool GetUnicodeCharacter(const winstd::com_obj<ADOField>& f, wchar_t& chr) const;
|
||||
|
||||
|
||||
///
|
||||
/// Gets encoded Unicode string from ZRCola.zrc database
|
||||
///
|
||||
@ -441,7 +452,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool GetUnicodeString(const winstd::com_obj<ADOField>& f, std::wstring& str) const;
|
||||
|
||||
|
||||
///
|
||||
/// Gets language ID from ZRCola.zrc database
|
||||
///
|
||||
@ -454,7 +464,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool GetLanguage(const winstd::com_obj<ADOField>& f, langid_t& lang) const;
|
||||
|
||||
|
||||
///
|
||||
/// Gets character category ID from ZRCola.zrc database
|
||||
///
|
||||
@ -467,6 +476,17 @@ namespace ZRCola {
|
||||
///
|
||||
bool GetChrCat(const winstd::com_obj<ADOField>& f, chrcatid_t& cc) const;
|
||||
|
||||
///
|
||||
/// Gets tag names from ZRCola.zrc database
|
||||
///
|
||||
/// \param[in] f Data field
|
||||
/// \param[out] names Output names
|
||||
///
|
||||
/// \returns
|
||||
/// - true when successful
|
||||
/// - false otherwise
|
||||
///
|
||||
bool GetTagNames(const winstd::com_obj<ADOField>& f, LCID lcid, std::list<std::wstring>& names) const;
|
||||
|
||||
///
|
||||
/// Returns character translations
|
||||
@ -479,7 +499,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool SelectTranslations(winstd::com_obj<ADORecordset>& rs) const;
|
||||
|
||||
|
||||
///
|
||||
/// Returns translation data
|
||||
///
|
||||
@ -492,7 +511,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool GetTranslation(const winstd::com_obj<ADORecordset>& rs, translation& t) const;
|
||||
|
||||
|
||||
///
|
||||
/// Returns key sequences
|
||||
///
|
||||
@ -504,7 +522,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool SelectKeySequences(winstd::com_obj<ADORecordset>& rs) const;
|
||||
|
||||
|
||||
///
|
||||
/// Returns key sequence data
|
||||
///
|
||||
@ -517,7 +534,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool GetKeySequence(const winstd::com_obj<ADORecordset>& rs, keyseq& ks) const;
|
||||
|
||||
|
||||
///
|
||||
/// Returns languages
|
||||
///
|
||||
@ -529,7 +545,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool SelectLanguages(winstd::com_obj<ADORecordset>& rs) const;
|
||||
|
||||
|
||||
///
|
||||
/// Returns language data
|
||||
///
|
||||
@ -542,7 +557,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool GetLanguage(const winstd::com_obj<ADORecordset>& rs, language& lang) const;
|
||||
|
||||
|
||||
///
|
||||
/// Returns language character
|
||||
///
|
||||
@ -554,7 +568,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool SelectLanguageCharacters(winstd::com_obj<ADORecordset>& rs) const;
|
||||
|
||||
|
||||
///
|
||||
/// Returns language character data
|
||||
///
|
||||
@ -567,7 +580,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool GetLanguageCharacter(const winstd::com_obj<ADORecordset>& rs, langchar& lc) const;
|
||||
|
||||
|
||||
///
|
||||
/// Returns character groups
|
||||
///
|
||||
@ -579,7 +591,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool SelectCharacterGroups(winstd::com_obj<ADORecordset>& rs) const;
|
||||
|
||||
|
||||
///
|
||||
/// Returns character group data
|
||||
///
|
||||
@ -603,7 +614,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool SelectCharacters(winstd::com_obj<ADORecordset>& rs) const;
|
||||
|
||||
|
||||
///
|
||||
/// Returns character data
|
||||
///
|
||||
@ -627,7 +637,6 @@ namespace ZRCola {
|
||||
///
|
||||
bool SelectCharacterCategories(winstd::com_obj<ADORecordset>& rs) const;
|
||||
|
||||
|
||||
///
|
||||
/// Returns character category data
|
||||
///
|
||||
@ -640,6 +649,52 @@ namespace ZRCola {
|
||||
///
|
||||
bool GetCharacterCategory(const winstd::com_obj<ADORecordset>& rs, chrcat& cc) const;
|
||||
|
||||
///
|
||||
/// Returns character tags
|
||||
///
|
||||
/// \param[out] rs Recordset with results
|
||||
///
|
||||
/// \returns
|
||||
/// - true when query succeeds
|
||||
/// - false otherwise
|
||||
///
|
||||
bool SelectCharacterTags(winstd::com_obj<ADORecordset>& rs) const;
|
||||
|
||||
///
|
||||
/// Returns character tag data
|
||||
///
|
||||
/// \param[in] rs Recordset with results
|
||||
/// \param[out] cc Character tag
|
||||
///
|
||||
/// \returns
|
||||
/// - true when succeeded
|
||||
/// - false otherwise
|
||||
///
|
||||
bool GetCharacterTag(const winstd::com_obj<ADORecordset>& rs, chrtag& tc) const;
|
||||
|
||||
///
|
||||
/// Returns tag names
|
||||
///
|
||||
/// \param[out] rs Recordset with results
|
||||
///
|
||||
/// \returns
|
||||
/// - true when query succeeds
|
||||
/// - false otherwise
|
||||
///
|
||||
bool SelectTagNames(winstd::com_obj<ADORecordset>& rs) const;
|
||||
|
||||
///
|
||||
/// Returns tag name data
|
||||
///
|
||||
/// \param[in] rs Recordset with results
|
||||
/// \param[out] tn Tag name
|
||||
///
|
||||
/// \returns
|
||||
/// - true when succeeded
|
||||
/// - false otherwise
|
||||
///
|
||||
bool GetTagName(const winstd::com_obj<ADORecordset>& rs, tagname& tn) const;
|
||||
|
||||
protected:
|
||||
std::basic_string<TCHAR> m_filename; ///< Database filename
|
||||
winstd::com_obj<ADOConnection> m_db; ///< Database
|
||||
|
@ -610,6 +610,103 @@ int _tmain(int argc, _TCHAR *argv[])
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Get characters tags.
|
||||
com_obj<ADORecordset> rs;
|
||||
if (src.SelectCharacterTags(rs)) {
|
||||
size_t count = src.GetRecordsetCount(rs);
|
||||
if (count < 0xffffffff) { // 4G check (-1 is reserved for error condition)
|
||||
ZRCola::DBSource::chrtag ct;
|
||||
ZRCola::chrtag_db db;
|
||||
|
||||
// Preallocate memory.
|
||||
db.idxChr.reserve(count);
|
||||
db.idxTag.reserve(count);
|
||||
db.data .reserve(count*4);
|
||||
|
||||
// Parse characters tags and build index and data.
|
||||
for (; !ZRCola::DBSource::IsEOF(rs); rs->MoveNext()) {
|
||||
// Read characters tags from the database.
|
||||
if (src.GetCharacterTag(rs, ct)) {
|
||||
// Add characters tags to index and data.
|
||||
unsigned __int32 idx = db.data.size();
|
||||
db.data.push_back(ct.chr);
|
||||
wxASSERT_MSG((int)0xffff8000 <= ct.tag && ct.tag <= (int)0x00007fff, wxT("tag out of bounds"));
|
||||
db.data.push_back((unsigned __int16)ct.tag);
|
||||
db.idxChr.push_back(idx);
|
||||
db.idxTag.push_back(idx);
|
||||
} else
|
||||
has_errors = true;
|
||||
}
|
||||
|
||||
// Sort indices.
|
||||
db.idxChr .sort();
|
||||
db.idxTag.sort();
|
||||
|
||||
// Write characters tags to file.
|
||||
dst << ZRCola::chrtag_rec(db);
|
||||
} else {
|
||||
_ftprintf(stderr, wxT("%s: error ZCC0021: Error getting characters tags count from database or too many character tags.\n"), (LPCTSTR)filenameIn.c_str());
|
||||
has_errors = true;
|
||||
}
|
||||
} else {
|
||||
_ftprintf(stderr, wxT("%s: error ZCC0020: Error getting characters tags from database. Please make sure the file is ZRCola.zrc compatible.\n"), (LPCTSTR)filenameIn.c_str());
|
||||
has_errors = true;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Get tag names.
|
||||
com_obj<ADORecordset> rs;
|
||||
if (src.SelectTagNames(rs)) {
|
||||
size_t count = src.GetRecordsetCount(rs);
|
||||
if (count < 0xffffffff) { // 4G check (-1 is reserved for error condition)
|
||||
ZRCola::DBSource::tagname tn;
|
||||
ZRCola::tagname_db db;
|
||||
|
||||
// Preallocate memory.
|
||||
db.idxName.reserve(count*3);
|
||||
db.data .reserve(count*3*4);
|
||||
|
||||
// Parse tags and build index and data.
|
||||
for (; !ZRCola::DBSource::IsEOF(rs); rs->MoveNext()) {
|
||||
// Read tag name from the database.
|
||||
if (src.GetTagName(rs, tn)) {
|
||||
// Add tag name to index and data.
|
||||
for (auto ln = tn.names.cbegin(), ln_end = tn.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 <= tn.tag && tn.tag <= (int)0x00007fff, wxT("tag out of bounds"));
|
||||
db.data.push_back((unsigned __int16)tn.tag);
|
||||
db.data.push_back(LOWORD(ln->first));
|
||||
db.data.push_back(HIWORD(ln->first));
|
||||
wstring::size_type n = nm->length();
|
||||
wxASSERT_MSG(n <= 0xffff, wxT("tag name too long"));
|
||||
db.data.push_back((unsigned __int16)n);
|
||||
for (wstring::size_type i = 0; i < n; i++)
|
||||
db.data.push_back(nm->at(i));
|
||||
db.idxName.push_back(idx);
|
||||
}
|
||||
}
|
||||
} else
|
||||
has_errors = true;
|
||||
}
|
||||
|
||||
// Sort indices.
|
||||
db.idxName.sort();
|
||||
|
||||
// Write tags to file.
|
||||
dst << ZRCola::tagname_rec(db);
|
||||
} else {
|
||||
_ftprintf(stderr, wxT("%s: error ZCC0023: Error getting tag name count from database or too many tags.\n"), (LPCTSTR)filenameIn.c_str());
|
||||
has_errors = true;
|
||||
}
|
||||
} else {
|
||||
_ftprintf(stderr, wxT("%s: error ZCC0022: Error getting tags from database. Please make sure the file is ZRCola.zrc compatible.\n"), (LPCTSTR)filenameIn.c_str());
|
||||
has_errors = true;
|
||||
}
|
||||
}
|
||||
|
||||
idrec::close<ZRCola::recordid_t, ZRCola::recordsize_t, ZRCOLA_RECORD_ALIGN>(dst, dst_start);
|
||||
|
||||
if (dst.fail()) {
|
||||
|
@ -24,6 +24,7 @@
|
||||
|
||||
#include <zrcola/language.h>
|
||||
#include <zrcola/translate.h>
|
||||
#include <zrcola/tag.h>
|
||||
#include <zrcolaui/chargroup.h>
|
||||
#include <zrcolaui/keyboard.h>
|
||||
|
||||
|
@ -37,6 +37,7 @@
|
||||
<ClInclude Include="..\include\zrcola\common.h" />
|
||||
<ClInclude Include="..\include\zrcola\language.h" />
|
||||
<ClInclude Include="..\include\zrcola\normalize.h" />
|
||||
<ClInclude Include="..\include\zrcola\tag.h" />
|
||||
<ClInclude Include="..\include\zrcola\translate.h" />
|
||||
<ClInclude Include="..\src\stdafx.h" />
|
||||
</ItemGroup>
|
||||
|
@ -56,6 +56,9 @@
|
||||
<ClInclude Include="..\include\zrcola\character.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\include\zrcola\tag.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="..\res\libZRCola.rc">
|
||||
|
444
lib/libZRCola/include/zrcola/tag.h
Normal file
444
lib/libZRCola/include/zrcola/tag.h
Normal file
@ -0,0 +1,444 @@
|
||||
/*
|
||||
Copyright 2015-2016 Amebis
|
||||
|
||||
This file is part of ZRCola.
|
||||
|
||||
ZRCola is free software: you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
ZRCola is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with ZRCola. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdex/idrec.h>
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4200)
|
||||
#pragma warning(disable: 4251)
|
||||
#pragma warning(disable: 4512)
|
||||
|
||||
|
||||
namespace ZRCola {
|
||||
typedef unsigned __int16 tagid_t;
|
||||
|
||||
///
|
||||
/// Character Tag Database
|
||||
///
|
||||
class ZRCOLA_API chrtag_db {
|
||||
public:
|
||||
#pragma pack(push)
|
||||
#pragma pack(2)
|
||||
///
|
||||
/// Character tag data
|
||||
///
|
||||
struct chrtag {
|
||||
wchar_t chr; ///> Character
|
||||
tagid_t tag; ///< Tag ID
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
///
|
||||
/// Character Index
|
||||
///
|
||||
class indexChar : public index<unsigned __int16, unsigned __int32, chrtag>
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Constructs the index
|
||||
///
|
||||
/// \param[in] h Reference to vector holding the data
|
||||
///
|
||||
indexChar(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, chrtag>(h) {}
|
||||
|
||||
///
|
||||
/// Compares two character tags by character (for searching)
|
||||
///
|
||||
/// \param[in] a Pointer to first element
|
||||
/// \param[in] b Pointer to second element
|
||||
///
|
||||
/// \returns
|
||||
/// - <0 when a < b
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare(_In_ const chrtag &a, _In_ const chrtag &b) const
|
||||
{
|
||||
if (a.chr < b.chr) return -1;
|
||||
else if (a.chr > b.chr) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
///
|
||||
/// Compares two character tags by character (for sorting)
|
||||
///
|
||||
/// \param[in] a Pointer to first element
|
||||
/// \param[in] b Pointer to second element
|
||||
///
|
||||
/// \returns
|
||||
/// - <0 when a < b
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare_sort(_In_ const chrtag &a, _In_ const chrtag &b) const
|
||||
{
|
||||
if (a.chr < b.chr) return -1;
|
||||
else if (a.chr > b.chr) return 1;
|
||||
|
||||
if (a.tag < b.tag) return -1;
|
||||
else if (a.tag > b.tag) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
} idxChr; ///< Character index
|
||||
|
||||
|
||||
///
|
||||
/// Tag Index
|
||||
///
|
||||
class indexTag : public index<unsigned __int16, unsigned __int32, chrtag>
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Constructs the index
|
||||
///
|
||||
/// \param[in] h Reference to vector holding the data
|
||||
///
|
||||
indexTag(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, chrtag>(h) {}
|
||||
|
||||
///
|
||||
/// Compares two character tags by tag (for searching)
|
||||
///
|
||||
/// \param[in] a Pointer to first element
|
||||
/// \param[in] b Pointer to second element
|
||||
///
|
||||
/// \returns
|
||||
/// - <0 when a < b
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare(_In_ const chrtag &a, _In_ const chrtag &b) const
|
||||
{
|
||||
if (a.tag < b.tag) return -1;
|
||||
else if (a.tag > b.tag) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
///
|
||||
/// Compares two character tags by tag (for sorting)
|
||||
///
|
||||
/// \param[in] a Pointer to first element
|
||||
/// \param[in] b Pointer to second element
|
||||
///
|
||||
/// \returns
|
||||
/// - <0 when a < b
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare_sort(_In_ const chrtag &a, _In_ const chrtag &b) const
|
||||
{
|
||||
if (a.tag < b.tag) return -1;
|
||||
else if (a.tag > b.tag) return 1;
|
||||
|
||||
if (a.chr < b.chr) return -1;
|
||||
else if (a.chr > b.chr) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
} idxTag; ///< Tag index
|
||||
|
||||
std::vector<unsigned __int16> data; ///< Character tags data
|
||||
|
||||
public:
|
||||
///
|
||||
/// Constructs the database
|
||||
///
|
||||
inline chrtag_db() : idxChr(data), idxTag(data) {}
|
||||
};
|
||||
|
||||
|
||||
typedef ZRCOLA_API stdex::idrec::record<chrtag_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> chrtag_rec;
|
||||
|
||||
|
||||
///
|
||||
/// Tag name database
|
||||
///
|
||||
class ZRCOLA_API tagname_db {
|
||||
public:
|
||||
#pragma pack(push)
|
||||
#pragma pack(2)
|
||||
///
|
||||
/// Tag name data
|
||||
///
|
||||
struct tagname {
|
||||
tagid_t tag; ///< Tag ID
|
||||
LCID lang; ///< Language ID
|
||||
unsigned __int16 name_len; ///< \c name length (in characters)
|
||||
wchar_t name[]; ///< Tag localized name
|
||||
|
||||
///
|
||||
/// Compares two names
|
||||
///
|
||||
/// \param[in] lcid Locale ID to use for compare
|
||||
/// \param[in] str_a First name
|
||||
/// \param[in] count_a Number of characters in string \p str_a
|
||||
/// \param[in] str_b Second name
|
||||
/// \param[in] count_b Number of characters in string \p str_b
|
||||
///
|
||||
/// \returns
|
||||
/// - <0 when str_a < str_b
|
||||
/// - =0 when str_a == str_b
|
||||
/// - >0 when str_a > str_b
|
||||
///
|
||||
/// \note
|
||||
/// 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.
|
||||
///
|
||||
static inline int CompareName(LCID lcid, const wchar_t *str_a, unsigned __int16 count_a, const wchar_t *str_b, unsigned __int16 count_b)
|
||||
{
|
||||
switch (CompareString(lcid, LINGUISTIC_IGNORECASE | LINGUISTIC_IGNOREDIACRITIC | NORM_LINGUISTIC_CASING | NORM_IGNOREWIDTH, str_a, count_a, str_b, count_b)) {
|
||||
case CSTR_LESS_THAN : return -1;
|
||||
case CSTR_EQUAL : return 0;
|
||||
case CSTR_GREATER_THAN: return 1;
|
||||
default : assert(0); return -1;
|
||||
}
|
||||
}
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
///
|
||||
/// Name index
|
||||
///
|
||||
class indexName : public index<unsigned __int16, unsigned __int32, tagname>
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Constructs the index
|
||||
///
|
||||
/// \param[in] h Reference to vector holding the data
|
||||
/// \param[in] lcid Locale used to perform tag name comparison
|
||||
///
|
||||
indexName(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, tagname>(h) {}
|
||||
|
||||
///
|
||||
/// Compares two tag names by name (for searching)
|
||||
///
|
||||
/// \param[in] a Pointer to first element
|
||||
/// \param[in] b Pointer to second element
|
||||
///
|
||||
/// \returns
|
||||
/// - <0 when a < b
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare(_In_ const tagname &a, _In_ const tagname &b) const
|
||||
{
|
||||
if (a.lang < b.lang) return -1;
|
||||
else if (a.lang > b.lang) return 1;
|
||||
|
||||
int r = tagname::CompareName(a.lang, a.name, a.name_len, b.name, b.name_len);
|
||||
if (r != 0) return r;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
///
|
||||
/// Compares two tag names by name (for sorting)
|
||||
///
|
||||
/// \param[in] a Pointer to first element
|
||||
/// \param[in] b Pointer to second element
|
||||
///
|
||||
/// \returns
|
||||
/// - <0 when a < b
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare_sort(_In_ const tagname &a, _In_ const tagname &b) const
|
||||
{
|
||||
if (a.lang < b.lang) return -1;
|
||||
else if (a.lang > b.lang) return 1;
|
||||
|
||||
int r = tagname::CompareName(a.lang, a.name, a.name_len, b.name, b.name_len);
|
||||
if (r != 0) return r;
|
||||
|
||||
if (a.tag < b.tag) return -1;
|
||||
else if (a.tag > b.tag) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
} idxName; ///< Name index
|
||||
|
||||
std::vector<unsigned __int16> data; ///< Tag data
|
||||
|
||||
public:
|
||||
///
|
||||
/// Constructs the database
|
||||
///
|
||||
/// \param[in] lcid Locale used to perform tag name comparison
|
||||
///
|
||||
inline tagname_db() : idxName(data) {}
|
||||
};
|
||||
|
||||
|
||||
typedef ZRCOLA_API stdex::idrec::record<tagname_db, recordid_t, recordsize_t, ZRCOLA_RECORD_ALIGN> tagname_rec;
|
||||
};
|
||||
|
||||
|
||||
const ZRCola::recordid_t stdex::idrec::record<ZRCola::chrtag_db, ZRCola::recordid_t, ZRCola::recordsize_t, ZRCOLA_RECORD_ALIGN>::id = *(ZRCola::recordid_t*)"C-T";
|
||||
const ZRCola::recordid_t stdex::idrec::record<ZRCola::tagname_db, ZRCola::recordid_t, ZRCola::recordsize_t, ZRCOLA_RECORD_ALIGN>::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)*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)*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 tag index.
|
||||
if (stream.fail()) return stream;
|
||||
stream << db.idxName;
|
||||
|
||||
// Write data count.
|
||||
auto data_count = db.data.size();
|
||||
#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
|
||||
// 4G check
|
||||
if (data_count > 0xffffffff) {
|
||||
stream.setstate(std::ios_base::failbit);
|
||||
return stream;
|
||||
}
|
||||
#endif
|
||||
if (stream.fail()) return stream;
|
||||
unsigned __int32 count = (unsigned __int32)data_count;
|
||||
stream.write((const char*)&count, sizeof(count));
|
||||
|
||||
// Write data.
|
||||
if (stream.fail()) return stream;
|
||||
stream.write((const char*)db.data.data(), sizeof(unsigned __int16)*count);
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Reads 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 tag index.
|
||||
stream >> db.idxName;
|
||||
if (!stream.good()) return stream;
|
||||
|
||||
// Read data count.
|
||||
unsigned __int32 count;
|
||||
stream.read((char*)&count, sizeof(count));
|
||||
if (!stream.good()) return stream;
|
||||
|
||||
if (count) {
|
||||
// Read data.
|
||||
db.data.resize(count);
|
||||
stream.read((char*)db.data.data(), sizeof(unsigned __int16)*count);
|
||||
} else
|
||||
db.data.clear();
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
#pragma warning(pop)
|
@ -25,6 +25,7 @@
|
||||
#include "../include/zrcola/language.h"
|
||||
#include "../include/zrcola/normalize.h"
|
||||
#include "../include/zrcola/translate.h"
|
||||
#include "../include/zrcola/tag.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user