The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rawtext4.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * rawtext4.cpp - code for class 'RawText4'- a module that reads raw text
4  * files: ot and nt using indexs ??.bks ??.cps ??.vss
5  *
6  * $Id: rawtext4.cpp 3821 2020-11-02 18:33:02Z scribe $
7  *
8  * Copyright 2007-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 <stdio.h>
25 #include <fcntl.h>
26 #include <sysdata.h>
27 
28 #include <utilstr.h>
29 #include <rawverse4.h>
30 #include <rawtext4.h>
31 #include <rawstr4.h>
32 #include <filemgr.h>
33 #include <versekey.h>
34 #include <stringmgr.h>
35 
37 
38 /******************************************************************************
39  * RawText4 Constructor - Initializes data for instance of RawText4
40  *
41  * ENT: iname - Internal name for module
42  * idesc - Name to display to user for module
43  * idisp - Display object to use for displaying
44  */
45 
46 RawText4::RawText4(const char *ipath, const char *iname, const char *idesc, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char* ilang, const char *versification)
47  : SWText(iname, idesc, idisp, enc, dir, mark, ilang, versification),
48  RawVerse4(ipath) {
49 }
50 
51 
52 /******************************************************************************
53  * RawText4 Destructor - Cleans up instance of RawText4
54  */
55 
57 }
58 
59 
60 bool RawText4::isWritable() const {
61  return ((idxfp[0]->getFd() > 0) && ((idxfp[0]->mode & FileMgr::RDWR) == FileMgr::RDWR));
62 }
63 
64 
65 /******************************************************************************
66  * RawText4::getRawEntry - Returns the correct verse when char * cast
67  * is requested
68  *
69  * RET: string buffer with verse
70  */
71 
73  long start = 0;
74  unsigned long size = 0;
75  const VerseKey &key = getVerseKey();
76 
77  findOffset(key.getTestament(), key.getTestamentIndex(), &start, &size);
78  entrySize = (int)size; // support getEntrySize call
79 
80  entryBuf = "";
81  readText(key.getTestament(), start, size, entryBuf);
82 
83  rawFilter(entryBuf, 0); // hack, decipher
84  rawFilter(entryBuf, &key);
85 
86 // if (!isUnicode())
88 
89  return entryBuf;
90 }
91 
92 
93 void RawText4::setEntry(const char *inbuf, long len) {
95  doSetText(key.getTestament(), key.getTestamentIndex(), inbuf, len);
96 }
97 
98 
99 void RawText4::linkEntry(const SWKey *inkey) {
100  VerseKey &destkey = getVerseKey();
101  const VerseKey *srckey = &getVerseKeyConst(inkey);
102  doLinkEntry(destkey.getTestament(), destkey.getTestamentIndex(), srckey->getTestamentIndex());
103 }
104 
105 
106 /******************************************************************************
107  * RawText4::deleteEntry - deletes this entry
108  *
109  * RET: *this
110  */
111 
113  VerseKey &key = getVerseKey();
114  doSetText(key.getTestament(), key.getTestamentIndex(), "");
115 }
116 
117 /******************************************************************************
118  * RawText4::increment - Increments module key a number of entries
119  *
120  * ENT: increment - Number of entries to jump forward
121  *
122  * RET: *this
123  */
124 
125 void RawText4::increment(int steps) {
126  long start;
127  unsigned long size;
128  VerseKey *tmpkey = &getVerseKey();
129 
130  findOffset(tmpkey->getTestament(), tmpkey->getTestamentIndex(), &start, &size);
131 
132  SWKey lastgood = *tmpkey;
133  while (steps) {
134  long laststart = start;
135  unsigned long lastsize = size;
136  SWKey lasttry = *tmpkey;
137  (steps > 0) ? ++(*key) : --(*key);
138  tmpkey = &getVerseKey();
139 
140  if ((error = key->popError())) {
141  *key = lastgood;
142  break;
143  }
144  long index = tmpkey->getTestamentIndex();
145  findOffset(tmpkey->getTestament(), index, &start, &size);
146  if (
147  (((laststart != start) || (lastsize != size)) // we're a different entry
148 // && (start > 0)
149  && (size)) // and we actually have a size
150  ||(!skipConsecutiveLinks)) { // or we don't want to skip consecutive links
151  steps += (steps < 0) ? 1 : -1;
152  lastgood = *tmpkey;
153  }
154  }
155  error = (error) ? KEYERR_OUTOFBOUNDS : 0;
156 }
157 
158 bool RawText4::isLinked(const SWKey *k1, const SWKey *k2) const {
159  long start1, start2;
160  unsigned long size1, size2;
161  const VerseKey *vk1 = &getVerseKey(k1);
162  const VerseKey *vk2 = &getVerseKey(k2);
163  if (vk1->getTestament() != vk2->getTestament()) return false;
164 
165  findOffset(vk1->getTestament(), vk1->getTestamentIndex(), &start1, &size1);
166  findOffset(vk2->getTestament(), vk2->getTestamentIndex(), &start2, &size2);
167  if (!size1 || !size2) return false;
168  return start1 == start2;
169 }
170 
171 bool RawText4::hasEntry(const SWKey *k) const {
172  long start;
173  unsigned long size;
174  const VerseKey *vk = &getVerseKey(k);
175 
176  findOffset(vk->getTestament(), vk->getTestamentIndex(), &start, &size);
177  return size;
178 }
179 
#define SWTextEncoding
Definition: swmodule.h:78
#define SWORD_NAMESPACE_START
Definition: defs.h:39
void doSetText(char testmt, long idxoff, const char *buf, long len=-1)
Definition: rawverse4.cpp:179
Definition: swbuf.h:47
virtual bool hasEntry(const SWKey *k) const
Definition: rawtext4.cpp:171
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
static unsigned int RDWR
Definition: filemgr.h:76
virtual SWBuf & getRawEntryBuf() const
Definition: rawtext4.cpp:72
void doLinkEntry(char testmt, long destidxoff, long srcidxoff)
Definition: rawverse4.cpp:220
int size2
Definition: regex.c:5079
char error
Definition: swmodule.h:120
const VerseKey & getVerseKeyConst(const SWKey *key=0) const
Definition: swtext.cpp:95
virtual void increment(int steps=1)
Definition: rawtext4.cpp:125
bool skipConsecutiveLinks
Definition: swmodule.h:121
RawText4(const char *ipath, const char *iname=0, const char *idesc=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: rawtext4.cpp:46
virtual bool isLinked(const SWKey *k1, const SWKey *k2) const
Definition: rawtext4.cpp:158
#define KEYERR_OUTOFBOUNDS
Definition: swkey.h:35
int entrySize
Definition: swmodule.h:157
virtual char popError()
Definition: swkey.cpp:147
virtual void deleteEntry()
Definition: rawtext4.cpp:112
virtual void linkEntry(const SWKey *linkKey)
Definition: rawtext4.cpp:99
const VerseKey & getVerseKey(const SWKey *key=0) const
Definition: swtext.h:46
void readText(char testmt, long start, unsigned long size, SWBuf &buf) const
Definition: rawverse4.cpp:155
virtual bool isWritable() const
Definition: rawtext4.cpp:60
int size
Definition: regex.c:5043
SWBuf entryBuf
Definition: swmodule.h:140
virtual void setEntry(const char *inbuf, long len=-1)
Definition: rawtext4.cpp:93
virtual long getTestamentIndex() const
Definition: versekey.cpp:1704
#define SWTextDirection
Definition: swmodule.h:77
SWKey * key
Definition: swmodule.h:124
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77
FileDesc * idxfp[2]
Definition: rawverse4.h:43
#define SWTextMarkup
Definition: swmodule.h:79
void findOffset(char testmt, long idxoff, long *start, unsigned long *end) const
Definition: rawverse4.cpp:119
int size1
Definition: regex.c:5079
virtual ~RawText4()
Definition: rawtext4.cpp:56
virtual char getTestament() const
Definition: versekey.cpp:1498