LCOV - code coverage report
Current view: top level - libs/libbib - search.cpp (source / functions) Hit Total Coverage
Test: GNU roff Lines: 31 60 51.7 %
Date: 2026-01-16 17:51:41 Functions: 6 14 42.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 1989-2020 Free Software Foundation, Inc.
       2             :      Written by James Clark (jjc@jclark.com)
       3             : 
       4             : This file is part of groff, the GNU roff typesetting system.
       5             : 
       6             : groff is free software; you can redistribute it and/or modify it under
       7             : the terms of the GNU General Public License as published by the Free
       8             : Software Foundation, either version 3 of the License, or
       9             : (at your option) any later version.
      10             : 
      11             : groff is distributed in the hope that it will be useful, but WITHOUT ANY
      12             : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13             : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14             : for more details.
      15             : 
      16             : You should have received a copy of the GNU General Public License
      17             : along with this program.  If not, see <http://www.gnu.org/licenses/>. */
      18             : 
      19             : #ifdef HAVE_CONFIG_H
      20             : #include <config.h>
      21             : #endif
      22             : 
      23             : #include <assert.h>
      24             : #include <errno.h>
      25             : #include <string.h> // strchr(), strcmp(), strerror(), strlen()
      26             : 
      27             : #include "lib.h"
      28             : 
      29             : #include "posix.h"
      30             : #include "errarg.h"
      31             : #include "error.h"
      32             : #include "nonposix.h"
      33             : 
      34             : #include "refid.h"
      35             : #include "search.h"
      36             : 
      37             : int linear_truncate_len = 6;
      38             : const char *linear_ignore_fields = "XYZ";
      39             : 
      40           6 : search_list::search_list()
      41           6 : : list(0), niterators(0), next_fid(1)
      42             : {
      43           6 : }
      44             : 
      45          12 : search_list::~search_list()
      46             : {
      47           6 :   assert(niterators == 0);
      48           7 :   while (list) {
      49           1 :     search_item *tem = list->next;
      50           1 :     delete list;
      51           1 :     list = tem;
      52             :   }
      53           6 : }
      54             : 
      55           2 : void search_list::add_file(const char *filename, int silent)
      56             : {
      57           2 :   search_item *p = make_index_search_item(filename, next_fid);
      58           2 :   if (!p) {
      59           2 :     int fd = open(filename, O_RDONLY | O_BINARY);
      60           2 :     if (fd < 0) {
      61           1 :       if (!silent)
      62           1 :         error("can't open '%1': %2", filename, strerror(errno));
      63             :     }
      64             :     else
      65           1 :       p = make_linear_search_item(fd, filename, next_fid);
      66             :   }
      67           2 :   if (p) {
      68             :     search_item **pp;
      69           1 :     for (pp = &list; *pp; pp = &(*pp)->next)
      70             :       ;
      71           1 :     *pp = p;
      72           1 :     next_fid = p->next_filename_id();
      73             :   }
      74           2 : }
      75             : 
      76           0 : int search_list::nfiles() const
      77             : {
      78           0 :   int n = 0;
      79           0 :   for (search_item *ptr = list; ptr; ptr = ptr->next)
      80           0 :     n++;
      81           0 :   return n;
      82             : }
      83             : 
      84           0 : search_list_iterator::search_list_iterator(search_list *p, const char *q)
      85           0 : : list(p), ptr(p->list), iter(0), query(strsave(q)),
      86           0 :   searcher(q, strlen(q), linear_ignore_fields, linear_truncate_len)
      87             : {
      88           0 :   list->niterators += 1;
      89           0 : }
      90             : 
      91           0 : search_list_iterator::~search_list_iterator()
      92             : {
      93           0 :   list->niterators -= 1;
      94           0 :   delete[] query;
      95           0 :   delete iter;
      96           0 : }
      97             : 
      98           0 : int search_list_iterator::next(const char **pp, int *lenp, reference_id *ridp)
      99             : {
     100           0 :   while (ptr) {
     101           0 :     if (iter == 0)
     102           0 :       iter = ptr->make_search_item_iterator(query);
     103           0 :     if (iter->next(searcher, pp, lenp, ridp))
     104           0 :       return 1;
     105           0 :     delete iter;
     106           0 :     iter = 0;
     107           0 :     ptr = ptr->next;
     108             :   }
     109           0 :   return 0;
     110             : }
     111             : 
     112           1 : search_item::search_item(const char *nm, int fid)
     113           1 : : name(strsave(nm)), filename_id(fid), next(0)
     114             : {
     115           1 : }
     116             : 
     117           2 : search_item::~search_item()
     118             : {
     119           1 :   delete[] name;
     120           1 : }
     121             : 
     122           0 : int search_item::is_named(const char *nm) const
     123             : {
     124           0 :   return strcmp(name, nm) == 0;
     125             : }
     126             : 
     127           1 : int search_item::next_filename_id() const
     128             : {
     129           1 :   return filename_id + 1;
     130             : }
     131             : 
     132           0 : search_item_iterator::~search_item_iterator()
     133             : {
     134           0 : }
     135             : 
     136             : // Local Variables:
     137             : // fill-column: 72
     138             : // mode: C++
     139             : // End:
     140             : // vim: set cindent noexpandtab shiftwidth=2 textwidth=72:

Generated by: LCOV version 1.14