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