home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 720 / PDF090B4-SorceCode / pdf / Object.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  5.0 KB  |  270 lines

  1. //========================================================================
  2. //
  3. // Object.cc
  4. //
  5. // Copyright 1996 Derek B. Noonburg
  6. //
  7. //========================================================================
  8. //
  9. // Ported to EPOC by Sander van der Wal
  10. //
  11. // $Log: Object.cpp $
  12. // Revision 1.3  2000-09-25 20:59:04+02  svdwal
  13. // Profile macro's removed
  14. //
  15. // Revision 1.2  2000-09-17 13:38:22+02  svdwal
  16. // Ported
  17. //
  18.  
  19. #ifdef __GNUC__
  20. #pragma implementation
  21. #endif
  22.  
  23. #include "Object.h"
  24. #include "Array.h"
  25. #include "Dict.h"
  26. #include "Error.h"
  27. #include "Stream.h"
  28. #include "XRef.h"
  29.  
  30. #include "PDFGlobal.h"
  31. #include "PROFILE.h"
  32.  
  33. //------------------------------------------------------------------------
  34. // Object
  35. //------------------------------------------------------------------------
  36.  
  37. static const char* const objTypeNames[numObjTypes] = {
  38.   "boolean",
  39.   "integer",
  40.   "real",
  41.   "string",
  42.   "name",
  43.   "null",
  44.   "array",
  45.   "dictionary",
  46.   "stream",
  47.   "ref",
  48.   "cmd",
  49.   "error",
  50.   "eof",
  51.   "none"
  52. };
  53.  
  54. #ifdef DEBUG_MEM
  55. int Object::numAlloc[numObjTypes] =
  56.   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  57. #endif
  58.   
  59. Object *Object::initNameL(char *name1) { 
  60.   initObj(objName); 
  61.   name = 0; 
  62.   name = copyStringL(name1); 
  63.   return this;
  64. }
  65.  
  66. Object *Object::initArrayL() {
  67.   initObj(objArray);
  68.   array = 0;
  69.   array = new(ELeave) Array();
  70.   return this;
  71. }
  72.  
  73. Object *Object::initDictL() {
  74.   initObj(objDict);
  75.   dict = 0;
  76.   dict = new(ELeave) Dict();
  77.   return this;
  78. }
  79.  
  80. Object *Object::initStream(Stream *stream1) {
  81.   initObj(objStream);
  82.   stream = stream1;
  83.   return this;
  84. }
  85.  
  86. Object *Object::initCmdL(char *cmd1) { 
  87.   initObj(objCmd); 
  88.   cmd = 0;
  89.   cmd = copyStringL(cmd1); 
  90.   return this;
  91. }
  92.  
  93. Object *Object::copyL(Object *obj) {
  94.   *obj = *this;
  95.   switch (type) {
  96.   case objString:
  97.     obj->string = 0;
  98.     obj->string = string->copyL();
  99.     break;
  100.   case objName:
  101.     obj->name = 0;
  102.     obj->name = copyStringL(name);
  103.     break;
  104.   case objArray:
  105.     array->incRef();
  106.     break;
  107.   case objDict:
  108.     dict->incRef();
  109.     break;
  110.   case objStream:
  111.     stream->incRef();
  112.     break;
  113.   case objCmd:
  114.     obj->cmd = 0;
  115.     obj->cmd = copyStringL(cmd);
  116.     break;
  117.   default:
  118.     break;
  119.   }
  120. #ifdef DEBUG_MEM
  121.   ++numAlloc[type];
  122. #endif
  123.   return obj;
  124. }
  125.  
  126. Object *Object::fetchL(Object *obj) {
  127.   if (type != objRef)
  128.     return copyL(obj);
  129.   XRef* xref = Global().XRef.xref;
  130.   return (xref) ?
  131.          xref->fetchL(ref.num, ref.gen, obj) : copyL(obj);
  132. }
  133.  
  134. void Object::free() {
  135.   switch (type) {
  136.   case objString:
  137.     delete string;
  138.     string=0;
  139.     break;
  140.   case objName:
  141.     User::Free(name);
  142.     name=0;
  143.     break;
  144.   case objArray:
  145.     if (array && !array->decRef()) {
  146.       delete array;
  147.       array = 0;
  148.     }
  149.     break;
  150.   case objDict:
  151.     if (dict && !dict->decRef()) {
  152.       delete dict;
  153.       dict = 0;
  154.     }
  155.     break;
  156.   case objStream:
  157.     if (stream && !stream->decRef()) {
  158.       delete stream;
  159.       stream = 0;
  160.     }
  161.     break;
  162.   case objCmd:
  163.     User::Free(cmd);
  164.     cmd = 0;
  165.     break;
  166.   default:
  167.     break;
  168.   }
  169. #ifdef DEBUG_MEM
  170.   --numAlloc[type];
  171. #endif
  172.   type = objNone;
  173. }
  174.  
  175. char *Object::getTypeName() {
  176.   return (char*) objTypeNames[type];
  177. }
  178.  
  179.  
  180. #ifndef __SYMBIAN32__
  181. void Object::print(FILE *f) {
  182.   Object obj;
  183.   int i;
  184.  
  185.   switch (type) {
  186.   case objBool:
  187.     fprintf(f, "%s", booln ? "true" : "false");
  188.     break;
  189.   case objInt:
  190.     fprintf(f, "%d", intg);
  191.     break;
  192.   case objReal:
  193.     fprintf(f, "%g", real);
  194.     break;
  195.   case objString:
  196.     fprintf(f, "(%s)", string->getCString());
  197.     break;
  198.   case objName:
  199.     fprintf(f, "/%s", name);
  200.     break;
  201.   case objNull:
  202.     fprintf(f, "null");
  203.     break;
  204.   case objArray:
  205.     fprintf(f, "[");
  206.     for (i = 0; i < arrayGetLength(); ++i) {
  207.       if (i > 0)
  208.     fprintf(f, " ");
  209.       arrayGetNFL(i, &obj);
  210.       obj.print(f);
  211.       obj.free();
  212.     }
  213.     fprintf(f, "]");
  214.     break;
  215.   case objDict:
  216.     fprintf(f, "<<");
  217.     for (i = 0; i < dictGetLength(); ++i) {
  218.       fprintf(f, " /%s ", dictGetKey(i));
  219.       dictGetValNF(i, &obj);
  220.       obj.print(f);
  221.       obj.free();
  222.     }
  223.     fprintf(f, " >>");
  224.     break;
  225.   case objStream:
  226.     fprintf(f, "<stream>");
  227.     break;
  228.   case objRef:
  229.     fprintf(f, "%d %d R", ref.num, ref.gen);
  230.     break;
  231.   case objCmd:
  232.     fprintf(f, "%s", cmd);
  233.     break;
  234.   case objError:
  235.     fprintf(f, "<error>");
  236.     break;
  237.   case objEOF:
  238.     fprintf(f, "<EOF>");
  239.     break;
  240.   case objNone:
  241.     fprintf(f, "<none>");
  242.     break;
  243.   }
  244. }
  245.  
  246.  
  247. void Object::memCheck(FILE *f) {
  248. #ifdef DEBUG_MEM
  249.   int i;
  250.   int t;
  251.  
  252.   t = 0;
  253.   for (i = 0; i < numObjTypes; ++i)
  254.     t += numAlloc[i];
  255.   if (t > 0) {
  256.     fprintf(f, "Allocated objects:\n");
  257.     for (i = 0; i < numObjTypes; ++i) {
  258.       if (numAlloc[i] > 0)
  259.     fprintf(f, "  %-20s: %6d\n", objTypeNames[i], numAlloc[i]);
  260.     }
  261.   }
  262. #endif
  263. }
  264. #endif
  265.  
  266. void Object::Cleanup(TAny* aPtr)
  267. {
  268.   ((Object*) aPtr)->free();
  269. }
  270.