summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/htsearch/ResultMatch.cc
blob: 54e5f611723e36b3858f04f5fc7f314727b85fab (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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// ResultMatch.cc
//
// ResultMatch: Contains information related to a given
//              document that was matched by a search. For instance, the
//              score of the document for this search. Similar to the
//              DocMatch class but designed for result display purposes.
//
// 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: ResultMatch.cc,v 1.10 2004/05/28 13:15:24 lha Exp $
//

#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */

#include "ResultMatch.h"

// Definition of how to search
ResultMatch::SortType ResultMatch::mySortType;

//*****************************************************************************
//
ResultMatch::ResultMatch()
{
}


//*****************************************************************************
//
ResultMatch::~ResultMatch()
{
}


//*****************************************************************************
// Default-access-methods.  Just dummies when that data is not used.
char *ResultMatch::getTitle()
{ return ""; }

time_t ResultMatch::getTime()
{ return 0; }

void ResultMatch::setTitle(char *)
{ }

void ResultMatch::setTime(time_t)
{ }

// Then for each sort-type, we derive a class, which will keep
// any necessary additional piece of data, and return the compare-function.

// We could have a real cute implementation with global
// constructors registering a factory method with ResultMatch,
// so it would just check a list and never need to be changed
// when new search methods are introduced, but that seems futile.
//  It is more practical to just add search methods here and
// change the createMatch method, last.


//*****************************************************************************
class ScoreMatch : public ResultMatch
{
  // This one needs no additional data
public:
  virtual ResultMatch::CmpFun getSortFun();
  ScoreMatch();
  ~ScoreMatch();
private:
  static int compare(const void *a1, const void *a2);
};

ScoreMatch::ScoreMatch() {}
ScoreMatch::~ScoreMatch() {}

int
ScoreMatch::compare(const void *a1, const void *a2)
{
    ResultMatch	*m1 = *((ResultMatch **) a1);
    ResultMatch	*m2 = *((ResultMatch **) a2);
    double score1 = m1->getScore();
    double score2 = m2->getScore();

    if(score1 == score2)
       return 0;
    else if(score1 < score2)
       return 1;
    else
       return -1;

    //    return m2->getScore() - m1->getScore();
}

ResultMatch::CmpFun
ScoreMatch::getSortFun() { return compare; }

//*****************************************************************************
class TimeMatch : public ResultMatch
{
public:
  virtual ResultMatch::CmpFun getSortFun();
  virtual void setTime(time_t);
  virtual time_t getTime();
  TimeMatch();
  ~TimeMatch();
private:
  // We need a time_t here, and to override the get/setTime methods.
  time_t myTime;

  static int compare(const void *a1, const void *a2);
};

TimeMatch::TimeMatch() {}
TimeMatch::~TimeMatch() {}

void
TimeMatch::setTime(time_t t)
{
  myTime = t;
}

time_t TimeMatch::getTime()
{
  return myTime;
}

int
TimeMatch::compare(const void *a1, const void *a2)
{
    ResultMatch	*m1 = *((ResultMatch **) a1);
    ResultMatch	*m2 = *((ResultMatch **) a2);
    time_t	t1 = m1->getTime();
    time_t	t2 = m2->getTime();

    return (int) (t2 - t1);
}

ResultMatch::CmpFun
TimeMatch::getSortFun() { return compare; }

//*****************************************************************************
class IDMatch : public ResultMatch
{
  // This one needs no additional data
public:
  virtual ResultMatch::CmpFun getSortFun();
  IDMatch();
  ~IDMatch();
private:
  static int compare(const void *a1, const void *a2);
};

IDMatch::IDMatch() {}
IDMatch::~IDMatch() {}

int
IDMatch::compare(const void *a1, const void *a2)
{
    ResultMatch	*m1 = *((ResultMatch **) a1);
    ResultMatch	*m2 = *((ResultMatch **) a2);
    int               i1 = m1->getID();
    int               i2 = m2->getID();

    return (i1 - i2);
}

ResultMatch::CmpFun
IDMatch::getSortFun() { return compare; }

//*****************************************************************************
class TitleMatch : public ResultMatch
{
public:
  virtual ResultMatch::CmpFun getSortFun();
  virtual void setTitle(char *t);
  virtual char *getTitle();
  TitleMatch();
  ~TitleMatch();
private:
  // We need a String here, and to override the get/setTitle methods.
  // It has to be a String, as the "char *" goes away shortly
  // after creating the object.
  String myTitle;

  static int compare(const void *a1, const void *a2);
};

TitleMatch::TitleMatch() {}
TitleMatch::~TitleMatch() {}

void
TitleMatch::setTitle(char *t)
{
  myTitle = t;
}

char *
TitleMatch::getTitle()
{
  return myTitle;
}

int
TitleMatch::compare(const void *a1, const void *a2)
{
    ResultMatch	*m1 = *((ResultMatch **) a1);
    ResultMatch	*m2 = *((ResultMatch **) a2);
    char	*t1 = m1->getTitle();
    char	*t2 = m2->getTitle();

    if (!t1) t1 = "";
    if (!t2) t2 = "";
    return mystrcasecmp(t1, t2);
}

ResultMatch::CmpFun
TitleMatch::getSortFun() { return compare; }

//*****************************************************************************
int
ResultMatch::setSortType(const String& sorttype)
{
    static const struct
    {
	char		*typest;
	SortType	type;
    }
    sorttypes[] =
    {
	{"score", SortByScore},
	{"date", SortByTime},
	{"time", SortByTime},
        {"title", SortByTitle},
        {"id", SortByID}
    };
    int		i = 0;
    const char	*st = sorttype;
    if (st && *st)
    {
	if (mystrncasecmp("rev", st, 3) == 0)
	    st += 3;
	for (i = sizeof(sorttypes)/sizeof(sorttypes[0]); --i >= 0; )
	{
	    if (mystrcasecmp(sorttypes[i].typest, st) == 0)
	    {
		mySortType = sorttypes[i].type;
		return 1;
	    }
	}
	return 0;
    }
    else
    {
      // If not specified, default to SortByScore
      mySortType = SortByScore;
      return 1;
    }
}

//*****************************************************************************
// Now here's the switchboard: a create-function that returns a
// "new":ed object of the right class for what to compare.
//  To have the pairing managed in a (dynamically registered)
// list may seem interesting, but since everything is here
// anyway, there's little need but a small cuteness-factor.
//  We could also change the guts to use some kind of creator
// object, if there would be a win.

ResultMatch *
ResultMatch::create()
{
  switch (mySortType)
  {
    case ResultMatch::SortByScore:
      return new ScoreMatch();

    case ResultMatch::SortByTime:
      return new TimeMatch();

    case ResultMatch::SortByTitle:
      return new TitleMatch();

    case ResultMatch::SortByID:
      return new IDMatch();

    default:
      // It is doubtful which is better: to abort() or paper
      // over something bad here.
      return new ScoreMatch();
  }
}