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