No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / groff / src / libs / libbib / search.cpp
blob772c61a49fb33ab21d3d0eb58b4e59b4d2b4d864
1 /* $NetBSD$ */
3 // -*- C++ -*-
4 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001
5 Free Software Foundation, Inc.
6 Written by James Clark (jjc@jclark.com)
8 This file is part of groff.
10 groff is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
15 groff is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License along
21 with groff; see the file COPYING. If not, write to the Free Software
22 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
24 #include "lib.h"
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <errno.h>
30 #include "posix.h"
31 #include "errarg.h"
32 #include "error.h"
33 #include "nonposix.h"
35 #include "refid.h"
36 #include "search.h"
38 int linear_truncate_len = 6;
39 const char *linear_ignore_fields = "XYZ";
41 search_list::search_list()
42 : list(0), niterators(0), next_fid(1)
46 search_list::~search_list()
48 assert(niterators == 0);
49 while (list) {
50 search_item *tem = list->next;
51 delete list;
52 list = tem;
56 void search_list::add_file(const char *filename, int silent)
58 search_item *p = make_index_search_item(filename, next_fid);
59 if (!p) {
60 int fd = open(filename, O_RDONLY | O_BINARY);
61 if (fd < 0) {
62 if (!silent)
63 error("can't open `%1': %2", filename, strerror(errno));
65 else
66 p = make_linear_search_item(fd, filename, next_fid);
68 if (p) {
69 search_item **pp;
70 for (pp = &list; *pp; pp = &(*pp)->next)
72 *pp = p;
73 next_fid = p->next_filename_id();
77 int search_list::nfiles() const
79 int n = 0;
80 for (search_item *ptr = list; ptr; ptr = ptr->next)
81 n++;
82 return n;
85 search_list_iterator::search_list_iterator(search_list *p, const char *q)
86 : list(p), ptr(p->list), iter(0), query(strsave(q)),
87 searcher(q, strlen(q), linear_ignore_fields, linear_truncate_len)
89 list->niterators += 1;
92 search_list_iterator::~search_list_iterator()
94 list->niterators -= 1;
95 a_delete query;
96 delete iter;
99 int search_list_iterator::next(const char **pp, int *lenp, reference_id *ridp)
101 while (ptr) {
102 if (iter == 0)
103 iter = ptr->make_search_item_iterator(query);
104 if (iter->next(searcher, pp, lenp, ridp))
105 return 1;
106 delete iter;
107 iter = 0;
108 ptr = ptr->next;
110 return 0;
113 search_item::search_item(const char *nm, int fid)
114 : name(strsave(nm)), filename_id(fid), next(0)
118 search_item::~search_item()
120 a_delete name;
123 int search_item::is_named(const char *nm) const
125 return strcmp(name, nm) == 0;
128 int search_item::next_filename_id() const
130 return filename_id + 1;
133 search_item_iterator::~search_item_iterator()