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