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