i hope the node casts are correct here.
[AROS-Contrib.git] / arospdf / xpdf / Object.h
blobdd86dfbb93aed6f873a29d050f226cb84880dce5
1 //========================================================================
2 //
3 // xObject.h
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
9 #ifndef xObject_H
10 #define xObject_H
12 #include <aconf.h>
14 #ifdef USE_GCC_PRAGMAS
15 #pragma interface
16 #endif
18 #include <stdio.h>
19 #include <string.h>
20 #include "gtypes.h"
21 #include "gmem.h"
22 #include "GString.h"
24 class XRef;
25 class Array;
26 class Dict;
27 class Stream;
29 //------------------------------------------------------------------------
30 // Ref
31 //------------------------------------------------------------------------
33 struct Ref {
34 int num; // xObject number
35 int gen; // generation number
38 //------------------------------------------------------------------------
39 // xObject types
40 //------------------------------------------------------------------------
42 enum ObjType {
43 // simple xObjects
44 objBool, // boolean
45 objInt, // integer
46 objReal, // real
47 objString, // string
48 objName, // name
49 objNull, // null
51 // complex xObjects
52 objArray, // array
53 objDict, // dictionary
54 objStream, // stream
55 objRef, // indirect reference
57 // special xObjects
58 objCmd, // command name
59 objError, // error return from Lexer
60 objEOF, // end of file return from Lexer
61 objNone // uninitialized xObject
64 #define numObjTypes 14 // total number of xObject types
66 //------------------------------------------------------------------------
67 // xObject
68 //------------------------------------------------------------------------
70 #ifdef DEBUG_MEM
71 #define initObj(t) ++numAlloc[type = t]
72 #else
73 #define initObj(t) type = t
74 #endif
76 class xObject {
77 public:
79 // Default constructor.
80 xObject():
81 type(objNone) {}
83 // Initialize an xObject.
84 xObject *initBool(GBool boolnA)
85 { initObj(objBool); booln = boolnA; return this; }
86 xObject *initInt(int intgA)
87 { initObj(objInt); intg = intgA; return this; }
88 xObject *initReal(double realA)
89 { initObj(objReal); real = realA; return this; }
90 xObject *initString(GString *stringA)
91 { initObj(objString); string = stringA; return this; }
92 xObject *initName(char *nameA)
93 { initObj(objName); name = copyString(nameA); return this; }
94 xObject *initNull()
95 { initObj(objNull); return this; }
96 xObject *initArray(XRef *xref);
97 xObject *initDict(XRef *xref);
98 xObject *initDict(Dict *dictA);
99 xObject *initStream(Stream *streamA);
100 xObject *initRef(int numA, int genA)
101 { initObj(objRef); ref.num = numA; ref.gen = genA; return this; }
102 xObject *initCmd(char *cmdA)
103 { initObj(objCmd); cmd = copyString(cmdA); return this; }
104 xObject *initError()
105 { initObj(objError); return this; }
106 xObject *initEOF()
107 { initObj(objEOF); return this; }
109 // Copy an xObject.
110 xObject *copy(xObject *obj);
112 // If xObject is a Ref, fetch and return the referenced xObject.
113 // Otherwise, return a copy of the xObject.
114 xObject *fetch(XRef *xref, xObject *obj);
116 // Free xObject contents.
117 void free();
119 // Type checking.
120 ObjType getType() { return type; }
121 GBool isBool() { return type == objBool; }
122 GBool isInt() { return type == objInt; }
123 GBool isReal() { return type == objReal; }
124 GBool isNum() { return type == objInt || type == objReal; }
125 GBool isString() { return type == objString; }
126 GBool isName() { return type == objName; }
127 GBool isNull() { return type == objNull; }
128 GBool isArray() { return type == objArray; }
129 GBool isDict() { return type == objDict; }
130 GBool isStream() { return type == objStream; }
131 GBool isRef() { return type == objRef; }
132 GBool isCmd() { return type == objCmd; }
133 GBool isError() { return type == objError; }
134 GBool isEOF() { return type == objEOF; }
135 GBool isNone() { return type == objNone; }
137 // Special type checking.
138 GBool isName(char *nameA)
139 { return type == objName && !strcmp(name, nameA); }
140 GBool isDict(char *dictType);
141 GBool isStream(char *dictType);
142 GBool isCmd(char *cmdA)
143 { return type == objCmd && !strcmp(cmd, cmdA); }
145 // Accessors. NB: these assume xObject is of correct type.
146 GBool getBool() { return booln; }
147 int getInt() { return intg; }
148 double getReal() { return real; }
149 double getNum() { return type == objInt ? (double)intg : real; }
150 GString *getString() { return string; }
151 char *getName() { return name; }
152 Array *getArray() { return array; }
153 Dict *getDict() { return dict; }
154 Stream *getStream() { return stream; }
155 Ref getRef() { return ref; }
156 int getRefNum() { return ref.num; }
157 int getRefGen() { return ref.gen; }
158 char *getCmd() { return cmd; }
160 // Array accessors.
161 int arrayGetLength();
162 void arrayAdd(xObject *elem);
163 xObject *arrayGet(int i, xObject *obj);
164 xObject *arrayGetNF(int i, xObject *obj);
166 // Dict accessors.
167 int dictGetLength();
168 void dictAdd(char *key, xObject *val);
169 GBool dictIs(char *dictType);
170 xObject *dictLookup(char *key, xObject *obj);
171 xObject *dictLookupNF(char *key, xObject *obj);
172 char *dictGetKey(int i);
173 xObject *dictGetVal(int i, xObject *obj);
174 xObject *dictGetValNF(int i, xObject *obj);
176 // Stream accessors.
177 GBool streamIs(char *dictType);
178 void streamReset();
179 void streamClose();
180 int streamGetChar();
181 int streamLookChar();
182 char *streamGetLine(char *buf, int size);
183 Guint streamGetPos();
184 void streamSetPos(Guint pos, int dir = 0);
185 Dict *streamGetDict();
187 // Output.
188 char *getTypeName();
189 void print(FILE *f = stdout);
191 // Memory testing.
192 static void memCheck(FILE *f);
194 private:
196 ObjType type; // xObject type
197 union { // value for each type:
198 GBool booln; // boolean
199 int intg; // integer
200 double real; // real
201 GString *string; // string
202 char *name; // name
203 Array *array; // array
204 Dict *dict; // dictionary
205 Stream *stream; // stream
206 Ref ref; // indirect reference
207 char *cmd; // command
210 #ifdef DEBUG_MEM
211 static int // number of each type of xObject
212 numAlloc[numObjTypes]; // currently allocated
213 #endif
216 //------------------------------------------------------------------------
217 // Array accessors.
218 //------------------------------------------------------------------------
220 #include "Array.h"
222 inline int xObject::arrayGetLength()
223 { return array->getLength(); }
225 inline void xObject::arrayAdd(xObject *elem)
226 { array->add(elem); }
228 inline xObject *xObject::arrayGet(int i, xObject *obj)
229 { return array->get(i, obj); }
231 inline xObject *xObject::arrayGetNF(int i, xObject *obj)
232 { return array->getNF(i, obj); }
234 //------------------------------------------------------------------------
235 // Dict accessors.
236 //------------------------------------------------------------------------
238 #include "Dict.h"
240 inline int xObject::dictGetLength()
241 { return dict->getLength(); }
243 inline void xObject::dictAdd(char *key, xObject *val)
244 { dict->add(key, val); }
246 inline GBool xObject::dictIs(char *dictType)
247 { return dict->is(dictType); }
249 inline GBool xObject::isDict(char *dictType)
250 { return type == objDict && dictIs(dictType); }
252 inline xObject *xObject::dictLookup(char *key, xObject *obj)
253 { return dict->lookup(key, obj); }
255 inline xObject *xObject::dictLookupNF(char *key, xObject *obj)
256 { return dict->lookupNF(key, obj); }
258 inline char *xObject::dictGetKey(int i)
259 { return dict->getKey(i); }
261 inline xObject *xObject::dictGetVal(int i, xObject *obj)
262 { return dict->getVal(i, obj); }
264 inline xObject *xObject::dictGetValNF(int i, xObject *obj)
265 { return dict->getValNF(i, obj); }
267 //------------------------------------------------------------------------
268 // Stream accessors.
269 //------------------------------------------------------------------------
271 #include "Stream.h"
273 inline GBool xObject::streamIs(char *dictType)
274 { return stream->getDict()->is(dictType); }
276 inline GBool xObject::isStream(char *dictType)
277 { return type == objStream && streamIs(dictType); }
279 inline void xObject::streamReset()
280 { stream->reset(); }
282 inline void xObject::streamClose()
283 { stream->close(); }
285 inline int xObject::streamGetChar()
286 { return stream->getChar(); }
288 inline int xObject::streamLookChar()
289 { return stream->lookChar(); }
291 inline char *xObject::streamGetLine(char *buf, int size)
292 { return stream->getLine(buf, size); }
294 inline Guint xObject::streamGetPos()
295 { return stream->getPos(); }
297 inline void xObject::streamSetPos(Guint pos, int dir)
298 { stream->setPos(pos, dir); }
300 inline Dict *xObject::streamGetDict()
301 { return stream->getDict(); }
303 #endif