1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
//
// HtWordList.cc
//
// HtWordList: Specialized WordList class that can hold a list
// of words waiting to be inserted in the database.
//
// Part of the ht://Dig package <http://www.htdig.org/>
// Copyright (c) 1995-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: HtWordList.cc,v 1.7 2004/05/28 13:15:12 lha Exp $
//
#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */
#include "HtWordList.h"
#include "HtWordReference.h"
#include "WordRecord.h"
#include "WordType.h"
#include "HtConfiguration.h"
#include "htString.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#ifdef HAVE_STD
#include <iostream>
#include <fstream>
#ifdef HAVE_NAMESPACES
using namespace std;
#endif
#else
#include <iostream.h>
#include <fstream.h>
#endif /* HAVE_STD */
#include <errno.h>
//*****************************************************************************
// HtWordList::~HtWordList()
//
HtWordList::~HtWordList()
{
delete words;
}
//*****************************************************************************
//
HtWordList::HtWordList(const HtConfiguration& config_arg) :
WordList(config_arg)
{
words = new List;
}
//*****************************************************************************
//
void HtWordList::Replace(const WordReference& arg)
{
//
// New word. Create a new reference for it and cache it in the object.
//
words->Add(new WordReference(arg));
}
//*****************************************************************************
// void HtWordList::Flush()
// Dump the current list of words to the database. After
// the words have been dumped, the list will be destroyed to make
// room for the words of the next document.
//
void HtWordList::Flush()
{
HtWordReference *wordRef;
// Provided for backwards compatibility
if (!isopen)
Open(config["word_db"], O_RDWR);
words->Start_Get();
while ((wordRef = (HtWordReference *) words->Get_Next()))
{
if (wordRef->Word().length() == 0) {
cerr << "HtWordList::Flush: unexpected empty word\n";
continue;
}
Override(*wordRef);
}
// Cleanup
words->Destroy();
}
//*****************************************************************************
// void HtWordList::Skip()
// The current document has disappeared or been modified.
// We do not need to store these words.
//
void HtWordList::Skip()
{
words->Destroy();
}
//
// Callback data dedicated to Dump and dump_word communication
//
class DumpWordData : public Object
{
public:
DumpWordData(FILE* fl_arg) { fl = fl_arg; }
FILE* fl;
};
//*****************************************************************************
//
// Write the ascii representation of a word occurence. Helper
// of WordList::Dump
//
static int dump_word(WordList *, WordDBCursor &, const WordReference *word, Object &data)
{
const HtWordReference *word_tmp = (const HtWordReference *)word;
DumpWordData &info = (DumpWordData &)data;
word_tmp->Dump(info.fl);
return OK;
}
//*****************************************************************************
// int HtWordList::Dump(char* filename)
//
// Write an ascii version of the word database in <filename>
//
int HtWordList::Dump(const String& filename)
{
FILE *fl;
if (!isopen) {
cerr << "WordList::Dump: database must be opened first\n";
return NOTOK;
}
if((fl = fopen(filename, "w")) == 0) {
perror(form("WordList::Dump: opening %s for writing", (const char*)filename));
return NOTOK;
}
HtWordReference::DumpHeader(fl);
DumpWordData data(fl);
WordCursor* search = Cursor(dump_word, &data);
search->Walk();
delete search;
fclose(fl);
return OK;
}
//*****************************************************************************
// int HtWordList::Load(char* filename)
//
// Read in an ascii version of the word database in <filename>
//
int HtWordList::Load(const String& filename)
{
FILE *fl;
String data;
HtWordReference *next;
if (!isopen) {
cerr << "WordList::Load: database must be opened first\n";
return NOTOK;
}
if((fl = fopen(filename, "r")) == 0) {
perror(form("WordList::Load: opening %s for reading", (const char*)filename));
return NOTOK;
}
if (HtWordReference::LoadHeader(fl) != OK)
{
cerr << "WordList::Load: header is not correct\n";
return NOTOK;
}
while (data.readLine(fl))
{
next = new HtWordReference;
if (next->Load(data) != OK)
{
delete next;
continue;
}
words->Add(next);
}
Flush();
fclose(fl);
return OK;
}
|