The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
zld.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * zld.cpp - code for class 'zLD'- a module that reads zlib compressed
4  * lexicon and dictionary files
5  *
6  * $Id: zld.cpp 3503 2017-11-01 10:36:13Z 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 <utilstr.h>
27 #include <zstr.h>
28 #include <zld.h>
29 #include <filemgr.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 zLD::zLD(const char *ipath, const char *iname, const char *idesc, long blockCount, SWCompress *icomp, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char* ilang, bool caseSensitive, bool strongsPadding) : zStr(ipath, -1, blockCount, icomp, 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  flushCache();
57 }
58 
59 
60 bool zLD::isWritable() const {
61  return ((idxfd->getFd() > 0) && ((idxfd->mode & FileMgr::RDWR) == FileMgr::RDWR));
62 }
63 
64 
65 /******************************************************************************
66  * zLD::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 zLD::getEntry(long away) const {
75  char *idxbuf = 0;
76  char *ebuf = 0;
77  char retval = 0;
78  long index;
79  unsigned long size;
80  char *buf = new char [ strlen(*key) + 6 ];
81  strcpy(buf, *key);
82 
83  if (strongsPadding) strongsPad(buf);
84 
85  entryBuf = "";
86  if (!(retval = findKeyIndex(buf, &index, away))) {
87  getText(index, &idxbuf, &ebuf);
88  size = strlen(ebuf) + 1;
89  entryBuf = ebuf;
90 
92 
93  entrySize = (int)size; // support getEntrySize call
94  if (!key->isPersist()) // If we have our own key
95  *key = idxbuf; // reset it to entry index buffer
96 
97  stdstr(&entkeytxt, idxbuf); // set entry key text that module 'snapped' to.
98  free(idxbuf);
99  free(ebuf);
100  }
101 
102  delete [] buf;
103  return retval;
104 }
105 
106 
107 /******************************************************************************
108  * zLD::getRawEntry - Returns the correct entry when char * cast
109  * is requested
110  *
111  * RET: string buffer with entry
112  */
113 
115  if (!getEntry() /*&& !isUnicode()*/) {
117  }
118 
119  return entryBuf;
120 }
121 
122 
123 /******************************************************************************
124  * zLD::increment - Increments module key a number of entries
125  *
126  * ENT: increment - Number of entries to jump forward
127  *
128  * RET: *this
129  */
130 
131 void zLD::increment(int steps) {
132  char tmperror;
133 
134  if (key->isTraversable()) {
135  *key += steps;
136  error = key->popError();
137  steps = 0;
138  }
139 
140  tmperror = (getEntry(steps)) ? KEYERR_OUTOFBOUNDS : 0;
141  error = (error)?error:tmperror;
142  *key = entkeytxt;
143 }
144 
145 
146 void zLD::setEntry(const char *inbuf, long len) {
147  char *buf = new char [ strlen(*key) + 6 ];
148  strcpy(buf, *key);
149 
150  if (strongsPadding) strongsPad(buf);
151 
152  setText(buf, inbuf, len);
153 
154  delete [] buf;
155 }
156 
157 
158 void zLD::linkEntry(const SWKey *inkey) {
159  char *buf = new char [ strlen(*key) + 6 ];
160  strcpy(buf, *key);
161 
162  if (strongsPadding) strongsPad(buf);
163 
164  zStr::linkEntry(buf, *inkey);
165 
166  delete [] buf;
167 }
168 
169 
170 /******************************************************************************
171  * RawFiles::deleteEntry - deletes this entry
172  *
173  * RET: *this
174  */
175 
177  char *buf = new char [ strlen(*key) + 6 ];
178  strcpy(buf, *key);
179 
180  if (strongsPadding) strongsPad(buf);
181 
182  setText(buf, "");
183 
184  delete [] buf;
185 }
186 
187 
188 long zLD::getEntryCount() const
189 {
190  if (!idxfd || idxfd->getFd() < 0) return 0;
191  return idxfd->seek(0, SEEK_END) / IDXENTRYSIZE;
192 }
193 
194 
195 long zLD::getEntryForKey(const char* key) const
196 {
197  long offset;
198  char *buf = new char [ strlen(key) + 6 ];
199  strcpy(buf, key);
200 
201  if (strongsPadding) strongsPad(buf);
202 
203  findKeyIndex(buf, &offset);
204 
205  delete [] buf;
206 
207  return offset/IDXENTRYSIZE;
208 }
209 
210 
211 char *zLD::getKeyForEntry(long entry) const
212 {
213  char *key = 0;
214  getKeyFromIdxOffset(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 void setEntry(const char *inbuf, long len=-1)
Definition: zld.cpp:146
Definition: swbuf.h:47
static void strongsPad(char *buf)
Definition: swld.cpp:124
virtual void deleteEntry()
Definition: zld.cpp:176
static void prepText(SWBuf &buf)
Definition: swmodule.cpp:1695
virtual void rawFilter(SWBuf &buf, const SWKey *key) const
Definition: swmodule.h:716
void getKeyFromIdxOffset(long ioffset, char **buf) const
Definition: zstr.cpp:168
#define SEEK_END
Definition: zconf.h:246
static unsigned int RDWR
Definition: filemgr.h:76
int mode
Definition: filemgr.h:248
bool strongsPadding
Definition: swld.h:40
virtual void linkEntry(const SWKey *linkKey)
Definition: zld.cpp:158
virtual SWBuf & getRawEntryBuf() const
Definition: zld.cpp:114
FileDesc * idxfd
Definition: zstr.h:50
virtual bool isWritable() const
Definition: zld.cpp:60
void flushCache() const
Definition: zstr.cpp:590
int getFd()
Definition: filemgr.h:231
signed char findKeyIndex(const char *ikey, long *idxoff, long away=0) const
Definition: zstr.cpp:193
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 ~zLD()
Definition: zld.cpp:55
free(preg->fastmap)
void setText(const char *ikey, const char *buf, long len=-1)
Definition: zstr.cpp:435
char getEntry(long away=0) const
Definition: zld.cpp:74
virtual long getEntryForKey(const char *key) const
Definition: zld.cpp:195
virtual char * getKeyForEntry(long entry) const
Definition: zld.cpp:211
#define KEYERR_OUTOFBOUNDS
Definition: swkey.h:35
int entrySize
Definition: swmodule.h:157
virtual char popError()
Definition: swkey.cpp:147
virtual void increment(int steps=1)
Definition: zld.cpp:131
void getText(long index, char **idxbuf, char **buf) const
Definition: zstr.cpp:327
int size
Definition: regex.c:5043
static const int IDXENTRYSIZE
Definition: zstr.h:54
SWBuf entryBuf
Definition: swmodule.h:140
zLD(const char *ipath, const char *iname=0, const char *idesc=0, long blockCount=200, SWCompress *icomp=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: zld.cpp:46
void linkEntry(const char *destkey, const char *srckey)
Definition: zstr.cpp:582
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: zstr.h:36
Definition: swkey.h:77
Definition: swld.h:36
#define SWTextMarkup
Definition: swmodule.h:79
virtual long getEntryCount() const
Definition: zld.cpp:188