• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Array class for the CUPS PPD Compiler.
3 //
4 // Copyright © 2020-2024 by OpenPrinting.
5 // Copyright 2007-2019 by Apple Inc.
6 // Copyright 2002-2005 by Easy Software Products.
7 //
8 // Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
9 //
10 
11 //
12 // Include necessary headers...
13 //
14 
15 #include "ppdc-private.h"
16 
17 
18 //
19 // 'ppdcArray::ppdcArray()' - Create a new array.
20 //
21 
ppdcArray(ppdcArray * a)22 ppdcArray::ppdcArray(ppdcArray *a)
23   : ppdcShared()
24 {
25   PPDC_NEW;
26 
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 
37       memcpy(data, a->data, (size_t)count * sizeof(ppdcShared *));
38 
39       for (size_t i = 0; i < count; i ++)
40         data[i]->retain();
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 
~ppdcArray()60 ppdcArray::~ppdcArray()
61 {
62   PPDC_DELETE;
63 
64   for (size_t i = 0; i < count; i ++)
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 
76 void
add(ppdcShared * d)77 ppdcArray::add(ppdcShared *d)
78 {
79   ppdcShared	**temp;
80 
81 
82   if (count >= alloc)
83   {
84     alloc += 10;
85     temp  = new ppdcShared *[alloc];
86 
87     memcpy(temp, data, (size_t)count * sizeof(ppdcShared *));
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 
101 ppdcShared *
first()102 ppdcArray::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 
117 ppdcShared *
next()118 ppdcArray::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 
131 void
remove(ppdcShared * d)132 ppdcArray::remove(ppdcShared *d)		// I - Data element
133 {
134   size_t	i;				// Looping var
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)
148     memmove(data + i, data + i + 1, (size_t)(count - i) * sizeof(ppdcShared *));
149 }
150