1//
2// "$Id: ppdc-array.cxx 11560 2014-02-06 20:10:19Z msweet $"
3//
4// Array class for the CUPS PPD Compiler.
5//
6// Copyright 2007-2014 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
16//
17// Include necessary headers...
18//
19
20#include "ppdc-private.h"
21
22
23//
24// 'ppdcArray::ppdcArray()' - Create a new array.
25//
26
27ppdcArray::ppdcArray(ppdcArray *a)
28  : ppdcShared()
29{
30  PPDC_NEW;
31
32  if (a)
33  {
34    count = a->count;
35    alloc = count;
36
37    if (count)
38    {
39      // Make a copy of the array...
40      data = new ppdcShared *[count];
41
42      memcpy(data, a->data, (size_t)count * sizeof(ppdcShared *));
43
44      for (int i = 0; i < count; i ++)
45        data[i]->retain();
46    }
47    else
48      data = 0;
49  }
50  else
51  {
52    count = 0;
53    alloc = 0;
54    data  = 0;
55  }
56
57  current = 0;
58}
59
60
61//
62// 'ppdcArray::~ppdcArray()' - Destroy an array.
63//
64
65ppdcArray::~ppdcArray()
66{
67  PPDC_DELETE;
68
69  for (int i = 0; i < count; i ++)
70    data[i]->release();
71
72  if (alloc)
73    delete[] data;
74}
75
76
77//
78// 'ppdcArray::add()' - Add an element to an array.
79//
80
81void
82ppdcArray::add(ppdcShared *d)
83{
84  ppdcShared	**temp;
85
86
87  if (count >= alloc)
88  {
89    alloc += 10;
90    temp  = new ppdcShared *[alloc];
91
92    memcpy(temp, data, (size_t)count * sizeof(ppdcShared *));
93
94    delete[] data;
95    data = temp;
96  }
97
98  data[count++] = d;
99}
100
101
102//
103// 'ppdcArray::first()' - Return the first element in the array.
104//
105
106ppdcShared *
107ppdcArray::first()
108{
109  current = 0;
110
111  if (current >= count)
112    return (0);
113  else
114    return (data[current ++]);
115}
116
117
118//
119// 'ppdcArray::next()' - Return the next element in the array.
120//
121
122ppdcShared *
123ppdcArray::next()
124{
125  if (current >= count)
126    return (0);
127  else
128    return (data[current ++]);
129}
130
131
132//
133// 'ppdcArray::remove()' - Remove an element from the array.
134//
135
136void
137ppdcArray::remove(ppdcShared *d)		// I - Data element
138{
139  int	i;					// Looping var
140
141
142  for (i = 0; i < count; i ++)
143    if (d == data[i])
144      break;
145
146  if (i >= count)
147    return;
148
149  count --;
150  d->release();
151
152  if (i < count)
153    memmove(data + i, data + i + 1, (size_t)(count - i) * sizeof(ppdcShared *));
154}
155
156
157//
158// End of "$Id: ppdc-array.cxx 11560 2014-02-06 20:10:19Z msweet $".
159//
160