Back to index

tetex-bin  3.0
Object.cc
Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // Object.cc
00004 //
00005 // Copyright 1996-2003 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #include <aconf.h>
00010 
00011 #ifdef USE_GCC_PRAGMAS
00012 #pragma implementation
00013 #endif
00014 
00015 #include <stddef.h>
00016 #include "Object.h"
00017 #include "Array.h"
00018 #include "Dict.h"
00019 #include "Error.h"
00020 #include "Stream.h"
00021 #include "XRef.h"
00022 
00023 //------------------------------------------------------------------------
00024 // Object
00025 //------------------------------------------------------------------------
00026 
00027 char *objTypeNames[numObjTypes] = {
00028   "boolean",
00029   "integer",
00030   "real",
00031   "string",
00032   "name",
00033   "null",
00034   "array",
00035   "dictionary",
00036   "stream",
00037   "ref",
00038   "cmd",
00039   "error",
00040   "eof",
00041   "none"
00042 };
00043 
00044 #ifdef DEBUG_MEM
00045 int Object::numAlloc[numObjTypes] =
00046   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
00047 #endif
00048 
00049 Object *Object::initArray(XRef *xref) {
00050   initObj(objArray);
00051   array = new Array(xref);
00052   return this;
00053 }
00054 
00055 Object *Object::initDict(XRef *xref) {
00056   initObj(objDict);
00057   dict = new Dict(xref);
00058   return this;
00059 }
00060 
00061 Object *Object::initDict(Dict *dictA) {
00062   initObj(objDict);
00063   dict = dictA;
00064   dict->incRef();
00065   return this;
00066 }
00067 
00068 Object *Object::initStream(Stream *streamA) {
00069   initObj(objStream);
00070   stream = streamA;
00071   return this;
00072 }
00073 
00074 Object *Object::copy(Object *obj) {
00075   *obj = *this;
00076   switch (type) {
00077   case objString:
00078     obj->string = string->copy();
00079     break;
00080   case objName:
00081     obj->name = copyString(name);
00082     break;
00083   case objArray:
00084     array->incRef();
00085     break;
00086   case objDict:
00087     dict->incRef();
00088     break;
00089   case objStream:
00090     stream->incRef();
00091     break;
00092   case objCmd:
00093     obj->cmd = copyString(cmd);
00094     break;
00095   default:
00096     break;
00097   }
00098 #ifdef DEBUG_MEM
00099   ++numAlloc[type];
00100 #endif
00101   return obj;
00102 }
00103 
00104 Object *Object::fetch(XRef *xref, Object *obj) {
00105   return (type == objRef && xref) ?
00106          xref->fetch(ref.num, ref.gen, obj) : copy(obj);
00107 }
00108 
00109 void Object::free() {
00110   switch (type) {
00111   case objString:
00112     delete string;
00113     break;
00114   case objName:
00115     gfree(name);
00116     break;
00117   case objArray:
00118     if (!array->decRef()) {
00119       delete array;
00120     }
00121     break;
00122   case objDict:
00123     if (!dict->decRef()) {
00124       delete dict;
00125     }
00126     break;
00127   case objStream:
00128     if (!stream->decRef()) {
00129       delete stream;
00130     }
00131     break;
00132   case objCmd:
00133     gfree(cmd);
00134     break;
00135   default:
00136     break;
00137   }
00138 #ifdef DEBUG_MEM
00139   --numAlloc[type];
00140 #endif
00141   type = objNone;
00142 }
00143 
00144 char *Object::getTypeName() {
00145   return objTypeNames[type];
00146 }
00147 
00148 void Object::print(FILE *f) {
00149   Object obj;
00150   int i;
00151 
00152   switch (type) {
00153   case objBool:
00154     fprintf(f, "%s", booln ? "true" : "false");
00155     break;
00156   case objInt:
00157     fprintf(f, "%d", intg);
00158     break;
00159   case objReal:
00160     fprintf(f, "%g", real);
00161     break;
00162   case objString:
00163     fprintf(f, "(");
00164     fwrite(string->getCString(), 1, string->getLength(), stdout);
00165     fprintf(f, ")");
00166     break;
00167   case objName:
00168     fprintf(f, "/%s", name);
00169     break;
00170   case objNull:
00171     fprintf(f, "null");
00172     break;
00173   case objArray:
00174     fprintf(f, "[");
00175     for (i = 0; i < arrayGetLength(); ++i) {
00176       if (i > 0)
00177        fprintf(f, " ");
00178       arrayGetNF(i, &obj);
00179       obj.print(f);
00180       obj.free();
00181     }
00182     fprintf(f, "]");
00183     break;
00184   case objDict:
00185     fprintf(f, "<<");
00186     for (i = 0; i < dictGetLength(); ++i) {
00187       fprintf(f, " /%s ", dictGetKey(i));
00188       dictGetValNF(i, &obj);
00189       obj.print(f);
00190       obj.free();
00191     }
00192     fprintf(f, " >>");
00193     break;
00194   case objStream:
00195     fprintf(f, "<stream>");
00196     break;
00197   case objRef:
00198     fprintf(f, "%d %d R", ref.num, ref.gen);
00199     break;
00200   case objCmd:
00201     fprintf(f, "%s", cmd);
00202     break;
00203   case objError:
00204     fprintf(f, "<error>");
00205     break;
00206   case objEOF:
00207     fprintf(f, "<EOF>");
00208     break;
00209   case objNone:
00210     fprintf(f, "<none>");
00211     break;
00212   }
00213 }
00214 
00215 void Object::memCheck(FILE *f) {
00216 #ifdef DEBUG_MEM
00217   int i;
00218   int t;
00219 
00220   t = 0;
00221   for (i = 0; i < numObjTypes; ++i)
00222     t += numAlloc[i];
00223   if (t > 0) {
00224     fprintf(f, "Allocated objects:\n");
00225     for (i = 0; i < numObjTypes; ++i) {
00226       if (numAlloc[i] > 0)
00227        fprintf(f, "  %-20s: %6d\n", objTypeNames[i], numAlloc[i]);
00228     }
00229   }
00230 #endif
00231 }