home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 110 / EnigmaAmiga110CD.iso / indispensabili / utility / apdf / xpdf-0.80 / xpdf / object.h < prev    next >
C/C++ Source or Header  |  1999-06-20  |  9KB  |  301 lines

  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 "mystdio.h"
  19. #include "gtypes.h"
  20. #include "gmem.h"
  21. #include "GString.h"
  22.  
  23. class Array;
  24. class Dict;
  25. class Stream;
  26.  
  27. //------------------------------------------------------------------------
  28. // Ref
  29. //------------------------------------------------------------------------
  30.  
  31. struct Ref {
  32.   int num;                      // object number
  33.   int gen;                      // generation number
  34. };
  35.  
  36. //------------------------------------------------------------------------
  37. // object types
  38. //------------------------------------------------------------------------
  39.  
  40. enum ObjType {
  41.   // simple objects
  42.   objBool,                      // boolean
  43.   objInt,                       // integer
  44.   objReal,                      // real
  45.   objString,                    // string
  46.   objName,                      // name
  47.   objNull,                      // null
  48.  
  49.   // complex objects
  50.   objArray,                     // array
  51.   objDict,                      // dictionary
  52.   objStream,                    // stream
  53.   objRef,                       // indirect reference
  54.  
  55.   // special objects
  56.   objCmd,                       // command name
  57.   objError,                     // error return from Lexer
  58.   objEOF,                       // end of file return from Lexer
  59.   objNone                       // uninitialized object
  60. };
  61.  
  62. #define numObjTypes 14          // total number of object types
  63.  
  64. //------------------------------------------------------------------------
  65. // Object
  66. //------------------------------------------------------------------------
  67.  
  68. #ifdef DEBUG_MEM
  69. #define initObj(t) ++numAlloc[type = t]
  70. #else
  71. #define initObj(t) type = t
  72. #endif
  73.  
  74. class Object {
  75. public:
  76.  
  77.   // Default constructor.
  78.   Object():
  79.     type(objNone) {}
  80.  
  81.   // Initialize an object.
  82.   Object *initBool(GBool booln1)
  83.     { initObj(objBool); booln = booln1; return this; }
  84.   Object *initInt(int intg1)
  85.     { initObj(objInt); intg = intg1; return this; }
  86.   Object *initReal(double real1)
  87.     { initObj(objReal); real = real1; return this; }
  88.   Object *initString(GString *string1)
  89.     { initObj(objString); string = string1; return this; }
  90.   Object *initName(char *name1)
  91.     { initObj(objName); name = copyString(name1); return this; }
  92.   Object *initNull()
  93.     { initObj(objNull); return this; }
  94.   Object *initArray();
  95.   Object *initDict();
  96.   Object *initDict(Dict *dict1)
  97.     { initObj(objDict); dict = dict1; return this; }
  98.   Object *initStream(Stream *stream1);
  99.   Object *initRef(int num1, int gen1)
  100.     { initObj(objRef); ref.num = num1; ref.gen = gen1; return this; }
  101.   Object *initCmd(char *cmd1)
  102.     { initObj(objCmd); cmd = copyString(cmd1); return this; }
  103.   Object *initError()
  104.     { initObj(objError); return this; }
  105.   Object *initEOF()
  106.     { initObj(objEOF); return this; }
  107.  
  108.   // Copy an object.
  109.   Object *copy(Object *obj);
  110.  
  111.   // If object is a Ref, fetch and return the referenced object.
  112.   // Otherwise, return a copy of the object.
  113.   Object *fetch(Object *obj);
  114.  
  115.   // Free object contents.
  116.   void free();
  117.  
  118.   // Type checking.
  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.   int streamGetChar();
  177.   int streamLookChar();
  178.   char *streamGetLine(char *buf, int size);
  179.   int streamGetPos();
  180.   void streamSetPos(int pos);
  181.   myFILE *streamGetFile();
  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 int Object::streamGetChar()
  280.   { return stream->getChar(); }
  281.  
  282. inline int Object::streamLookChar()
  283.   { return stream->lookChar(); }
  284.  
  285. inline char *Object::streamGetLine(char *buf, int size)
  286.   { return stream->getLine(buf, size); }
  287.  
  288. inline int Object::streamGetPos()
  289.   { return stream->getPos(); }
  290.  
  291. inline void Object::streamSetPos(int pos)
  292.   { stream->setPos(pos); }
  293.  
  294. inline myFILE *Object::streamGetFile()
  295.   { return stream->getFile(); }
  296.  
  297. inline Dict *Object::streamGetDict()
  298.   { return stream->getDict(); }
  299.  
  300. #endif
  301.