summaryrefslogtreecommitdiff
path: root/xpdf/Dict.cc
blob: 7e33fff192139cbb9e8eec239d694eaecadf7625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//========================================================================
//
// Dict.cc
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================

#include <aconf.h>

#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif

#include <stddef.h>
#include <string.h>
#include "gmem.h"
#include "Object.h"
#include "XRef.h"
#include "Dict.h"

//------------------------------------------------------------------------

struct DictEntry {
  char *key;
  Object val;
  DictEntry *next;
};

//------------------------------------------------------------------------
// Dict
//------------------------------------------------------------------------

Dict::Dict(XRef *xrefA) {
  xref = xrefA;
  size = 8;
  length = 0;
  entries = (DictEntry *)gmallocn(size, sizeof(DictEntry));
  hashTab = (DictEntry **)gmallocn(2 * size - 1, sizeof(DictEntry *));
  memset(hashTab, 0, (2 * size - 1) * sizeof(DictEntry *));
  ref = 1;
}

Dict::~Dict() {
  int i;

  for (i = 0; i < length; ++i) {
    gfree(entries[i].key);
    entries[i].val.free();
  }
  gfree(entries);
  gfree(hashTab);
}

void Dict::add(char *key, Object *val) {
  DictEntry *e;
  int h;

  if ((e = find(key))) {
    e->val.free();
    e->val = *val;
    gfree(key);
  } else {
    if (length == size) {
      expand();
    }
    h = hash(key);
    entries[length].key = key;
    entries[length].val = *val;
    entries[length].next = hashTab[h];
    hashTab[h] = &entries[length];
    ++length;
  }
}

void Dict::expand() {
  int h, i;

  size *= 2;
  entries = (DictEntry *)greallocn(entries, size, sizeof(DictEntry));
  hashTab = (DictEntry **)greallocn(hashTab, 2 * size - 1,
				    sizeof(DictEntry *));
  memset(hashTab, 0, (2 * size - 1) * sizeof(DictEntry *));
  for (i = 0; i < length; ++i) {
    h = hash(entries[i].key);
    entries[i].next = hashTab[h];
    hashTab[h] = &entries[i];
  }
}

inline DictEntry *Dict::find(const char *key) {
  DictEntry *e;
  int h;

  h = hash(key);
  for (e = hashTab[h]; e; e = e->next) {
    if (!strcmp(key, e->key)) {
      return e;
    }
  }
  return NULL;
}

int Dict::hash(const char *key) {
  const char *p;
  unsigned int h;

  h = 0;
  for (p = key; *p; ++p) {
    h = 17 * h + (int)(*p & 0xff);
  }
  return (int)(h % (2 * size - 1));
}

GBool Dict::is(const char *type) {
  DictEntry *e;

  return (e = find("Type")) && e->val.isName(type);
}

Object *Dict::lookup(const char *key, Object *obj, int recursion) {
  DictEntry *e;

  return (e = find(key)) ? e->val.fetch(xref, obj, recursion)
                         : obj->initNull();
}

Object *Dict::lookupNF(const char *key, Object *obj) {
  DictEntry *e;

  return (e = find(key)) ? e->val.copy(obj) : obj->initNull();
}

char *Dict::getKey(int i) {
  return entries[i].key;
}

Object *Dict::getVal(int i, Object *obj) {
  return entries[i].val.fetch(xref, obj);
}

Object *Dict::getValNF(int i, Object *obj) {
  return entries[i].val.copy(obj);
}