upgraded to xpdf-3.01pl1
[swftools.git] / pdf2swf / xpdf / Dict.cc
1 //========================================================================
2 //
3 // Dict.cc
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #include <aconf.h>
10
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
13 #endif
14
15 #include <stddef.h>
16 #include <string.h>
17 #include "gmem.h"
18 #include "Object.h"
19 #include "XRef.h"
20 #include "Dict.h"
21
22 //------------------------------------------------------------------------
23 // Dict
24 //------------------------------------------------------------------------
25
26 Dict::Dict(XRef *xrefA) {
27   xref = xrefA;
28   entries = NULL;
29   size = length = 0;
30   ref = 1;
31 }
32
33 Dict::~Dict() {
34   int i;
35
36   for (i = 0; i < length; ++i) {
37     gfree(entries[i].key);
38     entries[i].val.free();
39   }
40   gfree(entries);
41 }
42
43 void Dict::add(char *key, Object *val) {
44   if (length == size) {
45     if (length == 0) {
46       size = 8;
47     } else {
48       size *= 2;
49     }
50     entries = (DictEntry *)greallocn(entries, size, sizeof(DictEntry));
51   }
52   entries[length].key = key;
53   entries[length].val = *val;
54   ++length;
55 }
56
57 inline DictEntry *Dict::find(char *key) {
58   int i;
59
60   for (i = 0; i < length; ++i) {
61     if (!strcmp(key, entries[i].key))
62       return &entries[i];
63   }
64   return NULL;
65 }
66
67 GBool Dict::is(char *type) {
68   DictEntry *e;
69
70   return (e = find("Type")) && e->val.isName(type);
71 }
72
73 Object *Dict::lookup(char *key, Object *obj) {
74   DictEntry *e;
75
76   return (e = find(key)) ? e->val.fetch(xref, obj) : obj->initNull();
77 }
78
79 Object *Dict::lookupNF(char *key, Object *obj) {
80   DictEntry *e;
81
82   return (e = find(key)) ? e->val.copy(obj) : obj->initNull();
83 }
84
85 char *Dict::getKey(int i) {
86   return entries[i].key;
87 }
88
89 Object *Dict::getVal(int i, Object *obj) {
90   return entries[i].val.fetch(xref, obj);
91 }
92
93 Object *Dict::getValNF(int i, Object *obj) {
94   return entries[i].val.copy(obj);
95 }