00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef OBJECT_H
00010 #define OBJECT_H
00011
00012 #include <aconf.h>
00013
00014 #ifdef USE_GCC_PRAGMAS
00015 #pragma interface
00016 #endif
00017
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include "gtypes.h"
00021 #include "gmem.h"
00022 #include "GString.h"
00023
00024 class XRef;
00025 class Array;
00026 class Dict;
00027 class Stream;
00028
00029
00030
00031
00032
00033 struct Ref {
00034 int num;
00035 int gen;
00036 };
00037
00038
00039
00040
00041
00042 enum ObjType {
00043
00044 objBool,
00045 objInt,
00046 objReal,
00047 objString,
00048 objName,
00049 objNull,
00050
00051
00052 objArray,
00053 objDict,
00054 objStream,
00055 objRef,
00056
00057
00058 objCmd,
00059 objError,
00060 objEOF,
00061 objNone
00062 };
00063
00064 #define numObjTypes 14 // total number of object types
00065
00066
00067
00068
00069
00070 #ifdef DEBUG_MEM
00071 #define initObj(t) ++numAlloc[type = t]
00072 #else
00073 #define initObj(t) type = t
00074 #endif
00075
00076 class Object {
00077 public:
00078
00079
00080 Object():
00081 type(objNone) {}
00082
00083
00084 Object *initBool(GBool boolnA)
00085 { initObj(objBool); booln = boolnA; return this; }
00086 Object *initInt(int intgA)
00087 { initObj(objInt); intg = intgA; return this; }
00088 Object *initReal(double realA)
00089 { initObj(objReal); real = realA; return this; }
00090 Object *initString(GString *stringA)
00091 { initObj(objString); string = stringA; return this; }
00092 Object *initName(const char *nameA)
00093 { initObj(objName); name = copyString(nameA); return this; }
00094 Object *initNull()
00095 { initObj(objNull); return this; }
00096 Object *initArray(XRef *xref);
00097 Object *initDict(XRef *xref);
00098 Object *initStream(Stream *streamA);
00099 Object *initRef(int numA, int genA)
00100 { initObj(objRef); ref.num = numA; ref.gen = genA; return this; }
00101 Object *initCmd(const char *cmdA)
00102 { initObj(objCmd); cmd = copyString(cmdA); return this; }
00103 Object *initError()
00104 { initObj(objError); return this; }
00105 Object *initEOF()
00106 { initObj(objEOF); return this; }
00107
00108
00109 Object *copy(Object *obj);
00110
00111
00112
00113 Object *fetch(XRef *xref, Object *obj);
00114
00115
00116 void free();
00117
00118
00119 ObjType getType() { return type; }
00120 GBool isBool() { return type == objBool; }
00121 GBool isInt() { return type == objInt; }
00122 GBool isReal() { return type == objReal; }
00123 GBool isNum() { return type == objInt || type == objReal; }
00124 GBool isString() { return type == objString; }
00125 GBool isName() { return type == objName; }
00126 GBool isNull() { return type == objNull; }
00127 GBool isArray() { return type == objArray; }
00128 GBool isDict() { return type == objDict; }
00129 GBool isStream() { return type == objStream; }
00130 GBool isRef() { return type == objRef; }
00131 GBool isCmd() { return type == objCmd; }
00132 GBool isError() { return type == objError; }
00133 GBool isEOF() { return type == objEOF; }
00134 GBool isNone() { return type == objNone; }
00135
00136
00137 GBool isName(const char *nameA)
00138 { return type == objName && !strcmp(name, nameA); }
00139 GBool isDict(const char *dictType);
00140 GBool isStream(const char *dictType);
00141 GBool isCmd(const char *cmdA)
00142 { return type == objCmd && !strcmp(cmd, cmdA); }
00143
00144
00145 GBool getBool() { return booln; }
00146 int getInt() { return intg; }
00147 double getReal() { return real; }
00148 double getNum() { return type == objInt ? (double)intg : real; }
00149 GString *getString() { return string; }
00150 char *getName() { return name; }
00151 Array *getArray() { return array; }
00152 Dict *getDict() { return dict; }
00153 Stream *getStream() { return stream; }
00154 Ref getRef() { return ref; }
00155 int getRefNum() { return ref.num; }
00156 int getRefGen() { return ref.gen; }
00157 const char *getCmd() { return cmd; }
00158
00159
00160 int arrayGetLength();
00161 void arrayAdd(Object *elem);
00162 Object *arrayGet(int i, Object *obj);
00163 Object *arrayGetNF(int i, Object *obj);
00164
00165
00166 int dictGetLength();
00167 void dictAdd(char *key, Object *val);
00168 GBool dictIs(const char *dictType);
00169 Object *dictLookup(const char *key, Object *obj);
00170 Object *dictLookupNF(const char *key, Object *obj);
00171 char *dictGetKey(int i);
00172 Object *dictGetVal(int i, Object *obj);
00173 Object *dictGetValNF(int i, Object *obj);
00174
00175
00176 GBool streamIs(const char *dictType);
00177 void streamReset();
00178 void streamClose();
00179 int streamGetChar();
00180 int streamLookChar();
00181 char *streamGetLine(char *buf, int size);
00182 Guint streamGetPos();
00183 void streamSetPos(Guint pos, int dir = 0);
00184 Dict *streamGetDict();
00185
00186
00187 const char *getTypeName();
00188 void print(FILE *f = stdout);
00189
00190
00191 static void memCheck(FILE *f);
00192
00193 private:
00194
00195 ObjType type;
00196 union {
00197 GBool booln;
00198 int intg;
00199 double real;
00200 GString *string;
00201 char *name;
00202 Array *array;
00203 Dict *dict;
00204 Stream *stream;
00205 Ref ref;
00206 char *cmd;
00207 };
00208
00209 #ifdef DEBUG_MEM
00210 static int
00211 numAlloc[numObjTypes];
00212 #endif
00213 };
00214
00215
00216
00217
00218
00219 #include "Array.h"
00220
00221 inline int Object::arrayGetLength()
00222 { return array->getLength(); }
00223
00224 inline void Object::arrayAdd(Object *elem)
00225 { array->add(elem); }
00226
00227 inline Object *Object::arrayGet(int i, Object *obj)
00228 { return array->get(i, obj); }
00229
00230 inline Object *Object::arrayGetNF(int i, Object *obj)
00231 { return array->getNF(i, obj); }
00232
00233
00234
00235
00236
00237 #include "Dict.h"
00238
00239 inline int Object::dictGetLength()
00240 { return dict->getLength(); }
00241
00242 inline void Object::dictAdd(char *key, Object *val)
00243 { dict->add(key, val); }
00244
00245 inline GBool Object::dictIs(const char *dictType)
00246 { return dict->is(dictType); }
00247
00248 inline GBool Object::isDict(const char *dictType)
00249 { return type == objDict && dictIs(dictType); }
00250
00251 inline Object *Object::dictLookup(const char *key, Object *obj)
00252 { return dict->lookup(key, obj); }
00253
00254 inline Object *Object::dictLookupNF(const char *key, Object *obj)
00255 { return dict->lookupNF(key, obj); }
00256
00257 inline char *Object::dictGetKey(int i)
00258 { return dict->getKey(i); }
00259
00260 inline Object *Object::dictGetVal(int i, Object *obj)
00261 { return dict->getVal(i, obj); }
00262
00263 inline Object *Object::dictGetValNF(int i, Object *obj)
00264 { return dict->getValNF(i, obj); }
00265
00266
00267
00268
00269
00270 #include "Stream.h"
00271
00272 inline GBool Object::streamIs(const char *dictType)
00273 { return stream->getDict()->is(dictType); }
00274
00275 inline GBool Object::isStream(const char *dictType)
00276 { return type == objStream && streamIs(dictType); }
00277
00278 inline void Object::streamReset()
00279 { stream->reset(); }
00280
00281 inline void Object::streamClose()
00282 { stream->close(); }
00283
00284 inline int Object::streamGetChar()
00285 { return stream->getChar(); }
00286
00287 inline int Object::streamLookChar()
00288 { return stream->lookChar(); }
00289
00290 inline char *Object::streamGetLine(char *buf, int size)
00291 { return stream->getLine(buf, size); }
00292
00293 inline Guint Object::streamGetPos()
00294 { return stream->getPos(); }
00295
00296 inline void Object::streamSetPos(Guint pos, int dir)
00297 { stream->setPos(pos, dir); }
00298
00299 inline Dict *Object::streamGetDict()
00300 { return stream->getDict(); }
00301
00302 #endif