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