2 /* Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.uucp)
5 This file is part of groff.
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 1, or (at your option) any later
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
17 You should have received a copy of the GNU General Public License along
18 with groff; see the file LICENSE. If not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
35 class character_indexer
{
39 int ascii_char_index(unsigned char);
40 int named_char_index(const char *);
41 int numbered_char_index(int);
43 enum { NSMALL
= 256 };
46 int small_number_index
[NSMALL
];
48 int lookup_char(const char *, int);
51 character_indexer::character_indexer()
54 for (int i
= 0; i
< 256; i
++)
56 for (i
= 0; i
< NSMALL
; i
++)
57 small_number_index
[i
] = -1;
60 character_indexer::~character_indexer()
64 int character_indexer::ascii_char_index(unsigned char c
)
66 if (ascii_index
[c
] < 0)
67 ascii_index
[c
] = next_index
++;
68 return ascii_index
[c
];
71 int character_indexer::numbered_char_index(int n
)
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
];
78 // Not the most efficient possible implementation.
79 char buf
[INT_DIGITS
+ 3];
81 strcpy(buf
+ 1, itoa(n
));
82 return named_char_index(buf
);
85 int character_indexer::named_char_index(const char *s
)
87 int *np
= table
.lookup(s
);
96 static character_indexer indexer
;
98 int font::number_to_index(int n
)
100 return indexer
.numbered_char_index(n
);
103 int font::name_to_index(const char *s
)
105 assert(s
!= 0 && s
[0] != '\0' && s
[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') {
111 long n
= strtol(s
+ 4, &res
, 10);
112 if (res
!= s
+ 4 && *res
== '\0' && n
>= 0 && n
< 256)
113 return indexer
.ascii_char_index((unsigned char)n
);
115 return indexer
.named_char_index(s
);