4 Website: http://www.vultaire.net/software/jben/
5 License: GNU General Public License (GPL) version 2
6 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>
24 #include "preferences.h"
26 #include "file_utils.h"
27 #include "encoding_convert.h"
28 #include "string_utils.h"
36 Preferences
* Preferences::prefsSingleton
= NULL
;
38 Preferences
*Preferences::Get() {
40 prefsSingleton
= new Preferences
;
41 /* Load with default preferences, and overwrite with any stored
43 prefsSingleton
->SetDefaultPrefs();
44 /* Try to load .jben, followed by jben.cfg if the first fails. */
45 prefsSingleton
->LoadFile(".jben") == REF_SUCCESS
||
46 prefsSingleton
->LoadFile("jben.cfg") == REF_SUCCESS
;
49 return prefsSingleton
;
52 Preferences::Preferences() {
53 /* Nothing to be done */
56 void Preferences::Destroy() {
58 delete prefsSingleton
;
59 prefsSingleton
= NULL
;
63 void Preferences::SetDefaultPrefs() {
65 KDO_READINGS
| KDO_MEANINGS
| KDO_HIGHIMPORTANCE
| KDO_VOCABCROSSREF
;
66 kanjidicDictionaries
= 0;
67 stringOpts
["config_version"] = "1";
70 stringOpts
["kdict_kanjidic"] = "dicts\\kanjidic";
71 stringOpts
["kdict_kradfile"] = "dicts\\kradfile";
72 stringOpts
["kdict_radkfile"] = "dicts\\radkfile";
73 stringOpts
["wdict_edict2"] = "dicts\\edict2";
76 stringOpts
["kdict_kanjidic"] = "dicts/kanjidic";
77 stringOpts
["kdict_kradfile"] = "dicts/kradfile";
78 stringOpts
["kdict_radkfile"] = "dicts/radkfile";
79 stringOpts
["wdict_edict2"] = "dicts/edict2";
85 int Preferences::LoadFile(const char *filename
) {
86 cfgFile
= GetCWD().append(1, DIRSEP
).append(filename
);
88 kanjidicOptions
= KDO_ALL
| KDO_UNHANDLED
;
89 kanjidicDictionaries
= 0; /* KDD_ALL */
91 int e
= ReadEncodedFile(filename
, s
);
93 /* Split into strings for each line */
94 list
<wstring
> tokenList
= StrTokenize
<wchar_t>(s
, L
"\n");
95 wstring token
, subToken
;
97 while(tokenList
.size()>0) {
98 token
= tokenList
.front();
99 tokenList
.pop_front();
100 if( (token
.length()>0) && (token
[0]!=L
'#') ) {
101 /* We only want to split the string into two subtokens, so we'll
103 index
= token
.find_first_of(_T(" \t"));
104 if(index
!=wstring::npos
) {
105 subToken
= token
.substr(0, index
);
106 subToken
= ToLower(subToken
);
108 if(subToken
==L
"kanjidicoptionmask") {
109 subToken
= token
.substr(index
+1);
110 subToken
= Trim(subToken
);
111 /*subToken.ToULong(&kanjidicOptions, 0);*/
112 /* We know the subtoken starts with 0x, so skip the
113 first two characters. */
114 wistringstream(subToken
.substr(2))
115 >> hex
>> kanjidicOptions
;
117 } else if(subToken
==L
"kanjidicdictionarymask") {
118 subToken
= token
.substr(index
+1);
119 subToken
= Trim(subToken
);
120 /*subToken.ToULong(&kanjidicDictionaries, 0);*/
121 wistringstream(subToken
.substr(2))
122 >> hex
>> kanjidicDictionaries
;
124 } else if(subToken
==L
"kanjilist") {
125 subToken
= token
.substr(index
+1);
126 subToken
= Trim(subToken
);
127 stringOpts
["kanjilist"] = utfconv_wm(subToken
);
128 } else if(subToken
==L
"vocablist") {
129 subToken
= token
.substr(index
+1);
130 subToken
= Trim(subToken
);
132 = StrTokenize
<wchar_t>(subToken
, L
";");
133 while(tSub
.size()>0) {
134 subToken
= tSub
.front();
136 if(subToken
.length()>0) {
137 string
*temp
= &stringOpts
["vocablist"];
139 temp
->append(1, '\n');
140 temp
->append(utfconv_wm(subToken
));
144 } else if(subToken
==L
"edict") {
145 subToken
= token
.substr(index
+1);
147 = utfconv_wm(Trim(subToken
));
149 } else if(subToken
==L
"kanjidic") {
150 subToken
= token
.substr(index
+1);
151 stringOpts
["kanjidic"]
152 = utfconv_wm(Trim(subToken
));
154 } else if(subToken
==L
"kradfile") {
155 subToken
= token
.substr(index
+1);
156 stringOpts
["kradfile"]
157 = utfconv_wm(Trim(subToken
));
159 } else if(subToken
==L
"radkfile") {
160 subToken
= token
.substr(index
+1);
161 stringOpts
["radkfile"]
162 = utfconv_wm(Trim(subToken
));
165 /* Unhandled - do nothing */
168 /* No space/tab was found. Check no-arg options, if any.
169 (Currently there are none.) */
171 } /* if(tokenlen>0, token[0]!=# */
172 } /* while(hasmoretokens) */
173 } /* if(file opened) */
177 Preferences::~Preferences() {
178 string prefs
= GetPrefsStr();
179 ofstream
ofile(cfgFile
.c_str(), ios::out
| ios::binary
);
180 if(ofile
.is_open()) {
186 string
Preferences::GetPrefsStr() {
188 string kanjiList
, vocabList
;
190 /* Get kanji and vocab lists in UTF-8 encoded strings */
191 kanjiList
= utfconv_wm(jben
->kanjiList
->ToString());
192 vocabList
= utfconv_wm(jben
->vocabList
->ToString(';'));
194 prefs
<< "KanjidicOptionMask 0x"
195 << uppercase
<< hex
<< setw(8) << setfill('0')
196 << kanjidicOptions
<< '\n';
197 prefs
<< "KanjidicDictionaryMask 0x"
198 << uppercase
<< hex
<< setw(8) << setfill('0')
199 << kanjidicDictionaries
<< '\n';
200 prefs
<< "KanjiList "
201 << utfconv_wm(jben
->kanjiList
->ToString()) << '\n';
202 prefs
<< "VocabList "
203 << utfconv_wm(jben
->vocabList
->ToString(';')) << '\n';
208 string
& Preferences::GetSetting(string key
) {
209 return stringOpts
[key
];