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
*initDict(Dict
*dictA
);
99 Object
*initStream(Stream
*streamA
);
100 Object
*initRef(int numA
, int genA
)
101 { initObj(objRef
); ref
.num
= numA
; ref
.gen
= genA
; return this; }
102 Object
*initCmd(char *cmdA
)
103 { initObj(objCmd
); cmd
= copyString(cmdA
); return this; }
105 { initObj(objError
); return this; }
107 { initObj(objEOF
); return this; }
110 Object
*copy(Object
*obj
);
112 // If object is a Ref, fetch and return the referenced object.
113 // Otherwise, return a copy of the object.
114 Object
*fetch(XRef
*xref
, Object
*obj
);
116 // Free object contents.
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 object 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
; }
161 int arrayGetLength();
162 void arrayAdd(Object
*elem
);
163 Object
*arrayGet(int i
, Object
*obj
);
164 Object
*arrayGetNF(int i
, Object
*obj
);
168 void dictAdd(char *key
, Object
*val
);
169 GBool
dictIs(char *dictType
);
170 Object
*dictLookup(char *key
, Object
*obj
);
171 Object
*dictLookupNF(char *key
, Object
*obj
);
172 char *dictGetKey(int i
);
173 Object
*dictGetVal(int i
, Object
*obj
);
174 Object
*dictGetValNF(int i
, Object
*obj
);
177 GBool
streamIs(char *dictType
);
181 int streamLookChar();
182 char *streamGetLine(char *buf
, int size
);
183 Guint
streamGetPos();
184 void streamSetPos(Guint pos
, int dir
= 0);
185 Dict
*streamGetDict();
189 void print(FILE *f
= stdout
);
192 static void memCheck(FILE *f
);
196 ObjType type
; // object type
197 union { // value for each type:
198 GBool booln
; // boolean
201 GString
*string
; // string
203 Array
*array
; // array
204 Dict
*dict
; // dictionary
205 Stream
*stream
; // stream
206 Ref ref
; // indirect reference
207 char *cmd
; // command
211 static int // number of each type of object
212 numAlloc
[numObjTypes
]; // currently allocated
216 //------------------------------------------------------------------------
218 //------------------------------------------------------------------------
222 inline int Object::arrayGetLength()
223 { return array
->getLength(); }
225 inline void Object::arrayAdd(Object
*elem
)
226 { array
->add(elem
); }
228 inline Object
*Object::arrayGet(int i
, Object
*obj
)
229 { return array
->get(i
, obj
); }
231 inline Object
*Object::arrayGetNF(int i
, Object
*obj
)
232 { return array
->getNF(i
, obj
); }
234 //------------------------------------------------------------------------
236 //------------------------------------------------------------------------
240 inline int Object::dictGetLength()
241 { return dict
->getLength(); }
243 inline void Object::dictAdd(char *key
, Object
*val
)
244 { dict
->add(key
, val
); }
246 inline GBool
Object::dictIs(char *dictType
)
247 { return dict
->is(dictType
); }
249 inline GBool
Object::isDict(char *dictType
)
250 { return type
== objDict
&& dictIs(dictType
); }
252 inline Object
*Object::dictLookup(char *key
, Object
*obj
)
253 { return dict
->lookup(key
, obj
); }
255 inline Object
*Object::dictLookupNF(char *key
, Object
*obj
)
256 { return dict
->lookupNF(key
, obj
); }
258 inline char *Object::dictGetKey(int i
)
259 { return dict
->getKey(i
); }
261 inline Object
*Object::dictGetVal(int i
, Object
*obj
)
262 { return dict
->getVal(i
, obj
); }
264 inline Object
*Object::dictGetValNF(int i
, Object
*obj
)
265 { return dict
->getValNF(i
, obj
); }
267 //------------------------------------------------------------------------
269 //------------------------------------------------------------------------
273 inline GBool
Object::streamIs(char *dictType
)
274 { return stream
->getDict()->is(dictType
); }
276 inline GBool
Object::isStream(char *dictType
)
277 { return type
== objStream
&& streamIs(dictType
); }
279 inline void Object::streamReset()
282 inline void Object::streamClose()
285 inline int Object::streamGetChar()
286 { return stream
->getChar(); }
288 inline int Object::streamLookChar()
289 { return stream
->lookChar(); }
291 inline char *Object::streamGetLine(char *buf
, int size
)
292 { return stream
->getLine(buf
, size
); }
294 inline Guint
Object::streamGetPos()
295 { return stream
->getPos(); }
297 inline void Object::streamSetPos(Guint pos
, int dir
)
298 { stream
->setPos(pos
, dir
); }
300 inline Dict
*Object::streamGetDict()
301 { return stream
->getDict(); }