diff options
Diffstat (limited to 'src/modules/swmodule.cpp')
-rw-r--r-- | src/modules/swmodule.cpp | 276 |
1 files changed, 170 insertions, 106 deletions
diff --git a/src/modules/swmodule.cpp b/src/modules/swmodule.cpp index c407894..08ad2a9 100644 --- a/src/modules/swmodule.cpp +++ b/src/modules/swmodule.cpp @@ -4,18 +4,18 @@ * etc.) */ -#include <string.h> #include <swmodule.h> #include <utilfuns.h> #include <regex.h> // GNU #include <swfilter.h> #include <versekey.h> // KLUDGE for Search #ifndef _MSC_VER -#include <iostream.h> +#include <iostream> #endif +SWORD_NAMESPACE_START + SWDisplay SWModule::rawdisp; -void SWModule::nullPercent(char percent, void *percentUserData) {} /****************************************************************************** * SWModule Constructor - Initializes data for instance of SWModule @@ -28,13 +28,10 @@ void SWModule::nullPercent(char percent, void *percentUserData) {} * unicode - if this module is unicode */ -SWModule::SWModule(const char *imodname, const char *imoddesc, SWDisplay *idisp, char *imodtype, SWTextEncoding encoding, SWTextDirection direction, SWTextMarkup markup, const char* imodlang) -{ +SWModule::SWModule(const char *imodname, const char *imoddesc, SWDisplay *idisp, char *imodtype, SWTextEncoding encoding, SWTextDirection direction, SWTextMarkup markup, const char* imodlang) { key = CreateKey(); - entrybuf = new char [1]; - *entrybuf = 0; + entryBuf = ""; config = &ownConfig; - entrybufallocsize = 0; modname = 0; error = 0; moddesc = 0; @@ -65,8 +62,6 @@ SWModule::SWModule(const char *imodname, const char *imoddesc, SWDisplay *idisp, SWModule::~SWModule() { - if (entrybuf) - delete [] entrybuf; if (modname) delete [] modname; if (moddesc) @@ -132,11 +127,14 @@ char SWModule::Error() * RET: pointer to modname */ -char *SWModule::Name(const char *imodname) -{ +char *SWModule::Name(const char *imodname) { return stdstr(&modname, imodname); } +char *SWModule::Name() const { + return modname; +} + /****************************************************************************** * SWModule::Description - Sets/gets module description @@ -147,11 +145,14 @@ char *SWModule::Name(const char *imodname) * RET: pointer to moddesc */ -char *SWModule::Description(const char *imoddesc) -{ +char *SWModule::Description(const char *imoddesc) { return stdstr(&moddesc, imoddesc); } +char *SWModule::Description() const { + return moddesc; +} + /****************************************************************************** * SWModule::Type - Sets/gets module type @@ -162,11 +163,14 @@ char *SWModule::Description(const char *imoddesc) * RET: pointer to modtype */ -char *SWModule::Type(const char *imodtype) -{ +char *SWModule::Type(const char *imodtype) { return stdstr(&modtype, imodtype); } +char *SWModule::Type() const { + return modtype; +} + /****************************************************************************** * SWModule::Direction - Sets/gets module direction * @@ -257,20 +261,27 @@ char SWModule::Display() /****************************************************************************** - * SWModule::SetKey - Sets a key to this module for position to a particular - * record or set of records + * SWModule::getKey - Gets the key from this module that points to the position + * record + * + * RET: key object + */ + +SWKey *SWModule::getKey() const { + return key; +} + + +/****************************************************************************** + * SWModule::setKey - Sets a key to this module for position to a particular + * record * * ENT: ikey - key with which to set this module * * RET: error status */ -char SWModule::SetKey(const SWKey &ikey) { - return SetKey(&ikey); -} - -char SWModule::SetKey(const SWKey *ikey) -{ +char SWModule::setKey(const SWKey *ikey) { SWKey *oldKey = 0; if (key) { @@ -292,33 +303,14 @@ char SWModule::SetKey(const SWKey *ikey) /****************************************************************************** - * SWModule::KeyText - Sets/gets module KeyText - * - * ENT: ikeytext - value which to set keytext - * [0] - only get - * - * RET: pointer to keytext - */ - -const char *SWModule::KeyText(const char *ikeytext) -{ - if (ikeytext) - SetKey(ikeytext); - - return *key; -} - - -/****************************************************************************** - * SWModule::operator =(SW_POSITION) - Positions this modules to an entry + * SWModule::setPosition(SW_POSITION) - Positions this modules to an entry * * ENT: p - position (e.g. TOP, BOTTOM) * * RET: *this */ -SWModule &SWModule::operator =(SW_POSITION p) -{ +void SWModule::setPosition(SW_POSITION p) { *key = p; char saveError = key->Error(); @@ -335,41 +327,34 @@ SWModule &SWModule::operator =(SW_POSITION p) } error = saveError; - return *this; } /****************************************************************************** - * SWModule::operator += - Increments module key a number of entries + * SWModule::increment - Increments module key a number of entries * * ENT: increment - Number of entries to jump forward * * RET: *this */ -SWModule &SWModule::operator +=(int increment) -{ - (*key) += increment; +void SWModule::increment(int steps) { + (*key) += steps; error = key->Error(); - - return *this; } /****************************************************************************** - * SWModule::operator -= - Decrements module key a number of entries + * SWModule::decrement - Decrements module key a number of entries * * ENT: decrement - Number of entries to jump backward * * RET: *this */ -SWModule &SWModule::operator -=(int increment) -{ - (*key) -= increment; +void SWModule::decrement(int steps) { + (*key) -= steps; error = key->Error(); - - return *this; } @@ -381,6 +366,7 @@ SWModule &SWModule::operator -=(int increment) * >=0 - regex * -1 - phrase * -2 - multiword + * -3 - entryAttrib * flags - options flags for search * justCheckIfSupported - if set, don't search, only tell if this * function supports requested search. @@ -388,8 +374,7 @@ SWModule &SWModule::operator -=(int increment) * RET: listkey set to verses that contain istr */ -ListKey &SWModule::Search(const char *istr, int searchType, int flags, SWKey *scope, bool *justCheckIfSupported, void (*percent)(char, void *), void *percentUserData) -{ +ListKey &SWModule::search(const char *istr, int searchType, int flags, SWKey *scope, bool *justCheckIfSupported, void (*percent)(char, void *), void *percentUserData) { SWKey *savekey = 0; SWKey *searchkey = 0; regex_t preg; @@ -402,7 +387,8 @@ ListKey &SWModule::Search(const char *istr, int searchType, int flags, SWKey *sc char perc = 1; bool savePEA = isProcessEntryAttributes(); - processEntryAttributes(false); + processEntryAttributes(searchType == -3); + listkey.ClearList(); if (!key->Persist()) { @@ -414,7 +400,7 @@ ListKey &SWModule::Search(const char *istr, int searchType, int flags, SWKey *sc searchkey = (scope)?scope->clone():(key->Persist())?key->clone():0; if (searchkey) { searchkey->Persist(1); - SetKey(*searchkey); + setKey(*searchkey); } (*percent)(perc, percentUserData); @@ -460,6 +446,32 @@ ListKey &SWModule::Search(const char *istr, int searchType, int flags, SWKey *sc } } + if (searchType == -3) { + wordBuf = (char *)calloc(sizeof(char), strlen(istr) + 1); + char *checkSlash = wordBuf; + strcpy(wordBuf, istr); + words = (char **)calloc(sizeof(char *), 10); + int allocWords = 10; + while (*checkSlash == '/') + words[wordCount++] = checkSlash++; + words[wordCount] = strtok(wordBuf, "/"); + while (words[wordCount]) { + wordCount++; + if (wordCount == allocWords) { + allocWords+=10; + words = (char **)realloc(words, sizeof(char *)*allocWords); + } + checkSlash = words[wordCount-1] + (strlen(words[wordCount-1]))+1; + while (*checkSlash == '/') + words[wordCount++] = checkSlash++; + words[wordCount] = strtok(NULL, "/"); + } + for (int i = 0; i < wordCount; i++) { + if (words[i][0] == '/') + words[i][0] = 0; + } + } + perc = 5; (*percent)(perc, percentUserData); @@ -481,12 +493,12 @@ ListKey &SWModule::Search(const char *istr, int searchType, int flags, SWKey *sc } else if (newperc < perc) { #ifndef _MSC_VER - cerr << "Serious error: new percentage complete is less than previous value\n"; - cerr << "using vk? " << ((vkcheck)?"yes":"no") << "\n"; - cerr << "index: " << ((vkcheck)?vkcheck->NewIndex():key->Index()) << "\n"; - cerr << "highIndex: " << highIndex << "\n"; - cerr << "newperc ==" << (int)newperc << "%" << "is smaller than\n"; - cerr << "perc == " << (int )perc << "% \n"; + std::cerr << "Serious error: new percentage complete is less than previous value\n"; + std::cerr << "using vk? " << ((vkcheck)?"yes":"no") << "\n"; + std::cerr << "index: " << ((vkcheck)?vkcheck->NewIndex():key->Index()) << "\n"; + std::cerr << "highIndex: " << highIndex << "\n"; + std::cerr << "newperc ==" << (int)newperc << "%" << "is smaller than\n"; + std::cerr << "perc == " << (int )perc << "% \n"; #endif } if (searchType >= 0) { @@ -495,27 +507,79 @@ ListKey &SWModule::Search(const char *istr, int searchType, int flags, SWKey *sc listkey << textkey; } } - else { - if (searchType == -1) { - sres = ((flags & REG_ICASE) == REG_ICASE) ? stristr(StripText(), istr) : strstr(StripText(), istr); - if (sres) { - textkey = KeyText(); - listkey << textkey; - } - } - if (searchType == -2) { - int i; - const char *stripBuf = StripText(); - for (i = 0; i < wordCount; i++) { - sres = ((flags & REG_ICASE) == REG_ICASE) ? stristr(stripBuf, words[i]) : strstr(stripBuf, words[i]); - if (!sres) - break; - } - if (i == wordCount) { + else if (searchType == -1) { + sres = ((flags & REG_ICASE) == REG_ICASE) ? stristr(StripText(), istr) : strstr(StripText(), istr); + if (sres) { textkey = KeyText(); listkey << textkey; + } + } + else if (searchType == -2) { + int i; + const char *stripBuf = StripText(); + for (i = 0; i < wordCount; i++) { + sres = ((flags & REG_ICASE) == REG_ICASE) ? stristr(stripBuf, words[i]) : strstr(stripBuf, words[i]); + if (!sres) + break; + } + if (i == wordCount) { + textkey = KeyText(); + listkey << textkey; + } + } + else if (searchType == -3) { + int i; + RenderText(); // force parse + AttributeTypeList &entryAttribs = getEntryAttributes(); + AttributeTypeList::iterator i1Start, i1End; + AttributeList::iterator i2Start, i2End; + AttributeValue::iterator i3Start, i3End; + + if ((words[0]) && (words[0][0])) { + i1Start = entryAttribs.find(words[0]); + i1End = i1Start; + if (i1End != entryAttribs.end()) + i1End++; + } + else { + i1Start = entryAttribs.begin(); + i1End = entryAttribs.end(); + } + for (;i1Start != i1End; i1Start++) { + if ((words[1]) && (words[1][0])) { + i2Start = i1Start->second.find(words[1]); + i2End = i2Start; + if (i2End != i1Start->second.end()) + i2End++; } - + else { + i2Start = i1Start->second.begin(); + i2End = i1Start->second.end(); + } + for (;i2Start != i2End; i2Start++) { + if ((words[2]) && (words[2][0])) { + i3Start = i2Start->second.find(words[2]); + i3End = i3Start; + if (i3End != i2Start->second.end()) + i3End++; + } + else { + i3Start = i2Start->second.begin(); + i3End = i2Start->second.end(); + } + for (;i3Start != i3End; i3Start++) { + sres = ((flags & REG_ICASE) == REG_ICASE) ? stristr(i3Start->second.c_str(), words[3]) : strstr(i3Start->second.c_str(), words[3]); + if (sres) { + textkey = KeyText(); + listkey << textkey; + break; + } + } + if (i3Start != i3End) + break; + } + if (i2Start != i2End) + break; } } (*this)++; @@ -528,7 +592,7 @@ ListKey &SWModule::Search(const char *istr, int searchType, int flags, SWKey *sc free(wordBuf); } - SetKey(*savekey); + setKey(*savekey); if (!savekey->Persist()) delete savekey; @@ -553,7 +617,7 @@ ListKey &SWModule::Search(const char *istr, int searchType, int flags, SWKey *sc * RET: this module's text at specified key location massaged by Strip filters */ -const char *SWModule::StripText(char *buf, int len) +const char *SWModule::StripText(const char *buf, int len) { return RenderText(buf, len, false); } @@ -567,26 +631,29 @@ const char *SWModule::StripText(char *buf, int len) * RET: listkey set to verses that contain istr */ - const char *SWModule::RenderText(char *buf, int len, bool render) { + const char *SWModule::RenderText(const char *buf, int len, bool render) { entryAttributes.clear(); - char *tmpbuf = (buf) ? buf : getRawEntry(); + + static SWBuf local; + if (buf) + local = buf; + + SWBuf &tmpbuf = (buf) ? local : getRawEntryBuf(); SWKey *key = 0; static char *null = ""; if (tmpbuf) { - unsigned long size = (len < 0) ? getEntrySize() * FILTERPAD : len; - if (size < 0) - size = strlen(tmpbuf); + unsigned long size = (len < 0) ? ((getEntrySize()<0) ? strlen(tmpbuf) : getEntrySize()) : len; if (size > 0) { key = (SWKey *)*this; - optionFilter(tmpbuf, size, key); + optionFilter(tmpbuf, key); if (render) { - renderFilter(tmpbuf, size, key); - encodingFilter(tmpbuf, size, key); + renderFilter(tmpbuf, key); + encodingFilter(tmpbuf, key); } - else stripFilter(tmpbuf, size, key); + else stripFilter(tmpbuf, key); } } else { @@ -616,11 +683,11 @@ const char *SWModule::StripText(char *buf, int len) } else savekey = key; - SetKey(*tmpKey); + setKey(*tmpKey); retVal = RenderText(); - SetKey(*savekey); + setKey(*savekey); if (!savekey->Persist()) delete savekey; @@ -648,11 +715,11 @@ const char *SWModule::StripText(SWKey *tmpKey) } else savekey = key; - SetKey(*tmpKey); + setKey(*tmpKey); retVal = StripText(); - SetKey(*savekey); + setKey(*savekey); if (!savekey->Persist()) delete savekey; @@ -661,11 +728,6 @@ const char *SWModule::StripText(SWKey *tmpKey) } -SWModule::operator const char*() { - return RenderText(); -} - - const char *SWModule::getConfigEntry(const char *key) const { ConfigEntMap::iterator it = config->find(key); return (it != config->end()) ? it->second.c_str() : 0; @@ -675,3 +737,5 @@ const char *SWModule::getConfigEntry(const char *key) const { void SWModule::setConfig(ConfigEntMap *config) { this->config = config; } + +SWORD_NAMESPACE_END |