summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/htsearch/WordSearcher.cc
blob: 7e1669f7258a3c294c440faa28f2e5dc98b2e68b (plain)
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
//
// WordSearcher.cc
//
// WordSearcher: a simple word database readonly-access wrapper
//               generates ResultLists for the Query framework.
//
// 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: WordSearcher.cc,v 1.4 2004/05/28 13:15:24 lha Exp $
//

#include "WordSearcher.h"
#include "WordType.h"
#include "ResultList.h"
#include "HtWordReference.h"
#include "defaults.h"

extern int debug;

//
// constructor, opens the database
//
WordSearcher::WordSearcher(const String &filename) :
	references(*(HtConfiguration::config()))
{
	references.Open(filename, O_RDONLY);
}

//
// gather results for a word, either from db or ignored
//
ResultList *
WordSearcher::Search(const String &word)
{
	ResultList *result = 0;
	if(IsIgnore(word))
	{
		if(debug) cerr << "IGNORE: " << word << endl;
		result = new ResultList;
		result->Ignore();
	}
	else
	{
		result = Fetch(word);
	}
	return result;
}

//
// see if word must be ignored
//
bool
WordSearcher::IsIgnore(const String &word)
{
	HtConfiguration* config= HtConfiguration::config();
	String copy = word;
	WordType type(*config);
	return 0 != type.Normalize(copy);
}

//
// gather all references in the db, construct a ResultList
//
ResultList *
WordSearcher::Fetch(const String &word)
{
	if(debug) cerr << "FETCH: " << word << endl;
	ResultList *result = 0;
	List *refs = references[word];

	if(refs && refs->Count())
	{
		if(debug) cerr << "REFERENCES: " << refs->Count() << endl;
		result = new ResultList;
		DocMatch *match = new DocMatch;

		refs->Start_Get();
		HtWordReference *ref = (HtWordReference *)refs->Get_Next();
		match->SetId(ref->DocID());
		match->SetAnchor(ref->Anchor());
		result->add(match);
		unsigned int current = ref->DocID();
		if(debug) cerr << "At: " << ref->DocID() << endl;
		while(ref)
		{
			if(ref->DocID() != current)
			{
				if(debug) cerr << "At: "<<ref->DocID()<< endl;
				match = new DocMatch;
				match->SetId(ref->DocID());
				match->SetAnchor(ref->Anchor());
				result->add(match);
				current = ref->DocID();
			}
			if(debug) cerr << "@ "<<ref->Location()<< endl;
			match->AddLocation(
				new Location(
					ref->Location(),
					ref->Location(),
					ref->Flags()));
			ref = (HtWordReference *)refs->Get_Next();
		}
	}
	return result;
}