The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rawcom.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * rawcom.cpp - code for class 'RawCom'- a module that reads raw
4  * commentary files:
5  * ot and nt using indexs ??.bks ??.cps ??.vss
6  *
7  * $Id: rawcom.cpp 3822 2020-11-03 18:54:47Z scribe $
8  *
9  * Copyright 1997-2013 CrossWire Bible Society (http://www.crosswire.org)
10  * CrossWire Bible Society
11  * P. O. Box 2528
12  * Tempe, AZ 85280-2528
13  *
14  * This program is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU General Public License as published by the
16  * Free Software Foundation version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * General Public License for more details.
22  *
23  */
24 
25 
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 
30 #include <filemgr.h>
31 #include <rawverse.h>
32 #include <rawcom.h>
33 #include <versekey.h>
34 
36 
37  /******************************************************************************
38  * RawCom Constructor - Initializes data for instance of RawCom
39  *
40  * ENT: iname - Internal name for module
41  * idesc - Name to display to user for module
42  * idisp - Display object to use for displaying
43  */
44 
45 RawCom::RawCom(const char *ipath, const char *iname, const char *idesc, SWDisplay *idisp, SWTextEncoding encoding, SWTextDirection dir, SWTextMarkup markup, const char *ilang, const char *versification)
46  : RawVerse(ipath),
47  SWCom(iname, idesc, idisp, encoding, dir, markup, ilang, versification) {
48 }
49 
50 
51 /******************************************************************************
52  * RawCom Destructor - Cleans up instance of RawCom
53  */
54 
56 {
57 }
58 
59 
60 bool RawCom::isWritable() const {
61  return ((idxfp[0]->getFd() > 0) && ((idxfp[0]->mode & FileMgr::RDWR) == FileMgr::RDWR));
62 }
63 /******************************************************************************
64  * RawCom::getRawEntry() - Returns the correct verse when char * cast
65  * is requested
66  *
67  * RET: string buffer with verse
68  */
69 
71  long start = 0;
72  unsigned short size = 0;
73  const VerseKey *key = &getVerseKey();
74 
75  findOffset(key->getTestament(), key->getTestamentIndex(), &start, &size);
76  entrySize = size; // support getEntrySize call
77 
78  entryBuf = "";
79  readText(key->getTestament(), start, size, entryBuf);
80 
81  rawFilter(entryBuf, 0); // hack, decipher
82  rawFilter(entryBuf, key);
83 
84 // if (!isUnicode())
86 
87  return entryBuf;
88 }
89 
90 
91 /******************************************************************************
92  * RawCom::increment - Increments module key a number of entries
93  *
94  * ENT: steps - Number of entries to jump forward
95  *
96  * RET: *this
97  */
98 
99 void RawCom::increment(int steps) {
100  long start;
101  unsigned short size;
102  VerseKey *tmpkey = &getVerseKey();
103 
104  findOffset(tmpkey->getTestament(), tmpkey->getTestamentIndex(), &start, &size);
105 
106  SWKey lastgood = *tmpkey;
107  while (steps) {
108  long laststart = start;
109  unsigned short lastsize = size;
110  SWKey lasttry = *tmpkey;
111  (steps > 0) ? ++(*key) : --(*key);
112  tmpkey = &getVerseKey();
113 
114  if ((error = key->popError())) {
115  *key = lastgood;
116  break;
117  }
118  long index = tmpkey->getTestamentIndex();
119  findOffset(tmpkey->getTestament(), index, &start, &size);
120  if (
121  (((laststart != start) || (lastsize != size)) // we're a different entry
122 // && (start > 0)
123  && (size)) // and we actually have a size
124  ||(!skipConsecutiveLinks)) { // or we don't want to skip consecutive links
125  steps += (steps < 0) ? 1 : -1;
126  lastgood = *tmpkey;
127  }
128  }
129  error = (error) ? KEYERR_OUTOFBOUNDS : 0;
130 }
131 
132 
133 void RawCom::setEntry(const char *inbuf, long len) {
134  VerseKey *key = &getVerseKey();
135  doSetText(key->getTestament(), key->getTestamentIndex(), inbuf, len);
136 }
137 
138 
139 void RawCom::linkEntry(const SWKey *inkey) {
140  VerseKey *destkey = &getVerseKey();
141  const VerseKey *srckey = &getVerseKeyConst(inkey);
142 
143  doLinkEntry(destkey->getTestament(), destkey->getTestamentIndex(), srckey->getTestamentIndex());
144 
145  if (inkey != srckey) // free our key if we created a VerseKey
146  delete srckey;
147 }
148 
149 
150 /******************************************************************************
151  * RawCom::deleteEntry - deletes this entry
152  *
153  * RET: *this
154  */
155 
157 
158  VerseKey *key = &getVerseKey();
159  doSetText(key->getTestament(), key->getTestamentIndex(), "");
160 }
161 
162 bool RawCom::isLinked(const SWKey *k1, const SWKey *k2) const {
163  long start1, start2;
164  unsigned short size1, size2;
165  const VerseKey *vk1 = &getVerseKey(k1);
166  const VerseKey *vk2 = &getVerseKey(k2);
167  if (vk1->getTestament() != vk2->getTestament()) return false;
168 
169  findOffset(vk1->getTestament(), vk1->getTestamentIndex(), &start1, &size1);
170  findOffset(vk2->getTestament(), vk2->getTestamentIndex(), &start2, &size2);
171  if (!size1 || !size2) return false;
172  return start1 == start2;
173 }
174 
175 bool RawCom::hasEntry(const SWKey *k) const {
176  long start;
177  unsigned short size;
178  const VerseKey *vk = &getVerseKey(k);
179 
180  findOffset(vk->getTestament(), vk->getTestamentIndex(), &start, &size);
181  return size;
182 }
183 
#define SWTextEncoding
Definition: swmodule.h:78
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
const VerseKey & getVerseKeyConst(const SWKey *key=0) const
Definition: swcom.cpp:90
static void prepText(SWBuf &buf)
Definition: swmodule.cpp:1695
virtual void rawFilter(SWBuf &buf, const SWKey *key) const
Definition: swmodule.h:716
virtual bool isWritable() const
Definition: rawcom.cpp:60
static unsigned int RDWR
Definition: filemgr.h:76
void findOffset(char testmt, long idxoff, long *start, unsigned short *end) const
Definition: rawverse.cpp:118
virtual void deleteEntry()
Definition: rawcom.cpp:156
int size2
Definition: regex.c:5079
virtual void linkEntry(const SWKey *linkKey)
Definition: rawcom.cpp:139
char error
Definition: swmodule.h:120
virtual bool hasEntry(const SWKey *k) const
Definition: rawcom.cpp:175
FileDesc * idxfp[2]
Definition: rawverse.h:42
virtual void increment(int steps=1)
Definition: rawcom.cpp:99
RawCom(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: rawcom.cpp:45
virtual SWBuf & getRawEntryBuf() const
Definition: rawcom.cpp:70
bool skipConsecutiveLinks
Definition: swmodule.h:121
void readText(char testmt, long start, unsigned short size, SWBuf &buf) const
Definition: rawverse.cpp:154
Definition: swcom.h:49
#define KEYERR_OUTOFBOUNDS
Definition: swkey.h:35
int entrySize
Definition: swmodule.h:157
virtual char popError()
Definition: swkey.cpp:147
virtual bool isLinked(const SWKey *k1, const SWKey *k2) const
Definition: rawcom.cpp:162
int size
Definition: regex.c:5043
void doLinkEntry(char testmt, long destidxoff, long srcidxoff)
Definition: rawverse.cpp:219
SWBuf entryBuf
Definition: swmodule.h:140
virtual long getTestamentIndex() const
Definition: versekey.cpp:1704
void doSetText(char testmt, long idxoff, const char *buf, long len=-1)
Definition: rawverse.cpp:178
#define SWTextDirection
Definition: swmodule.h:77
SWKey * key
Definition: swmodule.h:124
#define SWORD_NAMESPACE_END
Definition: defs.h:40
virtual ~RawCom()
Definition: rawcom.cpp:55
Definition: swkey.h:77
#define SWTextMarkup
Definition: swmodule.h:79
VerseKey & getVerseKey(SWKey *key=0)
Definition: swcom.cpp:123
int size1
Definition: regex.c:5079
virtual void setEntry(const char *inbuf, long len=-1)
Definition: rawcom.cpp:133
virtual char getTestament() const
Definition: versekey.cpp:1498