versekey.h

00001 /******************************************************************************
00002  *      versekey.h - code for class 'versekey'- a standard Biblical verse key
00003  *
00004  * $Id: versekey.h 2058 2007-07-08 04:24:42Z scribe $
00005  *
00006  * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
00007  *      CrossWire Bible Society
00008  *      P. O. Box 2528
00009  *      Tempe, AZ       85280-2528
00010  *
00011  * This program is free software; you can redistribute it and/or modify it
00012  * under the terms of the GNU General Public License as published by the
00013  * Free Software Foundation version 2.
00014  *
00015  * This program is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019  *
00020  */
00021 
00022 
00023 #ifndef VERSEKEY_H
00024 #define VERSEKEY_H
00025 
00026 #include <swkey.h>
00027 #include <swmacs.h>
00028 #include <listkey.h>
00029 
00030 #include <defs.h>
00031 
00032 SWORD_NAMESPACE_START
00033 
00034 #define POS_MAXVERSE ((char)3)
00035 #define POS_MAXCHAPTER ((char)4)
00036 #define POS_MAXBOOK ((char)5)
00037 
00038 #define MAXVERSE SW_POSITION(POS_MAXVERSE)
00039 #define MAXCHAPTER SW_POSITION(POS_MAXCHAPTER)
00040 #define MAXBOOK SW_POSITION(POS_MAXBOOK)
00041 
00042 
00043 struct sbook
00044 {
00047         const char *name;
00048 
00051         const char *prefAbbrev;
00052 
00055         unsigned char chapmax;
00058         int *versemax;
00059 };
00060 
00061 struct abbrev
00062 {
00063         const char *ab;
00064         int book;
00065 };
00066 
00067 
00068 class SWLocale;
00069 
00074 class SWDLLEXPORT VerseKey : public SWKey {
00075 
00076         class LocaleCache {
00077         public:
00078                 char *name;
00079                 unsigned int abbrevsCnt;
00080                 SWLocale *locale;
00081                         LocaleCache() {
00082                         name = 0;
00083                         abbrevsCnt = 0;
00084                         locale = 0;
00085                 }
00086                  virtual ~LocaleCache() {
00087                         if (name)
00088                         delete[]name;
00089                 }
00090         };
00091 
00092         static SWClass classdef;
00093 
00094         static long *offsets[2][2];
00095         static int offsize[2][2];
00098         static int instance;
00099         static struct sbook otbooks[];
00100         static struct sbook ntbooks[];
00101         static char *osisotbooks[];
00102         static char *osisntbooks[];
00103         static char **osisbooks[];
00104 #if 1
00105         static long otbks[];
00106         static long otcps[];
00107         static long ntbks[];
00108         static long ntcps[];
00109 #endif
00110         static int vm[];
00111         static LocaleCache localeCache;
00112         ListKey internalListKey;
00113 
00114         const struct abbrev *abbrevs;
00115         char *locale;
00116         int abbrevsCnt;
00117 
00120         signed char testament;
00121         mutable signed char book;
00122         mutable signed int chapter;
00123         mutable signed int verse;
00124 
00127         char autonorm;
00128 
00131         char headings;
00132 
00133         int getBookAbbrev(const char *abbr);
00134         void initBounds() const;
00135 
00138         void initstatics();
00139 
00142         void init();
00143 
00152         int findindex(long *array, int size, long value);
00153 
00154         mutable VerseKey *lowerBound, *upperBound;
00155 
00156 
00157 protected:
00158 
00163         void freshtext() const;
00167         virtual char parse();
00168 public:
00169 #if 0
00170         static long otbks[];
00171         static long otcps[];
00172         static long ntbks[];
00173         static long ntcps[];
00174 #endif
00175         static const char builtin_BMAX[2];
00176         static struct sbook *builtin_books[2];
00177         static const struct abbrev builtin_abbrevs[];
00178         const char *BMAX;
00179         struct sbook **books;
00180 
00187         VerseKey(const char *ikey = 0);
00188         
00195         VerseKey(const SWKey *ikey);
00196         
00203         VerseKey(const char *min, const char *max);
00204         
00210         VerseKey(const SWKey &k);
00211 
00217         VerseKey(const VerseKey &k);
00218         
00222         virtual ~VerseKey();
00223 
00230         VerseKey &LowerBound(const char *lb);
00231         
00237         VerseKey &UpperBound(const char *ub);
00238         
00242         VerseKey &LowerBound() const;
00243         
00247         VerseKey &UpperBound() const;
00248         
00251         void ClearBounds();
00252         
00256         virtual SWKey *clone() const;
00257         
00261         virtual const char *getText() const;
00262         virtual const char *getShortText() const;
00263         virtual void setText(const char *ikey) { SWKey::setText(ikey); parse(); }
00264         virtual void copyFrom(const SWKey &ikey);
00265         
00268         virtual void copyFrom(const VerseKey &ikey);
00269         
00275         virtual void setPosition(SW_POSITION newpos);
00276         
00282         virtual void decrement(int steps);
00283         
00289         virtual void increment(int steps);
00290         virtual bool isTraversable() const { return true; }
00291 
00292         virtual const char *getBookName() const;
00293         virtual const char *getBookAbbrev() const;
00298         virtual char Testament() const;
00299         
00304         virtual char Book() const;
00305         
00310         virtual int Chapter() const;
00311         
00316         virtual int Verse() const;
00317         
00325         virtual char Testament(char itestament);
00326         
00334         virtual char Book(char ibook);
00335         
00343         virtual int Chapter(int ichapter);
00344         
00352         virtual int Verse(int iverse);
00353         
00360         virtual void Normalize(char autocheck = 0);
00361         
00370         virtual char AutoNormalize(char iautonorm = MAXPOS(char));
00371         
00380         virtual char Headings(char iheadings = MAXPOS(char));
00381         
00382         virtual long NewIndex() const;
00383         
00388         virtual long Index() const;
00389         
00395         virtual long Index(long iindex);
00396 
00397         virtual const char *getOSISRef() const;
00398         static const int getOSISBookNum(const char *bookab);
00399 
00404         static const char *convertToOSIS(const char *inRef, const SWKey *defaultKey);
00405 
00406         virtual ListKey ParseVerseList(const char *buf, const char *defaultKey = 0, bool expandRange = false);
00407         virtual const char *getRangeText() const;
00415         virtual int compare(const SWKey & ikey);
00416         
00424         virtual int _compare(const VerseKey & ikey);
00425         
00426         virtual void setBookAbbrevs(const struct abbrev *bookAbbrevs, unsigned int size = 0 /* default determine size */ );
00427         virtual void setBooks(const char *iBMAX, struct sbook **ibooks);
00428         virtual void setLocale(const char *name);
00429         virtual const char *getLocale() const { return locale; }
00430 
00431 
00432 
00433         // OPERATORS --------------------------------------------------------------------
00434 
00435 
00436         SWKEY_OPERATORS
00437 
00438         virtual SWKey & operator =(const VerseKey & ikey) { copyFrom(ikey); return *this; }
00439 };
00440 
00441 SWORD_NAMESPACE_END
00442 
00443 #endif //VERSEKEY_H