The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swkey.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * swkey.h - class SWKey: SWKey is the basis for all indexing into modules
4  * (e.g. verses, table of contents tree, strings, etc.)
5  *
6  * $Id: swkey.h 3828 2020-11-24 23:15:44Z scribe $
7  *
8  * Copyright 1998-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 #ifndef SWKEY_H
25 #define SWKEY_H
26 
27 #include <swobject.h>
28 
29 #include <defs.h>
30 #include <sysdata.h>
31 #include <utilstr.h>
32 
34 
35 #define KEYERR_OUTOFBOUNDS 1
36 
37 #define SWKEY_OPERATORS \
38  SWKey &operator =(const char *ikey) { setText(ikey); return *this; } \
39  SWKey &operator =(const SWKey &ikey) { positionFrom(ikey); return *this; } \
40  SWKey &operator =(SW_POSITION pos) { setPosition(pos); return *this; } \
41  operator const char *() const { return getText(); } \
42  bool operator ==(const SWKey &ikey) { return equals(ikey); } \
43  bool operator !=(const SWKey &ikey) { return !equals(ikey); } \
44  virtual bool operator >(const SWKey &ikey) { return (compare(ikey) > 0); } \
45  virtual bool operator <(const SWKey &ikey) { return (compare(ikey) < 0); } \
46  virtual bool operator >=(const SWKey &ikey) { return (compare(ikey) > -1); } \
47  virtual bool operator <=(const SWKey &ikey) { return (compare(ikey) < 1); } \
48  SWKey &operator -=(int steps) { decrement(steps); return *this; } \
49  SWKey &operator +=(int steps) { increment(steps); return *this; } \
50  SWKey &operator ++() { increment(1); return *this; } \
51  SWKey operator ++(int) { SWKey temp = *this; increment(1); return temp; } \
52  SWKey &operator --() { decrement(1); return *this; } \
53  SWKey operator --(int) { SWKey temp = *this; decrement(1); return temp; }
54 
55 
58 class SW_POSITION {
59  char pos;
60 public:
61  SW_POSITION(char ipos) { pos = ipos; }
62  operator char() { return pos; }
63 };
64 
65 #define POS_TOP ((char)1)
66 #define POS_BOTTOM ((char)2)
67 
68 #define TOP SW_POSITION(POS_TOP)
69 #define BOTTOM SW_POSITION(POS_BOTTOM)
70 
71 class SWLocale;
72 
77 class SWDLLEXPORT SWKey : public SWObject {
78 
79  class LocaleCache {
80  public:
81  char *name;
84  name = 0;
85  locale = 0;
86  }
87  virtual ~LocaleCache() {
88  if (name)
89  delete[]name;
90  }
91  };
93  // for caching; don't use directly, call getPrivateLocale()
94  mutable SWLocale *locale;
95 
96 
97  long index;
98  void init();
99 
100 
101 protected:
102  char *keytext;
103  mutable char *rangeText;
104  mutable bool boundSet;
105  bool persist;
106  mutable char error;
107 
108  char *localeName;
109  SWLocale *getPrivateLocale() const;
110 
111 
112 public:
113 
114  // misc storage for whatever
116 
122  SWKey(const char *ikey = 0);
123 
127  SWKey(const SWKey &k);
128 
131  virtual ~SWKey();
132 
137  virtual SWKey *clone() const;
138 
139 
144  bool isPersist() const;
148  SWDEPRECATED char Persist() const { return isPersist(); }
149 
154  void setPersist(bool ipersist);
158  SWDEPRECATED char Persist(signed char ipersist) { setPersist(ipersist!=0); return isPersist(); }
159 
163  virtual char popError();
164  virtual char getError() const { return error; }
165  virtual void setError(char err) { error = err; }
169  SWDEPRECATED char Error() { return popError(); }
170 
174  virtual void setText(const char *ikey);
175 
179  virtual void copyFrom(const SWKey &ikey);
180  virtual void positionFrom(const SWKey &ikey) { copyFrom(ikey); }
181 
184  virtual const char *getText() const;
185  virtual const char *getShortText() const { return getText(); }
186  virtual const char *getRangeText() const;
187  virtual const char *getShortRangeText() const { return getRangeText(); }
188  virtual const char *getOSISRefRangeText() const;
189  virtual bool isBoundSet() const { return boundSet; }
190  virtual void clearBounds() const { boundSet = false; }
191 
198  virtual int compare(const SWKey &ikey);
199 
204  virtual bool equals(const SWKey &ikey) { return !compare(ikey); }
205 
206  virtual void setPosition(SW_POSITION);
207 
212  virtual void decrement(int steps = 1);
213 
218  virtual void increment(int steps = 1);
219 
222  virtual bool isTraversable() const { return false; }
223 
224  char *getLocale() const { return localeName; }
225  void setLocale(const char *name) { stdstr(&localeName, name); locale = 0; } // this will force an on demand lookup of our locale
226 
229  virtual long getIndex() const { return index; }
230 
233  virtual void setIndex(long iindex) { index = iindex; }
234 
236 
237  };
238 
240 #endif
SWLocale * locale
Definition: swkey.h:82
virtual bool isBoundSet() const
Definition: swkey.h:189
#define SWORD_NAMESPACE_START
Definition: defs.h:39
virtual const char * getShortRangeText() const
Definition: swkey.h:187
#define SWKEY_OPERATORS
Definition: swkey.h:37
virtual const char * getShortText() const
Definition: swkey.h:185
char * localeName
Definition: swkey.h:108
char pos
Definition: swkey.h:59
virtual void setIndex(long iindex)
Definition: swkey.h:233
#define SWDLLEXPORT
Definition: defs.h:171
SWDEPRECATED char Persist(signed char ipersist)
Definition: swkey.h:158
char * keytext
Definition: swkey.h:102
virtual bool equals(const SWKey &ikey)
Definition: swkey.h:204
#define SWDEPRECATED
Definition: defs.h:174
virtual void positionFrom(const SWKey &ikey)
Definition: swkey.h:180
SW_POSITION(char ipos)
Definition: swkey.h:61
char * name
Definition: swkey.h:81
virtual void clearBounds() const
Definition: swkey.h:190
bool boundSet
Definition: swkey.h:104
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
void init()
Definition: installmgr.cpp:164
virtual ~LocaleCache()
Definition: swkey.h:87
long index
Definition: swkey.h:97
SWLocale * locale
Definition: swkey.h:94
bool persist
Definition: swkey.h:105
unsigned long long SW_u64
Definition: sysdata.h:56
SWDEPRECATED char Error()
Definition: swkey.h:169
char * rangeText
Definition: swkey.h:103
static LocaleCache localeCache
Definition: swkey.h:92
char error
Definition: swkey.h:106
SW_u64 userData
Definition: swkey.h:115
virtual void setError(char err)
Definition: swkey.h:165
virtual bool isTraversable() const
Definition: swkey.h:222
#define SWORD_NAMESPACE_END
Definition: defs.h:40
virtual char getError() const
Definition: swkey.h:164
Definition: swkey.h:77
virtual long getIndex() const
Definition: swkey.h:229
void setLocale(const char *name)
Definition: swkey.h:225
SWDEPRECATED char Persist() const
Definition: swkey.h:148
char * getLocale() const
Definition: swkey.h:224