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