~ubuntu-branches/ubuntu/edgy/libcdio/edgy-updates

« back to all changes in this revision

Viewing changes to lib/driver/ds.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2005-11-15 16:53:23 UTC
  • mfrom: (3.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20051115165323-peroku75syl2j36u
Tags: 0.76-1ubuntu1
* Sync to new Debian version, manually apply Ubuntu patches:
  - debian/control: Remove dpkg-awk build dependency.
  - debian/rules: hardcode $LIBCDEV. This keeps the diff small (compared to
    the original patch of changing every ${libcdev} occurence).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    $Id: ds.c,v 1.3 2005/02/03 07:35:15 rocky Exp $
 
3
 
 
4
    Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
 
5
    Copyright (C) 2005 Rocky Bernstein <rocky@panix.com>
 
6
 
 
7
    This program is free software; you can redistribute it and/or modify
 
8
    it under the terms of the GNU General Public License as published by
 
9
    the Free Software Foundation; either version 2 of the License, or
 
10
    (at your option) any later version.
 
11
 
 
12
    This program is distributed in the hope that it will be useful,
 
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
    GNU General Public License for more details.
 
16
 
 
17
    You should have received a copy of the GNU General Public License
 
18
    along with this program; if not, write to the Free Software
 
19
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
*/
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
# include "config.h"
 
24
#endif
 
25
 
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
 
 
29
#include <cdio/ds.h>
 
30
#include <cdio/util.h>
 
31
#include <cdio/types.h>
 
32
#include "cdio_assert.h"
 
33
 
 
34
static const char _rcsid[] = "$Id: ds.c,v 1.3 2005/02/03 07:35:15 rocky Exp $";
 
35
 
 
36
struct _CdioList
 
37
{
 
38
  unsigned length;
 
39
 
 
40
  CdioListNode_t *begin;
 
41
  CdioListNode_t *end;
 
42
};
 
43
 
 
44
struct _CdioListNode
 
45
{
 
46
  CdioList_t *list;
 
47
 
 
48
  CdioListNode_t *next;
 
49
 
 
50
  void *data;
 
51
};
 
52
 
 
53
/* impl */
 
54
 
 
55
CdioList_t *
 
56
_cdio_list_new (void)
 
57
{
 
58
  CdioList_t *p_new_obj = calloc (1, sizeof (CdioList_t));
 
59
 
 
60
  return p_new_obj;
 
61
}
 
62
 
 
63
void
 
64
_cdio_list_free (CdioList_t *p_list, int free_data)
 
65
{
 
66
  while (_cdio_list_length (p_list))
 
67
    _cdio_list_node_free (_cdio_list_begin (p_list), free_data);
 
68
 
 
69
  free (p_list);
 
70
}
 
71
 
 
72
unsigned
 
73
_cdio_list_length (const CdioList_t *p_list)
 
74
{
 
75
  cdio_assert (p_list != NULL);
 
76
 
 
77
  return p_list->length;
 
78
}
 
79
 
 
80
void
 
81
_cdio_list_prepend (CdioList_t *p_list, void *p_data)
 
82
{
 
83
  CdioListNode_t *p_new_node;
 
84
 
 
85
  cdio_assert (p_list != NULL);
 
86
 
 
87
  p_new_node = calloc (1, sizeof (CdioListNode_t));
 
88
  
 
89
  p_new_node->list = p_list;
 
90
  p_new_node->next = p_list->begin;
 
91
  p_new_node->data = p_data;
 
92
 
 
93
  p_list->begin = p_new_node;
 
94
  if (p_list->length == 0)
 
95
    p_list->end = p_new_node;
 
96
 
 
97
  p_list->length++;
 
98
}
 
99
 
 
100
void
 
101
_cdio_list_append (CdioList_t *p_list, void *p_data)
 
102
{
 
103
  cdio_assert (p_list != NULL);
 
104
 
 
105
  if (p_list->length == 0)
 
106
    {
 
107
      _cdio_list_prepend (p_list, p_data);
 
108
    }
 
109
  else
 
110
    {
 
111
      CdioListNode_t *p_new_node = calloc (1, sizeof (CdioListNode_t));
 
112
      
 
113
      p_new_node->list = p_list;
 
114
      p_new_node->next = NULL;
 
115
      p_new_node->data = p_data;
 
116
 
 
117
      p_list->end->next = p_new_node;
 
118
      p_list->end = p_new_node;
 
119
 
 
120
      p_list->length++;
 
121
    }
 
122
}
 
123
 
 
124
void 
 
125
_cdio_list_foreach (CdioList_t *p_list, _cdio_list_iterfunc_t func, 
 
126
                    void *p_user_data)
 
127
{
 
128
  CdioListNode_t *node;
 
129
 
 
130
  cdio_assert (p_list != NULL);
 
131
  cdio_assert (func != 0);
 
132
  
 
133
  for (node = _cdio_list_begin (p_list);
 
134
       node != NULL;
 
135
       node = _cdio_list_node_next (node))
 
136
    func (_cdio_list_node_data (node), p_user_data);
 
137
}
 
138
 
 
139
CdioListNode_t *
 
140
_cdio_list_find (CdioList_t *p_list, _cdio_list_iterfunc_t cmp_func, 
 
141
                 void *p_user_data)
 
142
{
 
143
  CdioListNode_t *p_node;
 
144
 
 
145
  cdio_assert (p_list != NULL);
 
146
  cdio_assert (cmp_func != 0);
 
147
  
 
148
  for (p_node = _cdio_list_begin (p_list);
 
149
       p_node != NULL;
 
150
       p_node = _cdio_list_node_next (p_node))
 
151
    if (cmp_func (_cdio_list_node_data (p_node), p_user_data))
 
152
      break;
 
153
 
 
154
  return p_node;
 
155
}
 
156
 
 
157
CdioListNode_t *
 
158
_cdio_list_begin (const CdioList_t *p_list)
 
159
{
 
160
  cdio_assert (p_list != NULL);
 
161
 
 
162
  return p_list->begin;
 
163
}
 
164
 
 
165
CdioListNode_t *
 
166
_cdio_list_end (CdioList_t *p_list)
 
167
{
 
168
  cdio_assert (p_list != NULL);
 
169
 
 
170
  return p_list->end;
 
171
}
 
172
 
 
173
CdioListNode_t *
 
174
_cdio_list_node_next (CdioListNode_t *p_node)
 
175
{
 
176
  if (p_node)
 
177
    return p_node->next;
 
178
 
 
179
  return NULL;
 
180
}
 
181
 
 
182
void 
 
183
_cdio_list_node_free (CdioListNode_t *p_node, int free_data)
 
184
{
 
185
  CdioList_t *p_list;
 
186
  CdioListNode_t *prev_node;
 
187
 
 
188
  cdio_assert (p_node != NULL);
 
189
  
 
190
  p_list = p_node->list;
 
191
 
 
192
  cdio_assert (_cdio_list_length (p_list) > 0);
 
193
 
 
194
  if (free_data)
 
195
    free (_cdio_list_node_data (p_node));
 
196
 
 
197
  if (_cdio_list_length (p_list) == 1)
 
198
    {
 
199
      cdio_assert (p_list->begin == p_list->end);
 
200
 
 
201
      p_list->end = p_list->begin = NULL;
 
202
      p_list->length = 0;
 
203
      free (p_node);
 
204
      return;
 
205
    }
 
206
 
 
207
  cdio_assert (p_list->begin != p_list->end);
 
208
 
 
209
  if (p_list->begin == p_node)
 
210
    {
 
211
      p_list->begin = p_node->next;
 
212
      free (p_node);
 
213
      p_list->length--;
 
214
      return;
 
215
    }
 
216
 
 
217
  for (prev_node = p_list->begin; prev_node->next; prev_node = prev_node->next)
 
218
    if (prev_node->next == p_node)
 
219
      break;
 
220
 
 
221
  cdio_assert (prev_node->next != NULL);
 
222
 
 
223
  if (p_list->end == p_node)
 
224
    p_list->end = prev_node;
 
225
 
 
226
  prev_node->next = p_node->next;
 
227
 
 
228
  p_list->length--;
 
229
 
 
230
  free (p_node);
 
231
}
 
232
 
 
233
void *
 
234
_cdio_list_node_data (CdioListNode_t *p_node)
 
235
{
 
236
  if (p_node)
 
237
    return p_node->data;
 
238
 
 
239
  return NULL;
 
240
}
 
241
 
 
242
/* eof */
 
243
 
 
244
 
 
245
/* 
 
246
 * Local variables:
 
247
 *  c-file-style: "gnu"
 
248
 *  tab-width: 8
 
249
 *  indent-tabs-mode: nil
 
250
 * End:
 
251
 */
 
252