home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / groff / libgroff / nametoindex.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-30  |  2.1 KB  |  74 lines

  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990 Free Software Foundation, Inc.
  3.      Written by James Clark (jjc@jclark.uucp)
  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 1, 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 LICENSE.  If not, write to the Free Software
  19. Foundation, 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.  
  34. int 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.  
  70. int font::number_to_index(unsigned char n)
  71. {
  72.   return n + FIRST_NUMBERED_CHARACTER;
  73. }
  74.