The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
step2vpl.cpp File Reference
#include <iostream>
#include <string>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <io.h>
#include <filemgr.h>
#include <lzsscomprs.h>
+ Include dependency graph for step2vpl.cpp:

Go to the source code of this file.

Classes

struct  SectionLevelInfo
 
struct  SectionsHeader
 
struct  Version
 
struct  ViewableBlock
 
struct  ViewableHeader
 
struct  VSyncBooksInfo
 
struct  VSyncHeader
 
struct  VSyncPoint
 

Functions

void cleanBuf (char *buf)
 
void displayBook (int fdbook, int fdviewable, int fdvsync, int fdsections, VSyncBooksInfo *vSyncBooksInfo)
 
void extractVerseText (int fdviewable, int fdbook, SectionLevelInfo *sectionLevelInfo, char **verseText)
 
int main (int argc, char **argv)
 
void readHeaderControlWordAreaText (int fd, char **buf)
 
void readSectionLevelInfo (int fd, SectionLevelInfo *sli)
 
void readSectionName (int fd, SectionLevelInfo *sli, char **name)
 
void readSectionsHeader (int fd, SectionsHeader *sectionsHeaderRecord)
 
void readVersion (int fd, Version *versionRecord)
 
void readViewableBlock (int fd, ViewableBlock *vb)
 
void readViewableBlockText (int fd, ViewableBlock *vb, char **buf)
 
void readViewableHeader (int fd, ViewableHeader *viewableHeaderRecord)
 
void readVSyncBooksInfo (int fd, VSyncHeader *, VSyncBooksInfo **vSyncBooksInfo)
 
void readVSyncHeader (int fd, VSyncHeader *vSyncHeaderRecord)
 

Variables

SWCompresscompress = 0
 
long SECTIONSLEVELSIZE = 29
 
long SECTIONSLEVELSTART = 38
 
long VIEWABLEBLOCKSIZE = 0
 
long VIEWABLEBLOCKSTART = 0
 

Function Documentation

void cleanBuf ( char *  buf)

Definition at line 443 of file step2vpl.cpp.

443  {
444  char *from = buf;
445  char *to = buf;
446 
447  while (*from) {
448  if ((*from != 10) && (*from != 13)) {
449  *to++ = *from++;
450  }
451  else {
452  from++;
453  }
454  }
455  *to = 0;
456 }
void displayBook ( int  fdbook,
int  fdviewable,
int  fdvsync,
int  fdsections,
VSyncBooksInfo vSyncBooksInfo 
)

Definition at line 353 of file step2vpl.cpp.

353  {
354  VSyncPoint vSyncPoint;
355 
356  lseek(fdvsync, vSyncBooksInfo->offset, SEEK_SET);
357 
358  for (int i = 0; i < vSyncBooksInfo->count; i++) {
359 
360  SectionLevelInfo sectionLevelInfo;
361  char *sectionName;
362  char *verseText;
363 
364  read(fdvsync, &(vSyncPoint.chapter), 2);
365  read(fdvsync, &(vSyncPoint.verse), 2);
366  read(fdvsync, &(vSyncPoint.offset), 4);
367  vSyncPoint.offset = SECTIONSLEVELSTART + (vSyncPoint.offset * SECTIONSLEVELSIZE);
368  lseek(fdsections, vSyncPoint.offset, SEEK_SET);
369  readSectionLevelInfo(fdsections, &sectionLevelInfo);
370  readSectionName(fdsections, &sectionLevelInfo, &sectionName);
371  cout << sectionName << " ";
372  delete [] sectionName;
373  extractVerseText(fdviewable, fdbook, &sectionLevelInfo, &verseText);
374  cleanBuf(verseText);
375  cout << verseText << "\n";
376  delete [] verseText;
377  }
378 }
long offset
Definition: step2vpl.cpp:111
long SECTIONSLEVELSIZE
Definition: step2vpl.cpp:50
short chapter
Definition: step2vpl.cpp:109
void cleanBuf(char *buf)
Definition: step2vpl.cpp:443
void readSectionName(int fd, SectionLevelInfo *sli, char **name)
Definition: step2vpl.cpp:420
long SECTIONSLEVELSTART
Definition: step2vpl.cpp:49
#define SEEK_SET
Definition: zconf.h:244
void extractVerseText(int fdviewable, int fdbook, SectionLevelInfo *sectionLevelInfo, char **verseText)
Definition: step2vpl.cpp:381
short verse
Definition: step2vpl.cpp:110
void readSectionLevelInfo(int fd, SectionLevelInfo *sli)
Definition: step2vpl.cpp:429
void extractVerseText ( int  fdviewable,
int  fdbook,
SectionLevelInfo sectionLevelInfo,
char **  verseText 
)

Definition at line 381 of file step2vpl.cpp.

381  {
382  char numberBuf[16];
383  string startToken;
384  ViewableBlock vb;
385  int len = 0;
386  static long lastEntryOffset = -1;
387  static class FreeCachedEntryText {
388  public:
389  char *entryText;
390  FreeCachedEntryText() { entryText = 0; }
391  ~FreeCachedEntryText() { if (entryText) delete [] entryText; }
392  } _freeCachedEntryText;
393 
394  if (sectionLevelInfo->viewableOffset != lastEntryOffset) {
395  if (_freeCachedEntryText.entryText)
396  delete [] _freeCachedEntryText.entryText;
397 
398  lseek(fdviewable, sectionLevelInfo->viewableOffset, SEEK_SET);
399  readViewableBlock(fdviewable, &vb);
400  readViewableBlockText(fdbook, &vb, &(_freeCachedEntryText.entryText));
401  lastEntryOffset = sectionLevelInfo->viewableOffset;
402  }
403  sprintf(numberBuf, "%d", sectionLevelInfo->startLevel);
404  startToken = "\\stepstartlevel";
405  startToken += numberBuf;
406  char *start = strstr(_freeCachedEntryText.entryText, startToken.c_str());
407  if (start) {
408  start += strlen(startToken.c_str());
409  char *end = strstr(start, "\\stepstartlevel");
410  if (end)
411  len = end - start;
412  else len = strlen(start);
413  }
414  *verseText = new char [ len + 1 ];
415  strncpy(*verseText, start, len);
416  (*verseText)[len] = 0;
417 }
void readViewableBlockText(int fd, ViewableBlock *vb, char **buf)
Definition: step2vpl.cpp:312
#define SEEK_SET
Definition: zconf.h:244
void readViewableBlock(int fd, ViewableBlock *vb)
Definition: step2vpl.cpp:324
int main ( int  argc,
char **  argv 
)

Definition at line 142 of file step2vpl.cpp.

142  {
143 
144  compress = new LZSSCompress();
145  char *buf;
146  Version versionRecord;
147  VSyncHeader vSyncHeaderRecord;
148  VSyncBooksInfo *vSyncBooksInfo;
149  SectionsHeader sectionsHeaderRecord;
150  ViewableHeader viewableHeaderRecord;
151 
152 
153  if (argc < 2) {
154  cerr << "usage: "<< *argv << " <database to step module>\n";
155  exit (-1);
156  }
157 
158  string bookpath = argv[1];
159  string fileName;
160 
161  if ((argv[1][strlen(argv[1])-1] != '/') &&
162  (argv[1][strlen(argv[1])-1] != '\\'))
163  bookpath += "/";
164 
165  fileName = bookpath + "Book.dat";
166  int fdbook = FileMgr::openFileReadOnly(fileName.c_str());
167 
168  if (fdbook < 1) {
169  cerr << "error, couldn't open file: " << fileName << "\n";
170  exit (-2);
171  }
172 
173  readVersion(fdbook, &versionRecord);
174  readHeaderControlWordAreaText(fdbook, &buf);
175  delete [] buf;
176 
177 
178  fileName = bookpath + "Viewable.idx";
179  int fdviewable = FileMgr::openFileReadOnly(fileName.c_str());
180 
181  if (fdviewable < 1) {
182  cerr << "error, couldn't open file: " << fileName << "\n";
183  exit (-3);
184  }
185 
186  readVersion(fdviewable, &versionRecord);
187  readViewableHeader(fdviewable, &viewableHeaderRecord);
188 
189  VIEWABLEBLOCKSTART = lseek(fdviewable, 0, SEEK_CUR);
190  VIEWABLEBLOCKSIZE = viewableHeaderRecord.blockEntriesSize;
191 
192 
193  fileName = bookpath + "Vsync.idx";
194  int fdvsync = FileMgr::openFileReadOnly(fileName.c_str());
195 
196  if (fdvsync < 1) {
197  cerr << "error, couldn't open file: " << fileName << "\n";
198  exit (-4);
199  }
200 
201  fileName = bookpath + "Sections.idx";
202  int fdsections = FileMgr::openFileReadOnly(fileName.c_str());
203 
204  if (fdsections < 1) {
205  cerr << "error, couldn't open file: " << fileName << "\n";
206  exit (-4);
207  }
208  readVersion(fdsections, &versionRecord);
209  readSectionsHeader(fdsections, &sectionsHeaderRecord);
210  SECTIONSLEVELSTART = lseek(fdsections, 0, SEEK_CUR);
211  SECTIONSLEVELSIZE = sectionsHeaderRecord.levelEntriesSize;
212 
213  readVersion(fdvsync, &versionRecord);
214  readVSyncHeader(fdvsync, &vSyncHeaderRecord);
215  readVSyncBooksInfo(fdvsync, &vSyncHeaderRecord, &vSyncBooksInfo);
216  int bookCount = vSyncHeaderRecord.endBookNumber - vSyncHeaderRecord.startBookNumber;
217  for (int i = 0; i <= bookCount; i++) {
218  displayBook(fdbook, fdviewable, fdvsync, fdsections, &vSyncBooksInfo[i]);
219  }
220 
221  close(fdviewable);
222  close(fdvsync);
223  close(fdsections);
224  close(fdbook);
225 
226 }
#define SEEK_CUR
Definition: zconf.h:245
static int openFileReadOnly(const char *fName)
Definition: filemgr.cpp:474
long SECTIONSLEVELSIZE
Definition: step2vpl.cpp:50
short levelEntriesSize
Definition: step2vpl.cpp:73
SWCompress * compress
Definition: step2vpl.cpp:140
short blockEntriesSize
Definition: step2vpl.cpp:83
void readVSyncBooksInfo(int fd, VSyncHeader *, VSyncBooksInfo **vSyncBooksInfo)
Definition: step2vpl.cpp:343
short startBookNumber
Definition: step2vpl.cpp:89
long VIEWABLEBLOCKSIZE
Definition: step2vpl.cpp:53
long SECTIONSLEVELSTART
Definition: step2vpl.cpp:49
void readSectionsHeader(int fd, SectionsHeader *sectionsHeaderRecord)
Definition: step2vpl.cpp:254
long VIEWABLEBLOCKSTART
Definition: step2vpl.cpp:52
void readVersion(int fd, Version *versionRecord)
Definition: step2vpl.cpp:230
void displayBook(int fdbook, int fdviewable, int fdvsync, int fdsections, VSyncBooksInfo *vSyncBooksInfo)
Definition: step2vpl.cpp:353
void readViewableHeader(int fd, ViewableHeader *viewableHeaderRecord)
Definition: step2vpl.cpp:272
void readHeaderControlWordAreaText(int fd, char **buf)
Definition: step2vpl.cpp:332
short endBookNumber
Definition: step2vpl.cpp:90
void readVSyncHeader(int fd, VSyncHeader *vSyncHeaderRecord)
Definition: step2vpl.cpp:292
void readHeaderControlWordAreaText ( int  fd,
char **  buf 
)

Definition at line 332 of file step2vpl.cpp.

332  {
333  long headerControlWordAreaSize;
334  read(fd, &headerControlWordAreaSize, 4);
335 
336  *buf = new char [headerControlWordAreaSize + 1];
337 
338  read(fd, *buf, headerControlWordAreaSize);
339  (*buf)[headerControlWordAreaSize] = 0;
340 
341 }
void readSectionLevelInfo ( int  fd,
SectionLevelInfo sli 
)

Definition at line 429 of file step2vpl.cpp.

429  {
430 
431  read(fd, &(sli->parentOffset), 4);
432  read(fd, &(sli->previousOffset), 4);
433  read(fd, &(sli->nextOffset), 4);
434  read(fd, &(sli->viewableOffset), 4);
436  read(fd, &(sli->startLevel), 2);
437  read(fd, &(sli->level), 1);
438  read(fd, &(sli->nameOffset), 4);
439  read(fd, &(sli->outSync_1), 4);
440  read(fd, &(sli->outSync_2), 2);
441 }
long VIEWABLEBLOCKSIZE
Definition: step2vpl.cpp:53
long VIEWABLEBLOCKSTART
Definition: step2vpl.cpp:52
void readSectionName ( int  fd,
SectionLevelInfo sli,
char **  name 
)

Definition at line 420 of file step2vpl.cpp.

420  {
421  short size;
422  lseek(fd, sli->nameOffset, SEEK_SET);
423  read(fd, &size, 2);
424  *name = new char [ size + 1 ];
425  read(fd, *name, size);
426  (*name)[size] = 0;
427 }
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
void readSectionsHeader ( int  fd,
SectionsHeader sectionsHeaderRecord 
)

Definition at line 254 of file step2vpl.cpp.

254  {
255 
256  read(fd, &(sectionsHeaderRecord->sectionsHeaderRecordSize), 2);
257  read(fd, &(sectionsHeaderRecord->levelEntriesCount), 4);
258  read(fd, &(sectionsHeaderRecord->glossEntriesCount), 4);
259  read(fd, &(sectionsHeaderRecord->levelEntriesSize), 2);
260  read(fd, &(sectionsHeaderRecord->reserved), 4);
261 
262  int skip = sectionsHeaderRecord->sectionsHeaderRecordSize - 16/*sizeof(struct ViewableHeader)*/;
263 
264  if (skip) {
265  char *skipbuf = new char[skip];
266  read(fd, skipbuf, skip);
267  delete [] skipbuf;
268  }
269 }
short sectionsHeaderRecordSize
Definition: step2vpl.cpp:70
short levelEntriesSize
Definition: step2vpl.cpp:73
long glossEntriesCount
Definition: step2vpl.cpp:72
long levelEntriesCount
Definition: step2vpl.cpp:71
void readVersion ( int  fd,
Version versionRecord 
)

Definition at line 230 of file step2vpl.cpp.

230  {
231 
232  read(fd, &(versionRecord->versionRecordSize), 2);
233  read(fd, &(versionRecord->publisherID), 2);
234  read(fd, &(versionRecord->bookID), 2);
235  read(fd, &(versionRecord->setID), 2);
236  read(fd, &(versionRecord->conversionProgramVerMajor), 1);
237  read(fd, &(versionRecord->conversionProgramVerMinor), 1);
238  read(fd, &(versionRecord->leastCompatSTEPVerMajor), 1);
239  read(fd, &(versionRecord->leastCompatSTEPVerMinor), 1);
240  read(fd, &(versionRecord->encryptionType), 1);
241  read(fd, &(versionRecord->editionID), 1);
242  read(fd, &(versionRecord->modifiedBy), 2);
243 
244  int skip = versionRecord->versionRecordSize - 16/*sizeof(struct Version*/;
245 
246  if (skip) {
247  char *skipbuf = new char[skip];
248  read(fd, skipbuf, skip);
249  delete [] skipbuf;
250  }
251 }
short versionRecordSize
Definition: step2vpl.cpp:56
short modifiedBy
Definition: step2vpl.cpp:66
short publisherID
Definition: step2vpl.cpp:57
char leastCompatSTEPVerMajor
Definition: step2vpl.cpp:62
char conversionProgramVerMinor
Definition: step2vpl.cpp:61
char leastCompatSTEPVerMinor
Definition: step2vpl.cpp:63
short bookID
Definition: step2vpl.cpp:58
char conversionProgramVerMajor
Definition: step2vpl.cpp:60
char editionID
Definition: step2vpl.cpp:65
char encryptionType
Definition: step2vpl.cpp:64
short setID
Definition: step2vpl.cpp:59
void readViewableBlock ( int  fd,
ViewableBlock vb 
)

Definition at line 324 of file step2vpl.cpp.

324  {
325 
326  read(fd, &(vb->offset), 4);
327  read(fd, &(vb->uncompressedSize), 4);
328  read(fd, &(vb->size), 4);
329 }
long uncompressedSize
Definition: step2vpl.cpp:99
void readViewableBlockText ( int  fd,
ViewableBlock vb,
char **  buf 
)

Definition at line 312 of file step2vpl.cpp.

312  {
313  unsigned long size = vb->size;
314 
315  *buf = new char [ ((vb->size > vb->uncompressedSize) ? vb->size : vb->uncompressedSize) + 1 ];
316  lseek(fd, vb->offset, SEEK_SET);
317  read(fd, *buf, vb->size);
318 
319  compress->setCompressedBuf(&size, *buf);
320  strcpy(*buf, compress->getUncompressedBuf());
321 }
long uncompressedSize
Definition: step2vpl.cpp:99
virtual void setCompressedBuf(unsigned long *len, char *buf=0)
Definition: swcomprs.cpp:101
SWCompress * compress
Definition: step2vpl.cpp:140
virtual char * getUncompressedBuf(unsigned long *len=0)
Definition: swcomprs.cpp:90
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
void readViewableHeader ( int  fd,
ViewableHeader viewableHeaderRecord 
)

Definition at line 272 of file step2vpl.cpp.

272  {
273 
274  read(fd, &(viewableHeaderRecord->viewableHeaderRecordSize), 2);
275  read(fd, &(viewableHeaderRecord->viewableBlocksCount), 4);
276  read(fd, &(viewableHeaderRecord->glossBlocksCount), 4);
277  read(fd, &(viewableHeaderRecord->compressionType), 1);
278  read(fd, &(viewableHeaderRecord->reserved1), 1);
279  read(fd, &(viewableHeaderRecord->blockEntriesSize), 2);
280  read(fd, &(viewableHeaderRecord->reserved2), 2);
281 
282  int skip = viewableHeaderRecord->viewableHeaderRecordSize - 16/*sizeof(struct ViewableHeader)*/;
283 
284  if (skip) {
285  char *skipbuf = new char[skip];
286  read(fd, skipbuf, skip);
287  delete [] skipbuf;
288  }
289 }
short blockEntriesSize
Definition: step2vpl.cpp:83
short reserved2
Definition: step2vpl.cpp:84
long viewableBlocksCount
Definition: step2vpl.cpp:79
char compressionType
Definition: step2vpl.cpp:81
long glossBlocksCount
Definition: step2vpl.cpp:80
short viewableHeaderRecordSize
Definition: step2vpl.cpp:78
void readVSyncBooksInfo ( int  fd,
VSyncHeader vSyncHeaderRecord,
VSyncBooksInfo **  vSyncBooksInfo 
)

Definition at line 343 of file step2vpl.cpp.

343  {
344 
345  int bookCount = vSyncHeaderRecord->endBookNumber - vSyncHeaderRecord->startBookNumber;
346  *vSyncBooksInfo = new VSyncBooksInfo[bookCount];
347  for (int i = 0; i <= bookCount; i++) {
348  read(fd, &(*vSyncBooksInfo)[i].offset, 4);
349  read(fd, &(*vSyncBooksInfo)[i].count, 2);
350  }
351 }
short startBookNumber
Definition: step2vpl.cpp:89
short endBookNumber
Definition: step2vpl.cpp:90
void readVSyncHeader ( int  fd,
VSyncHeader vSyncHeaderRecord 
)

Definition at line 292 of file step2vpl.cpp.

292  {
293 
294  read(fd, &(vSyncHeaderRecord->vSyncHeaderRecordSize), 2);
295  read(fd, &(vSyncHeaderRecord->startBookNumber), 2);
296  read(fd, &(vSyncHeaderRecord->endBookNumber), 2);
297  read(fd, &(vSyncHeaderRecord->bookPointerEntriesSize), 2);
298  read(fd, &(vSyncHeaderRecord->syncPointEntriesSize), 2);
299  read(fd, &(vSyncHeaderRecord->reserved1_1), 4);
300  read(fd, &(vSyncHeaderRecord->reserved1_2), 2);
301 
302  int skip = vSyncHeaderRecord->vSyncHeaderRecordSize - 16/*sizeof(VSyncHeader)*/;
303 
304  if (skip) {
305  char *skipbuf = new char[skip];
306  read(fd, skipbuf, skip);
307  delete [] skipbuf;
308  }
309 }
short reserved1_2
Definition: step2vpl.cpp:94
short syncPointEntriesSize
Definition: step2vpl.cpp:92
short vSyncHeaderRecordSize
Definition: step2vpl.cpp:88
short startBookNumber
Definition: step2vpl.cpp:89
long reserved1_1
Definition: step2vpl.cpp:93
short bookPointerEntriesSize
Definition: step2vpl.cpp:91
short endBookNumber
Definition: step2vpl.cpp:90

Variable Documentation

SWCompress* compress = 0

Definition at line 140 of file step2vpl.cpp.

long SECTIONSLEVELSIZE = 29

Definition at line 50 of file step2vpl.cpp.

long SECTIONSLEVELSTART = 38

Definition at line 49 of file step2vpl.cpp.

long VIEWABLEBLOCKSIZE = 0

Definition at line 53 of file step2vpl.cpp.

long VIEWABLEBLOCKSTART = 0

Definition at line 52 of file step2vpl.cpp.