The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swlocale.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * swlocale.cpp - implementation of Class SWLocale used for retrieval
4  * of locale lookups
5  *
6  * $Id: swlocale.cpp 3640 2019-05-30 21:54:06Z scribe $
7  *
8  * Copyright 2000-2013 CrossWire Bible Society (http://www.crosswire.org)
9  * CrossWire Bible Society
10  * P. O. Box 2528
11  * Tempe, AZ 85280-2528
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  */
23 
24 #include <swlocale.h>
25 #include <utilstr.h>
26 #include <map>
27 #include <swconfig.h>
28 #include <versekey.h>
29 #include <versificationmgr.h>
30 
31 
33 
34 
35 namespace {
36  typedef std::map < SWBuf, SWBuf, std::less < SWBuf > >LookupMap;
37 }
38 
39 
40 const char *SWLocale::DEFAULT_LOCALE_NAME="en";
41 
42 
43 // I hate bridge patterns, but this hides swconfig and map from lots o stuff
45 public:
48 };
49 
50 
51 SWLocale::SWLocale(const char *ifilename) {
52  p = new Private;
53  ConfigEntMap::iterator confEntry;
54 
55  name = 0;
56  description = 0;
57  encoding = 0;
58  bookAbbrevs = 0;
59  bookLongNames = 0;
60  bookPrefAbbrev = 0;
61  if (ifilename) {
62  localeSource = new SWConfig(ifilename);
63  }
64  else {
65  localeSource = new SWConfig(0);
66  (*localeSource)["Meta"]["Name"] = DEFAULT_LOCALE_NAME;
67  (*localeSource)["Meta"]["Description"] = "English (US)";
70  }
71 
72  confEntry = localeSource->getSection("Meta").find("Name");
73  if (confEntry != localeSource->getSection("Meta").end())
74  stdstr(&name, (*confEntry).second.c_str());
75 
76  confEntry = localeSource->getSection("Meta").find("Description");
77  if (confEntry != localeSource->getSection("Meta").end())
78  stdstr(&description, (*confEntry).second.c_str());
79 
80  confEntry = localeSource->getSection("Meta").find("Encoding"); //Either empty (==Latin1) or UTF-8
81  if (confEntry != localeSource->getSection("Meta").end())
82  stdstr(&encoding, (*confEntry).second.c_str());
83 }
84 
85 
87 
88  delete localeSource;
89 
90  if (encoding)
91  delete [] encoding;
92 
93  if (description)
94  delete [] description;
95 
96  if (name)
97  delete [] name;
98 
100  delete [] bookAbbrevs;
101 
102  delete p;
103 }
104 
105 
106 const char *SWLocale::translate(const char *text) {
107  LookupMap::iterator entry;
108 
109  entry = p->lookupTable.find(text);
110 
111  if (entry == p->lookupTable.end()) {
112  ConfigEntMap::const_iterator confEntry;
113  bool found = false;
114 
115  SWBuf textBuf = text;
116  if (textBuf.startsWith("prefAbbr_")) {
117  textBuf.stripPrefix('_');
118  confEntry = localeSource->getSection("Pref Abbrevs").find(textBuf);
119  found = (confEntry != localeSource->getSection("Pref Abbrevs").end());
120  }
121  if (!found) {
122  confEntry = localeSource->getSection("Text").find(textBuf);
123  found = (confEntry != localeSource->getSection("Text").end());
124  }
125 
126  if (!found) {
127  p->lookupTable.insert(LookupMap::value_type(text, textBuf.c_str()));
128  }
129  else {//valid value found
130  /*
131  - If Encoding==Latin1 and we have a StringHelper, convert to UTF-8
132  - If StringHelper present and Encoding is UTF-8, use UTF8
133  - If StringHelper not present and Latin1, use Latin1
134  - If StringHelper not present and UTF-8, no idea what to do. Should't happen
135  */
136 /* if (StringHelper::getSystemStringHelper()) {
137  if (!strcmp(encoding, "UTF-8")) {
138  p->lookupTable.insert(LookupMap::value_type(text, (*confEntry).second.c_str()));
139  }
140  else { //latin1 expected, convert to UTF-8
141  SWBuf t((*confEntry).second.c_str());
142  t = StringHelper::getSystemStringHelper()->latin2unicode( t );
143 
144  p->lookupTable.insert(LookupMap::value_type(text, t.c_str()));
145  }
146  }
147  else { //no stringhelper, just insert. Nothing we can do*/
148  p->lookupTable.insert(LookupMap::value_type(text, (*confEntry).second.c_str()));
149 // }
150 
151  }
152  entry = p->lookupTable.find(text);
153  }
154  return (*entry).second.c_str();
155 }
156 
157 
158 const char *SWLocale::getName() {
159  return name;
160 }
161 
162 
164  return description;
165 }
166 
167 
168 const char *SWLocale::getEncoding() {
169  return encoding;
170 }
171 
172 
173 void SWLocale::augment(SWLocale &addFrom) {
174  *localeSource += *addFrom.localeSource;
175 }
176 
177 
178 const struct abbrev *SWLocale::getBookAbbrevs(int *retSize) {
179  static const char *nullstr = "";
180  if (!bookAbbrevs) {
181  // Assure all english abbrevs are present
182  for (int j = 0; builtin_abbrevs[j].osis[0]; j++) {
184  }
185  ConfigEntMap::iterator it = localeSource->getSection("Book Abbrevs").begin();
186  ConfigEntMap::iterator end = localeSource->getSection("Book Abbrevs").end();
187  for (; it != end; it++) {
188  p->mergedAbbrevs[it->first.c_str()] = it->second.c_str();
189  }
190  int size = (int)p->mergedAbbrevs.size();
191  bookAbbrevs = new struct abbrev[size + 1];
192  int i = 0;
193  for (LookupMap::iterator it = p->mergedAbbrevs.begin(); it != p->mergedAbbrevs.end(); it++, i++) {
194  bookAbbrevs[i].ab = it->first.c_str();
195  bookAbbrevs[i].osis = it->second.c_str();
196  }
197 
198  bookAbbrevs[i].ab = nullstr;
199  bookAbbrevs[i].osis = nullstr;
200  abbrevsCnt = size;
201  }
202 
203  *retSize = abbrevsCnt;
204  return bookAbbrevs;
205 }
206 
207 
209 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Private * p
Definition: swlocale.h:43
SWORD_NAMESPACE_START struct abbrev builtin_abbrevs[]
Definition: canon_abbrevs.h:36
std::map< SWBuf, SWBuf, std::less< SWBuf > > LookupMap
Definition: swlocale.cpp:36
virtual ~SWLocale()
Definition: swlocale.cpp:86
Definition: swbuf.h:47
char * name
Definition: swlocale.h:49
virtual struct abbrev * getBookAbbrevs(int *retSize)
Definition: swlocale.cpp:178
LookupMap lookupTable
Definition: swlocale.cpp:46
bool startsWith(const SWBuf &prefix) const
Definition: swbuf.h:486
const char * ab
int abbrevsCnt
Definition: swlocale.h:53
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
ConfigEntMap & getSection(const char *section)
Definition: swconfig.h:76
const char * c_str() const
Definition: swbuf.h:158
static const char * DEFAULT_LOCALE_NAME
Definition: swlocale.h:75
SWLocale(const char *ifilename)
Definition: swlocale.cpp:51
char * encoding
Definition: swlocale.h:51
struct abbrev * bookAbbrevs
Definition: swlocale.h:52
char * description
Definition: swlocale.h:50
const char * stripPrefix(char separator, bool endOfStringAsSeparator=false)
Definition: swbuf.h:457
virtual const char * translate(const char *text)
Definition: swlocale.cpp:106
SWConfig * localeSource
Definition: swlocale.h:47
int size
Definition: regex.c:5043
virtual const char * getDescription()
Definition: swlocale.cpp:163
LookupMap mergedAbbrevs
Definition: swlocale.cpp:47
const char ** bookPrefAbbrev
Definition: swlocale.h:55
virtual void augment(SWLocale &addFrom)
Definition: swlocale.cpp:173
const char * osis
#define SWORD_NAMESPACE_END
Definition: defs.h:40
virtual const char * getEncoding()
Definition: swlocale.cpp:168
const char ** bookLongNames
Definition: swlocale.h:54
virtual const char * getName()
Definition: swlocale.cpp:158