]>
Commit | Line | Data |
---|---|---|
ac884b6a | 1 | // |
7e86f2f6 | 2 | // Array class for the CUPS PPD Compiler. |
ac884b6a | 3 | // |
7e86f2f6 MS |
4 | // Copyright 2007-2014 by Apple Inc. |
5 | // Copyright 2002-2005 by Easy Software Products. | |
ac884b6a | 6 | // |
e3101897 | 7 | // Licensed under Apache License v2.0. See the file "LICENSE" for more information. |
ac884b6a MS |
8 | // |
9 | ||
10 | // | |
11 | // Include necessary headers... | |
12 | // | |
13 | ||
38e73f87 | 14 | #include "ppdc-private.h" |
ac884b6a MS |
15 | |
16 | ||
17 | // | |
18 | // 'ppdcArray::ppdcArray()' - Create a new array. | |
19 | // | |
20 | ||
21 | ppdcArray::ppdcArray(ppdcArray *a) | |
94da7e34 | 22 | : ppdcShared() |
ac884b6a | 23 | { |
94da7e34 MS |
24 | PPDC_NEW; |
25 | ||
ac884b6a MS |
26 | if (a) |
27 | { | |
28 | count = a->count; | |
29 | alloc = count; | |
30 | ||
31 | if (count) | |
32 | { | |
33 | // Make a copy of the array... | |
34 | data = new ppdcShared *[count]; | |
35 | ||
7e86f2f6 | 36 | memcpy(data, a->data, (size_t)count * sizeof(ppdcShared *)); |
ac884b6a MS |
37 | |
38 | for (int i = 0; i < count; i ++) | |
e4572d57 | 39 | data[i]->retain(); |
ac884b6a MS |
40 | } |
41 | else | |
42 | data = 0; | |
43 | } | |
44 | else | |
45 | { | |
46 | count = 0; | |
47 | alloc = 0; | |
48 | data = 0; | |
49 | } | |
50 | ||
51 | current = 0; | |
52 | } | |
53 | ||
54 | ||
55 | // | |
56 | // 'ppdcArray::~ppdcArray()' - Destroy an array. | |
57 | // | |
58 | ||
59 | ppdcArray::~ppdcArray() | |
60 | { | |
94da7e34 MS |
61 | PPDC_DELETE; |
62 | ||
ac884b6a MS |
63 | for (int i = 0; i < count; i ++) |
64 | data[i]->release(); | |
65 | ||
66 | if (alloc) | |
67 | delete[] data; | |
68 | } | |
69 | ||
70 | ||
71 | // | |
72 | // 'ppdcArray::add()' - Add an element to an array. | |
73 | // | |
74 | ||
75 | void | |
76 | ppdcArray::add(ppdcShared *d) | |
77 | { | |
78 | ppdcShared **temp; | |
79 | ||
80 | ||
81 | if (count >= alloc) | |
82 | { | |
83 | alloc += 10; | |
84 | temp = new ppdcShared *[alloc]; | |
85 | ||
7e86f2f6 | 86 | memcpy(temp, data, (size_t)count * sizeof(ppdcShared *)); |
ac884b6a MS |
87 | |
88 | delete[] data; | |
89 | data = temp; | |
90 | } | |
91 | ||
92 | data[count++] = d; | |
93 | } | |
94 | ||
95 | ||
96 | // | |
97 | // 'ppdcArray::first()' - Return the first element in the array. | |
98 | // | |
99 | ||
100 | ppdcShared * | |
101 | ppdcArray::first() | |
102 | { | |
103 | current = 0; | |
104 | ||
105 | if (current >= count) | |
106 | return (0); | |
107 | else | |
108 | return (data[current ++]); | |
109 | } | |
110 | ||
111 | ||
112 | // | |
113 | // 'ppdcArray::next()' - Return the next element in the array. | |
114 | // | |
115 | ||
116 | ppdcShared * | |
117 | ppdcArray::next() | |
118 | { | |
119 | if (current >= count) | |
120 | return (0); | |
121 | else | |
122 | return (data[current ++]); | |
123 | } | |
124 | ||
125 | ||
126 | // | |
127 | // 'ppdcArray::remove()' - Remove an element from the array. | |
128 | // | |
129 | ||
130 | void | |
131 | ppdcArray::remove(ppdcShared *d) // I - Data element | |
132 | { | |
133 | int i; // Looping var | |
134 | ||
135 | ||
136 | for (i = 0; i < count; i ++) | |
137 | if (d == data[i]) | |
138 | break; | |
139 | ||
140 | if (i >= count) | |
141 | return; | |
142 | ||
143 | count --; | |
144 | d->release(); | |
145 | ||
146 | if (i < count) | |
7e86f2f6 | 147 | memmove(data + i, data + i + 1, (size_t)(count - i) * sizeof(ppdcShared *)); |
ac884b6a | 148 | } |