The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rawverse4.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * rawverse4.cpp - code for class 'RawVerse4'- a module that reads raw
4  * text files:
5  * ot and nt using indexs ??.bks ??.cps ??.vss
6  * and provides lookup and parsing functions based on
7  * class VerseKey
8  *
9  * $Id: rawverse4.cpp 3749 2020-07-06 23:51:56Z scribe $
10  *
11  * Copyright 2007-2013 CrossWire Bible Society (http://www.crosswire.org)
12  * CrossWire Bible Society
13  * P. O. Box 2528
14  * Tempe, AZ 85280-2528
15  *
16  * This program is free software; you can redistribute it and/or modify it
17  * under the terms of the GNU General Public License as published by the
18  * Free Software Foundation version 2.
19  *
20  * This program is distributed in the hope that it will be useful, but
21  * WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * General Public License for more details.
24  *
25  */
26 
27 
28 
29 #include <ctype.h>
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 
34 #include <utilstr.h>
35 #include <rawverse4.h>
36 #include <versekey.h>
37 #include <sysdata.h>
38 #include <filemgr.h>
39 #include <swbuf.h>
40 
41 
43 
44 /******************************************************************************
45  * RawVerse4 Statics
46  */
47 
48 int RawVerse4::instance = 0;
49 const char RawVerse4::nl = '\n';
50 
51 
52 /******************************************************************************
53  * RawVerse4 Constructor - Initializes data for instance of RawVerse4
54  *
55  * ENT: ipath - path of the directory where data and index files are located.
56  * be sure to include the trailing separator (e.g. '/' or '\')
57  * (e.g. 'modules/texts/rawtext/webster/')
58  */
59 
60 RawVerse4::RawVerse4(const char *ipath, int fileMode)
61 {
62  SWBuf buf;
63 
64  path = 0;
65  stdstr(&path, ipath);
66 
67  if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
68  path[strlen(path)-1] = 0;
69 
70  if (fileMode == -1) { // try read/write if possible
71  fileMode = FileMgr::RDWR;
72  }
73 
74  buf.setFormatted("%s/ot.vss", path);
75  idxfp[0] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
76 
77  buf.setFormatted("%s/nt.vss", path);
78  idxfp[1] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
79 
80  buf.setFormatted("%s/ot", path);
81  textfp[0] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
82 
83  buf.setFormatted("%s/nt", path);
84  textfp[1] = FileMgr::getSystemFileMgr()->open(buf, fileMode, true);
85 
86  instance++;
87 }
88 
89 
90 /******************************************************************************
91  * RawVerse4 Destructor - Cleans up instance of RawVerse4
92  */
93 
95 {
96  int loop1;
97 
98  if (path)
99  delete [] path;
100 
101  --instance;
102 
103  for (loop1 = 0; loop1 < 2; loop1++) {
106  }
107 }
108 
109 
110 /******************************************************************************
111  * RawVerse4::findoffset - Finds the offset of the key verse from the indexes
112  *
113  * ENT: testmt - testament to find (0 - Bible/module introduction)
114  * idxoff - offset into .vss
115  * start - address to store the starting offset
116  * size - address to store the size of the entry
117  */
118 
119 void RawVerse4::findOffset(char testmt, long idxoff, long *start, unsigned long *size) const {
120  idxoff *= 8;
121  if (!testmt)
122  testmt = ((idxfp[1]) ? 1:2);
123 
124  if (idxfp[testmt-1]->getFd() >= 0) {
125  idxfp[testmt-1]->seek(idxoff, SEEK_SET);
126  SW_u32 tmpStart;
127  SW_u32 tmpSize;
128  idxfp[testmt-1]->read(&tmpStart, 4);
129  long len = idxfp[testmt-1]->read(&tmpSize, 4); // read size
130 
131  *start = swordtoarch32(tmpStart);
132  *size = swordtoarch32(tmpSize);
133 
134  if (len < 4) {
135  *size = (unsigned long)((*start) ? (textfp[testmt-1]->seek(0, SEEK_END) - (long)*start) : 0); // if for some reason we get an error reading size, make size to end of file
136  }
137  }
138  else {
139  *start = 0;
140  *size = 0;
141  }
142 }
143 
144 
145 /******************************************************************************
146  * RawVerse4::readtext - gets text at a given offset
147  *
148  * ENT: testmt - testament file to search in (0 - Old; 1 - New)
149  * start - starting offset where the text is located in the file
150  * size - size of text entry + 2 (null)(null)
151  * buf - buffer to store text
152  *
153  */
154 
155 void RawVerse4::readText(char testmt, long start, unsigned long size, SWBuf &buf) const {
156  buf = "";
157  buf.setFillByte(0);
158  buf.setSize(size + 1);
159  if (!testmt)
160  testmt = ((idxfp[1]) ? 1:2);
161  if (size) {
162  if (textfp[testmt-1]->getFd() >= 0) {
163  textfp[testmt-1]->seek(start, SEEK_SET);
164  textfp[testmt-1]->read(buf.getRawData(), (int)size);
165  }
166  }
167 }
168 
169 
170 /******************************************************************************
171  * RawVerse4::settext - Sets text for current offset
172  *
173  * ENT: testmt - testament to find (0 - Bible/module introduction)
174  * idxoff - offset into .vss
175  * buf - buffer to store
176  * len - length of buffer (0 - null terminated)
177  */
178 
179 void RawVerse4::doSetText(char testmt, long idxoff, const char *buf, long len)
180 {
181  SW_u32 start;
182  SW_u32 size;
183 
184  idxoff *= 8;
185  if (!testmt)
186  testmt = ((idxfp[1]) ? 1:2);
187 
188  size = (SW_u32)((len < 0) ? strlen(buf) : len);
189 
190  start = (SW_u32)textfp[testmt - 1]->seek(0, SEEK_END);
191  idxfp[testmt-1]->seek(idxoff, SEEK_SET);
192 
193  if (size) {
194  textfp[testmt-1]->seek(start, SEEK_SET);
195  textfp[testmt-1]->write(buf, (int)size);
196 
197  // add a new line to make data file easier to read in an editor
198  textfp[testmt-1]->write(&nl, 1);
199  }
200  else {
201  start = 0;
202  }
203 
204  start = archtosword32(start);
205  size = archtosword32(size);
206 
207  idxfp[testmt-1]->write(&start, 4);
208  idxfp[testmt-1]->write(&size, 4);
209 }
210 
211 
212 /******************************************************************************
213  * RawVerse4::linkentry - links one entry to another
214  *
215  * ENT: testmt - testament to find (0 - Bible/module introduction)
216  * destidxoff - dest offset into .vss
217  * srcidxoff - source offset into .vss
218  */
219 
220 void RawVerse4::doLinkEntry(char testmt, long destidxoff, long srcidxoff) {
221  SW_u32 start;
222  SW_u32 size;
223 
224  destidxoff *= 8;
225  srcidxoff *= 8;
226 
227  if (!testmt)
228  testmt = ((idxfp[1]) ? 1:2);
229 
230  // get source
231  idxfp[testmt-1]->seek(srcidxoff, SEEK_SET);
232  idxfp[testmt-1]->read(&start, 4);
233  idxfp[testmt-1]->read(&size, 4);
234 
235  // write dest
236  idxfp[testmt-1]->seek(destidxoff, SEEK_SET);
237  idxfp[testmt-1]->write(&start, 4);
238  idxfp[testmt-1]->write(&size, 4);
239 }
240 
241 
242 /******************************************************************************
243  * RawVerse4::CreateModule - Creates new module files
244  *
245  * ENT: path - directory to store module files
246  * RET: error status
247  */
248 
249 char RawVerse4::createModule(const char *ipath, const char *v11n)
250 {
251  char *path = 0;
252  char *buf = new char [ strlen (ipath) + 20 ];
253  FileDesc *fd, *fd2;
254 
255  stdstr(&path, ipath);
256 
257  if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
258  path[strlen(path)-1] = 0;
259 
260  sprintf(buf, "%s/ot", path);
261  FileMgr::removeFile(buf);
263  fd->getFd();
265 
266  sprintf(buf, "%s/nt", path);
267  FileMgr::removeFile(buf);
269  fd->getFd();
271 
272  sprintf(buf, "%s/ot.vss", path);
273  FileMgr::removeFile(buf);
275  fd->getFd();
276 
277  sprintf(buf, "%s/nt.vss", path);
278  FileMgr::removeFile(buf);
280  fd2->getFd();
281 
282  VerseKey vk;
283  vk.setVersificationSystem(v11n);
284  vk.setIntros(1);
285  SW_u32 offset = 0;
286  SW_u32 size = 0;
287  offset = archtosword32(offset);
288  size = archtosword32(size);
289 
290  for (vk = TOP; !vk.popError(); vk++) {
291  if (vk.getTestament() < 2) {
292  fd->write(&offset, 4);
293  fd->write(&size, 4);
294  }
295  else {
296  fd2->write(&offset, 4);
297  fd2->write(&size, 4);
298  }
299  }
300  fd2->write(&offset, 4);
301  fd2->write(&size, 4);
302 
305 
306  delete [] path;
307  delete [] buf;
308 
309  return 0;
310 }
311 
void setFillByte(char ch)
Definition: swbuf.h:146
#define TOP
Definition: swkey.h:68
#define SWORD_NAMESPACE_START
Definition: defs.h:39
static int instance
Definition: rawverse4.h:40
long seek(long offset, int whence)
Definition: filemgr.cpp:143
void doSetText(char testmt, long idxoff, const char *buf, long len=-1)
Definition: rawverse4.cpp:179
virtual ~RawVerse4()
Definition: rawverse4.cpp:94
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
Definition: swbuf.h:47
char * path
Definition: rawverse4.h:46
#define archtosword32(x)
Definition: sysdata.h:97
static char createModule(const char *path, const char *v11n="KJV")
Definition: rawverse4.cpp:249
#define SEEK_END
Definition: zconf.h:246
static unsigned int RDWR
Definition: filemgr.h:76
void doLinkEntry(char testmt, long destidxoff, long srcidxoff)
Definition: rawverse4.cpp:220
SWBuf v11n
Definition: osis2mod.cpp:107
int getFd()
Definition: filemgr.h:231
long write(const void *buf, long count)
Definition: filemgr.cpp:153
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
void close(FileDesc *file)
Definition: filemgr.cpp:196
virtual void setIntros(bool val)
Definition: versekey.cpp:1663
char * getRawData()
Definition: swbuf.h:379
virtual void setVersificationSystem(const char *name)
Definition: versekey.cpp:298
static int removeFile(const char *fName)
Definition: filemgr.cpp:517
#define swordtoarch32(x)
Definition: sysdata.h:94
FileDesc * textfp[2]
Definition: rawverse4.h:44
RawVerse4(const char *ipath, int fileMode=-1)
Definition: rawverse4.cpp:60
virtual char popError()
Definition: swkey.cpp:147
#define SEEK_SET
Definition: zconf.h:244
void readText(char testmt, long start, unsigned long size, SWBuf &buf) const
Definition: rawverse4.cpp:155
int size
Definition: regex.c:5043
static unsigned int CREAT
Definition: filemgr.h:72
unsigned int SW_u32
Definition: sysdata.h:41
static unsigned int IWRITE
Definition: filemgr.h:79
static unsigned int WRONLY
Definition: filemgr.h:77
long read(void *buf, long count)
Definition: filemgr.cpp:148
static unsigned int IREAD
Definition: filemgr.h:78
#define SWORD_NAMESPACE_END
Definition: defs.h:40
SWBuf & setFormatted(const char *format,...)
Definition: swbuf.cpp:50
FileDesc * idxfp[2]
Definition: rawverse4.h:43
static const char nl
Definition: rawverse4.h:51
void findOffset(char testmt, long idxoff, long *start, unsigned long *end) const
Definition: rawverse4.cpp:119
void setSize(unsigned long len)
Definition: swbuf.h:255
virtual char getTestament() const
Definition: versekey.cpp:1498
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101