Initial revision
[swftools.git] / pdf2swf / 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   ObjType getType() { return type; }
119   GBool isBool() { return type == objBool; }
120   GBool isInt() { return type == objInt; }
121   GBool isReal() { return type == objReal; }
122   GBool isNum() { return type == objInt || type == objReal; }
123   GBool isString() { return type == objString; }
124   GBool isName() { return type == objName; }
125   GBool isNull() { return type == objNull; }
126   GBool isArray() { return type == objArray; }
127   GBool isDict() { return type == objDict; }
128   GBool isStream() { return type == objStream; }
129   GBool isRef() { return type == objRef; }
130   GBool isCmd() { return type == objCmd; }
131   GBool isError() { return type == objError; }
132   GBool isEOF() { return type == objEOF; }
133   GBool isNone() { return type == objNone; }
134
135   // Special type checking.
136   GBool isName(char *name1)
137     { return type == objName && !strcmp(name, name1); }
138   GBool isDict(char *dictType);
139   GBool isStream(char *dictType);
140   GBool isCmd(char *cmd1)
141     { return type == objCmd && !strcmp(cmd, cmd1); }
142
143   // Accessors.  NB: these assume object is of correct type.
144   GBool getBool() { return booln; }
145   int getInt() { return intg; }
146   double getReal() { return real; }
147   double getNum() { return type == objInt ? (double)intg : real; }
148   GString *getString() { return string; }
149   char *getName() { return name; }
150   Array *getArray() { return array; }
151   Dict *getDict() { return dict; }
152   Stream *getStream() { return stream; }
153   Ref getRef() { return ref; }
154   int getRefNum() { return ref.num; }
155   int getRefGen() { return ref.gen; }
156
157   // Array accessors.
158   int arrayGetLength();
159   void arrayAdd(Object *elem);
160   Object *arrayGet(int i, Object *obj);
161   Object *arrayGetNF(int i, Object *obj);
162
163   // Dict accessors.
164   int dictGetLength();
165   void dictAdd(char *key, Object *val);
166   GBool dictIs(char *dictType);
167   Object *dictLookup(char *key, Object *obj);
168   Object *dictLookupNF(char *key, Object *obj);
169   char *dictGetKey(int i);
170   Object *dictGetVal(int i, Object *obj);
171   Object *dictGetValNF(int i, Object *obj);
172
173   // Stream accessors.
174   GBool streamIs(char *dictType);
175   void streamReset();
176   void streamClose();
177   int streamGetChar();
178   int streamLookChar();
179   char *streamGetLine(char *buf, int size);
180   int streamGetPos();
181   void streamSetPos(int pos);
182   Dict *streamGetDict();
183
184   // Output.
185   char *getTypeName();
186   void print(FILE *f = stdout);
187
188   // Memory testing.
189   static void memCheck(FILE *f);
190
191 private:
192
193   ObjType type;                 // object type
194   union {                       // value for each type:
195     GBool booln;                //   boolean
196     int intg;                   //   integer
197     double real;                //   real
198     GString *string;            //   string
199     char *name;                 //   name
200     Array *array;               //   array
201     Dict *dict;                 //   dictionary
202     Stream *stream;             //   stream
203     Ref ref;                    //   indirect reference
204     char *cmd;                  //   command
205   };
206
207 #ifdef DEBUG_MEM
208   static int                    // number of each type of object
209     numAlloc[numObjTypes];      //   currently allocated
210 #endif
211 };
212
213 //------------------------------------------------------------------------
214 // Array accessors.
215 //------------------------------------------------------------------------
216
217 #include "Array.h"
218
219 inline int Object::arrayGetLength()
220   { return array->getLength(); }
221
222 inline void Object::arrayAdd(Object *elem)
223   { array->add(elem); }
224
225 inline Object *Object::arrayGet(int i, Object *obj)
226   { return array->get(i, obj); }
227
228 inline Object *Object::arrayGetNF(int i, Object *obj)
229   { return array->getNF(i, obj); }
230
231 //------------------------------------------------------------------------
232 // Dict accessors.
233 //------------------------------------------------------------------------
234
235 #include "Dict.h"
236
237 inline int Object::dictGetLength()
238   { return dict->getLength(); }
239
240 inline void Object::dictAdd(char *key, Object *val)
241   { dict->add(key, val); }
242
243 inline GBool Object::dictIs(char *dictType)
244   { return dict->is(dictType); }
245
246 inline GBool Object::isDict(char *dictType)
247   { return type == objDict && dictIs(dictType); }
248
249 inline Object *Object::dictLookup(char *key, Object *obj)
250   { return dict->lookup(key, obj); }
251
252 inline Object *Object::dictLookupNF(char *key, Object *obj)
253   { return dict->lookupNF(key, obj); }
254
255 inline char *Object::dictGetKey(int i)
256   { return dict->getKey(i); }
257
258 inline Object *Object::dictGetVal(int i, Object *obj)
259   { return dict->getVal(i, obj); }
260
261 inline Object *Object::dictGetValNF(int i, Object *obj)
262   { return dict->getValNF(i, obj); }
263
264 //------------------------------------------------------------------------
265 // Stream accessors.
266 //------------------------------------------------------------------------
267
268 #include "Stream.h"
269
270 inline GBool Object::streamIs(char *dictType)
271   { return stream->getDict()->is(dictType); }
272
273 inline GBool Object::isStream(char *dictType)
274   { return type == objStream && streamIs(dictType); }
275
276 inline void Object::streamReset()
277   { stream->reset(); }
278
279 inline void Object::streamClose()
280   { stream->close(); }
281
282 inline int Object::streamGetChar()
283   { return stream->getChar(); }
284
285 inline int Object::streamLookChar()
286   { return stream->lookChar(); }
287
288 inline char *Object::streamGetLine(char *buf, int size)
289   { return stream->getLine(buf, size); }
290
291 inline int Object::streamGetPos()
292   { return stream->getPos(); }
293
294 inline void Object::streamSetPos(int pos)
295   { stream->setPos(pos); }
296
297 inline Dict *Object::streamGetDict()
298   { return stream->getDict(); }
299
300 #endif