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