Bringing apdf from vendor into main branch.
[AROS-Contrib.git] / apdf / xpdf / Array.cc
blob5890c49dfeb451d9160ccaa92ff02683b1f25143
1 //========================================================================
2 //
3 // Array.cc
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
9 #include <aconf.h>
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
13 #endif
15 #include <stdlib.h>
16 #include <stddef.h>
17 #include "gmem.h"
18 #include "Object.h"
19 #include "Array.h"
21 //------------------------------------------------------------------------
22 // Array
23 //------------------------------------------------------------------------
25 Array::Array(XRef *xrefA) {
26 xref = xrefA;
27 elems = NULL;
28 size = length = 0;
29 ref = 1;
32 Array::~Array() {
33 int i;
35 for (i = 0; i < length; ++i)
36 elems[i].free();
37 gfree(elems);
40 void Array::add(Object *elem) {
41 if (length == size) {
42 if (length == 0) {
43 size = 8;
44 } else {
45 size *= 2;
47 #ifdef DEBUG_MEM
48 for (int k = 0; k < length; ++k)
49 if (!elems[k].isNone() && !elems[k].isNull())
50 objTracker.unTrack(&elems[k]);
51 #endif
52 elems = (Object *)greallocn(elems, size, sizeof(Object));
53 #ifdef DEBUG_MEM
54 for (int k = 0; k < length; ++k)
55 if (!elems[k].isNone() && !elems[k].isNull())
56 objTracker.track(&elems[k]);
57 #endif
59 elems[length] = *elem;
60 #ifdef DEBUG_MEM
61 elem->clear();
62 #endif
63 ++length;
66 Object *Array::get(int i, Object *obj) {
67 if (i < 0 || i >= length) {
68 #ifdef DEBUG_MEM
69 abort();
70 #else
71 return obj->initNull();
72 #endif
74 return elems[i].fetch(xref, obj);
77 Object *Array::getNF(int i, Object *obj) {
78 if (i < 0 || i >= length) {
79 #ifdef DEBUG_MEM
80 abort();
81 #else
82 return obj->initNull();
83 #endif
85 return elems[i].copy(obj);