Dict.cpp
1//========================================================================
2//
3// Dict.cpp
4//
5// Copyright 1996-2002 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
26Dict::Dict(XRef *xrefA) {
27 xref = xrefA;
28 entries = NULL;
29 size = length = 0;
30 ref = 1;
31}
32
33Dict::~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
43void Dict::add(char *key, Object *val) {
44 if (length + 1 > size) {
45 size += 8;
46 entries = (DictEntry *)grealloc(entries, size * sizeof(DictEntry));
47 }
48 entries[length].key = key;
49 entries[length].val = *val;
50 ++length;
51}
52
53inline DictEntry *Dict::find(const char *key) {
54 int i;
55
56 for (i = 0; i < length; ++i) {
57 if (!strcmp(key, entries[i].key))
58 return &entries[i];
59 }
60 return NULL;
61}
62
63GBool Dict::is(const char *type) {
64 DictEntry *e;
65
66 return (e = find("Type")) && e->val.isName(type);
67}
68
69Object *Dict::lookup(const char *key, Object *obj) {
70 DictEntry *e;
71
72 return (e = find(key)) ? e->val.fetch(xref, obj) : obj->initNull();
73}
74
75Object *Dict::lookupNF(const char *key, Object *obj) {
76 DictEntry *e;
77
78 return (e = find(key)) ? e->val.copy(obj) : obj->initNull();
79}
80
81char *Dict::getKey(int i) {
82 return entries[i].key;
83}
84
85Object *Dict::getVal(int i, Object *obj) {
86 return entries[i].val.fetch(xref, obj);
87}
88
89Object *Dict::getValNF(int i, Object *obj) {
90 return entries[i].val.copy(obj);
91}