upgraded to xpdf-3.01pl1
[swftools.git] / pdf2swf / xpdf / Object.cc
1 //========================================================================
2 //
3 // Object.cc
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #include <aconf.h>
10
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
13 #endif
14
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"
22
23 //------------------------------------------------------------------------
24 // Object
25 //------------------------------------------------------------------------
26
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"
42 };
43
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
48
49 Object *Object::initArray(XRef *xref) {
50   initObj(objArray);
51   array = new Array(xref);
52   return this;
53 }
54
55 Object *Object::initDict(XRef *xref) {
56   initObj(objDict);
57   dict = new Dict(xref);
58   return this;
59 }
60
61 Object *Object::initDict(Dict *dictA) {
62   initObj(objDict);
63   dict = dictA;
64   dict->incRef();
65   return this;
66 }
67
68 Object *Object::initStream(Stream *streamA) {
69   initObj(objStream);
70   stream = streamA;
71   return this;
72 }
73
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;
97   }
98 #ifdef DEBUG_MEM
99   ++numAlloc[type];
100 #endif
101   return obj;
102 }
103
104 Object *Object::fetch(XRef *xref, Object *obj) {
105   return (type == objRef && xref) ?
106          xref->fetch(ref.num, ref.gen, obj) : copy(obj);
107 }
108
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;
120     }
121     break;
122   case objDict:
123     if (!dict->decRef()) {
124       delete dict;
125     }
126     break;
127   case objStream:
128     if (!stream->decRef()) {
129       delete stream;
130     }
131     break;
132   case objCmd:
133     gfree(cmd);
134     break;
135   default:
136     break;
137   }
138 #ifdef DEBUG_MEM
139   --numAlloc[type];
140 #endif
141   type = objNone;
142 }
143
144 char *Object::getTypeName() {
145   return objTypeNames[type];
146 }
147
148 void Object::print(FILE *f) {
149   Object obj;
150   int i;
151
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(), f);
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();
181     }
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();
191     }
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;
212   }
213 }
214
215 void Object::memCheck(FILE *f) {
216 #ifdef DEBUG_MEM
217   int i;
218   int t;
219
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]);
228     }
229   }
230 #endif
231 }