Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

ztext.cpp

00001 /******************************************************************************
00002  *  ztext.cpp - code for class 'zText'- a module that reads compressed text
00003  *                              files: ot and nt using indexs ??.vss
00004  */
00005 
00006 
00007 #include <ctype.h>
00008 #include <stdio.h>
00009 #include <fcntl.h>
00010 
00011 #ifndef __GNUC__
00012 #include <io.h>
00013 #else
00014 #include <unistd.h>
00015 #endif
00016 
00017 #include <iostream.h>
00018 #include <string.h>
00019 #include <utilfuns.h>
00020 //#include <rawverse.h>
00021 #include <ztext.h>
00022 //#include <zlib.h>
00023 
00024 
00025 /******************************************************************************
00026  * zText Constructor - Initializes data for instance of zText
00027  *
00028  * ENT: ipath - path to data files
00029  *              iname - Internal name for module
00030  *              idesc - Name to display to user for module
00031  *              iblockType - verse, chapter, book, etc. of index chunks
00032  *              icomp - Compressor object
00033  *              idisp - Display object to use for displaying
00034  */
00035 
00036 zText::zText(const char *ipath, const char *iname, const char *idesc, int iblockType, SWCompress *icomp, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char* ilang) : zVerse(ipath, -1, iblockType, icomp), SWText(iname, idesc, idisp, enc, dir, mark, ilang)/*, SWCompress()*/
00037 {
00038         blockType = iblockType;
00039         lastWriteKey = 0;
00040 }
00041 
00042 
00043 /******************************************************************************
00044  * zText Destructor - Cleans up instance of zText
00045  */
00046 
00047 zText::~zText()
00048 {
00049         flushCache();
00050 
00051         if (lastWriteKey)
00052                 delete lastWriteKey;
00053 }
00054 
00055 
00056 /******************************************************************************
00057  * zText::getRawEntry   - Returns the current verse buffer
00058  *
00059  * RET: buffer with verse
00060  */
00061 
00062 char *zText::getRawEntry()
00063 {
00064 /*
00065         long  start;
00066         unsigned long size;
00067         unsigned long destsize;
00068         char *tmpbuf;
00069         char *dest;
00070         VerseKey *lkey = (VerseKey *) SWModule::key;
00071         char sizebuf[3];
00072 
00073         lkey->Verse(0);
00074         if (chapcache != lkey->Index()) {
00075                 findoffset(lkey->Testament(), lkey->Index(), &start, &((unsigned short) size));
00076                 gettext(lkey->Testament(), start, 3, sizebuf);
00077                 memcpy(&size, sizebuf, 2);
00078                 tmpbuf = new char [ size + 1 ];
00079                 gettext(lkey->Testament(), start + 2, size + 1 , tmpbuf);
00080                 //zBuf(&size, tmpbuf);
00081                 dest = new char [ (size*4) + 1 ];
00082                 uncompress((Bytef *)dest, &destsize, (Bytef *) tmpbuf, size);
00083                 chapcache = lkey->Index();
00084                 delete [] tmpbuf;
00085         }
00086 
00087         //findoffset(key->Testament(), key->Index(), &start, &size);
00088         findoffset(lkey->Testament(), lkey->Index(), &start, &((unsigned short) size));
00089 
00090         if (versebuf)
00091                 delete [] versebuf;
00092         versebuf = new char [ size + 1 ];
00093         //memcpy(versebuf, Buf(), size);
00094         memcpy(versebuf, dest, destsize);
00095         delete [] dest;
00096 
00097         preptext(versebuf);
00098 
00099         return versebuf;
00100 */
00101 
00102         long  start = 0;
00103         unsigned short size = 0;
00104         VerseKey *key = 0;
00105 
00106         //printf ("zText char *\n");
00107 
00108         // see if we have a VerseKey * or decendant
00109 #ifndef _WIN32_WCE
00110         try {
00111 #endif
00112                 key = SWDYNAMIC_CAST(VerseKey, this->key);
00113 #ifndef _WIN32_WCE
00114         }
00115         catch ( ... ) {}
00116 #endif
00117         // if we don't have a VerseKey * decendant, create our own
00118         if (!key)
00119                 key = new VerseKey(this->key);
00120 
00121         //printf ("checking cache\n");
00122         //printf ("finding offset\n");
00123         findoffset(key->Testament(), key->Index(), &start, &size);
00124         entrySize = size;        // support getEntrySize call
00125 
00126         //printf ("deleting previous buffer\n");
00127         unsigned long newsize = (size + 2) * FILTERPAD;
00128         if (newsize > entrybufallocsize) {
00129                 if (entrybuf)
00130                         delete [] entrybuf;
00131                 entrybuf = new char [ newsize ];
00132                 entrybufallocsize = newsize;
00133         }
00134         *entrybuf = 0;
00135 
00136         //printf ("getting text\n");
00137         swgettext(key->Testament(), start, (size + 2), entrybuf);
00138         //printf ("got text\n");
00139 
00140         rawFilter(entrybuf, size, key);
00141 
00142         //printf ("preparing text\n");
00143         if (!isUnicode())
00144                 preptext(entrybuf);
00145 
00146         if (this->key != key) // free our key if we created a VerseKey
00147                 delete key;
00148 
00149         //printf ("returning text\n");
00150         return entrybuf;
00151 
00152 }
00153 
00154 
00155 bool zText::sameBlock(VerseKey *k1, VerseKey *k2) {
00156         if (k1->Testament() != k2->Testament())
00157                 return false;
00158 
00159         switch (blockType) {
00160         case VERSEBLOCKS:
00161                 if (k1->Verse() != k2->Verse())
00162                         return false;
00163         case CHAPTERBLOCKS:
00164                 if (k1->Chapter() != k2->Chapter())
00165                         return false;
00166         case BOOKBLOCKS:
00167                 if (k1->Book() != k2->Book())
00168                         return false;
00169         }
00170         return true;
00171 }
00172 
00173 
00174 SWModule &zText::setentry(const char *inbuf, long len) {
00175         VerseKey *key = 0;
00176         // see if we have a VerseKey * or decendant
00177 #ifndef _WIN32_WCE
00178         try {
00179 #endif
00180                 key = SWDYNAMIC_CAST(VerseKey, this->key);
00181 #ifndef _WIN32_WCE
00182         }
00183         catch ( ... ) {}
00184 #endif
00185         // if we don't have a VerseKey * decendant, create our own
00186         if (!key)
00187                 key = new VerseKey(this->key);
00188 
00189 
00190         // see if we've jumped across blocks since last write
00191         if (lastWriteKey) {
00192                 if (!sameBlock(lastWriteKey, key)) {
00193                         flushCache();
00194                 }
00195                 delete lastWriteKey;
00196         }
00197 
00198         settext(key->Testament(), key->Index(), inbuf, len);
00199 
00200         lastWriteKey = (VerseKey *)key->clone();        // must delete
00201 
00202         if (this->key != key) // free our key if we created a VerseKey
00203                 delete key;
00204 
00205         return *this;
00206 }
00207 
00208 SWModule &zText::operator <<(const char *inbuf) {
00209         return setentry(inbuf, 0);
00210 }
00211 
00212 
00213 SWModule &zText::operator <<(const SWKey *inkey) {
00214         VerseKey *destkey = 0;
00215         const VerseKey *srckey = 0;
00216         // see if we have a VerseKey * or decendant
00217 #ifndef _WIN32_WCE
00218         try {
00219 #endif
00220                 destkey = SWDYNAMIC_CAST(VerseKey, this->key);
00221 #ifndef _WIN32_WCE
00222         }
00223         catch ( ... ) {}
00224 #endif
00225         // if we don't have a VerseKey * decendant, create our own
00226         if (!destkey)
00227                 destkey = new VerseKey(this->key);
00228 
00229         // see if we have a VerseKey * or decendant
00230 #ifndef _WIN32_WCE
00231         try {
00232 #endif
00233                 srckey = (const VerseKey *) SWDYNAMIC_CAST(VerseKey, inkey);
00234 #ifndef _WIN32_WCE
00235         }
00236         catch ( ... ) {
00237         }
00238 #endif
00239         // if we don't have a VerseKey * decendant, create our own
00240         if (!srckey)
00241                 srckey = new VerseKey(inkey);
00242 
00243         linkentry(destkey->Testament(), destkey->Index(), srckey->Index());
00244 
00245         if (this->key != destkey) // free our key if we created a VerseKey
00246                 delete destkey;
00247 
00248         if (inkey != srckey) // free our key if we created a VerseKey
00249                 delete srckey;
00250 
00251         return *this;
00252 }
00253 
00254 
00255 /******************************************************************************
00256  * zFiles::deleteEntry  - deletes this entry
00257  *
00258  * RET: *this
00259  */
00260 
00261 void zText::deleteEntry() {
00262 
00263         VerseKey *key = 0;
00264 
00265 #ifndef _WIN32_WCE
00266         try {
00267 #endif
00268                 key = SWDYNAMIC_CAST(VerseKey, this->key);
00269 #ifndef _WIN32_WCE
00270         }
00271         catch ( ... ) {}
00272 #endif
00273         if (!key)
00274                 key = new VerseKey(this->key);
00275 
00276         settext(key->Testament(), key->Index(), "");
00277 
00278         if (key != this->key)
00279                 delete key;
00280 }
00281 
00282 
00283 /******************************************************************************
00284  * zText::operator +=   - Increments module key a number of entries
00285  *
00286  * ENT: increment       - Number of entries to jump forward
00287  *
00288  * RET: *this
00289  */
00290 
00291 SWModule &zText::operator +=(int increment)
00292 {
00293         long  start;
00294         unsigned short size;
00295         VerseKey *tmpkey = 0;
00296 
00297 #ifndef _WIN32_WCE
00298         try {
00299 #endif
00300                 tmpkey = SWDYNAMIC_CAST(VerseKey, key);
00301 #ifndef _WIN32_WCE
00302         }
00303         catch ( ... ) {}
00304 #endif
00305         if (!tmpkey)
00306                 tmpkey = new VerseKey(key);
00307 
00308         findoffset(tmpkey->Testament(), tmpkey->Index(), &start, &size);
00309 
00310         SWKey lastgood = *tmpkey;
00311         while (increment) {
00312                 long laststart = start;
00313                 unsigned short lastsize = size;
00314                 SWKey lasttry = *tmpkey;
00315                 (increment > 0) ? (*key)++ : (*key)--;
00316                 if (tmpkey != key)
00317                         delete tmpkey;
00318                 tmpkey = 0;
00319 #ifndef _WIN32_WCE
00320                 try {
00321 #endif
00322                         tmpkey = SWDYNAMIC_CAST(VerseKey, key);
00323 #ifndef _WIN32_WCE
00324                 }
00325                 catch ( ... ) {}
00326 #endif
00327                 if (!tmpkey)
00328                         tmpkey = new VerseKey(key);
00329 
00330                 if ((error = key->Error())) {
00331                         *key = lastgood;
00332                         break;
00333                 }
00334                 long index = tmpkey->Index();
00335                 findoffset(tmpkey->Testament(), index, &start, &size);
00336                 if ((((laststart != start) || (lastsize != size))||(!skipConsecutiveLinks)) && (start >= 0) && (size)) {
00337                         increment += (increment < 0) ? 1 : -1;
00338                         lastgood = *tmpkey;
00339                 }
00340         }
00341         error = (error) ? KEYERR_OUTOFBOUNDS : 0;
00342 
00343         if (tmpkey != key)
00344                 delete tmpkey;
00345 
00346         return *this;
00347 }

Generated on Thu Jun 20 22:13:01 2002 for The Sword Project by doxygen1.2.15