]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/Object.cc
Initial work I did on GNOME/PDF viewer -miguel
[evince.git] / pdf / 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     break;
112   case objDict:
113     if (!dict->decRef())
114       delete dict;
115     break;
116   case objStream:
117     if (!stream->decRef())
118       delete stream;
119     break;
120   case objCmd:
121     gfree(cmd);
122     break;
123   default:
124     break;
125   }
126 #ifdef DEBUG_MEM
127   --numAlloc[type];
128 #endif
129   type = objNone;
130 }
131
132 char *Object::getTypeName() {
133   return objTypeNames[type];
134 }
135
136 void Object::print(FILE *f) {
137   Object obj;
138   int i;
139
140   switch (type) {
141   case objBool:
142     fprintf(f, "%s", booln ? "true" : "false");
143     break;
144   case objInt:
145     fprintf(f, "%d", intg);
146     break;
147   case objReal:
148     fprintf(f, "%g", real);
149     break;
150   case objString:
151     fprintf(f, "(%s)", string->getCString());
152     break;
153   case objName:
154     fprintf(f, "/%s", name);
155     break;
156   case objNull:
157     fprintf(f, "null");
158     break;
159   case objArray:
160     fprintf(f, "[");
161     for (i = 0; i < arrayGetLength(); ++i) {
162       if (i > 0)
163         fprintf(f, " ");
164       arrayGetNF(i, &obj);
165       obj.print(f);
166       obj.free();
167     }
168     fprintf(f, "]");
169     break;
170   case objDict:
171     fprintf(f, "<<");
172     for (i = 0; i < dictGetLength(); ++i) {
173       fprintf(f, " /%s ", dictGetKey(i));
174       dictGetValNF(i, &obj);
175       obj.print(f);
176       obj.free();
177     }
178     fprintf(f, " >>");
179     break;
180   case objStream:
181     fprintf(f, "<stream>");
182     break;
183   case objRef:
184     fprintf(f, "%d %d R", ref.num, ref.gen);
185     break;
186   case objCmd:
187     fprintf(f, "%s", cmd);
188     break;
189   case objError:
190     fprintf(f, "<error>");
191     break;
192   case objEOF:
193     fprintf(f, "<EOF>");
194     break;
195   case objNone:
196     fprintf(f, "<none>");
197     break;
198   }
199 }
200
201 void Object::memCheck(FILE *f) {
202 #ifdef DEBUG_MEM
203   int i;
204   int t;
205
206   t = 0;
207   for (i = 0; i < numObjTypes; ++i)
208     t += numAlloc[i];
209   if (t > 0) {
210     fprintf(f, "Allocated objects:\n");
211     for (i = 0; i < numObjTypes; ++i) {
212       if (numAlloc[i] > 0)
213         fprintf(f, "  %-20s: %6d\n", objTypeNames[i], numAlloc[i]);
214     }
215   }
216 #endif
217 }