swmodule.h

00001 /******************************************************************************
00002  *  swmodule.h  - code for base class 'module'.  Module is the basis for all
00003  *                types of modules (e.g. texts, commentaries, maps, lexicons,
00004  *                etc.)
00005  *
00006  * $Id: swmodule.h 2093 2007-09-28 19:20:22Z scribe $
00007  *
00008  * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
00009  *      CrossWire Bible Society
00010  *      P. O. Box 2528
00011  *      Tempe, AZ  85280-2528
00012  *
00013  * This program is free software; you can redistribute it and/or modify it
00014  * under the terms of the GNU General Public License as published by the
00015  * Free Software Foundation version 2.
00016  *
00017  * This program is distributed in the hope that it will be useful, but
00018  * WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * General Public License for more details.
00021  *
00022  */
00023 
00024 #ifndef SWMODULE_H
00025 #define SWMODULE_H
00026 
00027 #include <swdisp.h>
00028 #include <listkey.h>
00029 #include <swconfig.h>
00030 
00031 #include <swcacher.h>
00032 #include <swsearchable.h>
00033 
00034 #include <list>
00035 
00036 #include <defs.h>
00037 
00038 SWORD_NAMESPACE_START
00039 
00040 class SWOptionFilter;
00041 class SWFilter;
00042 
00043 #define SEARCHFLAG_MATCHWHOLEENTRY 4096
00044 
00045 #define SWMODULE_OPERATORS \
00046         operator const char *() { return RenderText(); } \
00047         operator SWKey &() { return *getKey(); } \
00048         operator SWKey *() { return getKey(); } \
00049         SWModule &operator <<(const char *inbuf) { setEntry(inbuf); return *this; } \
00050         SWModule &operator <<(const SWKey *sourceKey) { linkEntry(sourceKey); return *this; } \
00051         SWModule &operator -=(int steps) { decrement(steps); return *this; } \
00052         SWModule &operator +=(int steps) { increment(steps); return *this; } \
00053         SWModule &operator ++(int) { return *this += 1; } \
00054         SWModule &operator --(int) { return *this -= 1; } \
00055         SWModule &operator =(SW_POSITION p) { setPosition(p); return *this; }
00056 
00057 
00058 typedef std::list < SWFilter * >FilterList;
00059 typedef std::list < SWOptionFilter * >OptionFilterList;
00060 typedef std::map < SWBuf, SWBuf, std::less < SWBuf > > AttributeValue;
00061 typedef std::map < SWBuf, AttributeValue, std::less < SWBuf > > AttributeList;
00062 typedef std::map < SWBuf, AttributeList, std::less < SWBuf > > AttributeTypeList;
00063 
00064 #define SWTextDirection char
00065 #define SWTextEncoding char
00066 #define SWTextMarkup char
00067 
00077 // TODO: should all SWModule decendents be SWCachers?  Only some really
00078 // cache data.  But if we don't do this, then we need another mechanism to
00079 // check if we are an SWCacher.  Maybe make SWModule extend SWObject (which
00080 // it probably should anyway.  But then we need to add all the cheezy
00081 // heirarchy info to all he decendent classes for our SWDYNAMIC_CAST and
00082 // then we can see if we implement SWCacher so we know whether or not to add
00083 // to the yet to be developed cachemgr.
00084 // Just leave for now.  This lets us always able to call module->flush()
00085 // to manually flush a cache, and doesn't hurt if there is no work done.
00086 
00087 class SWDLLEXPORT SWModule : public SWCacher, public SWSearchable {
00088 
00089 protected:
00090 
00091         ConfigEntMap ownConfig;
00092         ConfigEntMap *config;
00093         mutable AttributeTypeList entryAttributes;
00094         mutable bool procEntAttr;
00095 
00096         char error;
00097         bool skipConsecutiveLinks;
00098 
00100         SWKey *key;
00101 
00102         ListKey listKey;
00103         char *modname;
00104         char *moddesc;
00105         char *modtype;
00106         char *modlang;  
00107 
00108         char direction;
00109         char markup;
00110         char encoding;
00111 
00113         SWDisplay *disp;
00114 
00115         static SWDisplay rawdisp;
00116         SWBuf entryBuf;
00117 
00119         FilterList *stripFilters;
00120 
00122         FilterList *rawFilters;
00123 
00125         FilterList *renderFilters;
00126 
00128         OptionFilterList *optionFilters;
00129 
00131         FilterList *encodingFilters;
00132 
00133         int entrySize;
00134         mutable long entryIndex;         // internal common storage for index
00135 
00136 
00137 public:
00138 
00143         bool terminateSearch;
00144 
00156         SWModule(const char *imodname = 0, const char *imoddesc = 0, SWDisplay * idisp = 0, char *imodtype = 0, SWTextEncoding encoding = ENC_UNKNOWN, SWTextDirection dir = DIRECTION_LTR, SWTextMarkup markup = FMT_UNKNOWN, const char* modlang = 0);
00157 
00160         virtual ~SWModule();
00161 
00166         virtual char Error();
00167 
00171         virtual const bool isUnicode() const { return (encoding == (char)ENC_UTF8 || encoding == (char)ENC_SCSU); }
00172 
00173         // These methods are useful for modules that come from a standard SWORD install (most do).
00174         // SWMgr will call setConfig.  The user may use getConfig and getConfigEntry (if they
00175         // are not comfortable with, or don't wish to use  stl maps).
00176         virtual void setConfig(ConfigEntMap *config);
00177         virtual const ConfigEntMap &getConfig() const { return *config; }
00178         virtual const char *getConfigEntry(const char *key) const;
00179 
00183         virtual const int getEntrySize() const { return entrySize; }
00184 
00191         virtual char setKey(const SWKey *ikey);
00192 
00198         char setKey(const SWKey &ikey) { return setKey(&ikey); }
00202         char SetKey(const SWKey *ikey) { return setKey(ikey); }
00206         char SetKey(const SWKey &ikey) { return setKey(ikey); }
00210         char Key(const SWKey & ikey) { return setKey(ikey); }
00211 
00215         virtual SWKey *getKey() const;
00219         SWKey &Key() const { return *getKey(); }
00220 
00227         virtual const char *KeyText(const char *ikeytext = 0) {
00228                 if (ikeytext) setKey(ikeytext);
00229                 return *getKey();
00230         }
00231 
00237         virtual const char *getKeyText() const {
00238                 return *getKey();
00239         }
00240 
00241         
00242         virtual long Index() const { return entryIndex; }
00243         virtual long Index(long iindex) { entryIndex = iindex; return entryIndex; }
00244 
00249         virtual char Display();
00250 
00255         virtual SWDisplay *getDisplay() const;
00256 
00261         virtual void setDisplay(SWDisplay * idisp);
00262 
00266         SWDisplay *Disp(SWDisplay * idisp = 0) {
00267                 if (idisp)
00268                         setDisplay(idisp);
00269                 return getDisplay();
00270         }
00271 
00276         virtual char *Name() const;
00277 
00283         virtual char *Name(const char *imodname);
00284 
00289         virtual char *Description() const;
00290 
00296         virtual char *Description(const char *imoddesc);
00297 
00302         virtual char *Type() const;
00303 
00309         virtual char *Type(const char *imodtype);
00310 
00316         virtual char Direction(signed char newdir = -1);
00317 
00323         virtual char Encoding(signed char enc = -1);
00324 
00330         virtual char Markup(signed char markup = -1);
00331 
00337         virtual char *Lang(const char *imodlang = 0);
00338 
00339 
00340         // search interface -------------------------------------------------
00341 
00360         virtual ListKey &search(const char *istr, int searchType = 0, int flags = 0,
00361                         SWKey * scope = 0,
00362                         bool * justCheckIfSupported = 0,
00363                         void (*percent) (char, void *) = &nullPercent,
00364                         void *percentUserData = 0);
00365 
00366         // for backward compat-- deprecated
00367         virtual ListKey &Search(const char *istr, int searchType = 0, int flags = 0,
00368                         SWKey * scope = 0,
00369                         bool * justCheckIfSupported = 0,
00370                         void (*percent) (char, void *) = &nullPercent,
00371                         void *percentUserData = 0) {
00372                 return search(istr, searchType, flags, scope, justCheckIfSupported, percent, percentUserData);
00373         }
00374 
00375 
00382         virtual SWKey *CreateKey();
00383 
00388         virtual SWBuf &getRawEntryBuf() = 0;
00389 
00390         virtual const char *getRawEntry() { return getRawEntryBuf().c_str(); }
00391 
00392         // write interface ----------------------------
00396         virtual bool isWritable() { return false; }
00397 
00402         static signed char createModule(const char *path);
00403 
00406         virtual void setEntry(const char *inbuf, long len= -1);
00407 
00411         virtual void linkEntry(const SWKey *sourceKey);
00412 
00415         virtual void deleteEntry() {}
00416 
00417         // end write interface ------------------------
00418 
00422         virtual void decrement(int steps = 1);
00423 
00427         virtual void increment(int steps = 1);
00428 
00432         virtual void setPosition(SW_POSITION pos);
00433 
00439         virtual void filterBuffer(OptionFilterList *filters, SWBuf &buf, SWKey *key);
00440 
00446         virtual void filterBuffer(FilterList *filters, SWBuf &buf, SWKey *key);
00447 
00454         virtual SWModule &AddRenderFilter(SWFilter *newfilter) {
00455                 renderFilters->push_back(newfilter);
00456                 return *this;
00457         }
00458 
00463         virtual const FilterList &getRenderFilters() const {
00464                 return *renderFilters;
00465         }
00466 
00471         virtual SWModule &RemoveRenderFilter(SWFilter *oldfilter) {
00472                 renderFilters->remove(oldfilter);
00473                 return *this;
00474         }
00475 
00481         virtual SWModule &ReplaceRenderFilter(SWFilter *oldfilter, SWFilter *newfilter) {
00482                 FilterList::iterator iter;
00483                 for (iter = renderFilters->begin(); iter != renderFilters->end(); iter++) {
00484                         if (*iter == oldfilter)
00485                                 *iter = newfilter;
00486                 }
00487                 return *this;
00488         }
00489 
00494         virtual void renderFilter(SWBuf &buf, SWKey *key) {
00495                 filterBuffer(renderFilters, buf, key);
00496         }
00497 
00505         virtual SWModule &AddEncodingFilter(SWFilter *newfilter) {
00506                 encodingFilters->push_back(newfilter);
00507                 return *this;
00508         }
00509 
00514         virtual SWModule &RemoveEncodingFilter(SWFilter *oldfilter) {
00515                 encodingFilters->remove(oldfilter);
00516                 return *this;
00517         }
00518 
00524         virtual SWModule &ReplaceEncodingFilter(SWFilter *oldfilter, SWFilter *newfilter) {
00525                 FilterList::iterator iter;
00526                 for (iter = encodingFilters->begin(); iter != encodingFilters->end(); iter++) {
00527                         if (*iter == oldfilter)
00528                                 *iter = newfilter;
00529                 }
00530                 return *this;
00531         }
00532 
00537         virtual void encodingFilter(SWBuf &buf, SWKey *key) {
00538                 filterBuffer(encodingFilters, buf, key);
00539         }
00540 
00547         virtual SWModule &AddStripFilter(SWFilter *newfilter) {
00548                 stripFilters->push_back(newfilter);
00549                 return *this;
00550         }
00551 
00556         virtual SWModule &AddRawFilter(SWFilter *newfilter) {
00557                 rawFilters->push_back(newfilter);
00558                 return *this;
00559         }
00560 
00565         virtual void stripFilter(SWBuf &buf, SWKey *key) {
00566                 filterBuffer(stripFilters, buf, key);
00567         }
00568 
00569 
00574         virtual void rawFilter(SWBuf &buf, SWKey *key) {
00575                 filterBuffer(rawFilters, buf, key);
00576         }
00577 
00585         virtual SWModule &AddOptionFilter(SWOptionFilter *newfilter) {
00586                 optionFilters->push_back(newfilter);
00587                 return *this;
00588         }
00589 
00594         virtual void optionFilter(SWBuf &buf, SWKey *key) {
00595                 filterBuffer(optionFilters, buf, key);
00596         }
00597 
00606         virtual const char *StripText(const char *buf = 0, int len = -1);
00607 
00616         virtual const char *RenderText(const char *buf = 0, int len = -1, bool render = true);
00617 
00622         virtual const char *StripText(SWKey *tmpKey);
00623 
00628         virtual const char *RenderText(SWKey *tmpKey);
00629 
00634         virtual void setSkipConsecutiveLinks(bool val) { skipConsecutiveLinks = val; }
00635 
00639         virtual bool getSkipConsecutiveLinks() { return skipConsecutiveLinks; }
00640 
00647         virtual AttributeTypeList &getEntryAttributes() const { return entryAttributes; }
00648 
00654         virtual void processEntryAttributes(bool val) const { procEntAttr = val; }
00655 
00658         virtual bool isProcessEntryAttributes() const { return procEntAttr; }
00659 
00660         // OPERATORS -----------------------------------------------------------------
00661         
00662         virtual signed char createSearchFramework(
00663                         void (*percent) (char, void *) = &nullPercent,
00664                         void *percentUserData = 0);
00665         virtual void deleteSearchFramework();
00666         virtual bool hasSearchFramework();
00667 
00668         SWMODULE_OPERATORS
00669 
00670 };
00671 
00672 SWORD_NAMESPACE_END
00673 #endif