/contrib/groff/src/libs/libbib/search.cpp

https://bitbucket.org/freebsd/freebsd-head/ · C++ · 133 lines · 96 code · 17 blank · 20 comment · 13 complexity · 432bb9838282d39a93988a974cada61d MD5 · raw file

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