source: trunk/poppler/mypoppler/poppler/Object.h @ 277

Last change on this file since 277 was 277, checked in by rbri, 12 years ago

PDF plugin: Poppler library updated to version 0.12.3

File size: 11.0 KB
Line 
1//========================================================================
2//
3// Object.h
4//
5// Copyright 1996-2003 Glyph & Cog, LLC
6//
7//========================================================================
8
9//========================================================================
10//
11// Modified under the Poppler project - http://poppler.freedesktop.org
12//
13// All changes made under the Poppler project to this file are licensed
14// under GPL version 2 or later
15//
16// Copyright (C) 2007 Julien Rebetez <julienr@svn.gnome.org>
17// Copyright (C) 2008 Kees Cook <kees@outflux.net>
18// Copyright (C) 2008 Albert Astals Cid <aacid@kde.org>
19// Copyright (C) 2009 Jakub Wilk <ubanus@users.sf.net>
20//
21// To see a description of the changes please see the Changelog file that
22// came with your tarball or type make ChangeLog if you are building from git
23//
24//========================================================================
25
26#ifndef OBJECT_H
27#define OBJECT_H
28
29#ifdef USE_GCC_PRAGMAS
30#pragma interface
31#endif
32
33#include <stdio.h>
34#include <string.h>
35#include "goo/gtypes.h"
36#include "goo/gmem.h"
37#include "goo/GooString.h"
38#include "Error.h"
39
40#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
41# define likely(x)      __builtin_expect((x), 1)
42# define unlikely(x)    __builtin_expect((x), 0)
43#else
44# define likely(x)      (x)
45# define unlikely(x)    (x)
46#endif
47
48#define OBJECT_TYPE_CHECK(wanted_type) \
49    if (unlikely(type != wanted_type)) { \
50        error(0, (char *) "Call to Object where the object was type %d, " \
51                 "not the expected type %d", type, wanted_type); \
52        abort(); \
53    }
54
55#define OBJECT_2TYPES_CHECK(wanted_type1, wanted_type2) \
56    if (unlikely(type != wanted_type1) && unlikely(type != wanted_type2)) { \
57        error(0, (char *) "Call to Object where the object was type %d, " \
58                 "not the expected type %d or %d", type, wanted_type1, wanted_type2); \
59        abort(); \
60    }
61
62class XRef;
63class Array;
64class Dict;
65class Stream;
66
67//------------------------------------------------------------------------
68// Ref
69//------------------------------------------------------------------------
70
71struct Ref {
72  int num;                      // object number
73  int gen;                      // generation number
74};
75
76//------------------------------------------------------------------------
77// object types
78//------------------------------------------------------------------------
79
80enum ObjType {
81  // simple objects
82  objBool,                      // boolean
83  objInt,                       // integer
84  objReal,                      // real
85  objString,                    // string
86  objName,                      // name
87  objNull,                      // null
88
89  // complex objects
90  objArray,                     // array
91  objDict,                      // dictionary
92  objStream,                    // stream
93  objRef,                       // indirect reference
94
95  // special objects
96  objCmd,                       // command name
97  objError,                     // error return from Lexer
98  objEOF,                       // end of file return from Lexer
99  objNone                       // uninitialized object
100};
101
102#define numObjTypes 14          // total number of object types
103
104//------------------------------------------------------------------------
105// Object
106//------------------------------------------------------------------------
107
108#ifdef DEBUG_MEM
109#define initObj(t) zeroUnion(); ++numAlloc[type = t]
110#else
111#define initObj(t) zeroUnion(); type = t
112#endif
113
114class Object {
115public:
116  // clear the anonymous union as best we can -- clear at least a pointer
117  void zeroUnion() { this->name = NULL; }
118
119  // Default constructor.
120  Object():
121    type(objNone) { zeroUnion(); }
122
123  // Initialize an object.
124  Object *initBool(GBool boolnA)
125    { initObj(objBool); booln = boolnA; return this; }
126  Object *initInt(int intgA)
127    { initObj(objInt); intg = intgA; return this; }
128  Object *initReal(double realA)
129    { initObj(objReal); real = realA; return this; }
130  Object *initString(GooString *stringA)
131    { initObj(objString); string = stringA; return this; }
132  Object *initName(char *nameA)
133    { initObj(objName); name = copyString(nameA); return this; }
134  Object *initNull()
135    { initObj(objNull); return this; }
136  Object *initArray(XRef *xref);
137  Object *initDict(XRef *xref);
138  Object *initDict(Dict *dictA);
139  Object *initStream(Stream *streamA);
140  Object *initRef(int numA, int genA)
141    { initObj(objRef); ref.num = numA; ref.gen = genA; return this; }
142  Object *initCmd(char *cmdA)
143    { initObj(objCmd); cmd = copyString(cmdA); return this; }
144  Object *initError()
145    { initObj(objError); return this; }
146  Object *initEOF()
147    { initObj(objEOF); return this; }
148
149  // Copy an object.
150  Object *copy(Object *obj);
151  Object *shallowCopy(Object *obj) {
152    *obj = *this;
153    return obj;
154  }
155
156  // If object is a Ref, fetch and return the referenced object.
157  // Otherwise, return a copy of the object.
158  Object *fetch(XRef *xref, Object *obj);
159
160  // Free object contents.
161  void free();
162
163  // Type checking.
164  ObjType getType() { return type; }
165  GBool isBool() { return type == objBool; }
166  GBool isInt() { return type == objInt; }
167  GBool isReal() { return type == objReal; }
168  GBool isNum() { return type == objInt || type == objReal; }
169  GBool isString() { return type == objString; }
170  GBool isName() { return type == objName; }
171  GBool isNull() { return type == objNull; }
172  GBool isArray() { return type == objArray; }
173  GBool isDict() { return type == objDict; }
174  GBool isStream() { return type == objStream; }
175  GBool isRef() { return type == objRef; }
176  GBool isCmd() { return type == objCmd; }
177  GBool isError() { return type == objError; }
178  GBool isEOF() { return type == objEOF; }
179  GBool isNone() { return type == objNone; }
180
181  // Special type checking.
182  GBool isName(char *nameA)
183    { return type == objName && !strcmp(name, nameA); }
184  GBool isDict(char *dictType);
185  GBool isStream(char *dictType);
186  GBool isCmd(char *cmdA)
187    { return type == objCmd && !strcmp(cmd, cmdA); }
188
189  // Accessors.
190  GBool getBool() { OBJECT_TYPE_CHECK(objBool); return booln; }
191  int getInt() { OBJECT_TYPE_CHECK(objInt); return intg; }
192  double getReal() { OBJECT_TYPE_CHECK(objReal); return real; }
193  double getNum() { OBJECT_2TYPES_CHECK(objInt, objReal); return type == objInt ? (double)intg : real; }
194  GooString *getString() { OBJECT_TYPE_CHECK(objString); return string; }
195  char *getName() { OBJECT_TYPE_CHECK(objName); return name; }
196  Array *getArray() { OBJECT_TYPE_CHECK(objArray); return array; }
197  Dict *getDict() { OBJECT_TYPE_CHECK(objDict); return dict; }
198  Stream *getStream() { OBJECT_TYPE_CHECK(objStream); return stream; }
199  Ref getRef() { OBJECT_TYPE_CHECK(objRef); return ref; }
200  int getRefNum() { OBJECT_TYPE_CHECK(objRef); return ref.num; }
201  int getRefGen() { OBJECT_TYPE_CHECK(objRef); return ref.gen; }
202  char *getCmd() { OBJECT_TYPE_CHECK(objCmd); return cmd; }
203
204  // Array accessors.
205  int arrayGetLength();
206  void arrayAdd(Object *elem);
207  Object *arrayGet(int i, Object *obj);
208  Object *arrayGetNF(int i, Object *obj);
209
210  // Dict accessors.
211  int dictGetLength();
212  void dictAdd(char *key, Object *val);
213  void dictSet(char *key, Object *val);
214  GBool dictIs(char *dictType);
215  Object *dictLookup(char *key, Object *obj);
216  Object *dictLookupNF(char *key, Object *obj);
217  char *dictGetKey(int i);
218  Object *dictGetVal(int i, Object *obj);
219  Object *dictGetValNF(int i, Object *obj);
220
221  // Stream accessors.
222  GBool streamIs(char *dictType);
223  void streamReset();
224  void streamClose();
225  int streamGetChar();
226  int streamLookChar();
227  char *streamGetLine(char *buf, int size);
228  Guint streamGetPos();
229  void streamSetPos(Guint pos, int dir = 0);
230  Dict *streamGetDict();
231
232  // Output.
233  char *getTypeName();
234  void print(FILE *f = stdout);
235
236  // Memory testing.
237  static void memCheck(FILE *f);
238
239private:
240
241  ObjType type;                 // object type
242  union {                       // value for each type:
243    GBool booln;                //   boolean
244    int intg;                   //   integer
245    double real;                //   real
246    GooString *string;          //   string
247    char *name;                 //   name
248    Array *array;               //   array
249    Dict *dict;                 //   dictionary
250    Stream *stream;             //   stream
251    Ref ref;                    //   indirect reference
252    char *cmd;                  //   command
253  };
254
255#ifdef DEBUG_MEM
256  static int                    // number of each type of object
257    numAlloc[numObjTypes];      //   currently allocated
258#endif
259};
260
261//------------------------------------------------------------------------
262// Array accessors.
263//------------------------------------------------------------------------
264
265#include "Array.h"
266
267inline int Object::arrayGetLength()
268  { OBJECT_TYPE_CHECK(objArray); return array->getLength(); }
269
270inline void Object::arrayAdd(Object *elem)
271  { OBJECT_TYPE_CHECK(objArray); array->add(elem); }
272
273inline Object *Object::arrayGet(int i, Object *obj)
274  { OBJECT_TYPE_CHECK(objArray); return array->get(i, obj); }
275
276inline Object *Object::arrayGetNF(int i, Object *obj)
277  { OBJECT_TYPE_CHECK(objArray); return array->getNF(i, obj); }
278
279//------------------------------------------------------------------------
280// Dict accessors.
281//------------------------------------------------------------------------
282
283#include "Dict.h"
284
285inline int Object::dictGetLength()
286  { OBJECT_TYPE_CHECK(objDict); return dict->getLength(); }
287
288inline void Object::dictAdd(char *key, Object *val)
289  { OBJECT_TYPE_CHECK(objDict); dict->add(key, val); }
290
291inline void Object::dictSet(char *key, Object *val)
292        { OBJECT_TYPE_CHECK(objDict); dict->set(key, val); }
293
294inline GBool Object::dictIs(char *dictType)
295  { OBJECT_TYPE_CHECK(objDict); return dict->is(dictType); }
296
297inline GBool Object::isDict(char *dictType)
298  { return type == objDict && dictIs(dictType); }
299
300inline Object *Object::dictLookup(char *key, Object *obj)
301  { OBJECT_TYPE_CHECK(objDict); return dict->lookup(key, obj); }
302
303inline Object *Object::dictLookupNF(char *key, Object *obj)
304  { OBJECT_TYPE_CHECK(objDict); return dict->lookupNF(key, obj); }
305
306inline char *Object::dictGetKey(int i)
307  { OBJECT_TYPE_CHECK(objDict); return dict->getKey(i); }
308
309inline Object *Object::dictGetVal(int i, Object *obj)
310  { OBJECT_TYPE_CHECK(objDict); return dict->getVal(i, obj); }
311
312inline Object *Object::dictGetValNF(int i, Object *obj)
313  { OBJECT_TYPE_CHECK(objDict); return dict->getValNF(i, obj); }
314
315//------------------------------------------------------------------------
316// Stream accessors.
317//------------------------------------------------------------------------
318
319#include "Stream.h"
320
321inline GBool Object::streamIs(char *dictType)
322  { OBJECT_TYPE_CHECK(objStream); return stream->getDict()->is(dictType); }
323
324inline GBool Object::isStream(char *dictType)
325  { return type == objStream && streamIs(dictType); }
326
327inline void Object::streamReset()
328  { OBJECT_TYPE_CHECK(objStream); stream->reset(); }
329
330inline void Object::streamClose()
331  { OBJECT_TYPE_CHECK(objStream); stream->close(); }
332
333inline int Object::streamGetChar()
334  { OBJECT_TYPE_CHECK(objStream); return stream->getChar(); }
335
336inline int Object::streamLookChar()
337  { OBJECT_TYPE_CHECK(objStream); return stream->lookChar(); }
338
339inline char *Object::streamGetLine(char *buf, int size)
340  { OBJECT_TYPE_CHECK(objStream); return stream->getLine(buf, size); }
341
342inline Guint Object::streamGetPos()
343  { OBJECT_TYPE_CHECK(objStream); return stream->getPos(); }
344
345inline void Object::streamSetPos(Guint pos, int dir)
346  { OBJECT_TYPE_CHECK(objStream); stream->setPos(pos, dir); }
347
348inline Dict *Object::streamGetDict()
349  { OBJECT_TYPE_CHECK(objStream); return stream->getDict(); }
350
351#endif
Note: See TracBrowser for help on using the repository browser.