The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rawld4.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * rawld4.cpp - code for class 'RawLD'- a module that reads raw
4  * lexicon and dictionary files: *.dat *.idx
5  *
6  * $Id: rawld4.cpp 3749 2020-07-06 23:51:56Z scribe $
7  *
8  * Copyright 2001-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 <fcntl.h>
25 
26 #include <filemgr.h>
27 #include <utilstr.h>
28 #include <rawstr4.h>
29 #include <rawld4.h>
30 
31 #include <stdio.h>
32 
33 
35 
36 
37  /******************************************************************************
38  * RawLD Constructor - Initializes data for instance of RawLD
39  *
40  * ENT: ipath - path and filename of files (no extension)
41  * 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 RawLD4::RawLD4(const char *ipath, const char *iname, const char *idesc, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char* ilang, bool caseSensitive, bool strongsPadding) : RawStr4(ipath, -1, caseSensitive), SWLD(iname, idesc, idisp, enc, dir, mark, ilang, strongsPadding)
47 {
48 }
49 
50 
51 /******************************************************************************
52  * RawLD Destructor - Cleans up instance of RawLD
53  */
54 
56 {
57 }
58 
59 
60 bool RawLD4::isWritable() const {
61  return ((idxfd->getFd() > 0) && ((idxfd->mode & FileMgr::RDWR) == FileMgr::RDWR));
62 }
63 
64 
65 /******************************************************************************
66  * RawLD4::getEntry - Looks up entry from data file. 'Snaps' to closest
67  * entry and sets 'entrybuf'.
68  *
69  * ENT: away - number of entries offset from key (default = 0)
70  *
71  * RET: error status
72  */
73 
74 char RawLD4::getEntry(long away) const
75 {
76  SW_u32 start = 0;
77  SW_u32 size = 0;
78  char *idxbuf = 0;
79  char retval = 0;
80 
81  char *buf = new char [ strlen(*key) + 6 ];
82  strcpy(buf, *key);
83 
84  if (strongsPadding) strongsPad(buf);
85 
86  entryBuf = "";
87  if (!(retval = findOffset(buf, &start, &size, away))) {
88  readText(start, &size, &idxbuf, entryBuf);
89  rawFilter(entryBuf, 0); // hack, decipher
91  entrySize = size; // support getEntrySize call
92  if (!key->isPersist()) // If we have our own key
93  *key = idxbuf; // reset it to entry index buffer
94 
95  stdstr(&entkeytxt, idxbuf); // set entry key text that module 'snapped' to.
96  delete [] idxbuf;
97  }
98 
99  delete [] buf;
100  return retval;
101 }
102 
103 
104 /******************************************************************************
105  * RawLD4::getRawEntry - Returns the correct entry when char * cast
106  * is requested
107  *
108  * RET: string buffer with entry
109  */
110 
112 
113  char ret = getEntry();
114  if (!ret) {
115 // if (!isUnicode())
117  }
118  else error = ret;
119 
120  return entryBuf;
121 }
122 
123 
124 /******************************************************************************
125  * RawLD4::increment - Increments module key a number of entries
126  *
127  * ENT: increment - Number of entries to jump forward
128  *
129  * RET: *this
130  */
131 
132 void RawLD4::increment(int steps) {
133  char tmperror;
134 
135  if (key->isTraversable()) {
136  *key += steps;
137  error = key->popError();
138  steps = 0;
139  }
140 
141  tmperror = (getEntry(steps)) ? KEYERR_OUTOFBOUNDS : 0;
142  error = (error)?error:tmperror;
143  *key = entkeytxt;
144 }
145 
146 
147 void RawLD4::setEntry(const char *inbuf, long len) {
148  char *buf = new char [ strlen(*key) + 6 ];
149  strcpy(buf, *key);
150 
151  if (strongsPadding) strongsPad(buf);
152 
153  doSetText(buf, inbuf, len);
154 
155  delete [] buf;
156 }
157 
158 
159 void RawLD4::linkEntry(const SWKey *inkey) {
160  char *buf = new char [ strlen(*key) + 6 ];
161  strcpy(buf, *key);
162 
163  if (strongsPadding) strongsPad(buf);
164 
165  doLinkEntry(buf, *inkey);
166 
167  delete [] buf;
168 }
169 
170 
171 /******************************************************************************
172  * RawFiles::deleteEntry - deletes this entry
173  *
174  * RET: *this
175  */
176 
178  char *buf = new char [ strlen(*key) + 6 ];
179  strcpy(buf, *key);
180 
181  if (strongsPadding) strongsPad(buf);
182 
183  doSetText(buf, "");
184 
185  delete [] buf;
186 }
187 
188 
189 long RawLD4::getEntryCount() const {
190  if (!idxfd || idxfd->getFd() < 0) return 0;
191  return idxfd->seek(0, SEEK_END) / IDXENTRYSIZE;
192 }
193 
194 
195 long RawLD4::getEntryForKey(const char *key) const {
196  SW_u32 start, offset;
197  SW_u32 size;
198 
199  char *buf = new char [ strlen(key) + 6 ];
200  strcpy(buf, key);
201 
202  if (strongsPadding) strongsPad(buf);
203 
204  findOffset(buf, &start, &size, 0, &offset);
205 
206  delete [] buf;
207 
208  return offset / IDXENTRYSIZE;
209 }
210 
211 
212 char *RawLD4::getKeyForEntry(long entry) const {
213  char *key = 0;
214  getIDXBuf(entry * IDXENTRYSIZE, &key);
215  return key;
216 }
217 
218 
220 
#define SWTextEncoding
Definition: swmodule.h:78
#define SWORD_NAMESPACE_START
Definition: defs.h:39
long seek(long offset, int whence)
Definition: filemgr.cpp:143
virtual ~RawLD4()
Definition: rawld4.cpp:55
static const int IDXENTRYSIZE
Definition: rawstr4.h:44
Definition: swbuf.h:47
static void strongsPad(char *buf)
Definition: swld.cpp:124
static void prepText(SWBuf &buf)
Definition: swmodule.cpp:1695
virtual void rawFilter(SWBuf &buf, const SWKey *key) const
Definition: swmodule.h:716
#define SEEK_END
Definition: zconf.h:246
static unsigned int RDWR
Definition: filemgr.h:76
int mode
Definition: filemgr.h:248
virtual void increment(int steps=1)
Definition: rawld4.cpp:132
bool strongsPadding
Definition: swld.h:40
signed char findOffset(const char *key, SW_u32 *start, SW_u32 *size, long away=0, SW_u32 *idxoff=0) const
Definition: rawstr4.cpp:182
virtual SWBuf & getRawEntryBuf() const
Definition: rawld4.cpp:111
RawLD4(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, bool caseSensitive=false, bool strongsPadding=true)
Definition: rawld4.cpp:46
int getFd()
Definition: filemgr.h:231
virtual long getEntryCount() const
Definition: rawld4.cpp:189
char error
Definition: swmodule.h:120
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
char * entkeytxt
Definition: swld.h:38
bool isPersist() const
Definition: swkey.cpp:99
virtual char * getKeyForEntry(long entry) const
Definition: rawld4.cpp:212
virtual void setEntry(const char *inbuf, long len=-1)
Definition: rawld4.cpp:147
#define KEYERR_OUTOFBOUNDS
Definition: swkey.h:35
int entrySize
Definition: swmodule.h:157
FileDesc * idxfd
Definition: rawstr4.h:46
virtual char popError()
Definition: swkey.cpp:147
void readText(SW_u32 start, SW_u32 *size, char **idxbuf, SWBuf &buf) const
Definition: rawstr4.cpp:318
char getEntry(long away=0) const
Definition: rawld4.cpp:74
void doSetText(const char *key, const char *buf, long len=-1)
Definition: rawstr4.cpp:377
int size
Definition: regex.c:5043
void doLinkEntry(const char *destkey, const char *srckey)
Definition: rawstr4.cpp:496
SWBuf entryBuf
Definition: swmodule.h:140
virtual void deleteEntry()
Definition: rawld4.cpp:177
reg_syntax_t ret
Definition: regex.c:1351
unsigned int SW_u32
Definition: sysdata.h:41
virtual bool isTraversable() const
Definition: swkey.h:222
#define SWTextDirection
Definition: swmodule.h:77
SWKey * key
Definition: swmodule.h:124
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77
virtual void linkEntry(const SWKey *linkKey)
Definition: rawld4.cpp:159
Definition: swld.h:36
#define SWTextMarkup
Definition: swmodule.h:79
void getIDXBuf(long ioffset, char **buf) const
Definition: rawstr4.cpp:146
virtual long getEntryForKey(const char *key) const
Definition: rawld4.cpp:195
virtual bool isWritable() const
Definition: rawld4.cpp:60