00001 /****************************************************************************** 00002 * swkey.cpp - code for base class 'SWKey'. SWKey is the basis for all 00003 * types of keys for indexing into modules (e.g. verse, word, 00004 * place, etc.) 00005 * 00006 * 00007 * Copyright 2009 CrossWire Bible Society (http://www.crosswire.org) 00008 * CrossWire Bible Society 00009 * P. O. Box 2528 00010 * Tempe, AZ 85280-2528 00011 * 00012 * This program is free software; you can redistribute it and/or modify it 00013 * under the terms of the GNU General Public License as published by the 00014 * Free Software Foundation version 2. 00015 * 00016 * This program is distributed in the hope that it will be useful, but 00017 * WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 * General Public License for more details. 00020 * 00021 */ 00022 00023 00024 #include <swkey.h> 00025 #include <utilstr.h> 00026 #include <string.h> 00027 #include <localemgr.h> 00028 00029 SWORD_NAMESPACE_START 00030 00031 static const char *classes[] = {"SWKey", "SWObject", 0}; 00032 SWClass SWKey::classdef(classes); 00033 SWKey::LocaleCache SWKey::localeCache; 00034 00035 /****************************************************************************** 00036 * SWKey Constructor - initializes instance of SWKey 00037 * 00038 * ENT: ikey - text key 00039 */ 00040 00041 SWKey::SWKey(const char *ikey) 00042 { 00043 init(); 00044 index = 0; 00045 persist = 0; 00046 keytext = 0; 00047 rangeText = 0; 00048 error = 0; 00049 userData = 0; 00050 stdstr(&keytext, ikey); 00051 } 00052 00053 SWKey::SWKey(SWKey const &k) 00054 { 00055 init(); 00056 stdstr(&localeName, k.localeName); 00057 index = k.index; 00058 persist = k.persist; 00059 userData = k.userData; 00060 keytext = 0; 00061 rangeText = 0; 00062 error = k.error; 00063 setText(k.getText()); 00064 } 00065 00066 void SWKey::init() { 00067 myclass = &classdef; 00068 boundSet = false; 00069 locale = 0; 00070 localeName = 0; 00071 setLocale(LocaleMgr::getSystemLocaleMgr()->getDefaultLocaleName()); 00072 } 00073 00074 SWKey *SWKey::clone() const 00075 { 00076 return new SWKey(*this); 00077 } 00078 00079 /****************************************************************************** 00080 * SWKey Destructor - cleans up instance of SWKey 00081 */ 00082 00083 SWKey::~SWKey() { 00084 delete [] keytext; 00085 delete [] rangeText; 00086 delete [] localeName; 00087 } 00088 00089 00090 /****************************************************************************** 00091 * SWKey::Persist - Gets whether this object itself persists within a 00092 * module that it was used to setKey or just a copy. 00093 * (1 - persists in module; 0 - a copy is attempted 00094 * 00095 * RET: value of persist 00096 */ 00097 00098 bool SWKey::isPersist() const 00099 { 00100 return persist; 00101 } 00102 00103 00104 /****************************************************************************** 00105 * SWKey::getPrivateLocale - Gets a real locale object from our name 00106 * 00107 * RET: locale object associated with our name 00108 */ 00109 00110 SWLocale *SWKey::getPrivateLocale() const { 00111 if (!locale) { 00112 if ((!localeCache.name) || (strcmp(localeCache.name, localeName))) { 00113 stdstr(&(localeCache.name), localeName); 00114 // this line is the entire bit of work we're trying to avoid with the cache 00115 // worth it? compare time examples/cmdline/search KJV "God love world" to 00116 // same with this method reduced to: 00117 // if (!local) local = ... (call below); return locale; 00118 localeCache.locale = LocaleMgr::getSystemLocaleMgr()->getLocale(localeName); 00119 } 00120 locale = localeCache.locale; 00121 } 00122 return locale; 00123 } 00124 00125 00126 /****************************************************************************** 00127 * SWKey::Persist - Set/gets whether this object itself persists within a 00128 * module that it was used to setKey or just a copy. 00129 * (true - persists in module; false - a copy is attempted 00130 * 00131 * ENT: ipersist - value which to set persist 00132 */ 00133 00134 void SWKey::setPersist(bool ipersist) 00135 { 00136 persist = ipersist; 00137 } 00138 00139 00140 /****************************************************************************** 00141 * SWKey::Error - Gets and clears error status 00142 * 00143 * RET: error status 00144 */ 00145 00146 char SWKey::popError() 00147 { 00148 char retval = error; 00149 00150 error = 0; 00151 return retval; 00152 } 00153 00154 00155 /****************************************************************************** 00156 * SWKey::setText Equates this SWKey to a character string 00157 * 00158 * ENT: ikey - other swkey object 00159 */ 00160 00161 void SWKey::setText(const char *ikey) { 00162 stdstr(&keytext, ikey); 00163 } 00164 00165 00166 /****************************************************************************** 00167 * SWKey::copyFrom Equates this SWKey to another SWKey object 00168 * 00169 * ENT: ikey - other swkey object 00170 */ 00171 00172 void SWKey::copyFrom(const SWKey &ikey) { 00173 // not desirable Persist(ikey.Persist()); 00174 setLocale(ikey.getLocale()); 00175 setText((const char *)ikey); 00176 } 00177 00178 00179 /****************************************************************************** 00180 * SWKey::getText - returns text key if (const char *) cast is requested 00181 */ 00182 00183 const char *SWKey::getText() const { 00184 return keytext; 00185 } 00186 00187 00188 /****************************************************************************** 00189 * SWKey::getRangeText - returns parsable range text for this key 00190 */ 00191 00192 const char *SWKey::getRangeText() const { 00193 stdstr(&rangeText, keytext); 00194 return rangeText; 00195 } 00196 00197 00198 /****************************************************************************** 00199 * SWKey::getOSISRefRangeText - returns parsable range text for this key 00200 */ 00201 00202 const char *SWKey::getOSISRefRangeText() const { 00203 return getRangeText(); 00204 } 00205 00206 00207 /****************************************************************************** 00208 * SWKey::compare - Compares another VerseKey object 00209 * 00210 * ENT: ikey - key to compare with this one 00211 * 00212 * RET: > 0 if this key is greater than compare key 00213 * < 0 00214 * 0 00215 */ 00216 00217 int SWKey::compare(const SWKey &ikey) 00218 { 00219 return strcmp((const char *)*this, (const char *)ikey); 00220 } 00221 00222 00223 /****************************************************************************** 00224 * SWKey::setPosition(SW_POSITION) - Positions this key if applicable 00225 */ 00226 00227 void SWKey::setPosition(SW_POSITION p) { 00228 switch (p) { 00229 case POS_TOP: 00230 // *this = ""; 00231 break; 00232 case POS_BOTTOM: 00233 // *this = "zzzzzzzzz"; 00234 break; 00235 } 00236 } 00237 00238 00239 /****************************************************************************** 00240 * SWKey::increment - Increments key a number of entries 00241 * 00242 * ENT: increment - Number of entries to jump forward 00243 * 00244 * RET: *this 00245 */ 00246 00247 void SWKey::increment(int) { 00248 error = KEYERR_OUTOFBOUNDS; 00249 } 00250 00251 00252 /****************************************************************************** 00253 * SWKey::decrement - Decrements key a number of entries 00254 * 00255 * ENT: decrement - Number of entries to jump backward 00256 * 00257 * RET: *this 00258 */ 00259 00260 void SWKey::decrement(int) { 00261 error = KEYERR_OUTOFBOUNDS; 00262 } 00263 00264 SWORD_NAMESPACE_END
1.6.1