Back to index

tetex-bin  3.0
Dict.cc
Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // Dict.cc
00004 //
00005 // Copyright 1996-2003 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #include <aconf.h>
00010 
00011 #ifdef USE_GCC_PRAGMAS
00012 #pragma implementation
00013 #endif
00014 
00015 #include <stddef.h>
00016 #include <string.h>
00017 #include "gmem.h"
00018 #include "Object.h"
00019 #include "XRef.h"
00020 #include "Dict.h"
00021 
00022 //------------------------------------------------------------------------
00023 // Dict
00024 //------------------------------------------------------------------------
00025 
00026 Dict::Dict(XRef *xrefA) {
00027   xref = xrefA;
00028   entries = NULL;
00029   size = length = 0;
00030   ref = 1;
00031 }
00032 
00033 Dict::~Dict() {
00034   int i;
00035 
00036   for (i = 0; i < length; ++i) {
00037     gfree(entries[i].key);
00038     entries[i].val.free();
00039   }
00040   gfree(entries);
00041 }
00042 
00043 void Dict::add(char *key, Object *val) {
00044   if (length == size) {
00045     if (length == 0) {
00046       size = 8;
00047     } else {
00048       size *= 2;
00049     }
00050     entries = (DictEntry *)grealloc(entries, size * sizeof(DictEntry));
00051   }
00052   entries[length].key = key;
00053   entries[length].val = *val;
00054   ++length;
00055 }
00056 
00057 inline DictEntry *Dict::find(char *key) {
00058   int i;
00059 
00060   for (i = 0; i < length; ++i) {
00061     if (!strcmp(key, entries[i].key))
00062       return &entries[i];
00063   }
00064   return NULL;
00065 }
00066 
00067 GBool Dict::is(char *type) {
00068   DictEntry *e;
00069 
00070   return (e = find("Type")) && e->val.isName(type);
00071 }
00072 
00073 Object *Dict::lookup(char *key, Object *obj) {
00074   DictEntry *e;
00075 
00076   return (e = find(key)) ? e->val.fetch(xref, obj) : obj->initNull();
00077 }
00078 
00079 Object *Dict::lookupNF(char *key, Object *obj) {
00080   DictEntry *e;
00081 
00082   return (e = find(key)) ? e->val.copy(obj) : obj->initNull();
00083 }
00084 
00085 char *Dict::getKey(int i) {
00086   return entries[i].key;
00087 }
00088 
00089 Object *Dict::getVal(int i, Object *obj) {
00090   return entries[i].val.fetch(xref, obj);
00091 }
00092 
00093 Object *Dict::getValNF(int i, Object *obj) {
00094   return entries[i].val.copy(obj);
00095 }