1 //========================================================================
5 // Copyright 1996 Derek B. Noonburg
7 //========================================================================
26 //------------------------------------------------------------------------
28 //------------------------------------------------------------------------
31 int num; // object number
32 int gen; // generation number
35 //------------------------------------------------------------------------
37 //------------------------------------------------------------------------
50 objDict, // dictionary
52 objRef, // indirect reference
55 objCmd, // command name
56 objError, // error return from Lexer
57 objEOF, // end of file return from Lexer
58 objNone // uninitialized object
61 #define numObjTypes 14 // total number of object types
63 //------------------------------------------------------------------------
65 //------------------------------------------------------------------------
68 #define initObj(t) ++numAlloc[type = t]
70 #define initObj(t) type = t
76 // Default constructor.
80 // Initialize an object.
81 Object *initBool(GBool booln1)
82 { initObj(objBool); booln = booln1; return this; }
83 Object *initInt(int intg1)
84 { initObj(objInt); intg = intg1; return this; }
85 Object *initReal(double real1)
86 { initObj(objReal); real = real1; return this; }
87 Object *initString(GString *string1)
88 { initObj(objString); string = string1; return this; }
89 Object *initName(char *name1)
90 { initObj(objName); name = copyString(name1); return this; }
92 { initObj(objNull); return this; }
95 Object *initDict(Dict *dict1)
96 { initObj(objDict); dict = dict1; return this; }
97 Object *initStream(Stream *stream1);
98 Object *initRef(int num1, int gen1)
99 { initObj(objRef); ref.num = num1; ref.gen = gen1; return this; }
100 Object *initCmd(char *cmd1)
101 { initObj(objCmd); cmd = copyString(cmd1); return this; }
103 { initObj(objError); return this; }
105 { initObj(objEOF); return this; }
108 Object *copy(Object *obj);
110 // If object is a Ref, fetch and return the referenced object.
111 // Otherwise, return a copy of the object.
112 Object *fetch(Object *obj);
114 // Free object contents.
118 GBool isBool() { return type == objBool; }
119 GBool isInt() { return type == objInt; }
120 GBool isReal() { return type == objReal; }
121 GBool isNum() { return type == objInt || type == objReal; }
122 GBool isString() { return type == objString; }
123 GBool isName() { return type == objName; }
124 GBool isNull() { return type == objNull; }
125 GBool isArray() { return type == objArray; }
126 GBool isDict() { return type == objDict; }
127 GBool isStream() { return type == objStream; }
128 GBool isRef() { return type == objRef; }
129 GBool isCmd() { return type == objCmd; }
130 GBool isError() { return type == objError; }
131 GBool isEOF() { return type == objEOF; }
132 GBool isNone() { return type == objNone; }
134 // Special type checking.
135 GBool isName(char *name1)
136 { return type == objName && !strcmp(name, name1); }
137 GBool isDict(char *dictType);
138 GBool isStream(char *dictType);
139 GBool isCmd(char *cmd1)
140 { return type == objCmd && !strcmp(cmd, cmd1); }
142 // Accessors. NB: these assume object is of correct type.
143 GBool getBool() { return booln; }
144 int getInt() { return intg; }
145 double getReal() { return real; }
146 double getNum() { return type == objInt ? (double)intg : real; }
147 GString *getString() { return string; }
148 char *getName() { return name; }
149 Array *getArray() { return array; }
150 Dict *getDict() { return dict; }
151 Stream *getStream() { return stream; }
152 Ref getRef() { return ref; }
153 int getRefNum() { return ref.num; }
154 int getRefGen() { return ref.gen; }
157 int arrayGetLength();
158 void arrayAdd(Object *elem);
159 Object *arrayGet(int i, Object *obj);
160 Object *arrayGetNF(int i, Object *obj);
164 void dictAdd(char *key, Object *val);
165 GBool dictIs(char *dictType);
166 Object *dictLookup(char *key, Object *obj);
167 Object *dictLookupNF(char *key, Object *obj);
168 char *dictGetKey(int i);
169 Object *dictGetVal(int i, Object *obj);
170 Object *dictGetValNF(int i, Object *obj);
173 GBool streamIs(char *dictType);
176 int streamLookChar();
177 char *streamGetLine(char *buf, int size);
179 void streamSetPos(int pos);
180 FILE *streamGetFile();
181 Dict *streamGetDict();
185 void print(FILE *f = stdout);
188 static void memCheck(FILE *f);
192 ObjType type; // object type
193 union { // value for each type:
194 GBool booln; // boolean
197 GString *string; // string
199 Array *array; // array
200 Dict *dict; // dictionary
201 Stream *stream; // stream
202 Ref ref; // indirect reference
203 char *cmd; // command
207 static int // number of each type of object
208 numAlloc[numObjTypes]; // currently allocated
212 //------------------------------------------------------------------------
214 //------------------------------------------------------------------------
218 inline int Object::arrayGetLength()
219 { return array->getLength(); }
221 inline void Object::arrayAdd(Object *elem)
222 { array->add(elem); }
224 inline Object *Object::arrayGet(int i, Object *obj)
225 { return array->get(i, obj); }
227 inline Object *Object::arrayGetNF(int i, Object *obj)
228 { return array->getNF(i, obj); }
230 //------------------------------------------------------------------------
232 //------------------------------------------------------------------------
236 inline int Object::dictGetLength()
237 { return dict->getLength(); }
239 inline void Object::dictAdd(char *key, Object *val)
240 { dict->add(key, val); }
242 inline GBool Object::dictIs(char *dictType)
243 { return dict->is(dictType); }
245 inline GBool Object::isDict(char *dictType)
246 { return type == objDict && dictIs(dictType); }
248 inline Object *Object::dictLookup(char *key, Object *obj)
249 { return dict->lookup(key, obj); }
251 inline Object *Object::dictLookupNF(char *key, Object *obj)
252 { return dict->lookupNF(key, obj); }
254 inline char *Object::dictGetKey(int i)
255 { return dict->getKey(i); }
257 inline Object *Object::dictGetVal(int i, Object *obj)
258 { return dict->getVal(i, obj); }
260 inline Object *Object::dictGetValNF(int i, Object *obj)
261 { return dict->getValNF(i, obj); }
263 //------------------------------------------------------------------------
265 //------------------------------------------------------------------------
269 inline GBool Object::streamIs(char *dictType)
270 { return stream->getDict()->is(dictType); }
272 inline GBool Object::isStream(char *dictType)
273 { return type == objStream && streamIs(dictType); }
275 inline void Object::streamReset()
278 inline int Object::streamGetChar()
279 { return stream->getChar(); }
281 inline int Object::streamLookChar()
282 { return stream->lookChar(); }
284 inline char *Object::streamGetLine(char *buf, int size)
285 { return stream->getLine(buf, size); }
287 inline int Object::streamGetPos()
288 { return stream->getPos(); }
290 inline void Object::streamSetPos(int pos)
291 { stream->setPos(pos); }
293 inline FILE *Object::streamGetFile()
294 { return stream->getFile(); }
296 inline Dict *Object::streamGetDict()
297 { return stream->getDict(); }