]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/Object.cc
add tests for GPdfPersistFile.
[evince.git] / pdf / 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::initStream(Stream *streamA) {
62   initObj(objStream);
63   stream = streamA;
64   return this;
65 }
66
67 Object *Object::copy(Object *obj) {
68   *obj = *this;
69   switch (type) {
70   case objString:
71     obj->string = string->copy();
72     break;
73   case objName:
74     obj->name = copyString(name);
75     break;
76   case objArray:
77     array->incRef();
78     break;
79   case objDict:
80     dict->incRef();
81     break;
82   case objStream:
83     stream->incRef();
84     break;
85   case objCmd:
86     obj->cmd = copyString(cmd);
87     break;
88   default:
89     break;
90   }
91 #ifdef DEBUG_MEM
92   ++numAlloc[type];
93 #endif
94   return obj;
95 }
96
97 Object *Object::fetch(XRef *xref, Object *obj) {
98   return (type == objRef && xref) ?
99          xref->fetch(ref.num, ref.gen, obj) : copy(obj);
100 }
101
102 void Object::free() {
103   switch (type) {
104   case objString:
105     delete string;
106     break;
107   case objName:
108     gfree(name);
109     break;
110   case objArray:
111     if (!array->decRef()) {
112       delete array;
113     }
114     break;
115   case objDict:
116     if (!dict->decRef()) {
117       delete dict;
118     }
119     break;
120   case objStream:
121     if (!stream->decRef()) {
122       delete stream;
123     }
124     break;
125   case objCmd:
126     gfree(cmd);
127     break;
128   default:
129     break;
130   }
131 #ifdef DEBUG_MEM
132   --numAlloc[type];
133 #endif
134   type = objNone;
135 }
136
137 char *Object::getTypeName() {
138   return objTypeNames[type];
139 }
140
141 void Object::print(FILE *f) {
142   Object obj;
143   int i;
144
145   switch (type) {
146   case objBool:
147     fprintf(f, "%s", booln ? "true" : "false");
148     break;
149   case objInt:
150     fprintf(f, "%d", intg);
151     break;
152   case objReal:
153     fprintf(f, "%g", real);
154     break;
155   case objString:
156     fprintf(f, "(");
157     fwrite(string->getCString(), 1, string->getLength(), stdout);
158     fprintf(f, ")");
159     break;
160   case objName:
161     fprintf(f, "/%s", name);
162     break;
163   case objNull:
164     fprintf(f, "null");
165     break;
166   case objArray:
167     fprintf(f, "[");
168     for (i = 0; i < arrayGetLength(); ++i) {
169       if (i > 0)
170         fprintf(f, " ");
171       arrayGetNF(i, &obj);
172       obj.print(f);
173       obj.free();
174     }
175     fprintf(f, "]");
176     break;
177   case objDict:
178     fprintf(f, "<<");
179     for (i = 0; i < dictGetLength(); ++i) {
180       fprintf(f, " /%s ", dictGetKey(i));
181       dictGetValNF(i, &obj);
182       obj.print(f);
183       obj.free();
184     }
185     fprintf(f, " >>");
186     break;
187   case objStream:
188     fprintf(f, "<stream>");
189     break;
190   case objRef:
191     fprintf(f, "%d %d R", ref.num, ref.gen);
192     break;
193   case objCmd:
194     fprintf(f, "%s", cmd);
195     break;
196   case objError:
197     fprintf(f, "<error>");
198     break;
199   case objEOF:
200     fprintf(f, "<EOF>");
201     break;
202   case objNone:
203     fprintf(f, "<none>");
204     break;
205   }
206 }
207
208 void Object::memCheck(FILE *f) {
209 #ifdef DEBUG_MEM
210   int i;
211   int t;
212
213   t = 0;
214   for (i = 0; i < numObjTypes; ++i)
215     t += numAlloc[i];
216   if (t > 0) {
217     fprintf(f, "Allocated objects:\n");
218     for (i = 0; i < numObjTypes; ++i) {
219       if (numAlloc[i] > 0)
220         fprintf(f, "  %-20s: %6d\n", objTypeNames[i], numAlloc[i]);
221     }
222   }
223 #endif
224 }