Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

rawld.cpp

00001 /******************************************************************************
00002  *  rawld.cpp - code for class 'RawLD'- a module that reads raw lexicon and
00003  *                              dictionary files: *.dat *.idx
00004  */
00005 
00006 
00007 #include <ctype.h>
00008 #include <stdio.h>
00009 #include <fcntl.h>
00010 
00011 #ifndef __GNUC__
00012 #include <io.h>
00013 #else
00014 #include <unistd.h>
00015 #endif
00016 
00017 #include <string.h>
00018 #include <utilfuns.h>
00019 #include <rawstr.h>
00020 #include <rawld.h>
00021 
00022 
00023  /******************************************************************************
00024  * RawLD Constructor - Initializes data for instance of RawLD
00025  *
00026  * ENT: ipath   - path and filename of files (no extension)
00027  *              iname   - Internal name for module
00028  *              idesc   - Name to display to user for module
00029  *              idisp   - Display object to use for displaying
00030  */
00031 
00032 RawLD::RawLD(const char *ipath, const char *iname, const char *idesc, SWDisplay *idisp, SWTextEncoding enc, SWTextDirection dir, SWTextMarkup mark, const char* ilang) : RawStr(ipath), SWLD(iname, idesc, idisp, enc, dir, mark, ilang)
00033 {
00034 }
00035 
00036 
00037 /******************************************************************************
00038  * RawLD Destructor - Cleans up instance of RawLD
00039  */
00040 
00041 RawLD::~RawLD()
00042 {
00043 }
00044 
00045 
00046 /******************************************************************************
00047  * RawLD::strongsPad    - Pads a key if it is 100% digits to 5 places
00048  *
00049  * ENT: buf -   buffer to check and pad
00050  */
00051 
00052 void RawLD::strongsPad(char *buf)
00053 {
00054         const char *check;
00055         int size = 0;
00056         int len = strlen(buf);
00057         if ((len < 5) && (len > 0)) {
00058                 for (check = buf; *check; check++) {
00059                         if (!isdigit(*check))
00060                                 break;
00061                         else size++;
00062                 }
00063 
00064                 if ((size == len) && size) 
00065                         sprintf(buf, "%.5d", atoi(buf));
00066         }
00067 }
00068 
00069 
00070 /******************************************************************************
00071  * RawLD::getEntry      - Looks up entry from data file.  'Snaps' to closest
00072  *                              entry and sets 'entrybuf'.
00073  *
00074  * ENT: away - number of entries offset from key (default = 0)
00075  *
00076  * RET: error status
00077  */
00078 
00079 char RawLD::getEntry(long away)
00080 {
00081         long  start = 0;
00082         unsigned short size = 0;
00083         char *idxbuf = 0;
00084         char retval = 0;
00085 
00086         char *buf = new char [ strlen(*key) + 6 ];
00087         strcpy(buf, *key);
00088 
00089         strongsPad(buf);
00090 
00091         *entrybuf = 0;
00092         if (!(retval = findoffset(buf, &start, &size, away))) {
00093                 entrySize = size;        // support getEntrySize call
00094                 if (entrybuf)
00095                         delete [] entrybuf;
00096                 entrybuf = new char [ ++size * FILTERPAD ];
00097                 idxbuf   = new char [ size * FILTERPAD ];
00098 
00099                 gettext(start, size + 1, idxbuf, entrybuf);
00100                 if (!key->Persist())                    // If we have our own key
00101                         *key = idxbuf;                          // reset it to entry index buffer
00102 
00103                 stdstr(&entkeytxt, idxbuf);     // set entry key text that module 'snapped' to.
00104                 delete [] idxbuf;
00105         }
00106         else {
00107                 entrybuf = new char [ 5 ];
00108                 *entrybuf = 0;
00109         }
00110                 
00111         delete [] buf;
00112         return retval;
00113 }
00114 
00115 
00116 /******************************************************************************
00117  * RawLD::operator char *       - Returns the correct entry when char * cast
00118  *                                                      is requested
00119  *
00120  * RET: string buffer with entry
00121  */
00122 
00123 char *RawLD::getRawEntry() {
00124         if (!getEntry() && !isUnicode()) {
00125                 preptext(entrybuf);
00126         }
00127 
00128         return entrybuf;
00129 }
00130 
00131 
00132 /******************************************************************************
00133  * RawLD::operator +=   - Increments module key a number of entries
00134  *
00135  * ENT: increment       - Number of entries to jump forward
00136  *
00137  * RET: *this
00138  */
00139 
00140 SWModule &RawLD::operator +=(int increment)
00141 {
00142         char tmperror;
00143 
00144         if (key->Traversable()) {
00145                 *key += increment;
00146                 error = key->Error();
00147                 increment = 0;
00148         }
00149         
00150         tmperror = (getEntry(increment)) ? KEYERR_OUTOFBOUNDS : 0;
00151         error = (error)?error:tmperror;
00152         *key = entkeytxt;
00153         return *this;
00154 }
00155 
00156 
00157 /******************************************************************************
00158  * RawLD::operator =(SW_POSITION)       - Positions this key if applicable
00159  */
00160 
00161 SWModule &RawLD::operator =(SW_POSITION p)
00162 {
00163         if (!key->Traversable()) {
00164                 switch (p) {
00165                 case POS_TOP:
00166                         *key = "";
00167                         break;
00168                 case POS_BOTTOM:
00169                         *key = "zzzzzzzzz";
00170                         break;
00171                 } 
00172         }
00173         else    *key = p;
00174         return *this;
00175 }
00176 
00177 
00178 SWModule &RawLD::setentry(const char *inbuf, long len) {
00179         settext(*key, inbuf, len);
00180 
00181         return *this;
00182 }
00183 
00184 SWModule &RawLD::operator <<(const char *inbuf) {
00185         return setentry(inbuf, 0);
00186 }
00187 
00188 
00189 SWModule &RawLD::operator <<(const SWKey *inkey) {
00190         linkentry(*key, *inkey);
00191 
00192         return *this;
00193 }
00194 
00195 
00196 /******************************************************************************
00197  * RawFiles::deleteEntry        - deletes this entry
00198  *
00199  * RET: *this
00200  */
00201 
00202 void RawLD::deleteEntry() {
00203         settext(*key, "");
00204 }

Generated on Thu Jun 20 22:13:00 2002 for The Sword Project by doxygen1.2.15