home *** CD-ROM | disk | FTP | other *** search
- //========================================================================
- //
- // Object.cc
- //
- // Copyright 1996 Derek B. Noonburg
- //
- //========================================================================
- //
- // Ported to EPOC by Sander van der Wal
- //
- // $Log: Object.cpp $
- // Revision 1.3 2000-09-25 20:59:04+02 svdwal
- // Profile macro's removed
- //
- // Revision 1.2 2000-09-17 13:38:22+02 svdwal
- // Ported
- //
-
- #ifdef __GNUC__
- #pragma implementation
- #endif
-
- #include "Object.h"
- #include "Array.h"
- #include "Dict.h"
- #include "Error.h"
- #include "Stream.h"
- #include "XRef.h"
-
- #include "PDFGlobal.h"
- #include "PROFILE.h"
-
- //------------------------------------------------------------------------
- // Object
- //------------------------------------------------------------------------
-
- static const char* const objTypeNames[numObjTypes] = {
- "boolean",
- "integer",
- "real",
- "string",
- "name",
- "null",
- "array",
- "dictionary",
- "stream",
- "ref",
- "cmd",
- "error",
- "eof",
- "none"
- };
-
- #ifdef DEBUG_MEM
- int Object::numAlloc[numObjTypes] =
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- #endif
-
- Object *Object::initNameL(char *name1) {
- initObj(objName);
- name = 0;
- name = copyStringL(name1);
- return this;
- }
-
- Object *Object::initArrayL() {
- initObj(objArray);
- array = 0;
- array = new(ELeave) Array();
- return this;
- }
-
- Object *Object::initDictL() {
- initObj(objDict);
- dict = 0;
- dict = new(ELeave) Dict();
- return this;
- }
-
- Object *Object::initStream(Stream *stream1) {
- initObj(objStream);
- stream = stream1;
- return this;
- }
-
- Object *Object::initCmdL(char *cmd1) {
- initObj(objCmd);
- cmd = 0;
- cmd = copyStringL(cmd1);
- return this;
- }
-
- Object *Object::copyL(Object *obj) {
- *obj = *this;
- switch (type) {
- case objString:
- obj->string = 0;
- obj->string = string->copyL();
- break;
- case objName:
- obj->name = 0;
- obj->name = copyStringL(name);
- break;
- case objArray:
- array->incRef();
- break;
- case objDict:
- dict->incRef();
- break;
- case objStream:
- stream->incRef();
- break;
- case objCmd:
- obj->cmd = 0;
- obj->cmd = copyStringL(cmd);
- break;
- default:
- break;
- }
- #ifdef DEBUG_MEM
- ++numAlloc[type];
- #endif
- return obj;
- }
-
- Object *Object::fetchL(Object *obj) {
- if (type != objRef)
- return copyL(obj);
- XRef* xref = Global().XRef.xref;
- return (xref) ?
- xref->fetchL(ref.num, ref.gen, obj) : copyL(obj);
- }
-
- void Object::free() {
- switch (type) {
- case objString:
- delete string;
- string=0;
- break;
- case objName:
- User::Free(name);
- name=0;
- break;
- case objArray:
- if (array && !array->decRef()) {
- delete array;
- array = 0;
- }
- break;
- case objDict:
- if (dict && !dict->decRef()) {
- delete dict;
- dict = 0;
- }
- break;
- case objStream:
- if (stream && !stream->decRef()) {
- delete stream;
- stream = 0;
- }
- break;
- case objCmd:
- User::Free(cmd);
- cmd = 0;
- break;
- default:
- break;
- }
- #ifdef DEBUG_MEM
- --numAlloc[type];
- #endif
- type = objNone;
- }
-
- char *Object::getTypeName() {
- return (char*) objTypeNames[type];
- }
-
-
- #ifndef __SYMBIAN32__
- void Object::print(FILE *f) {
- Object obj;
- int i;
-
- switch (type) {
- case objBool:
- fprintf(f, "%s", booln ? "true" : "false");
- break;
- case objInt:
- fprintf(f, "%d", intg);
- break;
- case objReal:
- fprintf(f, "%g", real);
- break;
- case objString:
- fprintf(f, "(%s)", string->getCString());
- break;
- case objName:
- fprintf(f, "/%s", name);
- break;
- case objNull:
- fprintf(f, "null");
- break;
- case objArray:
- fprintf(f, "[");
- for (i = 0; i < arrayGetLength(); ++i) {
- if (i > 0)
- fprintf(f, " ");
- arrayGetNFL(i, &obj);
- obj.print(f);
- obj.free();
- }
- fprintf(f, "]");
- break;
- case objDict:
- fprintf(f, "<<");
- for (i = 0; i < dictGetLength(); ++i) {
- fprintf(f, " /%s ", dictGetKey(i));
- dictGetValNF(i, &obj);
- obj.print(f);
- obj.free();
- }
- fprintf(f, " >>");
- break;
- case objStream:
- fprintf(f, "<stream>");
- break;
- case objRef:
- fprintf(f, "%d %d R", ref.num, ref.gen);
- break;
- case objCmd:
- fprintf(f, "%s", cmd);
- break;
- case objError:
- fprintf(f, "<error>");
- break;
- case objEOF:
- fprintf(f, "<EOF>");
- break;
- case objNone:
- fprintf(f, "<none>");
- break;
- }
- }
-
-
- void Object::memCheck(FILE *f) {
- #ifdef DEBUG_MEM
- int i;
- int t;
-
- t = 0;
- for (i = 0; i < numObjTypes; ++i)
- t += numAlloc[i];
- if (t > 0) {
- fprintf(f, "Allocated objects:\n");
- for (i = 0; i < numObjTypes; ++i) {
- if (numAlloc[i] > 0)
- fprintf(f, " %-20s: %6d\n", objTypeNames[i], numAlloc[i]);
- }
- }
- #endif
- }
- #endif
-
- void Object::Cleanup(TAny* aPtr)
- {
- ((Object*) aPtr)->free();
- }
-