PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/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
  6This file is part of groff.
  7
  8groff is free software; you can redistribute it and/or modify it under
  9the terms of the GNU General Public License as published by the Free
 10Software Foundation; either version 2, or (at your option) any later
 11version.
 12
 13groff is distributed in the hope that it will be useful, but WITHOUT ANY
 14WARRANTY; without even the implied warranty of MERCHANTABILITY or
 15FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 16for more details.
 17
 18You should have received a copy of the GNU General Public License along
 19with groff; see the file COPYING.  If not, write to the Free Software
 20Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
 21
 22#include "lib.h"
 23
 24#include <ctype.h>
 25#include <assert.h>
 26#include <stdlib.h>
 27#include "errarg.h"
 28#include "error.h"
 29#include "font.h"
 30#include "ptable.h"
 31
 32declare_ptable(int)
 33implement_ptable(int)
 34
 35class character_indexer {
 36public:
 37  character_indexer();
 38  ~character_indexer();
 39  int ascii_char_index(unsigned char);
 40  int named_char_index(const char *);
 41  int numbered_char_index(int);
 42private:
 43  enum { NSMALL = 256 };
 44  int next_index;
 45  int ascii_index[256];
 46  int small_number_index[NSMALL];
 47  PTABLE(int) table;
 48};
 49
 50character_indexer::character_indexer()
 51: next_index(0)
 52{
 53  int i;
 54  for (i = 0; i < 256; i++)
 55    ascii_index[i] = -1;
 56  for (i = 0; i < NSMALL; i++)
 57    small_number_index[i] = -1;
 58}
 59
 60character_indexer::~character_indexer()
 61{
 62}
 63
 64int character_indexer::ascii_char_index(unsigned char c)
 65{
 66  if (ascii_index[c] < 0)
 67    ascii_index[c] = next_index++;
 68  return ascii_index[c];
 69}
 70
 71int character_indexer::numbered_char_index(int n)
 72{
 73  if (n >= 0 && n < NSMALL) {
 74    if (small_number_index[n] < 0)
 75      small_number_index[n] = next_index++;
 76    return small_number_index[n];
 77  }
 78  // Not the most efficient possible implementation.
 79  char buf[INT_DIGITS + 3];
 80  buf[0] = ' ';
 81  strcpy(buf + 1, i_to_a(n));
 82  return named_char_index(buf);
 83}
 84
 85int character_indexer::named_char_index(const char *s)
 86{
 87  int *np = table.lookup(s);
 88  if (!np) {
 89    np = new int[1];
 90    *np = next_index++;
 91    table.define(s, np);
 92  }
 93  return *np;
 94}
 95
 96static character_indexer indexer;
 97
 98int font::number_to_index(int n)
 99{
100  return indexer.numbered_char_index(n);
101}
102
103int font::name_to_index(const char *s)
104{
105  assert(s != 0 && s[0] != '\0' && s[0] != ' ');
106  if (s[1] == '\0')
107    return indexer.ascii_char_index(s[0]);
108  /* char128 and \200 are synonyms */
109  if (s[0] == 'c' && s[1] == 'h' && s[2] == 'a' && s[3] == 'r') {
110    char *val;
111    long n = strtol(s + 4, &val, 10);
112    if (val != s + 4 && *val == '\0' && n >= 0 && n < 256)
113      return indexer.ascii_char_index((unsigned char)n);
114  }
115  return indexer.named_char_index(s);
116}
117