zVerse Class Reference

#include <zverse.h>

Inheritance diagram for zVerse:
Inheritance graph
[legend]
Collaboration diagram for zVerse:
Collaboration graph
[legend]

List of all members.

Public Member Functions

void findOffset (char testmt, long idxoff, long *start, unsigned short *size, unsigned long *buffnum) const
virtual void rawZFilter (SWBuf &buf, char direction=0) const
void zReadText (char testmt, long start, unsigned short size, unsigned long buffnum, SWBuf &buf) const
 zVerse (const char *ipath, int fileMode=-1, int blockType=CHAPTERBLOCKS, SWCompress *icomp=0)
virtual ~zVerse ()

Static Public Member Functions

static char createModule (const char *path, int blockBound, const char *v11n="KJV")

Public Attributes

char nl

Static Public Attributes

static const char uniqueIndexID [] = {'X', 'r', 'v', 'c', 'b'}

Protected Member Functions

void doLinkEntry (char testmt, long destidxoff, long srcidxoff)
void doSetText (char testmt, long idxoff, const char *buf, long len=0)
void flushCache () const

Protected Attributes

char * cacheBuf
long cacheBufIdx
unsigned int cacheBufSize
char cacheTestament
FileDesccompfp [2]
bool dirtyCache
FileDescidxfp [2]
char * path
FileDesctextfp [2]

Static Protected Attributes

static int instance = 0

Private Attributes

SWCompresscompressor

Detailed Description

Definition at line 19 of file zverse.h.


Constructor & Destructor Documentation

zVerse::zVerse ( const char *  ipath,
int  fileMode = -1,
int  blockType = CHAPTERBLOCKS,
SWCompress icomp = 0 
)

Definition at line 61 of file zverse.cpp.

00062 {
00063     // this line, instead of just defaulting, to keep FileMgr out of header
00064     if (fileMode == -1) fileMode = FileMgr::RDONLY;
00065 
00066     SWBuf buf;
00067 
00068     nl = '\n';
00069     path = 0;
00070     cacheBufIdx = -1;
00071     cacheTestament = 0;
00072     cacheBuf = 0;
00073     dirtyCache = false;
00074     stdstr(&path, ipath);
00075 
00076     if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
00077         path[strlen(path)-1] = 0;
00078 
00079     compressor = (icomp) ? icomp : new SWCompress();
00080 
00081     if (fileMode == -1) { // try read/write if possible
00082         fileMode = FileMgr::RDWR;
00083     }
00084         
00085     buf.setFormatted("%s/ot.%czs", path, uniqueIndexID[blockType]);
00086     idxfp[0] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
00087 
00088     buf.setFormatted("%s/nt.%czs", path, uniqueIndexID[blockType]);
00089     idxfp[1] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
00090 
00091     buf.setFormatted("%s/ot.%czz", path, uniqueIndexID[blockType]);
00092     textfp[0] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
00093 
00094     buf.setFormatted("%s/nt.%czz", path, uniqueIndexID[blockType]);
00095     textfp[1] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
00096 
00097     buf.setFormatted("%s/ot.%czv", path, uniqueIndexID[blockType]);
00098     compfp[0] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
00099 
00100     buf.setFormatted("%s/nt.%czv", path, uniqueIndexID[blockType]);
00101     compfp[1] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
00102     
00103     instance++;
00104 }

zVerse::~zVerse (  )  [virtual]

Definition at line 111 of file zverse.cpp.

00112 {
00113     int loop1;
00114 
00115     if (cacheBuf) {
00116         flushCache();
00117         free(cacheBuf);
00118     }
00119 
00120     if (path)
00121         delete [] path;
00122 
00123     if (compressor)
00124         delete compressor;
00125 
00126     --instance;
00127 
00128     for (loop1 = 0; loop1 < 2; loop1++) {
00129         FileMgr::getSystemFileMgr()->close(idxfp[loop1]);
00130         FileMgr::getSystemFileMgr()->close(textfp[loop1]);
00131         FileMgr::getSystemFileMgr()->close(compfp[loop1]);
00132     }
00133 }


Member Function Documentation

char zVerse::createModule ( const char *  path,
int  blockBound,
const char *  v11n = "KJV" 
) [static]

Reimplemented in zCom, and zText.

Definition at line 425 of file zverse.cpp.

00426 {
00427     char *path = 0;
00428     char *buf = new char [ strlen (ipath) + 20 ];
00429     FileDesc *fd, *fd2;
00430 
00431     stdstr(&path, ipath);
00432 
00433     if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
00434         path[strlen(path)-1] = 0;
00435 
00436     sprintf(buf, "%s/ot.%czs", path, uniqueIndexID[blockBound]);
00437     FileMgr::removeFile(buf);
00438     fd = FileMgr::getSystemFileMgr()->open(buf, FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE);
00439     fd->getFd();
00440     FileMgr::getSystemFileMgr()->close(fd);
00441 
00442     sprintf(buf, "%s/nt.%czs", path, uniqueIndexID[blockBound]);
00443     FileMgr::removeFile(buf);
00444     fd = FileMgr::getSystemFileMgr()->open(buf, FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE);
00445     fd->getFd();
00446     FileMgr::getSystemFileMgr()->close(fd);
00447 
00448     sprintf(buf, "%s/ot.%czz", path, uniqueIndexID[blockBound]);
00449     FileMgr::removeFile(buf);
00450     fd = FileMgr::getSystemFileMgr()->open(buf, FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE);
00451     fd->getFd();
00452     FileMgr::getSystemFileMgr()->close(fd);
00453 
00454     sprintf(buf, "%s/nt.%czz", path, uniqueIndexID[blockBound]);
00455     FileMgr::removeFile(buf);
00456     fd2 = FileMgr::getSystemFileMgr()->open(buf, FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE);
00457     fd2->getFd();
00458     FileMgr::getSystemFileMgr()->close(fd);
00459 
00460     sprintf(buf, "%s/ot.%czv", path, uniqueIndexID[blockBound]);
00461     FileMgr::removeFile(buf);
00462     fd = FileMgr::getSystemFileMgr()->open(buf, FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE);
00463     fd->getFd();
00464 
00465     sprintf(buf, "%s/nt.%czv", path, uniqueIndexID[blockBound]);
00466     FileMgr::removeFile(buf);
00467     fd2 = FileMgr::getSystemFileMgr()->open(buf, FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE);
00468     fd2->getFd();
00469 
00470     VerseKey vk;
00471     vk.setVersificationSystem(v11n);
00472     vk.setIntros(true);
00473 
00474     __s32 offset = 0;
00475     __s16 size = 0;
00476     offset = archtosword32(offset);
00477     size   = archtosword16(size);
00478 
00479     for (vk = TOP; !vk.popError(); vk++) {
00480         if (vk.getTestament() < 2) {
00481             fd->write(&offset, 4);  //compBufIdxOffset
00482             fd->write(&offset, 4);
00483             fd->write(&size, 2);
00484         }
00485         else {
00486             fd2->write(&offset, 4); //compBufIdxOffset
00487             fd2->write(&offset, 4);
00488             fd2->write(&size, 2);
00489         }
00490     }
00491     fd2->write(&offset, 4); //compBufIdxOffset
00492     fd2->write(&offset, 4);
00493     fd2->write(&size, 2);
00494 
00495     FileMgr::getSystemFileMgr()->close(fd);
00496     FileMgr::getSystemFileMgr()->close(fd2);
00497 
00498     delete [] path;
00499     delete [] buf;
00500     
00501     return 0;
00502 }

void zVerse::doLinkEntry ( char  testmt,
long  destidxoff,
long  srcidxoff 
) [protected]

Definition at line 393 of file zverse.cpp.

00393                                                                      {
00394     __s32 bufidx;
00395     __s32 start;
00396     __u16 size;
00397 
00398     destidxoff *= 10;
00399     srcidxoff  *= 10;
00400 
00401     if (!testmt)
00402         testmt = ((idxfp[1]) ? 1:2);
00403 
00404     // get source
00405     compfp[testmt-1]->seek(srcidxoff, SEEK_SET);
00406     compfp[testmt-1]->read(&bufidx, 4);
00407     compfp[testmt-1]->read(&start, 4);
00408     compfp[testmt-1]->read(&size, 2);
00409 
00410     // write dest
00411     compfp[testmt-1]->seek(destidxoff, SEEK_SET);
00412     compfp[testmt-1]->write(&bufidx, 4);
00413     compfp[testmt-1]->write(&start, 4);
00414     compfp[testmt-1]->write(&size, 2);
00415 }

void zVerse::doSetText ( char  testmt,
long  idxoff,
const char *  buf,
long  len = 0 
) [protected]

Definition at line 298 of file zverse.cpp.

00298                                                                           {
00299 
00300     len = (len < 0) ? strlen(buf) : len;
00301     if (!testmt) 
00302         testmt = ((idxfp[0]) ? 1:2);
00303     if ((!dirtyCache) || (cacheBufIdx < 0)) {
00304         cacheBufIdx = idxfp[testmt-1]->seek(0, SEEK_END) / 12;
00305         cacheTestament = testmt;
00306         if (cacheBuf)
00307             free(cacheBuf);
00308         cacheBuf = (char *)calloc(len + 1, 1);
00309     }
00310     else cacheBuf = (char *)((cacheBuf)?realloc(cacheBuf, strlen(cacheBuf)+(len + 1)):calloc((len + 1), 1));
00311 
00312     dirtyCache = true;
00313 
00314     __u32 start;
00315     __u16 size;
00316     __u32 outBufIdx = cacheBufIdx;
00317 
00318     idxoff *= 10;
00319     size = len;
00320 
00321     start = strlen(cacheBuf);
00322 
00323     if (!size)
00324         start = outBufIdx = 0;
00325 
00326     outBufIdx = archtosword32(outBufIdx);
00327     start  = archtosword32(start);
00328     size   = archtosword16(size);
00329 
00330     compfp[testmt-1]->seek(idxoff, SEEK_SET);
00331     compfp[testmt-1]->write(&outBufIdx, 4);
00332     compfp[testmt-1]->write(&start, 4);
00333     compfp[testmt-1]->write(&size, 2);
00334     strcat(cacheBuf, buf);
00335 }

void zVerse::findOffset ( char  testmt,
long  idxoff,
long *  start,
unsigned short *  size,
unsigned long *  buffnum 
) const

Definition at line 149 of file zverse.cpp.

00150 {
00151     __u32 ulBuffNum    = 0;           // buffer number
00152     __u32 ulVerseStart = 0;        // verse offset within buffer
00153     __u16 usVerseSize  = 0;        // verse size
00154     // set start to offset in
00155     // set size to
00156     // set
00157     *start = *size = *buffnum = 0;
00158     //printf ("Finding offset %ld\n", idxoff);
00159     idxoff *= 10;
00160     if (!testmt) {
00161         testmt = ((idxfp[0]) ? 1:2);
00162     }
00163     
00164     // assert we have and valid file descriptor
00165     if (compfp[testmt-1]->getFd() < 1)
00166         return;
00167         
00168     long newOffset = compfp[testmt-1]->seek(idxoff, SEEK_SET);
00169     if (newOffset == idxoff) {
00170         if (compfp[testmt-1]->read(&ulBuffNum, 4) != 4) {
00171             printf ("Error reading ulBuffNum\n");
00172             return;
00173         }
00174     }
00175     else return;    
00176     
00177     if (compfp[testmt-1]->read(&ulVerseStart, 4) < 2)
00178     {
00179         printf ("Error reading ulVerseStart\n");
00180         return;
00181     }
00182     if (compfp[testmt-1]->read(&usVerseSize, 2) < 2)
00183     {
00184         printf ("Error reading usVerseSize\n");
00185         return;
00186     }
00187 
00188     *buffnum = swordtoarch32(ulBuffNum);
00189     *start = swordtoarch32(ulVerseStart);
00190     *size = swordtoarch16(usVerseSize);
00191 
00192 }

void zVerse::flushCache (  )  const [protected]

Definition at line 338 of file zverse.cpp.

00338                               {
00339     if (dirtyCache) {
00340         __u32 idxoff;
00341         __u32 start, outstart;
00342         __u32 size, outsize;
00343         __u32 zsize, outzsize;
00344 
00345         idxoff = cacheBufIdx * 12;
00346         if (cacheBuf) {
00347             size = outsize = zsize = outzsize = strlen(cacheBuf);
00348             if (size) {
00349     //          if (compressor) {
00350     //              delete compressor;
00351     //              compressor = new LZSSCompress();
00352     //          }
00353                 compressor->Buf(cacheBuf);
00354                 unsigned long tmpSize;
00355                 compressor->zBuf(&tmpSize);
00356                 outzsize = zsize = tmpSize;
00357 
00358                 SWBuf buf;
00359                 buf.setSize(zsize + 5);
00360                 memcpy(buf.getRawData(), compressor->zBuf(&tmpSize), tmpSize);
00361                 outzsize = zsize = tmpSize;
00362                 buf.setSize(zsize);
00363                 rawZFilter(buf, 1); // 1 = encipher
00364 
00365                 start = outstart = textfp[cacheTestament-1]->seek(0, SEEK_END);
00366 
00367                 outstart  = archtosword32(start);
00368                 outsize   = archtosword32(size);
00369                 outzsize  = archtosword32(zsize);
00370 
00371                 textfp[cacheTestament-1]->write(buf, zsize);
00372 
00373                 idxfp[cacheTestament-1]->seek(idxoff, SEEK_SET);
00374                 idxfp[cacheTestament-1]->write(&outstart, 4);
00375                 idxfp[cacheTestament-1]->write(&outzsize, 4);
00376                 idxfp[cacheTestament-1]->write(&outsize, 4);
00377             }
00378             free(cacheBuf);
00379             cacheBuf = 0;
00380         }
00381         dirtyCache = false;
00382     }
00383 }

virtual void zVerse::rawZFilter ( SWBuf buf,
char  direction = 0 
) const [inline, virtual]

Reimplemented in zCom, and zText.

Definition at line 54 of file zverse.h.

00054 { (void) buf; (void) direction; }

void zVerse::zReadText ( char  testmt,
long  start,
unsigned short  size,
unsigned long  buffnum,
SWBuf buf 
) const

Definition at line 205 of file zverse.cpp.

00205                                                                                                                 {
00206     __u32 ulCompOffset = 0;        // compressed buffer start
00207     __u32 ulCompSize   = 0;              // buffer size compressed
00208     __u32 ulUnCompSize = 0;           // buffer size uncompressed
00209 
00210     if (!testmt) {
00211         testmt = ((idxfp[0]) ? 1:2);
00212     }
00213     
00214     // assert we have and valid file descriptor
00215     if (compfp[testmt-1]->getFd() < 1)
00216         return;
00217     
00218     if (size && 
00219         !(((long) ulBuffNum == cacheBufIdx) && (testmt == cacheTestament) && (cacheBuf))) {
00220         //printf ("Got buffer number{%ld} versestart{%ld} versesize{%d}\n", ulBuffNum, ulVerseStart, usVerseSize);
00221 
00222         if (idxfp[testmt-1]->seek(ulBuffNum*12, SEEK_SET)!=(long) ulBuffNum*12)
00223         {
00224             printf ("Error seeking compressed file index\n");
00225             return;
00226         }
00227         if (idxfp[testmt-1]->read(&ulCompOffset, 4)<4)
00228         {
00229             printf ("Error reading ulCompOffset\n");
00230             return;
00231         }
00232         if (idxfp[testmt-1]->read(&ulCompSize, 4)<4)
00233         {
00234             printf ("Error reading ulCompSize\n");
00235             return;
00236         }
00237         if (idxfp[testmt-1]->read(&ulUnCompSize, 4)<4)
00238         {
00239             printf ("Error reading ulUnCompSize\n");
00240             return;
00241         }
00242 
00243         ulCompOffset  = swordtoarch32(ulCompOffset);
00244         ulCompSize  = swordtoarch32(ulCompSize);
00245         ulUnCompSize  = swordtoarch32(ulUnCompSize);
00246 
00247         if (textfp[testmt-1]->seek(ulCompOffset, SEEK_SET)!=(long)ulCompOffset)
00248         {
00249             printf ("Error: could not seek to right place in compressed text\n");
00250             return;
00251         }
00252         SWBuf pcCompText;
00253         pcCompText.setSize(ulCompSize+5);
00254 
00255         if (textfp[testmt-1]->read(pcCompText.getRawData(), ulCompSize)<(long)ulCompSize) {
00256             printf ("Error reading compressed text\n");
00257             return;
00258         }
00259         pcCompText.setSize(ulCompSize);
00260         rawZFilter(pcCompText, 0); // 0 = decipher
00261         
00262         unsigned long bufSize = ulCompSize;
00263         compressor->zBuf(&bufSize, pcCompText.getRawData());
00264 
00265         if (cacheBuf) {
00266             flushCache();
00267             free(cacheBuf);
00268         }
00269         
00270         unsigned long len = 0;
00271         compressor->Buf(0, &len);
00272         cacheBuf = (char *)calloc(len + 1, 1);
00273         memcpy(cacheBuf, compressor->Buf(), len);
00274         cacheBufSize = strlen(cacheBuf);  // TODO: can we just use len?
00275         cacheTestament = testmt;
00276         cacheBufIdx = ulBuffNum;
00277     }   
00278     
00279     inBuf = "";
00280     if ((size > 0) && cacheBuf && ((unsigned)start < cacheBufSize)) {
00281         inBuf.setFillByte(0);
00282         inBuf.setSize(size+1);
00283         strncpy(inBuf.getRawData(), &(cacheBuf[start]), size);
00284         inBuf.setSize(strlen(inBuf.c_str()));
00285     }
00286 }


Member Data Documentation

char* zVerse::cacheBuf [mutable, protected]

Definition at line 32 of file zverse.h.

long zVerse::cacheBufIdx [mutable, protected]

Definition at line 35 of file zverse.h.

unsigned int zVerse::cacheBufSize [mutable, protected]

Definition at line 33 of file zverse.h.

char zVerse::cacheTestament [mutable, protected]

Definition at line 34 of file zverse.h.

FileDesc* zVerse::compfp[2] [protected]

Definition at line 27 of file zverse.h.

Definition at line 20 of file zverse.h.

bool zVerse::dirtyCache [mutable, protected]

Definition at line 36 of file zverse.h.

FileDesc* zVerse::idxfp[2] [protected]

Definition at line 25 of file zverse.h.

SWORD_NAMESPACE_START int zVerse::instance = 0 [static, protected]

Definition at line 23 of file zverse.h.

char zVerse::nl

Definition at line 45 of file zverse.h.

char* zVerse::path [protected]

Definition at line 28 of file zverse.h.

FileDesc* zVerse::textfp[2] [protected]

Definition at line 26 of file zverse.h.

const char zVerse::uniqueIndexID = {'X', 'r', 'v', 'c', 'b'} [static]

Definition at line 44 of file zverse.h.


The documentation for this class was generated from the following files:

Generated on 18 Mar 2013 for The SWORD Project by  doxygen 1.6.1