/src/xpdf-4.06/xpdf/Dict.cc
Line | Count | Source |
1 | | //======================================================================== |
2 | | // |
3 | | // Dict.cc |
4 | | // |
5 | | // Copyright 1996-2003 Glyph & Cog, LLC |
6 | | // |
7 | | //======================================================================== |
8 | | |
9 | | #include <aconf.h> |
10 | | |
11 | | #include <stddef.h> |
12 | | #include <string.h> |
13 | | #include "gmem.h" |
14 | | #include "gmempp.h" |
15 | | #include "Object.h" |
16 | | #include "XRef.h" |
17 | | #include "Dict.h" |
18 | | |
19 | | //------------------------------------------------------------------------ |
20 | | |
21 | | struct DictEntry { |
22 | | char *key; |
23 | | Object val; |
24 | | DictEntry *next; |
25 | | }; |
26 | | |
27 | | //------------------------------------------------------------------------ |
28 | | // Dict |
29 | | //------------------------------------------------------------------------ |
30 | | |
31 | 1.42M | Dict::Dict(XRef *xrefA) { |
32 | 1.42M | xref = xrefA; |
33 | 1.42M | size = 8; |
34 | 1.42M | length = 0; |
35 | 1.42M | entries = (DictEntry *)gmallocn(size, sizeof(DictEntry)); |
36 | 1.42M | hashTab = (DictEntry **)gmallocn(2 * size - 1, sizeof(DictEntry *)); |
37 | 1.42M | memset(hashTab, 0, (2 * size - 1) * sizeof(DictEntry *)); |
38 | 1.42M | ref = 1; |
39 | 1.42M | } |
40 | | |
41 | 1.42M | Dict::~Dict() { |
42 | 1.42M | int i; |
43 | | |
44 | 6.52M | for (i = 0; i < length; ++i) { |
45 | 5.09M | gfree(entries[i].key); |
46 | 5.09M | entries[i].val.free(); |
47 | 5.09M | } |
48 | 1.42M | gfree(entries); |
49 | 1.42M | gfree(hashTab); |
50 | 1.42M | } |
51 | | |
52 | 6.01M | void Dict::add(char *key, Object *val) { |
53 | 6.01M | DictEntry *e; |
54 | 6.01M | int h; |
55 | | |
56 | 6.01M | if ((e = find(key))) { |
57 | 911k | e->val.free(); |
58 | 911k | e->val = *val; |
59 | 911k | gfree(key); |
60 | 5.10M | } else { |
61 | 5.10M | if (length == size) { |
62 | 126k | expand(); |
63 | 126k | } |
64 | 5.10M | h = hash(key); |
65 | 5.10M | entries[length].key = key; |
66 | 5.10M | entries[length].val = *val; |
67 | 5.10M | entries[length].next = hashTab[h]; |
68 | 5.10M | hashTab[h] = &entries[length]; |
69 | 5.10M | ++length; |
70 | 5.10M | } |
71 | 6.01M | } |
72 | | |
73 | 126k | void Dict::expand() { |
74 | 126k | int h, i; |
75 | | |
76 | 126k | size *= 2; |
77 | 126k | entries = (DictEntry *)greallocn(entries, size, sizeof(DictEntry)); |
78 | 126k | hashTab = (DictEntry **)greallocn(hashTab, 2 * size - 1, |
79 | 126k | sizeof(DictEntry *)); |
80 | 126k | memset(hashTab, 0, (2 * size - 1) * sizeof(DictEntry *)); |
81 | 1.38M | for (i = 0; i < length; ++i) { |
82 | 1.26M | h = hash(entries[i].key); |
83 | 1.26M | entries[i].next = hashTab[h]; |
84 | 1.26M | hashTab[h] = &entries[i]; |
85 | 1.26M | } |
86 | 126k | } |
87 | | |
88 | 31.8M | inline DictEntry *Dict::find(const char *key) { |
89 | 31.8M | DictEntry *e; |
90 | 31.8M | int h; |
91 | | |
92 | 31.8M | h = hash(key); |
93 | 41.7M | for (e = hashTab[h]; e; e = e->next) { |
94 | 15.1M | if (!strcmp(key, e->key)) { |
95 | 5.16M | return e; |
96 | 5.16M | } |
97 | 15.1M | } |
98 | 26.6M | return NULL; |
99 | 31.8M | } |
100 | | |
101 | 38.1M | int Dict::hash(const char *key) { |
102 | 38.1M | const char *p; |
103 | 38.1M | unsigned int h; |
104 | | |
105 | 38.1M | h = 0; |
106 | 283M | for (p = key; *p; ++p) { |
107 | 245M | h = 17 * h + (int)(*p & 0xff); |
108 | 245M | } |
109 | 38.1M | return (int)(h % (2 * size - 1)); |
110 | 38.1M | } |
111 | | |
112 | 5.88k | GBool Dict::is(const char *type) { |
113 | 5.88k | DictEntry *e; |
114 | | |
115 | 5.88k | return (e = find("Type")) && e->val.isName(type); |
116 | 5.88k | } |
117 | | |
118 | 21.4M | Object *Dict::lookup(const char *key, Object *obj, int recursion) { |
119 | 21.4M | DictEntry *e; |
120 | | |
121 | 21.4M | return (e = find(key)) ? e->val.fetch(xref, obj, recursion) |
122 | 21.4M | : obj->initNull(); |
123 | 21.4M | } |
124 | | |
125 | 4.29M | Object *Dict::lookupNF(const char *key, Object *obj) { |
126 | 4.29M | DictEntry *e; |
127 | | |
128 | 4.29M | return (e = find(key)) ? e->val.copy(obj) : obj->initNull(); |
129 | 4.29M | } |
130 | | |
131 | 270k | char *Dict::getKey(int i) { |
132 | 270k | return entries[i].key; |
133 | 270k | } |
134 | | |
135 | 24.2k | Object *Dict::getVal(int i, Object *obj) { |
136 | 24.2k | return entries[i].val.fetch(xref, obj); |
137 | 24.2k | } |
138 | | |
139 | 248k | Object *Dict::getValNF(int i, Object *obj) { |
140 | 248k | return entries[i].val.copy(obj); |
141 | 248k | } |