1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
7 //========================================================================
14 #ifdef USE_GCC_PRAGMAS
29 //------------------------------------------------------------------------
31 //------------------------------------------------------------------------
34 int num; // object number
35 int gen; // generation number
38 //------------------------------------------------------------------------
40 //------------------------------------------------------------------------
53 objDict, // dictionary
55 objRef, // indirect reference
58 objCmd, // command name
59 objError, // error return from Lexer
60 objEOF, // end of file return from Lexer
61 objNone // uninitialized object
64 #define numObjTypes 14 // total number of object types
66 //------------------------------------------------------------------------
68 //------------------------------------------------------------------------
71 #define initObj(t) ++numAlloc[type = t]
73 #define initObj(t) type = t
79 // Default constructor.
83 // Initialize an object.
84 Object *initBool(GBool boolnA)
85 { initObj(objBool); booln = boolnA; return this; }
86 Object *initInt(int intgA)
87 { initObj(objInt); intg = intgA; return this; }
88 Object *initReal(double realA)
89 { initObj(objReal); real = realA; return this; }
90 Object *initString(GString *stringA)
91 { initObj(objString); string = stringA; return this; }
92 Object *initName(char *nameA)
93 { initObj(objName); name = copyString(nameA); return this; }
95 { initObj(objNull); return this; }
96 Object *initArray(XRef *xref);
97 Object *initDict(XRef *xref);
98 Object *initStream(Stream *streamA);
99 Object *initRef(int numA, int genA)
100 { initObj(objRef); ref.num = numA; ref.gen = genA; return this; }
101 Object *initCmd(char *cmdA)
102 { initObj(objCmd); cmd = copyString(cmdA); return this; }
104 { initObj(objError); return this; }
106 { initObj(objEOF); return this; }
109 Object *copy(Object *obj);
111 // If object is a Ref, fetch and return the referenced object.
112 // Otherwise, return a copy of the object.
113 Object *fetch(XRef *xref, Object *obj);
115 // Free object contents.
119 ObjType getType() { return type; }
120 GBool isBool() { return type == objBool; }
121 GBool isInt() { return type == objInt; }
122 GBool isReal() { return type == objReal; }
123 GBool isNum() { return type == objInt || type == objReal; }
124 GBool isString() { return type == objString; }
125 GBool isName() { return type == objName; }
126 GBool isNull() { return type == objNull; }
127 GBool isArray() { return type == objArray; }
128 GBool isDict() { return type == objDict; }
129 GBool isStream() { return type == objStream; }
130 GBool isRef() { return type == objRef; }
131 GBool isCmd() { return type == objCmd; }
132 GBool isError() { return type == objError; }
133 GBool isEOF() { return type == objEOF; }
134 GBool isNone() { return type == objNone; }
136 // Special type checking.
137 GBool isName(char *nameA)
138 { return type == objName && !strcmp(name, nameA); }
139 GBool isDict(char *dictType);
140 GBool isStream(char *dictType);
141 GBool isCmd(char *cmdA)
142 { return type == objCmd && !strcmp(cmd, cmdA); }
144 // Accessors. NB: these assume object is of correct type.
145 GBool getBool() { return booln; }
146 int getInt() { return intg; }
147 double getReal() { return real; }
148 double getNum() { return type == objInt ? (double)intg : real; }
149 GString *getString() { return string; }
150 char *getName() { return name; }
151 Array *getArray() { return array; }
152 Dict *getDict() { return dict; }
153 Stream *getStream() { return stream; }
154 Ref getRef() { return ref; }
155 int getRefNum() { return ref.num; }
156 int getRefGen() { return ref.gen; }
157 char *getCmd() { return cmd; }
160 int arrayGetLength();
161 void arrayAdd(Object *elem);
162 Object *arrayGet(int i, Object *obj);
163 Object *arrayGetNF(int i, Object *obj);
167 void dictAdd(char *key, Object *val);
168 GBool dictIs(char *dictType);
169 Object *dictLookup(char *key, Object *obj);
170 Object *dictLookupNF(char *key, Object *obj);
171 char *dictGetKey(int i);
172 Object *dictGetVal(int i, Object *obj);
173 Object *dictGetValNF(int i, Object *obj);
176 GBool streamIs(char *dictType);
180 int streamLookChar();
181 char *streamGetLine(char *buf, int size);
182 Guint streamGetPos();
183 void streamSetPos(Guint pos, int dir = 0);
184 Dict *streamGetDict();
188 void print(FILE *f = stdout);
191 static void memCheck(FILE *f);
195 ObjType type; // object type
196 union { // value for each type:
197 GBool booln; // boolean
200 GString *string; // string
202 Array *array; // array
203 Dict *dict; // dictionary
204 Stream *stream; // stream
205 Ref ref; // indirect reference
206 char *cmd; // command
210 static int // number of each type of object
211 numAlloc[numObjTypes]; // currently allocated
215 //------------------------------------------------------------------------
217 //------------------------------------------------------------------------
221 inline int Object::arrayGetLength()
222 { return array->getLength(); }
224 inline void Object::arrayAdd(Object *elem)
225 { array->add(elem); }
227 inline Object *Object::arrayGet(int i, Object *obj)
228 { return array->get(i, obj); }
230 inline Object *Object::arrayGetNF(int i, Object *obj)
231 { return array->getNF(i, obj); }
233 //------------------------------------------------------------------------
235 //------------------------------------------------------------------------
239 inline int Object::dictGetLength()
240 { return dict->getLength(); }
242 inline void Object::dictAdd(char *key, Object *val)
243 { dict->add(key, val); }
245 inline GBool Object::dictIs(char *dictType)
246 { return dict->is(dictType); }
248 inline GBool Object::isDict(char *dictType)
249 { return type == objDict && dictIs(dictType); }
251 inline Object *Object::dictLookup(char *key, Object *obj)
252 { return dict->lookup(key, obj); }
254 inline Object *Object::dictLookupNF(char *key, Object *obj)
255 { return dict->lookupNF(key, obj); }
257 inline char *Object::dictGetKey(int i)
258 { return dict->getKey(i); }
260 inline Object *Object::dictGetVal(int i, Object *obj)
261 { return dict->getVal(i, obj); }
263 inline Object *Object::dictGetValNF(int i, Object *obj)
264 { return dict->getValNF(i, obj); }
266 //------------------------------------------------------------------------
268 //------------------------------------------------------------------------
272 inline GBool Object::streamIs(char *dictType)
273 { return stream->getDict()->is(dictType); }
275 inline GBool Object::isStream(char *dictType)
276 { return type == objStream && streamIs(dictType); }
278 inline void Object::streamReset()
281 inline void Object::streamClose()
284 inline int Object::streamGetChar()
285 { return stream->getChar(); }
287 inline int Object::streamLookChar()
288 { return stream->lookChar(); }
290 inline char *Object::streamGetLine(char *buf, int size)
291 { return stream->getLine(buf, size); }
293 inline Guint Object::streamGetPos()
294 { return stream->getPos(); }
296 inline void Object::streamSetPos(Guint pos, int dir)
297 { stream->setPos(pos, dir); }
299 inline Dict *Object::streamGetDict()
300 { return stream->getDict(); }