1
by Dmitrijs Ledkovs
Import upstream version 1.5.11 |
1 |
/******************************************************************************
|
2 |
* swkey.h - code for base class 'swkey'. swkey is the basis for all
|
|
3 |
* types of keys for indexing into modules (e.g. verse, word,
|
|
4 |
* place, etc.)
|
|
5 |
*
|
|
6 |
* $Id: swkey.h 2121 2007-11-24 16:42:32Z scribe $
|
|
7 |
*
|
|
8 |
* Copyright 1998 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 |
||
31 |
SWORD_NAMESPACE_START
|
|
32 |
||
33 |
#define KEYERR_OUTOFBOUNDS 1
|
|
34 |
||
35 |
#define SWKEY_OPERATORS \
|
|
36 |
SWKey &operator =(const char *ikey) { setText(ikey); return *this; } \
|
|
37 |
SWKey &operator =(const SWKey &ikey) { copyFrom(ikey); return *this; } \
|
|
38 |
SWKey &operator =(SW_POSITION pos) { setPosition(pos); return *this; } \
|
|
39 |
operator const char *() const { return getText(); } \
|
|
40 |
bool operator ==(const SWKey &ikey) { return equals(ikey); } \
|
|
41 |
bool operator !=(const SWKey &ikey) { return !equals(ikey); } \
|
|
42 |
virtual bool operator >(const SWKey &ikey) { return (compare(ikey) > 0); } \
|
|
43 |
virtual bool operator <(const SWKey &ikey) { return (compare(ikey) < 0); } \
|
|
44 |
virtual bool operator >=(const SWKey &ikey) { return (compare(ikey) > -1); } \
|
|
45 |
virtual bool operator <=(const SWKey &ikey) { return (compare(ikey) < 1); } \
|
|
46 |
SWKey &operator -=(int steps) { decrement(steps); return *this; } \
|
|
47 |
SWKey &operator +=(int steps) { increment(steps); return *this; } \
|
|
48 |
SWKey &operator++(int) { return *this += 1; } \
|
|
49 |
SWKey &operator--(int) { return *this -= 1; }
|
|
50 |
||
51 |
||
52 |
/** For use with = operator to position key.
|
|
53 |
*/
|
|
54 |
class SW_POSITION { |
|
55 |
char pos; |
|
56 |
public: |
|
57 |
SW_POSITION(char ipos) { pos = ipos; } |
|
58 |
operator char() { return pos; } |
|
59 |
};
|
|
60 |
||
61 |
#define POS_TOP ((char)1)
|
|
62 |
#define POS_BOTTOM ((char)2)
|
|
63 |
||
64 |
#define TOP SW_POSITION(POS_TOP)
|
|
65 |
#define BOTTOM SW_POSITION(POS_BOTTOM)
|
|
66 |
||
67 |
/** SWKey is used for positioning an SWModule to a specific entry.
|
|
68 |
* It always represents a possible location into a module and can additionally represent
|
|
69 |
* a domain of entries (e.g. "John 3:16" in the domain "John 1:1 - Mark 5:25")
|
|
70 |
*/
|
|
71 |
class SWDLLEXPORT SWKey : public SWObject { |
|
72 |
long index; |
|
73 |
static SWClass classdef; |
|
74 |
void init(); |
|
75 |
||
76 |
protected: |
|
77 |
char *keytext; |
|
78 |
mutable char *rangeText; |
|
79 |
mutable bool boundSet; |
|
80 |
char persist; |
|
81 |
char error; |
|
82 |
||
83 |
public: |
|
84 |
||
85 |
// misc pointer for whatever
|
|
86 |
void *userData; |
|
87 |
||
88 |
/** initializes instance of SWKey from a string
|
|
89 |
* All keys can be reduced to a string representation which should be able
|
|
90 |
* to be used to again set the key to the same position
|
|
91 |
* @param ikey string to use for initializing this new key
|
|
92 |
*/
|
|
93 |
SWKey(const char *ikey = 0); |
|
94 |
||
95 |
/** Copy Constructor
|
|
96 |
* @param k The SWKey object to copy.
|
|
97 |
*/
|
|
98 |
SWKey(SWKey const &k); |
|
99 |
||
100 |
/** Destructor, cleans up this instance of SWKey
|
|
101 |
*/
|
|
102 |
virtual ~SWKey(); |
|
103 |
||
104 |
/** Returns a new exact clone of this SWKey object. This allocates
|
|
105 |
* a new SWKey which must be deleted by the caller
|
|
106 |
* @return new clone of this key
|
|
107 |
*/
|
|
108 |
virtual SWKey *clone() const; |
|
109 |
||
110 |
/** Gets whether this key should persist in any module to which it is set
|
|
111 |
* otherwise just a copy will be used in the module.
|
|
112 |
* @return 1 - persists in module; 0 - a copy is attempted
|
|
113 |
*/
|
|
114 |
char Persist() const; |
|
115 |
||
116 |
/** Sets whether this key should persist in any module to which it is set
|
|
117 |
* otherwise just a copy will be used in the module.
|
|
118 |
* @param ipersist value which to set persist;
|
|
119 |
* @return 1 - persists in module; 0 - a copy is attempted
|
|
120 |
*/
|
|
121 |
char Persist(signed char ipersist); |
|
122 |
||
123 |
/** Gets and clears error status
|
|
124 |
* @return error status
|
|
125 |
*/
|
|
126 |
virtual char Error(); |
|
127 |
||
128 |
/** Sets this SWKey with a character string
|
|
129 |
* @param ikey string used to set this key
|
|
130 |
*/
|
|
131 |
virtual void setText(const char *ikey); |
|
132 |
||
133 |
/** Copies as much info (position, range, etc.) as possible from another SWKey object
|
|
134 |
* @param ikey other SWKey object from which to copy
|
|
135 |
*/
|
|
136 |
virtual void copyFrom(const SWKey &ikey); |
|
137 |
||
138 |
/** returns string representation of this key
|
|
139 |
*/
|
|
140 |
virtual const char *getText() const; |
|
141 |
virtual const char *getShortText() const { return getText(); } |
|
142 |
virtual const char *getRangeText() const; |
|
143 |
virtual bool isBoundSet() const { return boundSet; } |
|
144 |
virtual void clearBound() const { boundSet = false; } |
|
145 |
||
146 |
/** Compares this key object to another SWKey object
|
|
147 |
* @param ikey key to compare with this one
|
|
148 |
* @return >0 if this key is greater than compare key;
|
|
149 |
* <0 if this key is smaller than compare key;
|
|
150 |
* 0 if the keys are the same
|
|
151 |
*/
|
|
152 |
virtual int compare(const SWKey &ikey); |
|
153 |
||
154 |
/** test equality of this SWKey object's position with another SWKey
|
|
155 |
* @param ikey key to compare with this one
|
|
156 |
* @return true if the key positions are equal
|
|
157 |
*/
|
|
158 |
virtual bool equals(const SWKey &ikey) { return !compare(ikey); } |
|
159 |
||
160 |
virtual void setPosition(SW_POSITION); |
|
161 |
||
162 |
/** Decrements key a number of entry positions
|
|
163 |
* This is only valid if isTraversable is true
|
|
164 |
* @param steps Number of entries to jump backward
|
|
165 |
*/
|
|
166 |
virtual void decrement(int steps = 1); |
|
167 |
||
168 |
/** Increments key a number of entry positions
|
|
169 |
* This is only valid if isTraversable is true
|
|
170 |
* @param steps Number of entries to jump forward
|
|
171 |
*/
|
|
172 |
virtual void increment(int steps = 1); |
|
173 |
||
174 |
/** deprecated, use isTraversible
|
|
175 |
*/
|
|
176 |
char Traversable() { return (isTraversable()) ? 1:0; } |
|
177 |
||
178 |
/** Whether or not this key can be ++ -- incremented
|
|
179 |
*/
|
|
180 |
virtual bool isTraversable() const { return false; } |
|
181 |
||
182 |
/** Use this function to get an index position within a module.
|
|
183 |
* Here's a small example how to use this function and @ref Index(long).
|
|
184 |
* This function uses the GerLut module and chooses a random verse from the
|
|
185 |
* Bible and returns it.
|
|
186 |
* @code
|
|
187 |
* const char* randomVerse() {
|
|
188 |
* VerseKey vk;
|
|
189 |
* SWMgr mgr;
|
|
190 |
* LocaleMgr::getSystemLocaleMgr()->setDefaultLocaleName("de");
|
|
191 |
*
|
|
192 |
* SWModule* module = mgr->Modules("GerLut");
|
|
193 |
* srand( time(0) );
|
|
194 |
* const double newIndex = (double(rand())/RAND_MAX)*(24108+8224);
|
|
195 |
* vk.Index(newIndex);
|
|
196 |
* module->setKey(vk);
|
|
197 |
*
|
|
198 |
* char* text;
|
|
199 |
* sprintf(text, "%s: %s",(const char*)vk ,module->StripText(&vk));
|
|
200 |
* return text;
|
|
201 |
* @endcode
|
|
202 |
*/
|
|
203 |
virtual long Index() const { return index; } |
|
204 |
||
205 |
/** See documentation for @ref Index()
|
|
206 |
*/
|
|
207 |
virtual long Index(long iindex) { index = iindex; return index; } |
|
208 |
||
209 |
SWKEY_OPERATORS
|
|
210 |
||
211 |
};
|
|
212 |
||
213 |
SWORD_NAMESPACE_END
|
|
214 |
#endif
|