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