This repository has been archived on 2025-05-24. You can view files and clone it, but cannot push or open issues or pull requests.
Files
libcdio-osx/lib/driver/ds.c

253 lines
4.9 KiB
C
Raw Normal View History

2003-03-24 19:01:09 +00:00
/*
$Id: ds.c,v 1.3 2005/02/03 07:35:15 rocky Exp $
2003-03-24 19:01:09 +00:00
Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
Copyright (C) 2005 Rocky Bernstein <rocky@panix.com>
2003-03-24 19:01:09 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <cdio/ds.h>
#include <cdio/util.h>
#include <cdio/types.h>
2003-03-24 19:01:09 +00:00
#include "cdio_assert.h"
static const char _rcsid[] = "$Id: ds.c,v 1.3 2005/02/03 07:35:15 rocky Exp $";
2003-03-24 19:01:09 +00:00
struct _CdioList
{
unsigned length;
CdioListNode_t *begin;
CdioListNode_t *end;
2003-03-24 19:01:09 +00:00
};
struct _CdioListNode
{
CdioList_t *list;
2003-03-24 19:01:09 +00:00
CdioListNode_t *next;
2003-03-24 19:01:09 +00:00
void *data;
};
/* impl */
CdioList_t *
2003-03-24 19:01:09 +00:00
_cdio_list_new (void)
{
CdioList_t *p_new_obj = calloc (1, sizeof (CdioList_t));
2003-03-24 19:01:09 +00:00
return p_new_obj;
2003-03-24 19:01:09 +00:00
}
void
_cdio_list_free (CdioList_t *p_list, int free_data)
2003-03-24 19:01:09 +00:00
{
while (_cdio_list_length (p_list))
_cdio_list_node_free (_cdio_list_begin (p_list), free_data);
2003-03-24 19:01:09 +00:00
free (p_list);
2003-03-24 19:01:09 +00:00
}
unsigned
_cdio_list_length (const CdioList_t *p_list)
2003-03-24 19:01:09 +00:00
{
cdio_assert (p_list != NULL);
2003-03-24 19:01:09 +00:00
return p_list->length;
2003-03-24 19:01:09 +00:00
}
void
_cdio_list_prepend (CdioList_t *p_list, void *p_data)
2003-03-24 19:01:09 +00:00
{
CdioListNode_t *p_new_node;
2003-03-24 19:01:09 +00:00
cdio_assert (p_list != NULL);
2003-03-24 19:01:09 +00:00
p_new_node = calloc (1, sizeof (CdioListNode_t));
2003-03-24 19:01:09 +00:00
p_new_node->list = p_list;
p_new_node->next = p_list->begin;
p_new_node->data = p_data;
2003-03-24 19:01:09 +00:00
p_list->begin = p_new_node;
if (p_list->length == 0)
p_list->end = p_new_node;
2003-03-24 19:01:09 +00:00
p_list->length++;
2003-03-24 19:01:09 +00:00
}
void
_cdio_list_append (CdioList_t *p_list, void *p_data)
2003-03-24 19:01:09 +00:00
{
cdio_assert (p_list != NULL);
2003-03-24 19:01:09 +00:00
if (p_list->length == 0)
2003-03-24 19:01:09 +00:00
{
_cdio_list_prepend (p_list, p_data);
2003-03-24 19:01:09 +00:00
}
else
{
CdioListNode_t *p_new_node = calloc (1, sizeof (CdioListNode_t));
2003-03-24 19:01:09 +00:00
p_new_node->list = p_list;
p_new_node->next = NULL;
p_new_node->data = p_data;
2003-03-24 19:01:09 +00:00
p_list->end->next = p_new_node;
p_list->end = p_new_node;
2003-03-24 19:01:09 +00:00
p_list->length++;
2003-03-24 19:01:09 +00:00
}
}
void
_cdio_list_foreach (CdioList_t *p_list, _cdio_list_iterfunc_t func,
void *p_user_data)
2003-03-24 19:01:09 +00:00
{
CdioListNode_t *node;
2003-03-24 19:01:09 +00:00
cdio_assert (p_list != NULL);
2003-03-24 19:01:09 +00:00
cdio_assert (func != 0);
for (node = _cdio_list_begin (p_list);
2003-03-24 19:01:09 +00:00
node != NULL;
node = _cdio_list_node_next (node))
func (_cdio_list_node_data (node), p_user_data);
2003-03-24 19:01:09 +00:00
}
CdioListNode_t *
_cdio_list_find (CdioList_t *p_list, _cdio_list_iterfunc_t cmp_func,
void *p_user_data)
2003-03-24 19:01:09 +00:00
{
CdioListNode_t *p_node;
2003-03-24 19:01:09 +00:00
cdio_assert (p_list != NULL);
2003-03-24 19:01:09 +00:00
cdio_assert (cmp_func != 0);
for (p_node = _cdio_list_begin (p_list);
p_node != NULL;
p_node = _cdio_list_node_next (p_node))
if (cmp_func (_cdio_list_node_data (p_node), p_user_data))
2003-03-24 19:01:09 +00:00
break;
return p_node;
2003-03-24 19:01:09 +00:00
}
CdioListNode_t *
_cdio_list_begin (const CdioList_t *p_list)
2003-03-24 19:01:09 +00:00
{
cdio_assert (p_list != NULL);
2003-03-24 19:01:09 +00:00
return p_list->begin;
2003-03-24 19:01:09 +00:00
}
CdioListNode_t *
_cdio_list_end (CdioList_t *p_list)
2003-03-24 19:01:09 +00:00
{
cdio_assert (p_list != NULL);
2003-03-24 19:01:09 +00:00
return p_list->end;
2003-03-24 19:01:09 +00:00
}
CdioListNode_t *
_cdio_list_node_next (CdioListNode_t *p_node)
2003-03-24 19:01:09 +00:00
{
if (p_node)
return p_node->next;
2003-03-24 19:01:09 +00:00
return NULL;
}
void
_cdio_list_node_free (CdioListNode_t *p_node, int free_data)
2003-03-24 19:01:09 +00:00
{
CdioList_t *p_list;
CdioListNode_t *prev_node;
2003-03-24 19:01:09 +00:00
cdio_assert (p_node != NULL);
2003-03-24 19:01:09 +00:00
p_list = p_node->list;
2003-03-24 19:01:09 +00:00
cdio_assert (_cdio_list_length (p_list) > 0);
2003-03-24 19:01:09 +00:00
if (free_data)
free (_cdio_list_node_data (p_node));
2003-03-24 19:01:09 +00:00
if (_cdio_list_length (p_list) == 1)
2003-03-24 19:01:09 +00:00
{
cdio_assert (p_list->begin == p_list->end);
2003-03-24 19:01:09 +00:00
p_list->end = p_list->begin = NULL;
p_list->length = 0;
free (p_node);
2003-03-24 19:01:09 +00:00
return;
}
cdio_assert (p_list->begin != p_list->end);
2003-03-24 19:01:09 +00:00
if (p_list->begin == p_node)
2003-03-24 19:01:09 +00:00
{
p_list->begin = p_node->next;
free (p_node);
p_list->length--;
2003-03-24 19:01:09 +00:00
return;
}
for (prev_node = p_list->begin; prev_node->next; prev_node = prev_node->next)
if (prev_node->next == p_node)
2003-03-24 19:01:09 +00:00
break;
cdio_assert (prev_node->next != NULL);
if (p_list->end == p_node)
p_list->end = prev_node;
2003-03-24 19:01:09 +00:00
prev_node->next = p_node->next;
2003-03-24 19:01:09 +00:00
p_list->length--;
2003-03-24 19:01:09 +00:00
free (p_node);
2003-03-24 19:01:09 +00:00
}
void *
_cdio_list_node_data (CdioListNode_t *p_node)
2003-03-24 19:01:09 +00:00
{
if (p_node)
return p_node->data;
2003-03-24 19:01:09 +00:00
return NULL;
}
/* eof */
/*
* Local variables:
* c-file-style: "gnu"
* tab-width: 8
* indent-tabs-mode: nil
* End:
*/