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

zcom.cpp

00001 /******************************************************************************
00002  *  rawcom.cpp - code for class 'zCom'- a module that reads raw commentary
00003  *                      files:  ot and nt using indexs ??.bks ??.cps ??.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 <string.h>
00018 #include <utilfuns.h>
00019 #include <zverse.h>
00020 #include <zcom.h>
00021 
00022 
00023 /******************************************************************************
00024  * zCom Constructor - Initializes data for instance of zCom
00025  *
00026  * ENT: ipath - path to data files
00027  *              iname - Internal name for module
00028  *              idesc - Name to display to user for module
00029  *              iblockType - verse, chapter, book, etc. of index chunks
00030  *              icomp - Compressor object
00031  *              idisp - Display object to use for displaying
00032  */
00033 
00034 zCom::zCom(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), SWCom(iname, idesc, idisp, enc, dir, mark, ilang)/*, SWCompress()*/
00035 {
00036         blockType = iblockType;
00037         lastWriteKey = 0;
00038 }
00039 
00040 /******************************************************************************
00041  * zCom Destructor - Cleans up instance of zCom
00042  */
00043 
00044 zCom::~zCom() {
00045         flushCache();
00046 
00047         if (lastWriteKey)
00048                 delete lastWriteKey;
00049 }
00050 
00051 /******************************************************************************
00052  * zCom::operator char *        - Returns the correct verse when char * cast
00053  *                                      is requested
00054  *
00055  * RET: string buffer with verse
00056  */
00057 char *zCom::getRawEntry() {
00058         long  start = 0;
00059         unsigned short size = 0;
00060         VerseKey *key = 0;
00061 
00062 #ifndef _WIN32_WCE
00063         try {
00064 #endif
00065                 key = SWDYNAMIC_CAST(VerseKey, this->key);
00066 #ifndef _WIN32_WCE
00067         }
00068         catch ( ... ) {}
00069 #endif
00070         // if we don't have a VerseKey * decendant, create our own
00071         if (!key)
00072                 key = new VerseKey(this->key);
00073 
00074         findoffset(key->Testament(), key->Index(), &start, &size);
00075         entrySize = size;        // support getEntrySize call
00076 
00077         unsigned long newsize = (size + 2) * FILTERPAD;
00078         if (newsize > entrybufallocsize) {
00079                 if (entrybuf)
00080                         delete [] entrybuf;
00081                 entrybuf = new char [ newsize ];
00082                 entrybufallocsize = newsize;
00083         }
00084         *entrybuf = 0;
00085 
00086         swgettext(key->Testament(), start, (size + 2), entrybuf);
00087 
00088         rawFilter(entrybuf, size, key);
00089 
00090         if (!isUnicode())
00091                 preptext(entrybuf);
00092 
00093         if (this->key != key) // free our key if we created a VerseKey
00094                 delete key;
00095 
00096         return entrybuf;
00097 }
00098 
00099 
00100 bool zCom::sameBlock(VerseKey *k1, VerseKey *k2) {
00101         if (k1->Testament() != k2->Testament())
00102                 return false;
00103 
00104         switch (blockType) {
00105         case VERSEBLOCKS:
00106                 if (k1->Verse() != k2->Verse())
00107                         return false;
00108         case CHAPTERBLOCKS:
00109                 if (k1->Chapter() != k2->Chapter())
00110                         return false;
00111         case BOOKBLOCKS:
00112                 if (k1->Book() != k2->Book())
00113                         return false;
00114         }
00115         return true;
00116 }
00117 
00118 SWModule &zCom::setentry(const char *inbuf, long len) {
00119         VerseKey *key = 0;
00120         // see if we have a VerseKey * or decendant
00121 #ifndef _WIN32_WCE
00122         try {
00123 #endif
00124                 key = SWDYNAMIC_CAST(VerseKey, this->key);
00125 #ifndef _WIN32_WCE
00126         }
00127         catch ( ... ) {}
00128 #endif
00129         // if we don't have a VerseKey * decendant, create our own
00130         if (!key)
00131                 key = new VerseKey(this->key);
00132 
00133 
00134         // see if we've jumped across blocks since last write
00135         if (lastWriteKey) {
00136                 if (!sameBlock(lastWriteKey, key)) {
00137                         flushCache();
00138                 }
00139                 delete lastWriteKey;
00140         }
00141 
00142         settext(key->Testament(), key->Index(), inbuf, len);
00143 
00144         lastWriteKey = (VerseKey *)key->clone();        // must delete
00145 
00146         if (this->key != key) // free our key if we created a VerseKey
00147                 delete key;
00148 
00149         return *this;
00150 }
00151 
00152 SWModule &zCom::operator <<(const char *inbuf) {
00153         return setentry(inbuf, 0);
00154 }
00155 
00156 
00157 SWModule &zCom::operator <<(const SWKey *inkey) {
00158         VerseKey *destkey = 0;
00159         const VerseKey *srckey = 0;
00160         // see if we have a VerseKey * or decendant
00161 #ifndef _WIN32_WCE
00162         try {
00163 #endif
00164                 destkey = SWDYNAMIC_CAST(VerseKey, this->key);
00165 #ifndef _WIN32_WCE
00166         }
00167         catch ( ... ) {}
00168 #endif
00169         // if we don't have a VerseKey * decendant, create our own
00170         if (!destkey)
00171                 destkey = new VerseKey(this->key);
00172 
00173         // see if we have a VerseKey * or decendant
00174 #ifndef _WIN32_WCE
00175         try {
00176 #endif
00177                 srckey = (const VerseKey *) SWDYNAMIC_CAST(VerseKey, inkey);
00178 #ifndef _WIN32_WCE
00179         }
00180         catch ( ... ) {
00181         }
00182 #endif
00183         // if we don't have a VerseKey * decendant, create our own
00184         if (!srckey)
00185                 srckey = new VerseKey(inkey);
00186 
00187         linkentry(destkey->Testament(), destkey->Index(), srckey->Index());
00188 
00189         if (this->key != destkey) // free our key if we created a VerseKey
00190                 delete destkey;
00191 
00192         if (inkey != srckey) // free our key if we created a VerseKey
00193                 delete srckey;
00194 
00195         return *this;
00196 }
00197 
00198 /******************************************************************************
00199  * zCom::deleteEntry    - deletes this entry
00200  *
00201  * RET: *this
00202  */
00203 
00204 void zCom::deleteEntry() {
00205 
00206         VerseKey *key = 0;
00207 
00208 #ifndef _WIN32_WCE
00209         try {
00210 #endif
00211                 key = SWDYNAMIC_CAST(VerseKey, this->key);
00212 #ifndef _WIN32_WCE
00213         }
00214         catch ( ... ) {}
00215 #endif
00216         if (!key)
00217                 key = new VerseKey(this->key);
00218 
00219         settext(key->Testament(), key->Index(), "");
00220 
00221         if (key != this->key)
00222                 delete key;
00223 }
00224 
00225 
00226 /******************************************************************************
00227  * zCom::operator +=    - Increments module key a number of entries
00228  *
00229  * ENT: increment       - Number of entries to jump forward
00230  *
00231  * RET: *this
00232  */
00233 
00234 SWModule &zCom::operator +=(int increment) {
00235         long  start;
00236         unsigned short size;
00237         VerseKey *tmpkey = 0;
00238 
00239 #ifndef _WIN32_WCE
00240         try {
00241 #endif
00242                 tmpkey = SWDYNAMIC_CAST(VerseKey, key);
00243 #ifndef _WIN32_WCE
00244         }
00245         catch ( ... ) {}
00246 #endif
00247         if (!tmpkey)
00248                 tmpkey = new VerseKey(key);
00249 
00250         findoffset(tmpkey->Testament(), tmpkey->Index(), &start, &size);
00251 
00252         SWKey lastgood = *tmpkey;
00253         while (increment) {
00254                 long laststart = start;
00255                 unsigned short lastsize = size;
00256                 SWKey lasttry = *tmpkey;
00257                 (increment > 0) ? (*key)++ : (*key)--;
00258                 if (tmpkey != key)
00259                         delete tmpkey;
00260                 tmpkey = 0;
00261 #ifndef _WIN32_WCE
00262                 try {
00263 #endif
00264                         tmpkey = SWDYNAMIC_CAST(VerseKey, key);
00265 #ifndef _WIN32_WCE
00266                 }
00267                 catch ( ... ) {}
00268 #endif
00269                 if (!tmpkey)
00270                         tmpkey = new VerseKey(key);
00271 
00272                 if ((error = key->Error())) {
00273                         *key = lastgood;
00274                         break;
00275                 }
00276                 long index = tmpkey->Index();
00277                 findoffset(tmpkey->Testament(), index, &start, &size);
00278                 if ((((laststart != start) || (lastsize != size))||(!skipConsecutiveLinks)) && (start >= 0) && (size)) {
00279                         increment += (increment < 0) ? 1 : -1;
00280                         lastgood = *tmpkey;
00281                 }
00282         }
00283         error = (error) ? KEYERR_OUTOFBOUNDS : 0;
00284 
00285         if (tmpkey != key)
00286                 delete tmpkey;
00287 
00288         return *this;
00289 }
00290 

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