The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swld.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * swld.cpp - code for base class 'SWLD'. SWLD is the basis for all
4  * types of Lexicon and Dictionary modules (hence the 'LD').
5  *
6  * $Id: swld.cpp 3439 2016-10-23 08:32:02Z scribe $
7  *
8  * Copyright 1997-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 <ctype.h>
25 #include <stdio.h>
26 #include <swld.h>
27 #include <strkey.h>
28 #include <swkey.h>
29 
30 
32 
33 
34 /******************************************************************************
35  * SWLD Constructor - Initializes data for instance of SWLD
36  *
37  * ENT: imodname - Internal name for module
38  * imoddesc - Name to display to user for module
39  * idisp - Display object to use for displaying
40  */
41 
42 SWLD::SWLD(const char *imodname, const char *imoddesc, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char* ilang, bool strongsPadding) : SWModule(imodname, imoddesc, idisp, (char *)"Lexicons / Dictionaries", enc, dir, mark, ilang), strongsPadding(strongsPadding)
43 {
44  delete key;
45  key = createKey();
46  entkeytxt = new char [1];
47  *entkeytxt = 0;
48 }
49 
50 
51 /******************************************************************************
52  * SWLD Destructor - Cleans up instance of SWLD
53  */
54 
56 {
57  if (entkeytxt)
58  delete [] entkeytxt;
59 }
60 
61 
62 SWKey *SWLD::createKey() const { return new StrKey(); }
63 
64 
65 /******************************************************************************
66  * SWLD::KeyText - Sets/gets module KeyText, getting from saved text if key is
67  * persistent
68  *
69  * ENT: ikeytext - value which to set keytext
70  * [0] - only get
71  *
72  * RET: pointer to keytext
73  */
74 
75 const char *SWLD::getKeyText() const {
76  if (key->isPersist()) {
77  getRawEntryBuf(); // force module key to snap to entry
78  }
79  return entkeytxt;
80 }
81 
82 
83 /******************************************************************************
84  * SWLD::setPosition(SW_POSITION) - Positions this key if applicable
85  */
86 
88  if (!key->isTraversable()) {
89  switch (p) {
90  case POS_TOP:
91  *key = "";
92  break;
93  case POS_BOTTOM:
94  *key = "zzzzzzzzz";
95  break;
96  }
97  }
98  else *key = p;
100 }
101 
102 
103 bool SWLD::hasEntry(const SWKey *key) const {
104  const char *key_str = *key;
105  char *buf = new char [ strlen(key_str) + 6 ];
106  strcpy(buf, key_str);
107 
108  if (strongsPadding) strongsPad(buf);
109 
110  bool retVal = !strcmp(buf, getKeyForEntry(getEntryForKey(buf)));
111  delete [] buf;
112 
113  return retVal;
114 }
115 
116 
117 /******************************************************************************
118  * SWLD::strongsPad - Pads a key if (it-1) is 100% digits to 5 places
119  * allows for final to be alpha, e.g. '123B'
120  *
121  * ENT: buf - buffer to check and pad
122  */
123 
124 void SWLD::strongsPad(char *buf)
125 {
126  char *check;
127  int size = 0;
128  int len = (int)strlen(buf);
129  char subLet = 0;
130  bool bang = false, prefix=false;
131  if ((len < 9) && (len > 0)) {
132  // Handle initial G or H
133  if (*buf == 'G' || *buf == 'H' || *buf == 'g' || *buf == 'h') {
134  buf += 1;
135  len -= 1;
136  prefix = true;
137  }
138 
139  for (check = buf; *(check); check++) {
140  if (!isdigit(*check))
141  break;
142  else size++;
143  }
144 
145  if (size && ((size == len) || (size == len - 1) || (size == (len-2)))) {
146  if (*check == '!') {
147  bang = true;
148  check++;
149  }
150  if (isalpha(*check)) {
151  subLet = toupper(*check);
152  *(check-(bang?1:0)) = 0;
153  }
154  sprintf(buf, prefix?"%.4d":"%.5d", atoi(buf));
155  if (subLet) {
156  check = buf+(strlen(buf));
157  if (bang) {
158  *check++ = '!';
159  }
160  *check++ = subLet;
161  *check = 0;
162  }
163  }
164  }
165 }
166 
167 
169 
#define SWTextEncoding
Definition: swmodule.h:78
#define SWORD_NAMESPACE_START
Definition: defs.h:39
static void strongsPad(char *buf)
Definition: swld.cpp:124
SWLD(const char *imodname=0, const char *imoddesc=0, SWDisplay *idisp=0, SWTextEncoding encoding=ENC_UNKNOWN, SWTextDirection dir=DIRECTION_LTR, SWTextMarkup markup=FMT_UNKNOWN, const char *ilang=0, bool strongsPadding=true)
Definition: swld.cpp:42
virtual void setPosition(SW_POSITION pos)
Definition: swld.cpp:87
static int check(char *buf, char *monthname)
Definition: ftpparse.c:115
bool strongsPadding
Definition: swld.h:40
virtual long getEntryForKey(const char *key) const =0
char * entkeytxt
Definition: swld.h:38
bool isPersist() const
Definition: swkey.cpp:99
#define POS_TOP
Definition: swkey.h:65
virtual const char * getKeyText() const
Definition: swld.cpp:75
virtual SWBuf & getRawEntryBuf() const =0
#define POS_BOTTOM
Definition: swkey.h:66
int size
Definition: regex.c:5043
virtual bool hasEntry(const SWKey *k) const
Definition: swld.cpp:103
virtual char * getKeyForEntry(long entry) const =0
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: strkey.h:37
Definition: swkey.h:77
#define SWTextMarkup
Definition: swmodule.h:79
virtual SWKey * createKey() const
Definition: swld.cpp:62
virtual ~SWLD()
Definition: swld.cpp:55