upgrade to xpdf 3.00.
[swftools.git] / pdf2swf / xpdf / NameToCharCode.cc
blob8f22a9073a65ba4246a825d5c1e5e64c735f6e14
1 //========================================================================
2 //
3 // NameToCharCode.cc
4 //
5 // Copyright 2001-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
9 #include <aconf.h>
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
13 #endif
15 #include <string.h>
16 #include "gmem.h"
17 #include "NameToCharCode.h"
19 //------------------------------------------------------------------------
21 struct NameToCharCodeEntry {
22 char *name;
23 CharCode c;
26 //------------------------------------------------------------------------
28 NameToCharCode::NameToCharCode() {
29 int i;
31 size = 31;
32 len = 0;
33 tab = (NameToCharCodeEntry *)gmalloc(size * sizeof(NameToCharCodeEntry));
34 for (i = 0; i < size; ++i) {
35 tab[i].name = NULL;
39 NameToCharCode::~NameToCharCode() {
40 int i;
42 for (i = 0; i < size; ++i) {
43 if (tab[i].name) {
44 gfree(tab[i].name);
47 gfree(tab);
50 void NameToCharCode::add(char *name, CharCode c) {
51 NameToCharCodeEntry *oldTab;
52 int h, i, oldSize;
54 // expand the table if necessary
55 if (len >= size / 2) {
56 oldSize = size;
57 oldTab = tab;
58 size = 2*size + 1;
59 tab = (NameToCharCodeEntry *)gmalloc(size * sizeof(NameToCharCodeEntry));
60 for (h = 0; h < size; ++h) {
61 tab[h].name = NULL;
63 for (i = 0; i < oldSize; ++i) {
64 if (oldTab[i].name) {
65 h = hash(oldTab[i].name);
66 while (tab[h].name) {
67 if (++h == size) {
68 h = 0;
71 tab[h] = oldTab[i];
74 gfree(oldTab);
77 // add the new name
78 h = hash(name);
79 while (tab[h].name && strcmp(tab[h].name, name)) {
80 if (++h == size) {
81 h = 0;
84 if (!tab[h].name) {
85 tab[h].name = copyString(name);
87 tab[h].c = c;
89 ++len;
92 CharCode NameToCharCode::lookup(char *name) {
93 int h;
95 h = hash(name);
96 while (tab[h].name) {
97 if (!strcmp(tab[h].name, name)) {
98 return tab[h].c;
100 if (++h == size) {
101 h = 0;
104 return 0;
107 int NameToCharCode::hash(char *name) {
108 char *p;
109 unsigned int h;
111 h = 0;
112 for (p = name; *p; ++p) {
113 h = 17 * h + (int)(*p & 0xff);
115 return (int)(h % size);