summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/test/txt2mifluz.cc
blob: de9aeacc639b387b3c3aa07eb1a505080cb54f5d (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
//
// txt2mifluz.cc
//
// txt2mifluz: stress test the Berkeley DB database and WordList interface.
//
// Part of the ht://Dig package   <http://www.htdig.org/>
// Copyright (c) 1999-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: txt2mifluz.cc,v 1.4 2004/05/28 13:15:30 lha Exp $
//

#ifdef HAVE_CONFIG_H
#include <htconfig.h>
#endif /* HAVE_CONFIG_H */

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */

// If we have this, we probably want it.
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif /* HAVE_GETOPT_H */
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif /* HAVE_MALLOC_H */
#include <stdlib.h>

#include <htString.h>
#include <WordList.h>
#include <WordContext.h>

/*
 * Store all options from the command line
 */
class params_t
{
public:
  char* dbfile;
  int compress;
};

/*
 * Explain options
 */
static void usage();
/*
 * Verbosity level set with -v (++)
 */
static int verbose = 0;

class WordSearch {
public:
  WordSearch();

  WordKey *Search(const String& expr);
  WordKey *Search(const StringList& terms);
  WordKey *Search(WordKey* keys);

  WordKey* Terms2WordKey(const StringList& terms);

  void DocumentSet(const WordKey& from, WordKey& to);
  void DocumentCompare(const WordKey& a, const WordKey& b);

  int limit_bottom;
  int limit_count;
  WordList* words;
};

WordSearch::WordSearch()
{
  limit_bottom = 0;
  limit_count = 0;
  words = 0;
}

WordKey *WordSearch::Search(const String& expr)
{
  return Search(StringList(expr, " \t"));
}

WordKey *WordSearch::Search(const StringList& terms)
{
  return Search(Terms2WordKey(terms));
}

WordKey *WordSearch::Search(WordKey* keys)
{
  //  WordKey* AscendingFrequency(ter);
  return 0;
}

WordKey* WordSearch::Terms2WordKey(const StringList& terms)
{
  WordKey* keys = new WordKey[terms.Count() + 1];

  int i;
  String* term;
  ListCursor cursor;
  terms.Start_Get(cursor);
  for(i = 0; (term = (String*)terms.Get_Next(cursor)); i++) {
    keys[i].SetWord(*term);
  }

  for(i = 0; !keys[i].Empty(); i++) {
    fprintf(stderr, "%s\n", (char*)keys[i].Get());
  }
  return 0;
}


// *****************************************************************************
// Entry point
//
int main(int ac, char **av)
{
  int			c;
  extern char		*optarg;
  params_t		params;

  params.compress = 0;
  params.dbfile = strdup("test");

  while ((c = getopt(ac, av, "vB:f:z")) != -1)
    {
      switch (c)
	{
	case 'v':
	  verbose++;
	  break;
	case 'B':
	  free(params.dbfile);
	  params.dbfile = strdup(optarg);
	  break;
	case 'z':
	  params.compress = 1;
	  break;
	case '?':
	  usage();
	  break;
	}
    }

  Configuration* config = WordContext::Initialize();
  if(!config) {
    fprintf(stderr, "txt2mifluz: no config file found\n");
    exit(1);
  }

  if(params.compress) {
    config->Add("wordlist_compress", "true");
  }

  WordList words(*config);
  words.Open(params.dbfile, O_RDWR|O_TRUNC);
  int inserted = words.Read(stdin);
  if(verbose)
    printf("inserted %d WordReferences\n", inserted);
  words.Close();

  delete config;
  free(params.dbfile);
}

// *****************************************************************************
// void usage()
//   Display program usage information
//
static void usage()
{
    printf("usage: txt2mifluz [options] < txtfile\n");
    printf("Options:\n");
    printf("\t-v\t\tIncreases the verbosity\n");
    printf("\t-B dbfile\tuse <dbfile> as a db file name (default test).\n");
    exit(0);
}