home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / util / gnu / groff_src.lha / groff-1.10src / libbib / search.cc < prev   
C/C++ Source or Header  |  1995-06-22  |  3KB  |  132 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  20.  
  21. #include <stdlib.h>
  22. #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.     search_item **pp;
  66.     for (pp = &list; *pp; pp = &(*pp)->next)
  67.       ;
  68.     *pp = p;
  69.     next_fid = p->next_filename_id();
  70.   }
  71. }
  72.  
  73. int search_list::nfiles() const
  74. {
  75.   int n = 0;
  76.   for (search_item *ptr = list; ptr; ptr = ptr->next)
  77.     n++;
  78.   return n;
  79. }
  80.  
  81. search_list_iterator::search_list_iterator(search_list *p, const char *q)
  82. : list(p), ptr(p->list), iter(0), query(strsave(q)),
  83.   searcher(q, strlen(q), linear_ignore_fields, linear_truncate_len)
  84. {
  85.   list->niterators += 1;
  86. }
  87.  
  88. search_list_iterator::~search_list_iterator()
  89. {
  90.   list->niterators -= 1;
  91.   a_delete query;
  92.   delete iter;
  93. }
  94.  
  95. int search_list_iterator::next(const char **pp, int *lenp, reference_id *ridp)
  96. {
  97.   while (ptr) {
  98.     if (iter == 0)
  99.       iter = ptr->make_search_item_iterator(query);
  100.     if (iter->next(searcher, pp, lenp, ridp))
  101.       return 1;
  102.     delete iter;
  103.     iter = 0;
  104.     ptr = ptr->next;
  105.   }
  106.   return 0;
  107. }
  108.  
  109. search_item::search_item(const char *nm, int fid)
  110. : next(0), name(strsave(nm)), filename_id(fid)
  111. {
  112. }
  113.  
  114. search_item::~search_item()
  115. {
  116.   a_delete name;
  117. }
  118.  
  119. int search_item::is_named(const char *nm) const
  120. {
  121.   return strcmp(name, nm) == 0;
  122. }
  123.  
  124. int search_item::next_filename_id() const
  125. {
  126.   return filename_id + 1;
  127. }
  128.  
  129. search_item_iterator::~search_item_iterator()
  130. {
  131. }
  132.