BSD 4_3_Net_2 development
[unix-history] / usr / src / usr.bin / groff / libgroff / nametoindex.cc
CommitLineData
af2a502e
C
1// -*- C++ -*-
2/* Copyright (C) 1989, 1990 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.uucp)
4
5This file is part of groff.
6
7groff is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 1, or (at your option) any later
10version.
11
12groff is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License along
18with groff; see the file LICENSE. If not, write to the Free Software
19Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21#include <stdio.h>
22#include <string.h>
23#include <ctype.h>
24#include <assert.h>
25#include <stdlib.h>
26#include "errarg.h"
27#include "error.h"
28#include "cset.h"
29#include "font.h"
30
31#define FIRST_NUMBERED_CHARACTER 256
32#define FIRST_NAMED_CHARACTER 512
33
34int font::name_to_index(const char *s)
35{
36 static char **table;
37 static int table_used;
38 static int table_size;
39 assert(s != 0 && s[0] != '\0');
40 if (s[1] == '\0')
41 return (unsigned char)s[0];
42 /* char128 and \200 are synonyms */
43 if (s[0] == 'c' && s[1] == 'h' && s[2] == 'a' && s[3] == 'r') {
44 char *res;
45 long n = strtol(s + 4, &res, 10);
46 if (res != s + 4 && *res == '\0' && n >= 0 && n < 256)
47 return int(n);
48 }
49 for (int i = 0; i < table_used; i++)
50 if (strcmp(table[i], s) == 0)
51 return i + FIRST_NAMED_CHARACTER;
52 if (table_used >= table_size) {
53 if (table_size == 0) {
54 table_size = 24;
55 table = new char*[table_size];
56 }
57 else {
58 char **old_table = table;
59 table = new char *[table_size*2];
60 memcpy(table, old_table, table_size*sizeof(char*));
61 table_size *= 2;
62 delete old_table;
63 }
64 }
65 table[table_used] = new char[strlen(s) + 1];
66 strcpy(table[table_used], s);
67 return table_used++ + FIRST_NAMED_CHARACTER;
68}
69
70int font::number_to_index(unsigned char n)
71{
72 return n + FIRST_NUMBERED_CHARACTER;
73}