Adapt src/lib-bib (src/libs/libbib)
[s-roff.git] / src / lib-bib / search.cpp
blob8b951abec3534b3a378140b8d9eed3d25b607a4b
1 /*@
2 * Copyright (c) 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
4 * Copyright (C) 1989 - 1992, 2000, 2001
5 * Free Software Foundation, Inc.
6 * Written by James Clark (jjc@jclark.com)
8 * groff is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2, or (at your option) any later
11 * version.
13 * groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with groff; see the file COPYING. If not, write to the Free Software
20 * Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "config.h"
24 #include "lib.h"
26 #include <assert.h>
27 #include <errno.h>
28 #include <stdlib.h>
30 #include "errarg.h"
31 #include "error.h"
32 #include "nonposix.h"
33 #include "posix.h"
34 #include "refid.h"
35 #include "search.h"
37 int linear_truncate_len = 6;
38 const char *linear_ignore_fields = "XYZ";
40 search_list::search_list()
41 : list(0), niterators(0), next_fid(1)
45 search_list::~search_list()
47 assert(niterators == 0);
48 while (list) {
49 search_item *tem = list->next;
50 delete list;
51 list = tem;
55 void search_list::add_file(const char *filename, int silent)
57 search_item *p = make_index_search_item(filename, next_fid);
58 if (!p) {
59 int fd = open(filename, O_RDONLY | O_BINARY);
60 if (fd < 0) {
61 if (!silent)
62 error("can't open `%1': %2", filename, strerror(errno));
64 else
65 p = make_linear_search_item(fd, filename, next_fid);
67 if (p) {
68 search_item **pp;
69 for (pp = &list; *pp; pp = &(*pp)->next)
71 *pp = p;
72 next_fid = p->next_filename_id();
76 int search_list::nfiles() const
78 int n = 0;
79 for (search_item *ptr = list; ptr; ptr = ptr->next)
80 n++;
81 return n;
84 search_list_iterator::search_list_iterator(search_list *p, const char *q)
85 : list(p), ptr(p->list), iter(0), query(strsave(q)),
86 searcher(q, strlen(q), linear_ignore_fields, linear_truncate_len)
88 list->niterators += 1;
91 search_list_iterator::~search_list_iterator()
93 list->niterators -= 1;
94 a_delete query;
95 delete iter;
98 int search_list_iterator::next(const char **pp, int *lenp, reference_id *ridp)
100 while (ptr) {
101 if (iter == 0)
102 iter = ptr->make_search_item_iterator(query);
103 if (iter->next(searcher, pp, lenp, ridp))
104 return 1;
105 delete iter;
106 iter = 0;
107 ptr = ptr->next;
109 return 0;
112 search_item::search_item(const char *nm, int fid)
113 : name(strsave(nm)), filename_id(fid), next(0)
117 search_item::~search_item()
119 a_delete name;
122 int search_item::is_named(const char *nm) const
124 return strcmp(name, nm) == 0;
127 int search_item::next_filename_id() const
129 return filename_id + 1;
132 search_item_iterator::~search_item_iterator()
136 // s-it2-mode