~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/poppler/poppler/Object.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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, 2010 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 <set>
 
34
#include <stdio.h>
 
35
#include <string.h>
 
36
#include "goo/gtypes.h"
 
37
#include "goo/gmem.h"
 
38
#include "goo/GooString.h"
 
39
#include "goo/GooLikely.h"
 
40
#include "Error.h"
 
41
 
 
42
#define OBJECT_TYPE_CHECK(wanted_type) \
 
43
    if (unlikely(type != wanted_type)) { \
 
44
        error(0, (char *) "Call to Object where the object was type %d, " \
 
45
                 "not the expected type %d", type, wanted_type); \
 
46
        abort(); \
 
47
    }
 
48
 
 
49
#define OBJECT_2TYPES_CHECK(wanted_type1, wanted_type2) \
 
50
    if (unlikely(type != wanted_type1) && unlikely(type != wanted_type2)) { \
 
51
        error(0, (char *) "Call to Object where the object was type %d, " \
 
52
                 "not the expected type %d or %d", type, wanted_type1, wanted_type2); \
 
53
        abort(); \
 
54
    }
 
55
 
 
56
class XRef;
 
57
class Array;
 
58
class Dict;
 
59
class Stream;
 
60
 
 
61
//------------------------------------------------------------------------
 
62
// Ref
 
63
//------------------------------------------------------------------------
 
64
 
 
65
struct Ref {
 
66
  int num;                      // object number
 
67
  int gen;                      // generation number
 
68
};
 
69
 
 
70
//------------------------------------------------------------------------
 
71
// object types
 
72
//------------------------------------------------------------------------
 
73
 
 
74
enum ObjType {
 
75
  // simple objects
 
76
  objBool,                      // boolean
 
77
  objInt,                       // integer
 
78
  objReal,                      // real
 
79
  objString,                    // string
 
80
  objName,                      // name
 
81
  objNull,                      // null
 
82
 
 
83
  // complex objects
 
84
  objArray,                     // array
 
85
  objDict,                      // dictionary
 
86
  objStream,                    // stream
 
87
  objRef,                       // indirect reference
 
88
 
 
89
  // special objects
 
90
  objCmd,                       // command name
 
91
  objError,                     // error return from Lexer
 
92
  objEOF,                       // end of file return from Lexer
 
93
  objNone,                      // uninitialized object
 
94
 
 
95
  // poppler-only objects
 
96
  objUint                       // overflown integer that still fits in a unsigned integer
 
97
};
 
98
 
 
99
#define numObjTypes 15          // total number of object types
 
100
 
 
101
//------------------------------------------------------------------------
 
102
// Object
 
103
//------------------------------------------------------------------------
 
104
 
 
105
#ifdef DEBUG_MEM
 
106
#define initObj(t) zeroUnion(); ++numAlloc[type = t]
 
107
#else
 
108
#define initObj(t) zeroUnion(); type = t
 
109
#endif
 
110
 
 
111
class Object {
 
112
public:
 
113
  // clear the anonymous union as best we can -- clear at least a pointer
 
114
  // XXX Emscripten: Also null out ref.gen
 
115
  void zeroUnion() { this->name = NULL; this->ref.gen = 0; }
 
116
 
 
117
  // Default constructor.
 
118
  Object():
 
119
    type(objNone) { zeroUnion(); }
 
120
 
 
121
  // Initialize an object.
 
122
  Object *initBool(GBool boolnA)
 
123
    { initObj(objBool); booln = boolnA; return this; }
 
124
  Object *initInt(int intgA)
 
125
    { initObj(objInt); intg = intgA; return this; }
 
126
  Object *initReal(double realA)
 
127
    { initObj(objReal); real = realA; return this; }
 
128
  Object *initString(GooString *stringA)
 
129
    { initObj(objString); string = stringA; return this; }
 
130
  Object *initName(char *nameA)
 
131
    { initObj(objName); name = copyString(nameA); return this; }
 
132
  Object *initNull()
 
133
    { initObj(objNull); return this; }
 
134
  Object *initArray(XRef *xref);
 
135
  Object *initDict(XRef *xref);
 
136
  Object *initDict(Dict *dictA);
 
137
  Object *initStream(Stream *streamA);
 
138
  Object *initRef(int numA, int genA)
 
139
    { initObj(objRef); ref.num = numA; ref.gen = genA; return this; }
 
140
  Object *initCmd(char *cmdA)
 
141
    { initObj(objCmd); cmd = copyString(cmdA); return this; }
 
142
  Object *initError()
 
143
    { initObj(objError); return this; }
 
144
  Object *initEOF()
 
145
    { initObj(objEOF); return this; }
 
146
  Object *initUint(unsigned int uintgA)
 
147
    { initObj(objUint); uintg = uintgA; 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, std::set<int> *fetchOriginatorNums = NULL);
 
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
  GBool isUint() { return type == objUint; }
 
181
 
 
182
  // Special type checking.
 
183
  GBool isName(char *nameA)
 
184
    { return type == objName && !strcmp(name, nameA); }
 
185
  GBool isDict(char *dictType);
 
186
  GBool isStream(char *dictType);
 
187
  GBool isCmd(char *cmdA)
 
188
    { return type == objCmd && !strcmp(cmd, cmdA); }
 
189
 
 
190
  // Accessors.
 
191
  GBool getBool() { OBJECT_TYPE_CHECK(objBool); return booln; }
 
192
  int getInt() { OBJECT_TYPE_CHECK(objInt); return intg; }
 
193
  double getReal() { OBJECT_TYPE_CHECK(objReal); return real; }
 
194
  double getNum() { OBJECT_2TYPES_CHECK(objInt, objReal); return type == objInt ? (double)intg : real; }
 
195
  GooString *getString() { OBJECT_TYPE_CHECK(objString); return string; }
 
196
  char *getName() { OBJECT_TYPE_CHECK(objName); return name; }
 
197
  Array *getArray() { OBJECT_TYPE_CHECK(objArray); return array; }
 
198
  Dict *getDict() { OBJECT_TYPE_CHECK(objDict); return dict; }
 
199
  Stream *getStream() { OBJECT_TYPE_CHECK(objStream); return stream; }
 
200
  Ref getRef() { OBJECT_TYPE_CHECK(objRef); return ref; }
 
201
  int getRefNum() { OBJECT_TYPE_CHECK(objRef); return ref.num; }
 
202
  int getRefGen() { OBJECT_TYPE_CHECK(objRef); return ref.gen; }
 
203
  char *getCmd() { OBJECT_TYPE_CHECK(objCmd); return cmd; }
 
204
  unsigned int getUint() { OBJECT_TYPE_CHECK(objUint); return uintg; }
 
205
 
 
206
  // Array accessors.
 
207
  int arrayGetLength();
 
208
  void arrayAdd(Object *elem);
 
209
  Object *arrayGet(int i, Object *obj);
 
210
  Object *arrayGetNF(int i, Object *obj);
 
211
 
 
212
  // Dict accessors.
 
213
  int dictGetLength();
 
214
  void dictAdd(char *key, Object *val);
 
215
  void dictSet(char *key, Object *val);
 
216
  GBool dictIs(char *dictType);
 
217
  Object *dictLookup(char *key, Object *obj, std::set<int> *fetchOriginatorNums = NULL);
 
218
  Object *dictLookupNF(char *key, Object *obj);
 
219
  char *dictGetKey(int i);
 
220
  Object *dictGetVal(int i, Object *obj);
 
221
  Object *dictGetValNF(int i, Object *obj);
 
222
 
 
223
  // Stream accessors.
 
224
  GBool streamIs(char *dictType);
 
225
  void streamReset();
 
226
  void streamClose();
 
227
  int streamGetChar();
 
228
  int streamGetChars(int nChars, Guchar *buffer);
 
229
  int streamLookChar();
 
230
  char *streamGetLine(char *buf, int size);
 
231
  Guint streamGetPos();
 
232
  void streamSetPos(Guint pos, int dir = 0);
 
233
  Dict *streamGetDict();
 
234
 
 
235
  // Output.
 
236
  char *getTypeName();
 
237
  void print(FILE *f = stdout);
 
238
 
 
239
  // Memory testing.
 
240
  static void memCheck(FILE *f);
 
241
 
 
242
private:
 
243
 
 
244
  ObjType type;                 // object type
 
245
  union {                       // value for each type:
 
246
    GBool booln;                //   boolean
 
247
    int intg;                   //   integer
 
248
    unsigned int uintg;         //   unsigned integer
 
249
    double real;                //   real
 
250
    GooString *string;          //   string
 
251
    char *name;                 //   name
 
252
    Array *array;               //   array
 
253
    Dict *dict;                 //   dictionary
 
254
    Stream *stream;             //   stream
 
255
    Ref ref;                    //   indirect reference
 
256
    char *cmd;                  //   command
 
257
  };
 
258
 
 
259
#ifdef DEBUG_MEM
 
260
  static int                    // number of each type of object
 
261
    numAlloc[numObjTypes];      //   currently allocated
 
262
#endif
 
263
};
 
264
 
 
265
//------------------------------------------------------------------------
 
266
// Array accessors.
 
267
//------------------------------------------------------------------------
 
268
 
 
269
#include "Array.h"
 
270
 
 
271
inline int Object::arrayGetLength()
 
272
  { OBJECT_TYPE_CHECK(objArray); return array->getLength(); }
 
273
 
 
274
inline void Object::arrayAdd(Object *elem)
 
275
  { OBJECT_TYPE_CHECK(objArray); array->add(elem); }
 
276
 
 
277
inline Object *Object::arrayGet(int i, Object *obj)
 
278
  { OBJECT_TYPE_CHECK(objArray); return array->get(i, obj); }
 
279
 
 
280
inline Object *Object::arrayGetNF(int i, Object *obj)
 
281
  { OBJECT_TYPE_CHECK(objArray); return array->getNF(i, obj); }
 
282
 
 
283
//------------------------------------------------------------------------
 
284
// Dict accessors.
 
285
//------------------------------------------------------------------------
 
286
 
 
287
#include "Dict.h"
 
288
 
 
289
inline int Object::dictGetLength()
 
290
  { OBJECT_TYPE_CHECK(objDict); return dict->getLength(); }
 
291
 
 
292
inline void Object::dictAdd(char *key, Object *val)
 
293
  { OBJECT_TYPE_CHECK(objDict); dict->add(key, val); }
 
294
 
 
295
inline void Object::dictSet(char *key, Object *val)
 
296
        { OBJECT_TYPE_CHECK(objDict); dict->set(key, val); }
 
297
 
 
298
inline GBool Object::dictIs(char *dictType)
 
299
  { OBJECT_TYPE_CHECK(objDict); return dict->is(dictType); }
 
300
 
 
301
inline GBool Object::isDict(char *dictType)
 
302
  { return type == objDict && dictIs(dictType); }
 
303
 
 
304
inline Object *Object::dictLookup(char *key, Object *obj, std::set<int> *fetchOriginatorNums)
 
305
  { OBJECT_TYPE_CHECK(objDict); return dict->lookup(key, obj, fetchOriginatorNums); }
 
306
 
 
307
inline Object *Object::dictLookupNF(char *key, Object *obj)
 
308
  { OBJECT_TYPE_CHECK(objDict); return dict->lookupNF(key, obj); }
 
309
 
 
310
inline char *Object::dictGetKey(int i)
 
311
  { OBJECT_TYPE_CHECK(objDict); return dict->getKey(i); }
 
312
 
 
313
inline Object *Object::dictGetVal(int i, Object *obj)
 
314
  { OBJECT_TYPE_CHECK(objDict); return dict->getVal(i, obj); }
 
315
 
 
316
inline Object *Object::dictGetValNF(int i, Object *obj)
 
317
  { OBJECT_TYPE_CHECK(objDict); return dict->getValNF(i, obj); }
 
318
 
 
319
//------------------------------------------------------------------------
 
320
// Stream accessors.
 
321
//------------------------------------------------------------------------
 
322
 
 
323
#include "Stream.h"
 
324
 
 
325
inline GBool Object::streamIs(char *dictType)
 
326
  { OBJECT_TYPE_CHECK(objStream); return stream->getDict()->is(dictType); }
 
327
 
 
328
inline GBool Object::isStream(char *dictType)
 
329
  { return type == objStream && streamIs(dictType); }
 
330
 
 
331
inline void Object::streamReset()
 
332
  { OBJECT_TYPE_CHECK(objStream); stream->reset(); }
 
333
 
 
334
inline void Object::streamClose()
 
335
  { OBJECT_TYPE_CHECK(objStream); stream->close(); }
 
336
 
 
337
inline int Object::streamGetChar()
 
338
  { OBJECT_TYPE_CHECK(objStream); return stream->getChar(); }
 
339
 
 
340
inline int Object::streamGetChars(int nChars, Guchar *buffer)
 
341
  { OBJECT_TYPE_CHECK(objStream); return stream->doGetChars(nChars, buffer); }
 
342
 
 
343
inline int Object::streamLookChar()
 
344
  { OBJECT_TYPE_CHECK(objStream); return stream->lookChar(); }
 
345
 
 
346
inline char *Object::streamGetLine(char *buf, int size)
 
347
  { OBJECT_TYPE_CHECK(objStream); return stream->getLine(buf, size); }
 
348
 
 
349
inline Guint Object::streamGetPos()
 
350
  { OBJECT_TYPE_CHECK(objStream); return stream->getPos(); }
 
351
 
 
352
inline void Object::streamSetPos(Guint pos, int dir)
 
353
  { OBJECT_TYPE_CHECK(objStream); stream->setPos(pos, dir); }
 
354
 
 
355
inline Dict *Object::streamGetDict()
 
356
  { OBJECT_TYPE_CHECK(objStream); return stream->getDict(); }
 
357
 
 
358
#endif