The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VersificationMgr::System Class Reference

#include <versificationmgr.h>

+ Collaboration diagram for VersificationMgr::System:

Classes

class  Private
 

Public Member Functions

const int * getBMAX () const
 
const BookgetBook (int number) const
 
const BookgetBookByName (const char *bookName) const
 
int getBookCount () const
 
int getBookNumberByOSISName (const char *bookName) const
 
const char * getName () const
 
long getNTStartOffset () const
 
long getOffsetFromVerse (int book, int chapter, int verse) const
 
char getVerseFromOffset (long offset, int *book, int *chapter, int *verse) const
 
void loadFromSBook (const sbook *ot, const sbook *nt, int *chMax, const unsigned char *mappings=NULL)
 
Systemoperator= (const System &other)
 
 System ()
 
 System (const System &other)
 
 System (const char *name)
 
void translateVerse (const System *dstSys, const char **book, int *chapter, int *verse, int *verse_end) const
 
 ~System ()
 

Private Member Functions

void init ()
 

Private Attributes

int BMAX [2]
 
SWBuf name
 
long ntStartOffset
 
Privatep
 

Detailed Description

Definition at line 124 of file versificationmgr.h.

Constructor & Destructor Documentation

VersificationMgr::System::System ( )
inline

Definition at line 133 of file versificationmgr.h.

VersificationMgr::System::System ( const System other)

Definition at line 169 of file versificationmgr.cpp.

169  {
170  init();
171  name = other.name;
172  BMAX[0] = other.BMAX[0];
173  BMAX[1] = other.BMAX[1];
174  (*p) = *(other.p);
175  ntStartOffset = other.ntStartOffset;
176 }
VersificationMgr::System::System ( const char *  name)
inline

Definition at line 136 of file versificationmgr.h.

VersificationMgr::System::~System ( )

Definition at line 189 of file versificationmgr.cpp.

189  {
190  delete p;
191 }

Member Function Documentation

const int* VersificationMgr::System::getBMAX ( ) const
inline

Definition at line 146 of file versificationmgr.h.

146 { return BMAX; };
const VersificationMgr::Book * VersificationMgr::System::getBook ( int  number) const

Definition at line 194 of file versificationmgr.cpp.

194  {
195  return (number < (signed int)p->books.size()) ? &(p->books[number]) : 0;
196 }
const Book* VersificationMgr::System::getBookByName ( const char *  bookName) const
int VersificationMgr::System::getBookCount ( ) const

Definition at line 297 of file versificationmgr.cpp.

297  {
298  return (int)(p ? p->books.size() : 0);
299 }
int VersificationMgr::System::getBookNumberByOSISName ( const char *  bookName) const

Definition at line 199 of file versificationmgr.cpp.

199  {
200  map<SWBuf, int>::const_iterator it = p->osisLookup.find(bookName);
201  return (it != p->osisLookup.end()) ? it->second : -1;
202 }
const char* VersificationMgr::System::getName ( ) const
inline

Definition at line 138 of file versificationmgr.h.

138 { return name.c_str(); }
const char * c_str() const
Definition: swbuf.h:158
long VersificationMgr::System::getNTStartOffset ( ) const
inline

Definition at line 147 of file versificationmgr.h.

147 { return ntStartOffset; }
long VersificationMgr::System::getOffsetFromVerse ( int  book,
int  chapter,
int  verse 
) const

Definition at line 302 of file versificationmgr.cpp.

302  {
303  long offset = -1;
304  chapter--;
305 
306  const Book *b = getBook(book);
307 
308  if (!b) return -1; // assert we have a valid book
309  if ((chapter > -1) && (chapter >= (signed int)b->p->offsetPrecomputed.size())) return -1; // assert we have a valid chapter
310 
311  offset = b->p->offsetPrecomputed[(chapter > -1)?chapter:0];
312  if (chapter < 0) offset--;
313 
314 /* old code
315  *
316  offset = offsets[testament-1][0][book];
317  offset = offsets[testament-1][1][(int)offset + chapter];
318  if (!(offset|verse)) // if we have a testament but nothing else.
319  offset = 1;
320 
321 */
322 
323  return (offset + verse);
324 }
const Book * getBook(int number) const
char VersificationMgr::System::getVerseFromOffset ( long  offset,
int *  book,
int *  chapter,
int *  verse 
) const

Definition at line 327 of file versificationmgr.cpp.

327  {
328 
329  if (offset < 1) { // just handle the module heading corner case up front (and error case)
330  (*book) = -1;
331  (*chapter) = 0;
332  (*verse) = 0;
333  return offset; // < 0 = error
334  }
335 
336  // binary search for book
337  vector<Book>::iterator b = lower_bound(p->books.begin(), p->books.end(), offset, BookOffsetLess());
338  if (b == p->books.end()) b--;
339  (*book) = distance(p->books.begin(), b)+1;
340  if (offset < (*(b->p->offsetPrecomputed.begin()))-((((!(*book)) || (*book)==BMAX[0]+1))?2:1)) { // -1 for chapter headings
341  (*book)--;
342  if (b != p->books.begin()) {
343  b--;
344  }
345  }
346  vector<long>::iterator c = lower_bound(b->p->offsetPrecomputed.begin(), b->p->offsetPrecomputed.end(), offset);
347 
348  // if we're a book heading, we are lessthan chapter precomputes, but greater book. This catches corner case.
349  if (c == b->p->offsetPrecomputed.end()) {
350  c--;
351  }
352  if ((offset < *c) && (c == b->p->offsetPrecomputed.begin())) {
353  (*chapter) = (offset - *c)+1; // should be 0 or -1 (for testament heading)
354  (*verse) = 0;
355  }
356  else {
357  if (offset < *c) c--;
358  (*chapter) = distance(b->p->offsetPrecomputed.begin(), c)+1;
359  (*verse) = (offset - *c);
360  }
361  return ((*chapter > 0) && (*verse > b->getVerseMax(*chapter))) ? KEYERR_OUTOFBOUNDS : 0;
362 }
#define KEYERR_OUTOFBOUNDS
Definition: swkey.h:35
void VersificationMgr::System::init ( )
private

Definition at line 161 of file versificationmgr.cpp.

161  {
162  p = new Private();
163  BMAX[0] = 0;
164  BMAX[1] = 0;
165  ntStartOffset = 0;
166 }
void VersificationMgr::System::loadFromSBook ( const sbook ot,
const sbook nt,
int *  chMax,
const unsigned char *  mappings = NULL 
)

Definition at line 205 of file versificationmgr.cpp.

205  {
206  int chap = 0;
207  int book = 0;
208  long offset = 0; // module heading
209  offset++; // testament heading
210  while (ot->chapmax) {
211  p->books.push_back(Book(ot->name, ot->osis, ot->prefAbbrev, ot->chapmax));
212  offset++; // book heading
213  Book &b = p->books[p->books.size()-1];
214  p->osisLookup[b.getOSISName()] = (int)p->books.size();
215  for (int i = 0; i < ot->chapmax; i++) {
216  b.p->verseMax.push_back(chMax[chap]);
217  offset++; // chapter heading
218  b.p->offsetPrecomputed.push_back(offset);
219  offset += chMax[chap++];
220  }
221  ot++;
222  book++;
223  }
224  BMAX[0] = book;
225  book = 0;
226  ntStartOffset = offset;
227  offset++; // testament heading
228  while (nt->chapmax) {
229  p->books.push_back(Book(nt->name, nt->osis, nt->prefAbbrev, nt->chapmax));
230  offset++; // book heading
231  Book &b = p->books[p->books.size()-1];
232  p->osisLookup[b.getOSISName()] = (int)p->books.size();
233  for (int i = 0; i < nt->chapmax; i++) {
234  b.p->verseMax.push_back(chMax[chap]);
235  offset++; // chapter heading
236  b.p->offsetPrecomputed.push_back(offset);
237  offset += chMax[chap++];
238  }
239  nt++;
240  book++;
241  }
242  BMAX[1] = book;
243 
244  // TODO: build offset speed array
245 
246  // parse mappings
247  if (mappings != NULL) {
248  const unsigned char *m=mappings;
249  for (; *m != 0; m += strlen((const char*)m)+1) {
250  p->mappingsExtraBooks.push_back((const char*)m);
251  }
252  p->mappings.resize(p->books.size()+p->mappingsExtraBooks.size());
253 
254  for (++m; *m != 0; m += 7) {
255  p->mappings[m[0]-1].push_back(m);
256  if (*m > p->books.size()) {
257  p->mappings[m[7]-1].push_back(m);
258  m += 1;
259  }
260  }
261  }
262 }
const char * osis
return NULL
Definition: regex.c:7953
unsigned char chapmax
const char * name
const char * prefAbbrev
vector< const char * > mappingsExtraBooks
VersificationMgr::System & VersificationMgr::System::operator= ( const System other)

Definition at line 179 of file versificationmgr.cpp.

179  {
180  name = other.name;
181  BMAX[0] = other.BMAX[0];
182  BMAX[1] = other.BMAX[1];
183  (*p) = *(other.p);
184  ntStartOffset = other.ntStartOffset;
185  return *this;
186 }
void VersificationMgr::System::translateVerse ( const System dstSys,
const char **  book,
int *  chapter,
int *  verse,
int *  verse_end 
) const

Definition at line 434 of file versificationmgr.cpp.

434  {
435 //dbg_mapping SWLOGD("translate verse from %s to %s: %s.%i.%i-%i\n",getName(), dstSys->getName(), *book, *chapter, *verse, *verse_end);
436 
437  if (!strcmp(getName(),"KJVA") || !strcmp(getName(),"KJV")) {
438  if (!strcmp(dstSys->getName(),"KJVA") || !strcmp(dstSys->getName(),"KJV"))
439  return;
440  // reversed mapping
441  //dbg_mapping SWLOGD("Perform reversed mapping.\n");
442  int b = dstSys->getBookNumberByOSISName(*book)-1;
443 
444  //dbg_mapping SWLOGD("\tgetBookNumberByOSISName %i %s.\n", b, *book);
445 
446  if (b < 0) {
447  //dbg_mapping SWLOGD("\tmappingsExtraBooks.size() %i.\n", dstSys->p->mappingsExtraBooks.size());
448  for (int i=0; i<(int)dstSys->p->mappingsExtraBooks.size(); ++i) {
449  //dbg_mapping SWLOGD("\t%s %s.\n", *book, dstSys->p->mappingsExtraBooks[i]);
450  if (!strcmp(*book, dstSys->p->mappingsExtraBooks[i])) {
451  b = (int)p->books.size()+i-2;
452  break;
453  }
454  }
455  }
456 
457  //dbg_mapping SWLOGD("\tb %i.\n", b);
458 
459  if (b >= (int)dstSys->p->mappings.size() || b < 0) {
460  //dbg_mapping SWLOGD("no modification");
461  return;
462  }
463 
464  const unsigned char *a = NULL;
465 
466  // reversed mapping should use forward search for item
467  for (unsigned int i=0; i<dstSys->p->mappings[b].size(); ++i) {
468  const unsigned char *m = dstSys->p->mappings[b][i];
469 
470  if (m[0] != b+1) continue; // filter inter-book rules
471 
472  if (m[4] == *chapter && m[5] <= *verse) {
473  //dbg_mapping SWLOGD("found mapping %i %i %i %i %i %i\n",m[1],m[2],m[3],m[4],m[5],m[6]);
474  if (m[5] == *verse || (m[6] >= *verse && m[5] <= *verse)) {
475  // inside of any mapping range
476  *chapter = m[1];
477  *verse = m[2];
478  *verse_end = m[3];
479  if (*m >= dstSys->p->books.size()) {
480  SWLog::getSystemLog()->logWarning("map to extra books, possible bug source\n");
481  *book = dstSys->getBook(m[7]-1)->getOSISName();
482  }
483  return;
484  }
485  // destination mapping can have duplicate items, use the last (by using <=)
486  if (a == NULL || (a[5]>a[6]?a[5]:a[6]) <= (m[5]>m[6]?m[5]:m[6]))
487  a = m;
488  }
489  }
490  if (a != NULL) {
491  //dbg_mapping SWLOGD("set appropriate: %i %i %i %i %i %i\n",a[1],a[2],a[3],a[4],a[5],a[6]);
492  (*chapter) = a[1];
493  // shift verse
494  const int d = (a[3]>a[2]?a[3]:a[2])-(a[6]>a[5]?a[6]:a[5]);
495  if (*verse < *verse_end)
496  *verse_end += d;
497  else
498  *verse_end = (*verse) + d;
499  *verse += d;
500  if (*a > dstSys->p->books.size()) {
501  //dbg_mapping SWLOGD("appropriate: %i %i %i %i %i %i %i %i\n",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
502  //dbg_mapping SWLOGD("book: %s\n", dstSys->getBook(a[7]-1)->getOSISName());
503  *book = dstSys->getBook(a[7]-1)->getOSISName();
504  }
505  return;
506  }
507  //dbg_mapping SWLOGD("There is no mapping.\n");
508  }
509  else if (strcmp(dstSys->getName(),"KJVA") && strcmp(dstSys->getName(),"KJV")) {
511  const int src_verse = *verse;
512 
513  translateVerse(kjva, book, chapter, verse, verse_end);
514 
515  int interm_verse = *verse, interm_range = *verse_end, interm_chapter = *chapter;
516  const char *interm_book = *book;
517 
518  kjva->translateVerse(dstSys, book, chapter, verse, verse_end);
519 
520  // contraction->expansion fix
521  if (verse < verse_end && !(interm_verse < interm_range)) {
522  kjva->translateVerse(this, &interm_book, &interm_chapter, &interm_verse, &interm_range);
523  if (interm_verse < interm_range) {
524  *verse += src_verse - interm_verse;
525  if (*verse > *verse_end)
526  *verse = *verse_end;
527  else
528  *verse_end = *verse;
529  }
530  }
531  }
532  else {
533  //dbg_mapping SWLOGD("Perform forward mapping.\n");
534  const int b = getBookNumberByOSISName(*book)-1;
535  if (b >= (int)p->mappings.size())
536  return;
537  // forward mapping should use reversed search for item
538  for (int i = (int)p->mappings[b].size()-1; i>=0; --i) {
539  const unsigned char *m = p->mappings[b][i];
540  if (m[1] < *chapter) {
541  SWLog::getSystemLog()->logWarning("There is no mapping for this chapter.\n");
542  return;
543  }
544  if (m[1] == *chapter && m[2] <= *verse) {
545  //dbg_mapping SWLOGD("found mapping %i %i %i %i %i %i\n",m[1],m[2],m[3],m[4],m[5],m[6]);
546  if (m[2] == *verse || (m[3] >= *verse && m[2] <= *verse)) {
547  *chapter = m[4];
548  *verse = m[5];
549  *verse_end = m[6];
550  }
551  else {
552  *chapter = m[4];
553  // shift verse
554  const int d = (m[6]>m[5]?m[6]:m[5])-(m[3]>m[2]?m[3]:m[2]);
555  if (*verse < *verse_end)
556  *verse_end += d;
557  else
558  *verse_end = (*verse) + d;
559  *verse += d;
560  }
561  if (*m > p->books.size())
562  *book = p->mappingsExtraBooks[m[0]-p->books.size()-1];
563  return;
564  }
565  }
566  //dbg_mapping SWLOGD("No mapping.\n");
567  }
568 }
static SWLog * getSystemLog()
Definition: swlog.cpp:53
return NULL
Definition: regex.c:7953
void translateVerse(const System *dstSys, const char **book, int *chapter, int *verse, int *verse_end) const
int getBookNumberByOSISName(const char *bookName) const
const char * getName() const
void logWarning(const char *fmt,...) const
Definition: swlog.cpp:74
vector< const char * > mappingsExtraBooks
static VersificationMgr * getSystemVersificationMgr()
const System * getVersificationSystem(const char *name) const

Member Data Documentation

int VersificationMgr::System::BMAX[2]
private

Definition at line 129 of file versificationmgr.h.

SWBuf VersificationMgr::System::name
private

Definition at line 128 of file versificationmgr.h.

long VersificationMgr::System::ntStartOffset
private

Definition at line 130 of file versificationmgr.h.

Private* VersificationMgr::System::p
private

Definition at line 126 of file versificationmgr.h.


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