/contrib/groff/src/libs/libgroff/nametoindex.cpp

https://bitbucket.org/freebsd/freebsd-head/ · C++ · 117 lines · 82 code · 13 blank · 22 comment · 28 complexity · b8a5a4a9c1bdef48cc62fcb047c2c337 MD5 · raw file

  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003, 2004
  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 <ctype.h>
  19. #include <assert.h>
  20. #include <stdlib.h>
  21. #include "errarg.h"
  22. #include "error.h"
  23. #include "font.h"
  24. #include "ptable.h"
  25. declare_ptable(int)
  26. implement_ptable(int)
  27. class character_indexer {
  28. public:
  29. character_indexer();
  30. ~character_indexer();
  31. int ascii_char_index(unsigned char);
  32. int named_char_index(const char *);
  33. int numbered_char_index(int);
  34. private:
  35. enum { NSMALL = 256 };
  36. int next_index;
  37. int ascii_index[256];
  38. int small_number_index[NSMALL];
  39. PTABLE(int) table;
  40. };
  41. character_indexer::character_indexer()
  42. : next_index(0)
  43. {
  44. int i;
  45. for (i = 0; i < 256; i++)
  46. ascii_index[i] = -1;
  47. for (i = 0; i < NSMALL; i++)
  48. small_number_index[i] = -1;
  49. }
  50. character_indexer::~character_indexer()
  51. {
  52. }
  53. int character_indexer::ascii_char_index(unsigned char c)
  54. {
  55. if (ascii_index[c] < 0)
  56. ascii_index[c] = next_index++;
  57. return ascii_index[c];
  58. }
  59. int character_indexer::numbered_char_index(int n)
  60. {
  61. if (n >= 0 && n < NSMALL) {
  62. if (small_number_index[n] < 0)
  63. small_number_index[n] = next_index++;
  64. return small_number_index[n];
  65. }
  66. // Not the most efficient possible implementation.
  67. char buf[INT_DIGITS + 3];
  68. buf[0] = ' ';
  69. strcpy(buf + 1, i_to_a(n));
  70. return named_char_index(buf);
  71. }
  72. int character_indexer::named_char_index(const char *s)
  73. {
  74. int *np = table.lookup(s);
  75. if (!np) {
  76. np = new int[1];
  77. *np = next_index++;
  78. table.define(s, np);
  79. }
  80. return *np;
  81. }
  82. static character_indexer indexer;
  83. int font::number_to_index(int n)
  84. {
  85. return indexer.numbered_char_index(n);
  86. }
  87. int font::name_to_index(const char *s)
  88. {
  89. assert(s != 0 && s[0] != '\0' && s[0] != ' ');
  90. if (s[1] == '\0')
  91. return indexer.ascii_char_index(s[0]);
  92. /* char128 and \200 are synonyms */
  93. if (s[0] == 'c' && s[1] == 'h' && s[2] == 'a' && s[3] == 'r') {
  94. char *val;
  95. long n = strtol(s + 4, &val, 10);
  96. if (val != s + 4 && *val == '\0' && n >= 0 && n < 256)
  97. return indexer.ascii_char_index((unsigned char)n);
  98. }
  99. return indexer.named_char_index(s);
  100. }