The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ztext.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * ztext.cpp - code for class 'zText'- a module that reads compressed text
4  * files
5  *
6  * $Id: ztext.cpp 3821 2020-11-02 18:33:02Z scribe $
7  *
8  * Copyright 1996-2013 CrossWire Bible Society (http://www.crosswire.org)
9  * CrossWire Bible Society
10  * P. O. Box 2528
11  * Tempe, AZ 85280-2528
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  */
23 
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <sysdata.h>
28 #include <versekey.h>
29 #include <filemgr.h>
30 
31 #include <ztext.h>
32 
34 
35 /******************************************************************************
36  * zText Constructor - Initializes data for instance of zText
37  *
38  * ENT: ipath - path to data files
39  * iname - Internal name for module
40  * idesc - Name to display to user for module
41  * iblockType - verse, chapter, book, etc. of index chunks
42  * icomp - Compressor object
43  * idisp - Display object to use for displaying
44  */
45 
46 zText::zText(const char *ipath, const char *iname, const char *idesc, int iblockType, SWCompress *icomp, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char *ilang, const char *versification)
47  : zVerse(ipath, FileMgr::RDWR, iblockType, icomp), SWText(iname, idesc, idisp, enc, dir, mark, ilang, versification) {
48  blockType = iblockType;
49  lastWriteKey = 0;
50 }
51 
52 
53 /******************************************************************************
54  * zText Destructor - Cleans up instance of zText
55  */
56 
58 {
59  flushCache();
60 
61  if (lastWriteKey)
62  delete lastWriteKey;
63 
64 }
65 
66 
67 bool zText::isWritable() const { return ((idxfp[0]->getFd() > 0) && ((idxfp[0]->mode & FileMgr::RDWR) == FileMgr::RDWR)); }
68 
69 
70 /******************************************************************************
71  * zText::getRawEntry - Returns the current verse buffer
72  *
73  * RET: buffer with verse
74  */
75 
77  long start = 0;
78  unsigned short size = 0;
79  unsigned long buffnum = 0;
80  const VerseKey &key = getVerseKey();
81 
82  findOffset(key.getTestament(), key.getTestamentIndex(), &start, &size, &buffnum);
83  entrySize = size; // support getEntrySize call
84 
85  entryBuf = "";
86 
87  zReadText(key.getTestament(), start, size, buffnum, entryBuf);
88  rawFilter(entryBuf, &key);
89 
90 // if (!isUnicode())
92 
93  return entryBuf;
94 }
95 
96 
98  if (k1->getTestament() != k2->getTestament())
99  return false;
100 
101  switch (blockType) {
102  case VERSEBLOCKS:
103  if (k1->getVerse() != k2->getVerse())
104  return false;
105  case CHAPTERBLOCKS:
106  if (k1->getChapter() != k2->getChapter())
107  return false;
108  case BOOKBLOCKS:
109  if (k1->getBook() != k2->getBook())
110  return false;
111  }
112  return true;
113 }
114 
115 
116 void zText::setEntry(const char *inbuf, long len) {
117  VerseKey &key = getVerseKey();
118 
119  // see if we've jumped across blocks since last write
120  if (lastWriteKey) {
121  if (!sameBlock(lastWriteKey, &key)) {
122  flushCache();
123  }
124  delete lastWriteKey;
125  }
126 
127  doSetText(key.getTestament(), key.getTestamentIndex(), inbuf, len);
128 
129  lastWriteKey = (VerseKey *)key.clone(); // must delete
130 }
131 
132 
133 void zText::linkEntry(const SWKey *inkey) {
134  VerseKey &destkey = getVerseKey();
135  const VerseKey *srckey = &getVerseKeyConst(inkey);
136  doLinkEntry(destkey.getTestament(), destkey.getTestamentIndex(), srckey->getTestamentIndex());
137 }
138 
139 
140 /******************************************************************************
141  * zFiles::deleteEntry - deletes this entry
142  *
143  */
144 
146 
147  VerseKey &key = getVerseKey();
148 
149  doSetText(key.getTestament(), key.getTestamentIndex(), "");
150 }
151 
152 
153 /******************************************************************************
154  * zText::increment - Increments module key a number of entries
155  *
156  * ENT: increment - Number of entries to jump forward
157  *
158  */
159 
160 void zText::increment(int steps) {
161  long start;
162  unsigned short size;
163  unsigned long buffnum;
164  VerseKey *tmpkey = &getVerseKey();
165 
166  findOffset(tmpkey->getTestament(), tmpkey->getTestamentIndex(), &start, &size, &buffnum);
167 
168  SWKey lastgood = *tmpkey;
169  while (steps) {
170  long laststart = start;
171  unsigned short lastsize = size;
172  SWKey lasttry = *tmpkey;
173  (steps > 0) ? ++(*key) : --(*key);
174  tmpkey = &getVerseKey();
175 
176  if ((error = key->popError())) {
177  *key = lastgood;
178  break;
179  }
180  long index = tmpkey->getTestamentIndex();
181  findOffset(tmpkey->getTestament(), index, &start, &size, &buffnum);
182 
183  if (
184  (
185  ((laststart != start) || (lastsize != size)) // we're a different entry
186 // && (start > 0)
187  && (size) // and we actually have a size
188  )
190  ) { // or we don't want to skip consecutive links
191  steps += (steps < 0) ? 1 : -1;
192  lastgood = *tmpkey;
193  }
194  }
195  error = (error) ? KEYERR_OUTOFBOUNDS : 0;
196 }
197 
198 
199 bool zText::isLinked(const SWKey *k1, const SWKey *k2) const {
200  long start1, start2;
201  unsigned short size1, size2;
202  unsigned long buffnum1, buffnum2;
203  const VerseKey *vk1 = &getVerseKey(k1);
204  const VerseKey *vk2 = &getVerseKey(k2);
205  if (vk1->getTestament() != vk2->getTestament()) return false;
206 
207  findOffset(vk1->getTestament(), vk1->getTestamentIndex(), &start1, &size1, &buffnum1);
208  findOffset(vk2->getTestament(), vk2->getTestamentIndex(), &start2, &size2, &buffnum2);
209  return start1 == start2 && buffnum1 == buffnum2;
210 }
211 
212 bool zText::hasEntry(const SWKey *k) const {
213  long start;
214  unsigned short size;
215  unsigned long buffnum;
216  const VerseKey *vk = &getVerseKey(k);
217 
218  findOffset(vk->getTestament(), vk->getTestamentIndex(), &start, &size, &buffnum);
219  return size;
220 }
221 
222 
void doSetText(char testmt, long idxoff, const char *buf, long len=0)
Definition: zverse.cpp:299
#define SWTextEncoding
Definition: swmodule.h:78
#define SWORD_NAMESPACE_START
Definition: defs.h:39
virtual SWKey * clone() const
Definition: versekey.cpp:278
Definition: swbuf.h:47
virtual bool isWritable() const
Definition: ztext.cpp:67
virtual void deleteEntry()
Definition: ztext.cpp:145
Definition: swtext.h:36
static void prepText(SWBuf &buf)
Definition: swmodule.cpp:1695
virtual void rawFilter(SWBuf &buf, const SWKey *key) const
Definition: swmodule.h:716
#define VERSEBLOCKS
Definition: zverse.h:58
static unsigned int RDWR
Definition: filemgr.h:76
virtual bool isLinked(const SWKey *k1, const SWKey *k2) const
Definition: ztext.cpp:199
VerseKey * lastWriteKey
Definition: ztext.h:42
int blockType
Definition: ztext.h:44
void zReadText(char testmt, long start, unsigned short size, unsigned long buffnum, SWBuf &buf) const
Definition: zverse.cpp:206
#define CHAPTERBLOCKS
Definition: zverse.h:59
int size2
Definition: regex.c:5079
bool sameBlock(VerseKey *lastWriteKey, VerseKey *key)
Definition: ztext.cpp:97
char error
Definition: swmodule.h:120
virtual SWBuf & getRawEntryBuf() const
Definition: ztext.cpp:76
zText(const char *ipath, const char *iname=0, const char *idesc=0, int blockType=CHAPTERBLOCKS, SWCompress *icomp=0, SWDisplay *idisp=0, SWTextEncoding encoding=ENC_UNKNOWN, SWTextDirection dir=DIRECTION_LTR, SWTextMarkup markup=FMT_UNKNOWN, const char *ilang=0, const char *versification="KJV")
Definition: ztext.cpp:46
const VerseKey & getVerseKeyConst(const SWKey *key=0) const
Definition: swtext.cpp:95
virtual void linkEntry(const SWKey *linkKey)
Definition: ztext.cpp:133
virtual int getChapter() const
Definition: versekey.cpp:1522
void findOffset(char testmt, long idxoff, long *start, unsigned short *size, unsigned long *buffnum) const
Definition: zverse.cpp:150
virtual bool hasEntry(const SWKey *k) const
Definition: ztext.cpp:212
virtual char getBook() const
Definition: versekey.cpp:1510
bool skipConsecutiveLinks
Definition: swmodule.h:121
virtual int getVerse() const
Definition: versekey.cpp:1534
#define KEYERR_OUTOFBOUNDS
Definition: swkey.h:35
int entrySize
Definition: swmodule.h:157
virtual char popError()
Definition: swkey.cpp:147
const VerseKey & getVerseKey(const SWKey *key=0) const
Definition: swtext.h:46
int size
Definition: regex.c:5043
virtual void setEntry(const char *inbuf, long len=-1)
Definition: ztext.cpp:116
SWBuf entryBuf
Definition: swmodule.h:140
virtual long getTestamentIndex() const
Definition: versekey.cpp:1704
Definition: zverse.h:37
#define SWTextDirection
Definition: swmodule.h:77
SWKey * key
Definition: swmodule.h:124
#define SWORD_NAMESPACE_END
Definition: defs.h:40
virtual void increment(int steps=1)
Definition: ztext.cpp:160
Definition: swkey.h:77
#define BOOKBLOCKS
Definition: zverse.h:60
#define SWTextMarkup
Definition: swmodule.h:79
void doLinkEntry(char testmt, long destidxoff, long srcidxoff)
Definition: zverse.cpp:394
FileDesc * idxfp[2]
Definition: zverse.h:43
void flushCache() const
Definition: zverse.cpp:339
int size1
Definition: regex.c:5079
virtual char getTestament() const
Definition: versekey.cpp:1498
virtual ~zText()
Definition: ztext.cpp:57