From 92063cd2b2fad43efe18fcd089302d898dfe8265 Mon Sep 17 00:00:00 2001 From: Paul Goins Date: Tue, 29 Jan 2008 16:17:02 +0900 Subject: [PATCH] KDict converted to a singleton. --- dictionaries.cpp | 12 +++++++++--- dictionaries.h | 2 +- jben.cpp | 1 + kdict.cpp | 48 ++++++++++++++++++++++++++++++++++++++---------- kdict.h | 20 ++++++++++++++++---- 5 files changed, 65 insertions(+), 18 deletions(-) diff --git a/dictionaries.cpp b/dictionaries.cpp index 0905ddd..cf2f077 100644 --- a/dictionaries.cpp +++ b/dictionaries.cpp @@ -18,7 +18,8 @@ Dictionaries::Dictionaries() { Dictionaries::~Dictionaries() { if(wdict) delete wdict; - if(kdict) delete kdict; + /* kdict should NOT be destroyed here; KDict::Destroy() should be called + instead. I did this in jben.cpp's OnExit event. */ if(kradfile) delete kradfile; if(radkfile) delete radkfile; } @@ -31,9 +32,14 @@ bool Dictionaries::LoadWDict(const char* filename) { } bool Dictionaries::LoadKDict(const char* filename) { + /* I have a feeling this class will be tossed soon, as + the 4 classes it loads will be consolidated down to 2 + singletons, making this loader class obsolete. */ + /* Because of the above, I'm ignoring the filename arg + for now. */ int result; - kdict = KDict::LoadKDict(filename, result); - if(result == KD_SUCCESS) return true; + kdict = GetKDict(); + if(kdict!=NULL && kdict->GetHashTable()!=NULL) return true; return false; } diff --git a/dictionaries.h b/dictionaries.h index 8db5272..092937b 100644 --- a/dictionaries.h +++ b/dictionaries.h @@ -21,7 +21,7 @@ public: const RadKFile* GetRadKFile(); private: WDict* wdict; - KDict* kdict; + const KDict* kdict; KRadFile* kradfile; RadKFile* radkfile; }; diff --git a/jben.cpp b/jben.cpp index 5b5c457..85fa519 100644 --- a/jben.cpp +++ b/jben.cpp @@ -72,6 +72,7 @@ int JBen::OnExit() { #ifdef DEBUG printf("JBen::OnExit being processed...\n"); #endif + KDict::Destroy(); if(prefs) delete prefs; if(dicts) delete dicts; if(kanjiList) delete kanjiList; diff --git a/kdict.cpp b/kdict.cpp index ec0837e..4c867d5 100644 --- a/kdict.cpp +++ b/kdict.cpp @@ -30,10 +30,28 @@ along with this program. If not, see #include using namespace std; -KDict *KDict::LoadKDict(const char *filename, int& returnCode) { - KDict *k=NULL; +KDict* KDict::kdictSingleton = NULL; + +const KDict *KDict::GetKDict() { + if(kdictSingleton) return kdictSingleton; + kdictSingleton = new KDict; + kdictSingleton->LoadKanjidic(); + kdictSingleton->LoadKradfile(); + kdictSingleton->LoadRadkfile(); + return kdictSingleton; +} + +void KDict::Destroy() { + if(kdictSingleton) { + delete kdictSingleton; + kdictSingleton = NULL; + } +} + +int KDict::LoadKanjidic(const char *filename) { char *rawData = NULL; unsigned int size; + int returnCode=0xDEADBEEF; ifstream ifile(filename, ios::ate); /* "at end" to get our file size */ if(ifile) { @@ -51,7 +69,7 @@ KDict *KDict::LoadKDict(const char *filename, int& returnCode) { #endif /* Create the kanjidic object with our string data. */ - k = new KDict(rawData); + this->KanjidicParser(rawData); returnCode = KD_SUCCESS; } @@ -59,12 +77,22 @@ KDict *KDict::LoadKDict(const char *filename, int& returnCode) { returnCode = KD_FAILURE; if(rawData) delete[] rawData; - return k; + return returnCode; +} + +int KDict::LoadKradfile(const char *filename) { + int returnCode = 0xDEADBEEF; + return returnCode; +} + +int KDict::LoadRadkfile(const char *filename) { + int returnCode = 0xDEADBEEF; + return returnCode; } /* This could be sped up: copy the first UTF-8 character into a string, then run a conversion on that. Trivial though. */ -KDict::KDict(char *kanjidicRawData) { +void KDict::KanjidicParser(char *kanjidicRawData) { char *token = strtok(kanjidicRawData, "\n"); wxString wxToken; while(token) { @@ -73,7 +101,7 @@ KDict::KDict(char *kanjidicRawData) { /* Convert token to proper format */ wxToken = ConvertKanjidicEntry(wxToken); /* Add to hash table */ - if(!kanjiHash.assign(wxToken[0], token)) { + if(!kanjidicData.assign(wxToken[0], token)) { #ifdef DEBUG fprintf(stderr, "Error assigning (%lc, %ls) to hash table!\n", @@ -95,8 +123,8 @@ KDict::~KDict() { storage. This is followed by a slight reformatting of the string for better presentation. */ wxString KDict::GetKanjidicStr(wxChar c) const { - BoostHM::iterator it = kanjiHash.find(c); - if(it==kanjiHash.end()) return _T(""); + BoostHM::iterator it = kanjidicData.find(c); + if(it==kanjidicData.end()) return _T(""); wxString s; UTF8ToWx(it->second, s); return ConvertKanjidicEntry(s); @@ -623,8 +651,8 @@ int KDict::GetIntField(wxChar kanji, const wxString& marker) const { return (int)value; } -const BoostHM* const KDict::GetHashTable() const { - return &kanjiHash; +const BoostHM* KDict::GetHashTable() const { + return &kanjidicData; } enum { diff --git a/kdict.h b/kdict.h index f9b794d..adc574f 100644 --- a/kdict.h +++ b/kdict.h @@ -114,23 +114,35 @@ along with this program. If not, see class KDict { public: - static KDict *LoadKDict(const char *filename, int& returnCode); + static const KDict *GetKDict(); + static void Destroy(); ~KDict(); + /* Kanjidic-specific functions */ wxString GetKanjidicStr(wxChar c) const; static wxString KanjidicToHtml(const wxString& kanjidicStr); static wxString KanjidicToHtml(const wxString& kanjidicStr, long options, long dictionaries); int GetIntField(wxChar kanji, const wxString& marker) const; - const BoostHM* const GetHashTable() const; wxString GetOnyomiStr(wxChar c) const; wxString GetKunyomiStr(wxChar c) const; wxString GetEnglishStr(wxChar c) const; + const BoostHM* GetHashTable() const; + + /* Other functions (none yet) */ private: - KDict(char *kanjidicRawData); + /* Dictionary file loaders */ + int LoadKanjidic(const char *filename="kanjidic"); + int LoadRadkfile(const char *filename="radkfile"); + int LoadKradfile(const char *filename="kradfile"); + + /* Private kanjidic-specific stuff */ + void KanjidicParser(char *kanjidicRawData); static wxString ConvertKanjidicEntry(const wxString& s); wxString GetKanjidicReading(wxChar c, int readingType) const; - BoostHM kanjiHash; + BoostHM kanjidicData; + BoostHM radkData, kradData; + static KDict* kdictSingleton; }; #endif -- 2.11.4.GIT