WordRecord.h

Go to the documentation of this file.
00001 //
00002 // WordRecord.h
00003 //
00004 // NAME
00005 // inverted index record.
00006 //
00007 // SYNOPSIS
00008 //
00009 // #include <WordRecord.h>
00010 // 
00011 // WordContext* context;
00012 // WordRecord* record = context->Record();
00013 // if(record->DefaultType() == WORD_RECORD_DATA) {
00014 //   record->info.data = 120;
00015 // } else if(record->DefaultType() == WORD_RECORD_STR) {
00016 //   record->info.str = "foobar";
00017 // }
00018 // delete record;
00019 //
00020 // DESCRIPTION
00021 // 
00022 // The record can contain an integer, if the default record
00023 // type (see CONFIGURATION in <i>WordKeyInfo</i>) is set to <i>DATA</i>
00024 // or a string if set to <i>STR.</i>
00025 // If the type is set to <i>NONE</i> the record does not contain
00026 // any usable information.
00027 //
00028 // Although constructors may be used, the prefered way to create a 
00029 // WordRecord object is by using the <b>WordContext::Record</b> method.
00030 //
00031 // ASCII FORMAT
00032 //
00033 // If default type is <i>DATA</i> it is the decimal representation of
00034 // an integer. If default type is <i>NONE</i> it is the empty string.
00035 //
00036 // END
00037 //
00038 // Part of the ht://Dig package   <http://www.htdig.org/>
00039 // Copyright (c) 1999, 2000, 2001 The ht://Dig Group
00040 // For copyright details, see the file COPYING in your distribution
00041 // or the GNU General Public License version 2 or later
00042 // <http://www.gnu.org/copyleft/gpl.html>
00043 //
00044 // $Id: WordRecord_8h-source.html,v 1.1 2008/06/08 10:13:22 sebdiaz Exp $
00045 //
00046 
00047 #ifndef _WordRecord_h_
00048 #define _WordRecord_h_
00049 
00050 #ifndef SWIG
00051 #include "StringList.h"
00052 #include "WordContext.h"
00053 #endif /* SWIG */
00054 
00055 //
00056 // The data members of WordRecord. Should really be a union but
00057 // is quite difficult to handle properly for scripting language
00058 // interfaces.
00059 //
00060 class WordRecordStorage {
00061  public:
00062   //
00063   // Only a number. Could be stored in str but using this
00064   // allows the compression to perform better.
00065   //
00066   unsigned int  data;
00067   //
00068   // User data
00069   //
00070   String        str;
00071 };
00072 
00073 //
00074 // Describe the data associated with a key (WordKey)
00075 //
00076 // If type is:
00077 //    WORD_RECORD_DATA  info.data is valid
00078 //    WORD_RECORD_STR   info.str is valid
00079 //    WORD_RECORD_NONE  nothing valid
00080 //
00081 class WordRecord
00082 {
00083  public:
00084   //-
00085   // Constructor. Build an empty record.
00086   // The <b>ncontext</b> argument must be a pointer to a valid
00087   // WordContext object.
00088   //
00089   inline WordRecord(WordContext* ncontext) {
00090     context = ncontext;
00091     Clear();
00092   }
00093 
00094   //-
00095   // Reset to empty and set the type to the default specified
00096   // in the configuration.
00097   //
00098   inline void Clear() {
00099     memset((char*)&info, '\0', sizeof(info));
00100     type = DefaultType();
00101   }
00102 
00103   //-
00104   // Return the default type WORD_RECORD_{DATA,STR,NONE}
00105   //
00106   inline int DefaultType() {
00107     return context->GetRecordInfo().default_type;
00108   }
00109 
00110 #ifndef SWIG
00111   //-
00112   // Convert the object to a representation for disk storage written
00113   // in the <b>packed</b> string.
00114   // Return OK on success, NOTOK otherwise.
00115   //
00116   inline int Pack(String& packed) const {
00117     packed.trunc();
00118     switch(type) {
00119 
00120     case WORD_RECORD_DATA:
00121       {
00122         packed << (char)type;
00123         int offset = 1;
00124         packed.ber_push(offset, info.data);
00125       }
00126       break;
00127 
00128     case WORD_RECORD_STR:
00129       packed << (char)type;
00130       packed << info.str;
00131       break;
00132 
00133     case WORD_RECORD_NONE:
00134       packed.trunc();
00135       break;
00136 
00137     default:
00138       fprintf(stderr, "WordRecord::Pack: unknown type %d\n", type);
00139       return NOTOK;
00140       break;
00141     }
00142     return OK;
00143   }
00144 
00145   //-
00146   //
00147   // Alias for Unpack(String(string, length))
00148   //
00149   inline int Unpack(const char* string, int length) {
00150     return Unpack(String(string, length));
00151   }
00152 
00153   //-
00154   // Read the object from a representation for disk storage contained
00155   // in the <b>packed</b> argument.
00156   // Return OK on success, NOTOK otherwise.
00157   //
00158   inline int Unpack(const String& packed) {
00159     String decompressed;
00160 
00161     if(packed.length() == 0)
00162       type = WORD_RECORD_NONE;
00163     else
00164       type = packed[0];
00165     
00166     switch(type) {
00167 
00168     case WORD_RECORD_DATA:
00169       {
00170         int offset = 1;
00171         packed.ber_shift(offset, info.data);
00172       }
00173       break;
00174 
00175     case WORD_RECORD_STR:
00176       info.str = packed.sub(1);
00177       break;
00178 
00179     case WORD_RECORD_NONE:
00180       break;
00181 
00182     default:
00183       fprintf(stderr, "WordRecord::Pack: unknown type %d\n", (int)type);
00184       return NOTOK;
00185       break;
00186     }
00187 
00188     return OK;
00189   }
00190 #endif /* SWIG */
00191 
00192 #ifndef SWIG
00193   //-
00194   // Set the whole structure from ASCII string description stored
00195   // in the <b>bufferin</b> argument.
00196   // Return OK on success, NOTOK otherwise.
00197   //
00198   int Set(const String& bufferin);
00199   int SetList(StringList& fields);
00200   //-
00201   // Convert the whole structure to an ASCII string description
00202   // and return it in the <b>bufferout</b> argument.
00203   // Return OK on success, NOTOK otherwise.
00204   //
00205   int Get(String& bufferout) const;
00206   //-
00207   // Convert the whole structure to an ASCII string description
00208   // and return it.
00209   //
00210   String Get() const;
00211   //-
00212   // Return a pointer to the WordContext object used to create
00213   // this instance.
00214   //
00215   inline WordContext* GetContext() { return context; }
00216   //-
00217   // Return a pointer to the WordContext object used to create
00218   // this instance as a const.
00219   //
00220   inline const WordContext* GetContext() const { return context; }
00221 
00222   //-
00223   // Print object in ASCII form on descriptor <b>f</b> using the
00224   // Get method.
00225   //
00226   int Write(FILE* f) const;
00227 #endif /* SWIG */
00228   void Print() const;
00229   
00230   unsigned char                 type;
00231   WordRecordStorage             info;
00232 #ifndef SWIG
00233   WordContext*                  context;
00234 #endif /* SWIG */
00235 };
00236 
00237 #endif /* _WordRecord_h_ */
00238 

Generated on Sun Jun 8 10:56:40 2008 for GNUmifluz by  doxygen 1.5.5