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