]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/Object.h
kill traces of ltk, incorporate new sources
[evince.git] / pdf / xpdf / Object.h
1 //========================================================================
2 //
3 // Object.h
4 //
5 // Copyright 1996-2002 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #ifndef OBJECT_H
10 #define OBJECT_H
11
12 #include <aconf.h>
13
14 #ifdef USE_GCC_PRAGMAS
15 #pragma interface
16 #endif
17
18 #include <stdio.h>
19 #include <string.h>
20 #include "gtypes.h"
21 #include "gmem.h"
22 #include "GString.h"
23
24 class XRef;
25 class Array;
26 class Dict;
27 class Stream;
28
29 //------------------------------------------------------------------------
30 // Ref
31 //------------------------------------------------------------------------
32
33 struct Ref {
34   int num;                      // object number
35   int gen;                      // generation number
36 };
37
38 //------------------------------------------------------------------------
39 // object types
40 //------------------------------------------------------------------------
41
42 enum ObjType {
43   // simple objects
44   objBool,                      // boolean
45   objInt,                       // integer
46   objReal,                      // real
47   objString,                    // string
48   objName,                      // name
49   objNull,                      // null
50
51   // complex objects
52   objArray,                     // array
53   objDict,                      // dictionary
54   objStream,                    // stream
55   objRef,                       // indirect reference
56
57   // special objects
58   objCmd,                       // command name
59   objError,                     // error return from Lexer
60   objEOF,                       // end of file return from Lexer
61   objNone                       // uninitialized object
62 };
63
64 #define numObjTypes 14          // total number of object types
65
66 //------------------------------------------------------------------------
67 // Object
68 //------------------------------------------------------------------------
69
70 #ifdef DEBUG_MEM
71 #define initObj(t) ++numAlloc[type = t]
72 #else
73 #define initObj(t) type = t
74 #endif
75
76 class Object {
77 public:
78
79   // Default constructor.
80   Object():
81     type(objNone) {}
82
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; }
94   Object *initNull()
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; }
103   Object *initError()
104     { initObj(objError); return this; }
105   Object *initEOF()
106     { initObj(objEOF); return this; }
107
108   // Copy an object.
109   Object *copy(Object *obj);
110
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);
114
115   // Free object contents.
116   void free();
117
118   // Type checking.
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; }
135
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); }
143
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; }
158
159   // Array accessors.
160   int arrayGetLength();
161   void arrayAdd(Object *elem);
162   Object *arrayGet(int i, Object *obj);
163   Object *arrayGetNF(int i, Object *obj);
164
165   // Dict accessors.
166   int dictGetLength();
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);
174
175   // Stream accessors.
176   GBool streamIs(char *dictType);
177   void streamReset();
178   void streamClose();
179   int streamGetChar();
180   int streamLookChar();
181   char *streamGetLine(char *buf, int size);
182   Guint streamGetPos();
183   void streamSetPos(Guint pos, int dir = 0);
184   Dict *streamGetDict();
185
186   // Output.
187   char *getTypeName();
188   void print(FILE *f = stdout);
189
190   // Memory testing.
191   static void memCheck(FILE *f);
192
193 private:
194
195   ObjType type;                 // object type
196   union {                       // value for each type:
197     GBool booln;                //   boolean
198     int intg;                   //   integer
199     double real;                //   real
200     GString *string;            //   string
201     char *name;                 //   name
202     Array *array;               //   array
203     Dict *dict;                 //   dictionary
204     Stream *stream;             //   stream
205     Ref ref;                    //   indirect reference
206     char *cmd;                  //   command
207   };
208
209 #ifdef DEBUG_MEM
210   static int                    // number of each type of object
211     numAlloc[numObjTypes];      //   currently allocated
212 #endif
213 };
214
215 //------------------------------------------------------------------------
216 // Array accessors.
217 //------------------------------------------------------------------------
218
219 #include "Array.h"
220
221 inline int Object::arrayGetLength()
222   { return array->getLength(); }
223
224 inline void Object::arrayAdd(Object *elem)
225   { array->add(elem); }
226
227 inline Object *Object::arrayGet(int i, Object *obj)
228   { return array->get(i, obj); }
229
230 inline Object *Object::arrayGetNF(int i, Object *obj)
231   { return array->getNF(i, obj); }
232
233 //------------------------------------------------------------------------
234 // Dict accessors.
235 //------------------------------------------------------------------------
236
237 #include "Dict.h"
238
239 inline int Object::dictGetLength()
240   { return dict->getLength(); }
241
242 inline void Object::dictAdd(char *key, Object *val)
243   { dict->add(key, val); }
244
245 inline GBool Object::dictIs(char *dictType)
246   { return dict->is(dictType); }
247
248 inline GBool Object::isDict(char *dictType)
249   { return type == objDict && dictIs(dictType); }
250
251 inline Object *Object::dictLookup(char *key, Object *obj)
252   { return dict->lookup(key, obj); }
253
254 inline Object *Object::dictLookupNF(char *key, Object *obj)
255   { return dict->lookupNF(key, obj); }
256
257 inline char *Object::dictGetKey(int i)
258   { return dict->getKey(i); }
259
260 inline Object *Object::dictGetVal(int i, Object *obj)
261   { return dict->getVal(i, obj); }
262
263 inline Object *Object::dictGetValNF(int i, Object *obj)
264   { return dict->getValNF(i, obj); }
265
266 //------------------------------------------------------------------------
267 // Stream accessors.
268 //------------------------------------------------------------------------
269
270 #include "Stream.h"
271
272 inline GBool Object::streamIs(char *dictType)
273   { return stream->getDict()->is(dictType); }
274
275 inline GBool Object::isStream(char *dictType)
276   { return type == objStream && streamIs(dictType); }
277
278 inline void Object::streamReset()
279   { stream->reset(); }
280
281 inline void Object::streamClose()
282   { stream->close(); }
283
284 inline int Object::streamGetChar()
285   { return stream->getChar(); }
286
287 inline int Object::streamLookChar()
288   { return stream->lookChar(); }
289
290 inline char *Object::streamGetLine(char *buf, int size)
291   { return stream->getLine(buf, size); }
292
293 inline Guint Object::streamGetPos()
294   { return stream->getPos(); }
295
296 inline void Object::streamSetPos(Guint pos, int dir)
297   { stream->setPos(pos, dir); }
298
299 inline Dict *Object::streamGetDict()
300   { return stream->getDict(); }
301
302 #endif