The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swkey.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * swkey.cpp - code for base class 'SWKey'. SWKey is the basis for all
4  * types of keys for indexing into modules (e.g. verse, word,
5  * place, etc.)
6  *
7  * $Id: swkey.cpp 3808 2020-10-02 13:23:34Z scribe $
8  *
9  * Copyright 1997-2013 CrossWire Bible Society (http://www.crosswire.org)
10  * CrossWire Bible Society
11  * P. O. Box 2528
12  * Tempe, AZ 85280-2528
13  *
14  * This program is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU General Public License as published by the
16  * Free Software Foundation version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * General Public License for more details.
22  *
23  */
24 
25 
26 #include <swkey.h>
27 #include <utilstr.h>
28 #include <string.h>
29 #include <localemgr.h>
30 
32 
33 static const char *classes[] = {"SWKey", "SWObject", 0};
34 static const SWClass classdef(classes);
36 
37 /******************************************************************************
38  * SWKey Constructor - initializes instance of SWKey
39  *
40  * ENT: ikey - text key
41  */
42 
43 SWKey::SWKey(const char *ikey) : SWObject(classdef)
44 {
45  init();
46  index = 0;
47  persist = 0;
48  keytext = 0;
49  rangeText = 0;
50  error = 0;
51  userData = 0;
52  stdstr(&keytext, ikey);
53 }
54 
56 {
57  init();
59  index = k.index;
60  persist = k.persist;
61  userData = k.userData;
62  keytext = 0;
63  rangeText = 0;
64  error = k.error;
65  setText(k.getText());
66 }
67 
68 void SWKey::init() {
69  boundSet = false;
70  locale = 0;
71  localeName = 0;
72  setLocale(LocaleMgr::getSystemLocaleMgr()->getDefaultLocaleName());
73 }
74 
76 {
77  return new SWKey(*this);
78 }
79 
80 /******************************************************************************
81  * SWKey Destructor - cleans up instance of SWKey
82  */
83 
85  delete [] keytext;
86  delete [] rangeText;
87  delete [] localeName;
88 }
89 
90 
91 /******************************************************************************
92  * SWKey::Persist - Gets whether this object itself persists within a
93  * module that it was used to setKey or just a copy.
94  * (1 - persists in module; 0 - a copy is attempted
95  *
96  * RET: value of persist
97  */
98 
99 bool SWKey::isPersist() const
100 {
101  return persist;
102 }
103 
104 
105 /******************************************************************************
106  * SWKey::getPrivateLocale - Gets a real locale object from our name
107  *
108  * RET: locale object associated with our name
109  */
110 
112  if (!locale) {
113  if ((!localeCache.name) || (strcmp(localeCache.name, localeName))) {
115  // this line is the entire bit of work we're trying to avoid with the cache
116  // worth it? compare time examples/cmdline/search KJV "God love world" to
117  // same with this method reduced to:
118  // if (!local) local = ... (call below); return locale;
120  }
122  }
123  return locale;
124 }
125 
126 
127 /******************************************************************************
128  * SWKey::Persist - Set/gets whether this object itself persists within a
129  * module that it was used to setKey or just a copy.
130  * (true - persists in module; false - a copy is attempted
131  *
132  * ENT: ipersist - value which to set persist
133  */
134 
135 void SWKey::setPersist(bool ipersist)
136 {
137  persist = ipersist;
138 }
139 
140 
141 /******************************************************************************
142  * SWKey::Error - Gets and clears error status
143  *
144  * RET: error status
145  */
146 
148 {
149  char retval = error;
150 
151  error = 0;
152  return retval;
153 }
154 
155 
156 /******************************************************************************
157  * SWKey::setText Equates this SWKey to a character string
158  *
159  * ENT: ikey - other swkey object
160  */
161 
162 void SWKey::setText(const char *ikey) {
163  stdstr(&keytext, ikey);
164 }
165 
166 
167 /******************************************************************************
168  * SWKey::copyFrom Equates this SWKey to another SWKey object
169  *
170  * ENT: ikey - other swkey object
171  */
172 
173 void SWKey::copyFrom(const SWKey &ikey) {
174 // not desirable Persist(ikey.Persist());
175  setLocale(ikey.getLocale());
176  setText((const char *)ikey);
177 }
178 
179 
180 /******************************************************************************
181  * SWKey::getText - returns text key if (const char *) cast is requested
182  */
183 
184 const char *SWKey::getText() const {
185  return keytext;
186 }
187 
188 
189 /******************************************************************************
190  * SWKey::getRangeText - returns parsable range text for this key
191  */
192 
193 const char *SWKey::getRangeText() const {
195  return rangeText;
196 }
197 
198 
199 /******************************************************************************
200  * SWKey::getOSISRefRangeText - returns parsable range text for this key
201  */
202 
203 const char *SWKey::getOSISRefRangeText() const {
204  return getRangeText();
205 }
206 
207 
208 /******************************************************************************
209  * SWKey::compare - Compares another VerseKey object
210  *
211  * ENT: ikey - key to compare with this one
212  *
213  * RET: > 0 if this key is greater than compare key
214  * < 0
215  * 0
216  */
217 
218 int SWKey::compare(const SWKey &ikey)
219 {
220  return strcmp((const char *)*this, (const char *)ikey);
221 }
222 
223 
224 /******************************************************************************
225  * SWKey::setPosition(SW_POSITION) - Positions this key if applicable
226  */
227 
229  switch (p) {
230  case POS_TOP:
231 // *this = "";
232  break;
233  case POS_BOTTOM:
234 // *this = "zzzzzzzzz";
235  break;
236  }
237 }
238 
239 
240 /******************************************************************************
241  * SWKey::increment - Increments key a number of entries
242  *
243  * ENT: increment - Number of entries to jump forward
244  *
245  * RET: *this
246  */
247 
248 void SWKey::increment(int) {
250 }
251 
252 
253 /******************************************************************************
254  * SWKey::decrement - Decrements key a number of entries
255  *
256  * ENT: decrement - Number of entries to jump backward
257  *
258  * RET: *this
259  */
260 
261 void SWKey::decrement(int) {
263 }
264 
SWLocale * locale
Definition: swkey.h:82
virtual void setText(const char *ikey)
Definition: swkey.cpp:162
#define SWORD_NAMESPACE_START
Definition: defs.h:39
virtual void decrement(int steps=1)
Definition: swkey.cpp:261
char * localeName
Definition: swkey.h:108
SWKey(const char *ikey=0)
Definition: swkey.cpp:43
virtual const char * getRangeText() const
Definition: swkey.cpp:193
virtual void increment(int steps=1)
Definition: swkey.cpp:248
char * keytext
Definition: swkey.h:102
char * name
Definition: swkey.h:81
virtual SWKey * clone() const
Definition: swkey.cpp:75
bool boundSet
Definition: swkey.h:104
virtual const char * getText() const
Definition: swkey.cpp:184
void setPersist(bool ipersist)
Definition: swkey.cpp:135
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
bool isPersist() const
Definition: swkey.cpp:99
void init()
Definition: swkey.cpp:68
virtual SWLocale * getLocale(const char *name)
Definition: localemgr.cpp:210
#define POS_TOP
Definition: swkey.h:65
virtual int compare(const SWKey &ikey)
Definition: swkey.cpp:218
long index
Definition: swkey.h:97
static LocaleMgr * getSystemLocaleMgr()
Definition: localemgr.cpp:54
static const SWClass classdef(classes)
SWLocale * locale
Definition: swkey.h:94
virtual void copyFrom(const SWKey &ikey)
Definition: swkey.cpp:173
bool persist
Definition: swkey.h:105
virtual const char * getOSISRefRangeText() const
Definition: swkey.cpp:203
static SWORD_NAMESPACE_START const char * classes[]
Definition: swkey.cpp:33
virtual void setPosition(SW_POSITION)
Definition: swkey.cpp:228
#define KEYERR_OUTOFBOUNDS
Definition: swkey.h:35
SWLocale * getPrivateLocale() const
Definition: swkey.cpp:111
virtual char popError()
Definition: swkey.cpp:147
#define POS_BOTTOM
Definition: swkey.h:66
char * rangeText
Definition: swkey.h:103
virtual ~SWKey()
Definition: swkey.cpp:84
static LocaleCache localeCache
Definition: swkey.h:92
char error
Definition: swkey.h:106
SW_u64 userData
Definition: swkey.h:115
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77
void setLocale(const char *name)
Definition: swkey.h:225
char * getLocale() const
Definition: swkey.h:224