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

rawverse.cpp

00001 /******************************************************************************
00002  *  rawverse.cpp   - code for class 'RawVerse'- a module that reads raw text
00003  *                      files:  ot and nt using indexs ??.bks ??.cps ??.vss
00004  *                      and provides lookup and parsing functions based on
00005  *                      class VerseKey
00006  */
00007 
00008 
00009 #include <ctype.h>
00010 #include <stdio.h>
00011 #include <fcntl.h>
00012 #include <errno.h>
00013 
00014 #ifndef __GNUC__
00015 #include <io.h>
00016 #include <sys/stat.h>
00017 #else
00018 #include <unistd.h>
00019 #endif
00020 
00021 #include <string.h>
00022 #include <utilfuns.h>
00023 #include <rawverse.h>
00024 #include <versekey.h>
00025 #include <sysdata.h>
00026 
00027 #ifndef O_BINARY                // O_BINARY is needed in Borland C++ 4.53
00028 #define O_BINARY 0              // If it hasn't been defined than we probably
00029 #endif                          // don't need it.
00030 
00031 
00032 /******************************************************************************
00033  * RawVerse Statics
00034  */
00035 
00036  int RawVerse::instance = 0;
00037 
00038 
00039 /******************************************************************************
00040  * RawVerse Constructor - Initializes data for instance of RawVerse
00041  *
00042  * ENT: ipath - path of the directory where data and index files are located.
00043  *              be sure to include the trailing separator (e.g. '/' or '\')
00044  *              (e.g. 'modules/texts/rawtext/webster/')
00045  */
00046 
00047 RawVerse::RawVerse(const char *ipath, int fileMode)
00048 {
00049         char *buf;
00050 
00051         nl = '\n';
00052         path = 0;
00053         stdstr(&path, ipath);
00054      buf = new char [ strlen(path) + 80 ];
00055         if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
00056                 path[strlen(path)-1] = 0;
00057 
00058         if (fileMode == -1) { // try read/write if possible
00059                 fileMode = O_RDWR;
00060         }
00061                 
00062         sprintf(buf, "%s/ot.vss", path);
00063         idxfp[0] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
00064 
00065         sprintf(buf, "%s/nt.vss", path);
00066         idxfp[1] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
00067 
00068         sprintf(buf, "%s/ot", path);
00069         textfp[0] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
00070 
00071         sprintf(buf, "%s/nt", path);
00072         textfp[1] = FileMgr::systemFileMgr.open(buf, fileMode|O_BINARY, true);
00073 
00074         delete [] buf;
00075         instance++;
00076 }
00077 
00078 
00079 /******************************************************************************
00080  * RawVerse Destructor - Cleans up instance of RawVerse
00081  */
00082 
00083 RawVerse::~RawVerse()
00084 {
00085         int loop1;
00086 
00087         if (path)
00088                 delete [] path;
00089 
00090         --instance;
00091 
00092         for (loop1 = 0; loop1 < 2; loop1++) {
00093                 FileMgr::systemFileMgr.close(idxfp[loop1]);
00094                 FileMgr::systemFileMgr.close(textfp[loop1]);
00095         }
00096 }
00097 
00098 
00099 /******************************************************************************
00100  * RawVerse::findoffset - Finds the offset of the key verse from the indexes
00101  *
00102  * ENT: testmt  - testament to find (0 - Bible/module introduction)
00103  *      idxoff  - offset into .vss
00104  *      start   - address to store the starting offset
00105  *      size    - address to store the size of the entry
00106  */
00107 
00108 void RawVerse::findoffset(char testmt, long idxoff, long *start, unsigned short *size) {
00109         idxoff *= 6;
00110         if (!testmt)
00111                 testmt = ((idxfp[1]) ? 1:2);
00112                 
00113         if (idxfp[testmt-1]->getFd() >= 0) {
00114                 lseek(idxfp[testmt-1]->getFd(), idxoff, SEEK_SET);
00115                 read(idxfp[testmt-1]->getFd(), start, 4);
00116                 long len = read(idxfp[testmt-1]->getFd(), size, 2);             // read size
00117 
00118                 *start = swordtoarch32(*start);
00119                 *size  = swordtoarch16(*size);
00120 
00121                 if (len < 2) {
00122                         *size = (unsigned short)((*start) ? (lseek(textfp[testmt-1]->getFd(), 0, SEEK_END) - (long)*start) : 0);        // if for some reason we get an error reading size, make size to end of file
00123                 }
00124         }
00125         else {
00126                 *start = 0;
00127                 *size = 0;
00128         }
00129 }
00130 
00131 
00132 /******************************************************************************
00133  * RawVerse::preptext   - Prepares the text before returning it to external
00134  *                              objects
00135  *
00136  * ENT: buf     - buffer where text is stored and where to store the prep'd
00137  *                      text.
00138  */
00139 
00140 void RawVerse::preptext(char *buf)
00141 {
00142         char *to, *from, space = 0, cr = 0, realdata = 0, nlcnt = 0;
00143 
00144         for (to = from = buf; *from; from++) {
00145                 switch (*from) {
00146                 case 10:
00147                         if (!realdata)
00148                                 continue;
00149                         space = (cr) ? 0 : 1;
00150                         cr = 0;
00151                         nlcnt++;
00152                         if (nlcnt > 1) {
00153 //                              *to++ = nl;
00154                                 *to++ = nl;
00155 //                              nlcnt = 0;
00156                         }
00157                         continue;
00158                 case 13:
00159                         if (!realdata)
00160                                 continue;
00161                         *to++ = nl;
00162                         space = 0;
00163                         cr = 1;
00164                         continue;
00165                 }
00166                 realdata = 1;
00167                 nlcnt = 0;
00168                 if (space) {
00169                         space = 0;
00170                         if (*from != ' ') {
00171                                 *to++ = ' ';
00172                                 from--;
00173                                 continue;
00174                         }
00175                 }
00176                 *to++ = *from;
00177         }
00178         *to = 0;
00179 
00180         while (to > (buf+1)) {                  // remove trailing excess
00181                 to--;
00182                 if ((*to == 10) || (*to == ' '))
00183                         *to = 0;
00184                 else break;
00185         }
00186 }
00187 
00188 
00189 /******************************************************************************
00190  * RawVerse::gettext    - gets text at a given offset
00191  *
00192  * ENT: testmt  - testament file to search in (0 - Old; 1 - New)
00193  *      start   - starting offset where the text is located in the file
00194  *      size    - size of text entry + 2 (null)(null)
00195  *      buf     - buffer to store text
00196  *
00197  */
00198 
00199 void RawVerse::gettext(char testmt, long start, unsigned short size, char *buf) {
00200         memset(buf, 0, size+1);
00201         if (size) {
00202                 if (textfp[testmt-1]->getFd() >= 0) {
00203                         lseek(textfp[testmt-1]->getFd(), start, SEEK_SET);
00204                         read(textfp[testmt-1]->getFd(), buf, (int)size - 2); 
00205                 }
00206         }
00207 }
00208 
00209 
00210 /******************************************************************************
00211  * RawVerse::settext    - Sets text for current offset
00212  *
00213  * ENT: testmt  - testament to find (0 - Bible/module introduction)
00214  *      idxoff  - offset into .vss
00215  *      buf     - buffer to store
00216  *      len     - length of buffer (0 - null terminated)
00217  */
00218 
00219 void RawVerse::settext(char testmt, long idxoff, const char *buf, long len)
00220 {
00221         long start, outstart;
00222         unsigned short size;
00223         unsigned short outsize;
00224         static const char nl[] = {13, 10};
00225 
00226         idxoff *= 6;
00227         if (!testmt)
00228                 testmt = ((idxfp[1]) ? 1:2);
00229 
00230         size = outsize = len ? len : strlen(buf);
00231 
00232         start = outstart = lseek(textfp[testmt-1]->getFd(), 0, SEEK_END);
00233         lseek(idxfp[testmt-1]->getFd(), idxoff, SEEK_SET);
00234 
00235         if (size) {
00236                 lseek(textfp[testmt-1]->getFd(), start, SEEK_SET);
00237                 write(textfp[testmt-1]->getFd(), buf, (int)size);
00238 
00239                 // add a new line to make data file easier to read in an editor
00240                 write(textfp[testmt-1]->getFd(), &nl, 2);
00241         }
00242         else {
00243                 start = 0;
00244         }
00245 
00246         outstart = archtosword32(start);
00247         outsize  = archtosword16(size);
00248 
00249         write(idxfp[testmt-1]->getFd(), &outstart, 4);
00250         write(idxfp[testmt-1]->getFd(), &outsize, 2);
00251 
00252 
00253 }
00254 
00255 
00256 /******************************************************************************
00257  * RawVerse::linkentry  - links one entry to another
00258  *
00259  * ENT: testmt  - testament to find (0 - Bible/module introduction)
00260  *      destidxoff      - dest offset into .vss
00261  *      srcidxoff               - source offset into .vss
00262  */
00263 
00264 void RawVerse::linkentry(char testmt, long destidxoff, long srcidxoff) {
00265         long start;
00266         unsigned short size;
00267 
00268         destidxoff *= 6;
00269         srcidxoff  *= 6;
00270 
00271         if (!testmt)
00272                 testmt = ((idxfp[1]) ? 1:2);
00273 
00274         // get source
00275         lseek(idxfp[testmt-1]->getFd(), srcidxoff, SEEK_SET);
00276         read(idxfp[testmt-1]->getFd(), &start, 4);
00277         read(idxfp[testmt-1]->getFd(), &size, 2);
00278 
00279         // write dest
00280         lseek(idxfp[testmt-1]->getFd(), destidxoff, SEEK_SET);
00281         write(idxfp[testmt-1]->getFd(), &start, 4);
00282         write(idxfp[testmt-1]->getFd(), &size, 2);
00283 }
00284 
00285 
00286 /******************************************************************************
00287  * RawVerse::CreateModule       - Creates new module files
00288  *
00289  * ENT: path    - directory to store module files
00290  * RET: error status
00291  */
00292 
00293 char RawVerse::createModule(const char *ipath)
00294 {
00295         char *path = 0;
00296         char *buf = new char [ strlen (ipath) + 20 ];
00297         FileDesc *fd, *fd2;
00298 
00299         stdstr(&path, ipath);
00300 
00301         if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
00302                 path[strlen(path)-1] = 0;
00303 
00304         sprintf(buf, "%s/ot", path);
00305         unlink(buf);
00306         fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00307         fd->getFd();
00308         FileMgr::systemFileMgr.close(fd);
00309 
00310         sprintf(buf, "%s/nt", path);
00311         unlink(buf);
00312         fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00313         fd->getFd();
00314         FileMgr::systemFileMgr.close(fd);
00315 
00316         sprintf(buf, "%s/ot.vss", path);
00317         unlink(buf);
00318         fd = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00319         fd->getFd();
00320 
00321         sprintf(buf, "%s/nt.vss", path);
00322         unlink(buf);
00323         fd2 = FileMgr::systemFileMgr.open(buf, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
00324         fd2->getFd();
00325 
00326         VerseKey vk;
00327         vk.Headings(1);
00328         long offset = 0;
00329         short size = 0;
00330         for (vk = TOP; !vk.Error(); vk++) {
00331                 write((vk.Testament() == 1) ? fd->getFd() : fd2->getFd(), &offset, 4);
00332                 write((vk.Testament() == 1) ? fd->getFd() : fd2->getFd(), &size, 2);
00333         }
00334 
00335         FileMgr::systemFileMgr.close(fd);
00336         FileMgr::systemFileMgr.close(fd2);
00337 
00338         delete [] path;
00339 /*
00340         RawVerse rv(path);
00341         VerseKey mykey("Rev 22:21");
00342 */
00343         
00344         return 0;
00345 }

Generated on Wed Apr 3 22:34:14 2002 for The Sword Project by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002