upgrade to xpdf 3.00.
[swftools.git] / pdf2swf / xpdf / Object.cc
blobddd6da693cd771902347191b52efff2197366b76
1 //========================================================================
2 //
3 // Object.cc
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
9 #include <aconf.h>
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
13 #endif
15 #include <stddef.h>
16 #include "Object.h"
17 #include "Array.h"
18 #include "Dict.h"
19 #include "Error.h"
20 #include "Stream.h"
21 #include "XRef.h"
23 //------------------------------------------------------------------------
24 // Object
25 //------------------------------------------------------------------------
27 char *objTypeNames[numObjTypes] = {
28 "boolean",
29 "integer",
30 "real",
31 "string",
32 "name",
33 "null",
34 "array",
35 "dictionary",
36 "stream",
37 "ref",
38 "cmd",
39 "error",
40 "eof",
41 "none"
44 #ifdef DEBUG_MEM
45 int Object::numAlloc[numObjTypes] =
46 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
47 #endif
49 Object *Object::initArray(XRef *xref) {
50 initObj(objArray);
51 array = new Array(xref);
52 return this;
55 Object *Object::initDict(XRef *xref) {
56 initObj(objDict);
57 dict = new Dict(xref);
58 return this;
61 Object *Object::initDict(Dict *dictA) {
62 initObj(objDict);
63 dict = dictA;
64 dict->incRef();
65 return this;
68 Object *Object::initStream(Stream *streamA) {
69 initObj(objStream);
70 stream = streamA;
71 return this;
74 Object *Object::copy(Object *obj) {
75 *obj = *this;
76 switch (type) {
77 case objString:
78 obj->string = string->copy();
79 break;
80 case objName:
81 obj->name = copyString(name);
82 break;
83 case objArray:
84 array->incRef();
85 break;
86 case objDict:
87 dict->incRef();
88 break;
89 case objStream:
90 stream->incRef();
91 break;
92 case objCmd:
93 obj->cmd = copyString(cmd);
94 break;
95 default:
96 break;
98 #ifdef DEBUG_MEM
99 ++numAlloc[type];
100 #endif
101 return obj;
104 Object *Object::fetch(XRef *xref, Object *obj) {
105 return (type == objRef && xref) ?
106 xref->fetch(ref.num, ref.gen, obj) : copy(obj);
109 void Object::free() {
110 switch (type) {
111 case objString:
112 delete string;
113 break;
114 case objName:
115 gfree(name);
116 break;
117 case objArray:
118 if (!array->decRef()) {
119 delete array;
121 break;
122 case objDict:
123 if (!dict->decRef()) {
124 delete dict;
126 break;
127 case objStream:
128 if (!stream->decRef()) {
129 delete stream;
131 break;
132 case objCmd:
133 gfree(cmd);
134 break;
135 default:
136 break;
138 #ifdef DEBUG_MEM
139 --numAlloc[type];
140 #endif
141 type = objNone;
144 char *Object::getTypeName() {
145 return objTypeNames[type];
148 void Object::print(FILE *f) {
149 Object obj;
150 int i;
152 switch (type) {
153 case objBool:
154 fprintf(f, "%s", booln ? "true" : "false");
155 break;
156 case objInt:
157 fprintf(f, "%d", intg);
158 break;
159 case objReal:
160 fprintf(f, "%g", real);
161 break;
162 case objString:
163 fprintf(f, "(");
164 fwrite(string->getCString(), 1, string->getLength(), stdout);
165 fprintf(f, ")");
166 break;
167 case objName:
168 fprintf(f, "/%s", name);
169 break;
170 case objNull:
171 fprintf(f, "null");
172 break;
173 case objArray:
174 fprintf(f, "[");
175 for (i = 0; i < arrayGetLength(); ++i) {
176 if (i > 0)
177 fprintf(f, " ");
178 arrayGetNF(i, &obj);
179 obj.print(f);
180 obj.free();
182 fprintf(f, "]");
183 break;
184 case objDict:
185 fprintf(f, "<<");
186 for (i = 0; i < dictGetLength(); ++i) {
187 fprintf(f, " /%s ", dictGetKey(i));
188 dictGetValNF(i, &obj);
189 obj.print(f);
190 obj.free();
192 fprintf(f, " >>");
193 break;
194 case objStream:
195 fprintf(f, "<stream>");
196 break;
197 case objRef:
198 fprintf(f, "%d %d R", ref.num, ref.gen);
199 break;
200 case objCmd:
201 fprintf(f, "%s", cmd);
202 break;
203 case objError:
204 fprintf(f, "<error>");
205 break;
206 case objEOF:
207 fprintf(f, "<EOF>");
208 break;
209 case objNone:
210 fprintf(f, "<none>");
211 break;
215 void Object::memCheck(FILE *f) {
216 #ifdef DEBUG_MEM
217 int i;
218 int t;
220 t = 0;
221 for (i = 0; i < numObjTypes; ++i)
222 t += numAlloc[i];
223 if (t > 0) {
224 fprintf(f, "Allocated objects:\n");
225 for (i = 0; i < numObjTypes; ++i) {
226 if (numAlloc[i] > 0)
227 fprintf(f, " %-20s: %6d\n", objTypeNames[i], numAlloc[i]);
230 #endif