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 2833 2013-06-29 06:40:28Z chrislit $
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 
35 SWORD_NAMESPACE_START
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 
RawCom(const char * ipath,const char * iname,const char * idesc,SWDisplay * idisp,SWTextEncoding encoding,SWTextDirection dir,SWTextMarkup markup,const char * ilang,const char * versification)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 
~RawCom()55 RawCom::~RawCom()
56 {
57 }
58 
59 
isWritable() const60 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 
getRawEntryBuf() const70 SWBuf &RawCom::getRawEntryBuf() const {
71 	long  start = 0;
72 	unsigned short size = 0;
73 	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())
85 		prepText(entryBuf);
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 
increment(int steps)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 
setEntry(const char * inbuf,long len)133 void RawCom::setEntry(const char *inbuf, long len) {
134 	VerseKey *key = &getVerseKey();
135 	doSetText(key->getTestament(), key->getTestamentIndex(), inbuf, len);
136 }
137 
138 
linkEntry(const SWKey * inkey)139 void RawCom::linkEntry(const SWKey *inkey) {
140 	VerseKey *destkey = &getVerseKey();
141 	const VerseKey *srckey = &getVerseKey(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 
deleteEntry()156 void RawCom::deleteEntry() {
157 
158 	VerseKey *key = &getVerseKey();
159 	doSetText(key->getTestament(), key->getTestamentIndex(), "");
160 }
161 
isLinked(const SWKey * k1,const SWKey * k2) const162 bool RawCom::isLinked(const SWKey *k1, const SWKey *k2) const {
163 	long start1, start2;
164 	unsigned short size1, size2;
165 	VerseKey *vk1 = &getVerseKey(k1);
166 	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 
hasEntry(const SWKey * k) const175 bool RawCom::hasEntry(const SWKey *k) const {
176 	long start;
177 	unsigned short size;
178 	VerseKey *vk = &getVerseKey(k);
179 
180 	findOffset(vk->getTestament(), vk->getTestamentIndex(), &start, &size);
181 	return size;
182 }
183 
184 SWORD_NAMESPACE_END
185