~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/*
17
  Code for handling dubble-linked lists in C
18
*/
19
20
#include "mysys_priv.h"
685.1.3 by Monty Taylor
Turned off stdinc - and then fixed the carnage.
21
#include "my_list.h"
22
612.2.6 by Monty Taylor
OpenSolaris builds.
23
#include <stdlib.h>
1 by brian
clean slate
24
25
26
27
	/* Add a element to start of list */
28
29
LIST *list_add(LIST *root, LIST *element)
30
{
31
  if (root)
32
  {
33
    if (root->prev)			/* If add in mid of list */
34
      root->prev->next= element;
35
    element->prev=root->prev;
36
    root->prev=element;
37
  }
38
  else
39
    element->prev=0;
40
  element->next=root;
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
41
  return(element);			/* New root */
1 by brian
clean slate
42
}
43
44
45
LIST *list_delete(LIST *root, LIST *element)
46
{
47
  if (element->prev)
48
    element->prev->next=element->next;
49
  else
50
    root=element->next;
51
  if (element->next)
52
    element->next->prev=element->prev;
53
  return root;
54
}
55
56
482 by Brian Aker
Remove uint.
57
void list_free(LIST *root, uint32_t free_data)
1 by brian
clean slate
58
{
59
  LIST *next;
60
  while (root)
61
  {
62
    next=root->next;
63
    if (free_data)
481 by Brian Aker
Remove all of uchar.
64
      free((unsigned char*) root->data);
65
    free((unsigned char*) root);
1 by brian
clean slate
66
    root=next;
67
  }
68
}
69
70
71
LIST *list_cons(void *data, LIST *list)
72
{
656.3.2 by Monty Taylor
Removed my_nosys and my_quick. /me shudders.
73
  LIST * const new_charset=(LIST*) malloc(sizeof(LIST));
1 by brian
clean slate
74
  if (!new_charset)
266.6.9 by Andy Lester
Removed unused function list_length
75
    return NULL;
1 by brian
clean slate
76
  new_charset->data=data;
77
  return list_add(list,new_charset);
78
}
79
80
81
LIST *list_reverse(LIST *root)
82
{
83
  LIST *last;
84
85
  last=root;
86
  while (root)
87
  {
88
    last=root;
89
    root=root->next;
90
    last->next=last->prev;
91
    last->prev=root;
92
  }
93
  return last;
94
}
95
481 by Brian Aker
Remove all of uchar.
96
int list_walk(LIST *list, list_walk_action action, unsigned char* argument)
1 by brian
clean slate
97
{
98
  while (list)
99
  {
266.6.9 by Andy Lester
Removed unused function list_length
100
    int error;
1 by brian
clean slate
101
    if ((error = (*action)(list->data,argument)))
102
      return error;
103
    list=list_rest(list);
104
  }
105
  return 0;
106
}