Diffstat (limited to 'noncore/unsupported/qpdf/xpdf/NameToCharCode.cc') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/unsupported/qpdf/xpdf/NameToCharCode.cc | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/noncore/unsupported/qpdf/xpdf/NameToCharCode.cc b/noncore/unsupported/qpdf/xpdf/NameToCharCode.cc new file mode 100644 index 0000000..06be2f4 --- a/dev/null +++ b/noncore/unsupported/qpdf/xpdf/NameToCharCode.cc | |||
@@ -0,0 +1,115 @@ | |||
1 | //======================================================================== | ||
2 | // | ||
3 | // NameToCharCode.cc | ||
4 | // | ||
5 | // Copyright 2001 Derek B. Noonburg | ||
6 | // | ||
7 | //======================================================================== | ||
8 | |||
9 | #ifdef __GNUC__ | ||
10 | #pragma implementation | ||
11 | #endif | ||
12 | |||
13 | #include <aconf.h> | ||
14 | #include <string.h> | ||
15 | #include "gmem.h" | ||
16 | #include "NameToCharCode.h" | ||
17 | |||
18 | //------------------------------------------------------------------------ | ||
19 | |||
20 | struct NameToCharCodeEntry { | ||
21 | char *name; | ||
22 | CharCode c; | ||
23 | }; | ||
24 | |||
25 | //------------------------------------------------------------------------ | ||
26 | |||
27 | NameToCharCode::NameToCharCode() { | ||
28 | int i; | ||
29 | |||
30 | size = 31; | ||
31 | len = 0; | ||
32 | tab = (NameToCharCodeEntry *)gmalloc(size * sizeof(NameToCharCodeEntry)); | ||
33 | for (i = 0; i < size; ++i) { | ||
34 | tab[i].name = NULL; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | NameToCharCode::~NameToCharCode() { | ||
39 | int i; | ||
40 | |||
41 | for (i = 0; i < size; ++i) { | ||
42 | if (tab[i].name) { | ||
43 | gfree(tab[i].name); | ||
44 | } | ||
45 | } | ||
46 | gfree(tab); | ||
47 | } | ||
48 | |||
49 | void NameToCharCode::add(char *name, CharCode c) { | ||
50 | NameToCharCodeEntry *oldTab; | ||
51 | int h, i, oldSize; | ||
52 | |||
53 | // expand the table if necessary | ||
54 | if (len >= size / 2) { | ||
55 | oldSize = size; | ||
56 | oldTab = tab; | ||
57 | size = 2*size + 1; | ||
58 | tab = (NameToCharCodeEntry *)gmalloc(size * sizeof(NameToCharCodeEntry)); | ||
59 | for (h = 0; h < size; ++h) { | ||
60 | tab[h].name = NULL; | ||
61 | } | ||
62 | for (i = 0; i < oldSize; ++i) { | ||
63 | if (oldTab[i].name) { | ||
64 | h = hash(oldTab[i].name); | ||
65 | while (tab[h].name) { | ||
66 | if (++h == size) { | ||
67 | h = 0; | ||
68 | } | ||
69 | } | ||
70 | tab[h] = oldTab[i]; | ||
71 | } | ||
72 | } | ||
73 | gfree(oldTab); | ||
74 | } | ||
75 | |||
76 | // add the new name | ||
77 | h = hash(name); | ||
78 | while (tab[h].name && strcmp(tab[h].name, name)) { | ||
79 | if (++h == size) { | ||
80 | h = 0; | ||
81 | } | ||
82 | } | ||
83 | if (!tab[h].name) { | ||
84 | tab[h].name = copyString(name); | ||
85 | } | ||
86 | tab[h].c = c; | ||
87 | |||
88 | ++len; | ||
89 | } | ||
90 | |||
91 | CharCode NameToCharCode::lookup(char *name) { | ||
92 | int h; | ||
93 | |||
94 | h = hash(name); | ||
95 | while (tab[h].name) { | ||
96 | if (!strcmp(tab[h].name, name)) { | ||
97 | return tab[h].c; | ||
98 | } | ||
99 | if (++h == size) { | ||
100 | h = 0; | ||
101 | } | ||
102 | } | ||
103 | return 0; | ||
104 | } | ||
105 | |||
106 | int NameToCharCode::hash(char *name) { | ||
107 | char *p; | ||
108 | unsigned int h; | ||
109 | |||
110 | h = 0; | ||
111 | for (p = name; *p; ++p) { | ||
112 | h = 17 * h + (int)(*p & 0xff); | ||
113 | } | ||
114 | return (int)(h % size); | ||
115 | } | ||