00001 // 00002 // Part of the ht://Dig package <http://www.htdig.org/> 00003 // Copyright (c) 1999, 2000, 2001 The ht://Dig Group 00004 // For copyright details, see the file COPYING in your distribution 00005 // or the GNU General Public License version 2 or later 00006 // <http://www.gnu.org/copyleft/gpl.html> 00007 // 00008 // 00009 // WordRecord.cc 00010 // 00011 // WordRecord: data portion of the inverted index database 00012 // 00013 #ifdef HAVE_CONFIG_H 00014 #include "config.h" 00015 #endif /* HAVE_CONFIG_H */ 00016 00017 #include <stdlib.h> 00018 00019 #include "WordRecord.h" 00020 00021 // 00022 // WordRecord implementation 00023 // 00024 00025 // 00026 // Convert the whole structure to an ascii string description 00027 // 00028 int 00029 WordRecord::Get(String& buffer) const 00030 { 00031 buffer.trunc(); 00032 00033 switch(type) { 00034 00035 case WORD_RECORD_DATA: 00036 buffer << info.data; 00037 break; 00038 00039 case WORD_RECORD_STR: 00040 buffer << info.str; 00041 break; 00042 00043 case WORD_RECORD_NONE: 00044 break; 00045 00046 default: 00047 fprintf(stderr, "WordRecord::Get: unknown type %d\n", type); 00048 return NOTOK; 00049 break; 00050 } 00051 00052 return OK; 00053 } 00054 00055 String 00056 WordRecord::Get() const 00057 { 00058 String tmp; 00059 Get(tmp); 00060 return tmp; 00061 } 00062 00063 // 00064 // Set a record from an ascii representation 00065 // 00066 int 00067 WordRecord::Set(const String& buffer) 00068 { 00069 StringList fields(buffer, "\t "); 00070 return SetList(fields); 00071 } 00072 00073 int 00074 WordRecord::SetList(StringList& fields) 00075 { 00076 int i = 0; 00077 00078 switch(type) 00079 { 00080 00081 case WORD_RECORD_DATA: 00082 { 00083 String* field = (String*)fields.Get_First(); 00084 00085 if(field == 0) { 00086 fprintf(stderr, "WordRecord::Set: failed to retrieve field %d\n", i); 00087 return NOTOK; 00088 } 00089 info.data = (unsigned int)atoi(field->get()); 00090 fields.Remove(0); 00091 i++; 00092 } 00093 break; 00094 00095 case WORD_RECORD_STR: 00096 { 00097 String* field = (String*)fields.Get_First(); 00098 info.str = *field; 00099 fields.Remove(0); 00100 i++; 00101 } 00102 break; 00103 00104 case WORD_RECORD_NONE: 00105 break; 00106 00107 default: 00108 fprintf(stderr, "WordRecord::Set: unknown type %d\n", type); 00109 break; 00110 } 00111 00112 return OK; 00113 } 00114 00115 int 00116 WordRecord::Write(FILE* f) const 00117 { 00118 String tmp; 00119 Get(tmp); 00120 fprintf(f, "%s", (char*)tmp); 00121 return 0; 00122 } 00123 00124 void 00125 WordRecord::Print() const 00126 { 00127 Write(stderr); 00128 }