]> git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/ppdc-array.cxx
d56b92a33d187ce10c93cfd04dcaec8dc00431c1
[thirdparty/cups.git] / ppdc / ppdc-array.cxx
1 //
2 // Array class for the CUPS PPD Compiler.
3 //
4 // Copyright 2007-2014 by Apple Inc.
5 // Copyright 2002-2005 by Easy Software Products.
6 //
7 // These coded instructions, statements, and computer programs are the
8 // property of Apple Inc. and are protected by Federal copyright
9 // law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 // which should have been included with this file. If this file is
11 // file is missing or damaged, see the license at "http://www.cups.org/".
12 //
13
14 //
15 // Include necessary headers...
16 //
17
18 #include "ppdc-private.h"
19
20
21 //
22 // 'ppdcArray::ppdcArray()' - Create a new array.
23 //
24
25 ppdcArray::ppdcArray(ppdcArray *a)
26 : ppdcShared()
27 {
28 PPDC_NEW;
29
30 if (a)
31 {
32 count = a->count;
33 alloc = count;
34
35 if (count)
36 {
37 // Make a copy of the array...
38 data = new ppdcShared *[count];
39
40 memcpy(data, a->data, (size_t)count * sizeof(ppdcShared *));
41
42 for (int i = 0; i < count; i ++)
43 data[i]->retain();
44 }
45 else
46 data = 0;
47 }
48 else
49 {
50 count = 0;
51 alloc = 0;
52 data = 0;
53 }
54
55 current = 0;
56 }
57
58
59 //
60 // 'ppdcArray::~ppdcArray()' - Destroy an array.
61 //
62
63 ppdcArray::~ppdcArray()
64 {
65 PPDC_DELETE;
66
67 for (int i = 0; i < count; i ++)
68 data[i]->release();
69
70 if (alloc)
71 delete[] data;
72 }
73
74
75 //
76 // 'ppdcArray::add()' - Add an element to an array.
77 //
78
79 void
80 ppdcArray::add(ppdcShared *d)
81 {
82 ppdcShared **temp;
83
84
85 if (count >= alloc)
86 {
87 alloc += 10;
88 temp = new ppdcShared *[alloc];
89
90 memcpy(temp, data, (size_t)count * sizeof(ppdcShared *));
91
92 delete[] data;
93 data = temp;
94 }
95
96 data[count++] = d;
97 }
98
99
100 //
101 // 'ppdcArray::first()' - Return the first element in the array.
102 //
103
104 ppdcShared *
105 ppdcArray::first()
106 {
107 current = 0;
108
109 if (current >= count)
110 return (0);
111 else
112 return (data[current ++]);
113 }
114
115
116 //
117 // 'ppdcArray::next()' - Return the next element in the array.
118 //
119
120 ppdcShared *
121 ppdcArray::next()
122 {
123 if (current >= count)
124 return (0);
125 else
126 return (data[current ++]);
127 }
128
129
130 //
131 // 'ppdcArray::remove()' - Remove an element from the array.
132 //
133
134 void
135 ppdcArray::remove(ppdcShared *d) // I - Data element
136 {
137 int i; // Looping var
138
139
140 for (i = 0; i < count; i ++)
141 if (d == data[i])
142 break;
143
144 if (i >= count)
145 return;
146
147 count --;
148 d->release();
149
150 if (i < count)
151 memmove(data + i, data + i + 1, (size_t)(count - i) * sizeof(ppdcShared *));
152 }