The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rawgenbook.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * rawgenbook.cpp - code for class 'RawGenBook'- a module that reads raw
4  * text files: ot and nt using indexs ??.bks ??.cps ??.vss
5  *
6  * $Id: rawgenbook.cpp 3808 2020-10-02 13:23:34Z scribe $
7  *
8  * Copyright 2002-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 
25 
26 #include <stdio.h>
27 #include <fcntl.h>
28 
29 #include <rawgenbook.h>
30 #include <rawstr.h>
31 #include <utilstr.h>
32 #include <filemgr.h>
33 #include <sysdata.h>
34 #include <treekeyidx.h>
35 #include <versetreekey.h>
36 
38 
39 /******************************************************************************
40  * RawGenBook Constructor - Initializes data for instance of RawGenBook
41  *
42  * ENT: iname - Internal name for module
43  * idesc - Name to display to user for module
44  * idisp - Display object to use for displaying
45  */
46 
47 RawGenBook::RawGenBook(const char *ipath, const char *iname, const char *idesc, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char* ilang, const char *keyType)
48  : SWGenBook(iname, idesc, idisp, enc, dir, mark, ilang) {
49 
50  char *buf = new char [ strlen (ipath) + 20 ];
51 
52  path = 0;
53  stdstr(&path, ipath);
54  verseKey = !strcmp("VerseKey", keyType);
55 
56  if (verseKey) setType("Biblical Texts");
57 
58  if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
59  path[strlen(path)-1] = 0;
60 
61  delete key;
62  key = createKey();
63 
64 
65  sprintf(buf, "%s.bdt", path);
67 
68  delete [] buf;
69 
70 }
71 
72 
73 /******************************************************************************
74  * RawGenBook Destructor - Cleans up instance of RawGenBook
75  */
76 
78 
80 
81  if (path)
82  delete [] path;
83 
84 }
85 
86 
87 bool RawGenBook::isWritable() const {
88  return ((bdtfd->getFd() > 0) && ((bdtfd->mode & FileMgr::RDWR) == FileMgr::RDWR));
89 }
90 
91 
92 /******************************************************************************
93  * RawGenBook::getRawEntry - Returns the correct verse when char * cast
94  * is requested
95  *
96  * RET: string buffer with verse
97  */
98 
100 
101  SW_u32 offset = 0;
102  SW_u32 size = 0;
103 
104  const TreeKey &key = getTreeKey();
105 
106  int dsize;
107  key.getUserData(&dsize);
108  entryBuf = "";
109  if (dsize > 7) {
110  memcpy(&offset, key.getUserData(), 4);
111  offset = swordtoarch32(offset);
112 
113  memcpy(&size, key.getUserData() + 4, 4);
114  size = swordtoarch32(size);
115 
116  entrySize = size; // support getEntrySize call
117 
119  entryBuf.setSize(size);
120  bdtfd->seek(offset, SEEK_SET);
122 
123  rawFilter(entryBuf, 0); // hack, decipher
124  rawFilter(entryBuf, &key);
125 
126 // if (!isUnicode())
128  }
129 
130  return entryBuf;
131 }
132 
133 
134 void RawGenBook::setEntry(const char *inbuf, long len) {
135 
136  SW_u32 offset = (SW_u32)archtosword32(bdtfd->seek(0, SEEK_END));
137  SW_u32 size = 0;
138  TreeKeyIdx *key = ((TreeKeyIdx *)&(getTreeKey()));
139 
140  char userData[8];
141 
142  if (len < 0)
143  len = strlen(inbuf);
144 
145  bdtfd->write(inbuf, len);
146 
147  size = (SW_u32)archtosword32(len);
148  memcpy(userData, &offset, 4);
149  memcpy(userData+4, &size, 4);
150  key->setUserData(userData, 8);
151  key->save();
152 }
153 
154 
155 void RawGenBook::linkEntry(const SWKey *inkey) {
156  const TreeKeyIdx *srcKey = 0;
157  TreeKeyIdx *tmpKey = 0;
158  TreeKeyIdx *key = ((TreeKeyIdx *)&(getTreeKey()));
159  // see if we have a VerseKey * or decendant
160  SWTRY {
161  srcKey = SWDYNAMIC_CAST(const TreeKeyIdx, inkey);
162  }
163  SWCATCH ( ... ) {}
164  // if we don't have a VerseKey * decendant, create our own
165  if (!srcKey) {
166  tmpKey = (TreeKeyIdx *)createKey();
167  (*tmpKey) = *inkey;
168  srcKey = tmpKey;
169  }
170 
171  key->setUserData(srcKey->getUserData(), 8);
172  key->save();
173 
174  if (tmpKey) // free our key if we created a VerseKey
175  delete tmpKey;
176 }
177 
178 
179 /******************************************************************************
180  * RawGenBook::deleteEntry - deletes this entry
181  *
182  * RET: *this
183  */
184 
186  TreeKeyIdx *key = ((TreeKeyIdx *)&(getTreeKey()));
187  key->remove();
188 }
189 
190 
191 char RawGenBook::createModule(const char *ipath) {
192  char *path = 0;
193  char *buf = new char [ strlen (ipath) + 20 ];
194  FileDesc *fd;
195  signed char retval;
196 
197  stdstr(&path, ipath);
198 
199  if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\'))
200  path[strlen(path)-1] = 0;
201 
202  sprintf(buf, "%s.bdt", path);
203  FileMgr::removeFile(buf);
205  fd->getFd();
207 
208  retval = TreeKeyIdx::create(path);
209  delete [] path;
210  return retval;
211 }
212 
213 
215  TreeKey *tKey = new TreeKeyIdx(path);
216  if (verseKey) { SWKey *vtKey = new VerseTreeKey(tKey); delete tKey; return vtKey; }
217  return tKey;
218 }
219 
220 bool RawGenBook::hasEntry(const SWKey *k) const {
221  const TreeKey &key = getTreeKey(k);
222 
223  int dsize;
224  key.getUserData(&dsize);
225  return (dsize > 7) && key.getError() == '\x00';
226 }
227 
void setFillByte(char ch)
Definition: swbuf.h:146
#define SWTextEncoding
Definition: swmodule.h:78
#define SWORD_NAMESPACE_START
Definition: defs.h:39
virtual bool isWritable() const
Definition: rawgenbook.cpp:87
long seek(long offset, int whence)
Definition: filemgr.cpp:143
char * path
Definition: rawgenbook.h:38
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
Definition: swbuf.h:47
virtual bool hasEntry(const SWKey *k) const
Definition: rawgenbook.cpp:220
virtual ~RawGenBook()
Definition: rawgenbook.cpp:77
virtual const char * getUserData(int *size=0) const
Definition: treekeyidx.cpp:97
static void prepText(SWBuf &buf)
Definition: swmodule.cpp:1695
virtual void rawFilter(SWBuf &buf, const SWKey *key) const
Definition: swmodule.h:716
static char createModule(const char *ipath)
Definition: rawgenbook.cpp:191
#define archtosword32(x)
Definition: sysdata.h:97
#define SEEK_END
Definition: zconf.h:246
static unsigned int RDWR
Definition: filemgr.h:76
int mode
Definition: filemgr.h:248
virtual void setUserData(const char *userData, int size=0)
Definition: treekeyidx.cpp:105
#define SWTRY
Definition: defs.h:57
int getFd()
Definition: filemgr.h:231
virtual const char * getUserData(int *size=0) const =0
static signed char create(const char *path)
Definition: treekeyidx.cpp:290
long write(const void *buf, long count)
Definition: filemgr.cpp:153
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
virtual void linkEntry(const SWKey *linkKey)
Definition: rawgenbook.cpp:155
void close(FileDesc *file)
Definition: filemgr.cpp:196
virtual SWKey * createKey() const
Definition: rawgenbook.cpp:214
#define SWCATCH(x)
Definition: defs.h:58
char * getRawData()
Definition: swbuf.h:379
static int removeFile(const char *fName)
Definition: filemgr.cpp:517
#define swordtoarch32(x)
Definition: sysdata.h:94
const TreeKey & getTreeKey(const SWKey *k=0) const
Definition: swgenbook.cpp:51
virtual void setEntry(const char *inbuf, long len=-1)
Definition: rawgenbook.cpp:134
#define SWDYNAMIC_CAST(className, object)
Definition: defs.h:47
FileDesc * bdtfd
Definition: rawgenbook.h:39
int entrySize
Definition: swmodule.h:157
bool verseKey
Definition: rawgenbook.h:40
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
SWBuf entryBuf
Definition: swmodule.h:140
static unsigned int CREAT
Definition: filemgr.h:72
virtual void remove()
Definition: treekeyidx.cpp:243
unsigned int SW_u32
Definition: sysdata.h:41
static unsigned int IWRITE
Definition: filemgr.h:79
static unsigned int WRONLY
Definition: filemgr.h:77
long read(void *buf, long count)
Definition: filemgr.cpp:148
static unsigned int IREAD
Definition: filemgr.h:78
#define SWTextDirection
Definition: swmodule.h:77
SWKey * key
Definition: swmodule.h:124
virtual SWBuf & getRawEntryBuf() const
Definition: rawgenbook.cpp:99
#define SWORD_NAMESPACE_END
Definition: defs.h:40
virtual char getError() const
Definition: swkey.h:164
void setType(const char *imodtype)
Definition: swmodule.h:397
Definition: swkey.h:77
#define SWTextMarkup
Definition: swmodule.h:79
virtual void deleteEntry()
Definition: rawgenbook.cpp:185
RawGenBook(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 *keyType="TreeKey")
Definition: rawgenbook.cpp:47
void setSize(unsigned long len)
Definition: swbuf.h:255
virtual void save()
Definition: treekeyidx.cpp:126
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101