~ubuntu-branches/ubuntu/dapper/groff/dapper

« back to all changes in this revision

Viewing changes to src/libs/libgroff/nametoindex.cc

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2002-03-17 04:11:50 UTC
  • Revision ID: james.westby@ubuntu.com-20020317041150-wkgfawjc3gxlk0o5
Tags: upstream-1.17.2
ImportĀ upstreamĀ versionĀ 1.17.2

Show diffs side-by-side

added added

removed removed

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