Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Dict.cc

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

Generated on Sat Nov 5 16:18:13 2005 for OPIE by  doxygen 1.4.2