1
/******************************************************************************
2
* swlocale.cpp - implementation of Class SWLocale used for retrieval
5
* $Id: swlocale.cpp 2346 2009-04-27 01:53:58Z scribe $
7
* Copyright 2000 CrossWire Bible Society (http://www.crosswire.org)
8
* CrossWire Bible Society
10
* Tempe, AZ 85280-2528
12
* This program is free software; you can redistribute it and/or modify it
13
* under the terms of the GNU General Public License as published by the
14
* Free Software Foundation version 2.
16
* This program is distributed in the hope that it will be useful, but
17
* WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
* General Public License for more details.
32
typedef std::map < SWBuf, SWBuf, std::less < SWBuf > >LookupMap;
34
const char *SWLocale::DEFAULT_LOCALE_NAME="en_US";
36
// I have bridge patterns, but this hides swconfig and map from lots o stuff
37
class SWLocale::Private {
39
LookupMap lookupTable;
40
LookupMap mergedAbbrevs;
44
SWLocale::SWLocale(const char *ifilename) {
46
ConfigEntMap::iterator confEntry;
55
localeSource = new SWConfig(ifilename);
58
localeSource = new SWConfig(0);
59
(*localeSource)["Meta"]["Name"] = DEFAULT_LOCALE_NAME;
60
(*localeSource)["Meta"]["Description"] = "English (US)";
61
bookAbbrevs = (struct abbrev *)builtin_abbrevs;
62
for (abbrevsCnt = 0; builtin_abbrevs[abbrevsCnt].osis[0]; abbrevsCnt++);
65
confEntry = localeSource->Sections["Meta"].find("Name");
66
if (confEntry != localeSource->Sections["Meta"].end())
67
stdstr(&name, (*confEntry).second.c_str());
69
confEntry = localeSource->Sections["Meta"].find("Description");
70
if (confEntry != localeSource->Sections["Meta"].end())
71
stdstr(&description, (*confEntry).second.c_str());
73
confEntry = localeSource->Sections["Meta"].find("Encoding"); //Either empty (==Latin1) or UTF-8
74
if (confEntry != localeSource->Sections["Meta"].end())
75
stdstr(&encoding, (*confEntry).second.c_str());
79
SWLocale::~SWLocale() {
87
delete [] description;
92
if (bookAbbrevs != builtin_abbrevs)
93
delete [] bookAbbrevs;
99
const char *SWLocale::translate(const char *text) {
100
LookupMap::iterator entry;
102
entry = p->lookupTable.find(text);
104
if (entry == p->lookupTable.end()) {
105
ConfigEntMap::iterator confEntry;
106
confEntry = localeSource->Sections["Text"].find(text);
107
if (confEntry == localeSource->Sections["Text"].end())
108
p->lookupTable.insert(LookupMap::value_type(text, text));
109
else {//valid value found
111
- If Encoding==Latin1 and we have a StringHelper, convert to UTF-8
112
- If StringHelper present and Encoding is UTF-8, use UTF8
113
- If StringHelper not present and Latin1, use Latin1
114
- If StringHelper not present and UTF-8, no idea what to do. Should't happen
116
/* if (StringHelper::getSystemStringHelper()) {
117
if (!strcmp(encoding, "UTF-8")) {
118
p->lookupTable.insert(LookupMap::value_type(text, (*confEntry).second.c_str()));
120
else { //latin1 expected, convert to UTF-8
121
SWBuf t((*confEntry).second.c_str());
122
t = StringHelper::getSystemStringHelper()->latin2unicode( t );
124
p->lookupTable.insert(LookupMap::value_type(text, t.c_str()));
127
else { //no stringhelper, just insert. Nothing we can do*/
128
p->lookupTable.insert(LookupMap::value_type(text, (*confEntry).second.c_str()));
132
entry = p->lookupTable.find(text);
134
return (*entry).second.c_str();
138
const char *SWLocale::getName() {
143
const char *SWLocale::getDescription() {
147
const char *SWLocale::getEncoding() {
151
void SWLocale::augment(SWLocale &addFrom) {
152
*localeSource += *addFrom.localeSource;
156
const struct abbrev *SWLocale::getBookAbbrevs(int *retSize) {
157
static const char *nullstr = "";
159
// Assure all english abbrevs are present
160
for (int j = 0; builtin_abbrevs[j].osis[0]; j++) {
161
p->mergedAbbrevs[builtin_abbrevs[j].ab] = builtin_abbrevs[j].osis;
163
ConfigEntMap::iterator it = localeSource->Sections["Book Abbrevs"].begin();
164
ConfigEntMap::iterator end = localeSource->Sections["Book Abbrevs"].end();
165
for (; it != end; it++) {
166
p->mergedAbbrevs[it->first.c_str()] = it->second.c_str();
168
int size = p->mergedAbbrevs.size();
169
bookAbbrevs = new struct abbrev[size + 1];
171
for (LookupMap::iterator it = p->mergedAbbrevs.begin(); it != p->mergedAbbrevs.end(); it++, i++) {
172
bookAbbrevs[i].ab = it->first.c_str();
173
bookAbbrevs[i].osis = it->second.c_str();
176
bookAbbrevs[i].ab = nullstr;
177
bookAbbrevs[i].osis = nullstr;
181
*retSize = abbrevsCnt;