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