home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / groff-1.09-src.lha / src / amiga / groff-1.09 / libbib / search.cc < prev   
C/C++ Source or Header  |  1994-02-21  |  3KB  |  131 lines

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