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