/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.52M | Dict::Dict(XRef *xrefA) { |
32 | 1.52M | xref = xrefA; |
33 | 1.52M | size = 8; |
34 | 1.52M | length = 0; |
35 | 1.52M | entries = (DictEntry *)gmallocn(size, sizeof(DictEntry)); |
36 | 1.52M | hashTab = (DictEntry **)gmallocn(2 * size - 1, sizeof(DictEntry *)); |
37 | 1.52M | memset(hashTab, 0, (2 * size - 1) * sizeof(DictEntry *)); |
38 | 1.52M | ref = 1; |
39 | 1.52M | } |
40 | | |
41 | 1.39M | Dict::~Dict() { |
42 | 1.39M | int i; |
43 | | |
44 | 6.74M | for (i = 0; i < length; ++i) { |
45 | 5.34M | gfree(entries[i].key); |
46 | 5.34M | entries[i].val.free(); |
47 | 5.34M | } |
48 | 1.39M | gfree(entries); |
49 | 1.39M | gfree(hashTab); |
50 | 1.39M | } |
51 | | |
52 | 6.67M | void Dict::add(char *key, Object *val) { |
53 | 6.67M | DictEntry *e; |
54 | 6.67M | int h; |
55 | | |
56 | 6.67M | if ((e = find(key))) { |
57 | 843k | e->val.free(); |
58 | 843k | e->val = *val; |
59 | 843k | gfree(key); |
60 | 5.83M | } else { |
61 | 5.83M | if (length == size) { |
62 | 154k | expand(); |
63 | 154k | } |
64 | 5.83M | h = hash(key); |
65 | 5.83M | entries[length].key = key; |
66 | 5.83M | entries[length].val = *val; |
67 | 5.83M | entries[length].next = hashTab[h]; |
68 | 5.83M | hashTab[h] = &entries[length]; |
69 | 5.83M | ++length; |
70 | 5.83M | } |
71 | 6.67M | } |
72 | | |
73 | 154k | void Dict::expand() { |
74 | 154k | int h, i; |
75 | | |
76 | 154k | size *= 2; |
77 | 154k | entries = (DictEntry *)greallocn(entries, size, sizeof(DictEntry)); |
78 | 154k | hashTab = (DictEntry **)greallocn(hashTab, 2 * size - 1, |
79 | 154k | sizeof(DictEntry *)); |
80 | 154k | memset(hashTab, 0, (2 * size - 1) * sizeof(DictEntry *)); |
81 | 1.66M | for (i = 0; i < length; ++i) { |
82 | 1.51M | h = hash(entries[i].key); |
83 | 1.51M | entries[i].next = hashTab[h]; |
84 | 1.51M | hashTab[h] = &entries[i]; |
85 | 1.51M | } |
86 | 154k | } |
87 | | |
88 | 33.8M | inline DictEntry *Dict::find(const char *key) { |
89 | 33.8M | DictEntry *e; |
90 | 33.8M | int h; |
91 | | |
92 | 33.8M | h = hash(key); |
93 | 43.9M | for (e = hashTab[h]; e; e = e->next) { |
94 | 15.7M | if (!strcmp(key, e->key)) { |
95 | 5.62M | return e; |
96 | 5.62M | } |
97 | 15.7M | } |
98 | 28.2M | return NULL; |
99 | 33.8M | } |
100 | | |
101 | 41.2M | int Dict::hash(const char *key) { |
102 | 41.2M | const char *p; |
103 | 41.2M | unsigned int h; |
104 | | |
105 | 41.2M | h = 0; |
106 | 297M | for (p = key; *p; ++p) { |
107 | 255M | h = 17 * h + (int)(*p & 0xff); |
108 | 255M | } |
109 | 41.2M | return (int)(h % (2 * size - 1)); |
110 | 41.2M | } |
111 | | |
112 | 8.04k | GBool Dict::is(const char *type) { |
113 | 8.04k | DictEntry *e; |
114 | | |
115 | 8.04k | return (e = find("Type")) && e->val.isName(type); |
116 | 8.04k | } |
117 | | |
118 | 22.6M | Object *Dict::lookup(const char *key, Object *obj, int recursion) { |
119 | 22.6M | DictEntry *e; |
120 | | |
121 | 22.6M | return (e = find(key)) ? e->val.fetch(xref, obj, recursion) |
122 | 22.6M | : obj->initNull(); |
123 | 22.6M | } |
124 | | |
125 | 4.58M | Object *Dict::lookupNF(const char *key, Object *obj) { |
126 | 4.58M | DictEntry *e; |
127 | | |
128 | 4.58M | return (e = find(key)) ? e->val.copy(obj) : obj->initNull(); |
129 | 4.58M | } |
130 | | |
131 | 615k | char *Dict::getKey(int i) { |
132 | 615k | return entries[i].key; |
133 | 615k | } |
134 | | |
135 | 309k | Object *Dict::getVal(int i, Object *obj) { |
136 | 309k | return entries[i].val.fetch(xref, obj); |
137 | 309k | } |
138 | | |
139 | 312k | Object *Dict::getValNF(int i, Object *obj) { |
140 | 312k | return entries[i].val.copy(obj); |
141 | 312k | } |