44599602c265c6707bc8bb0f6256290215a803a6
[debian/amanda] / common-src / sl.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Author: James da Silva, Systems Design and Analysis Group
24  *                         Computer Science Department
25  *                         University of Maryland at College Park
26  */
27 /*
28  * $Id: sl.c,v 1.5 2005/09/30 19:01:43 martinea Exp $
29  *
30  * A doubly linked list of string (char *)
31  */
32
33 #include "amanda.h"
34 #include "sl.h"
35
36
37 void init_sl(sl)
38 sl_t *sl;
39 {
40     sl->first = NULL;
41     sl->last  = NULL;
42     sl->nb_element = 0;
43 }
44
45
46 sl_t *new_sl() {
47     sl_t *sl;
48     sl = alloc(sizeof(sl_t));
49     init_sl(sl);
50     return(sl);
51 }
52
53
54 sl_t *insert_sl(sl, name)
55 sl_t *sl;
56 char *name;
57 {
58     sle_t *a;
59
60     if(!sl) {
61         sl = new_sl();
62     }
63     a = alloc(sizeof(sle_t));
64     a->name = stralloc(name);
65     a->next = sl->first;
66     a->prev = NULL;
67     if(a->next)
68         a->next->prev = a;
69     else
70         sl->last = a;
71     sl->first = a;
72     sl->nb_element++;
73     return(sl);
74 }
75
76
77 sl_t *append_sl(sl, name)
78 sl_t *sl;
79 char *name;
80 {
81     sle_t *a;
82
83     if(!sl) {
84         sl = new_sl();
85     }
86     a = alloc(sizeof(sle_t));
87     a->name = stralloc(name);
88     a->prev = sl->last;
89     a->next = NULL;
90     if(a->prev)
91         a->prev->next = a;
92     else
93         sl->first = a;
94     sl->last = a;
95     sl->nb_element++;
96     return(sl);
97 }
98
99
100 sl_t *insert_sort_sl(sl, name)
101 sl_t *sl;
102 char *name;
103 {
104     sle_t *a, *b;
105
106     if(!sl) {
107         sl = new_sl();
108     }
109
110     for(b=sl->first; b != NULL; b=b->next) {
111         int i = strcmp(b->name, name);
112         if(i==0) return(sl); /* already there, no need to insert */
113         if(i>0) break;
114     }
115
116     if(b == sl->first) return insert_sl(sl, name);
117     if(b == NULL)      return append_sl(sl, name);
118
119     a = alloc(sizeof(sle_t));
120     a->name = stralloc(name);
121
122     /* insert before b */
123     a->next = b;
124     a->prev = b->prev;
125     b->prev->next = a;
126     b->prev = a;
127     sl->nb_element++;
128     return(sl);
129 }
130
131
132 void free_sl(sl)
133 sl_t *sl;
134 {
135     sle_t *a, *b;
136
137     if(!sl) return;
138
139     a = sl->first;
140     while(a != NULL) {
141         b = a;
142         a = a->next;
143         amfree(b->name);
144         amfree(b);
145     }
146     amfree(sl);
147 }
148
149
150 void remove_sl(sl, elem)
151 sl_t *sl;
152 sle_t *elem;
153 {
154     if(elem->prev)
155         elem->prev->next = elem->next;
156     else
157         sl->first = elem->next;
158
159     if(elem->next)
160         elem->next->prev = elem->prev;
161     else
162         sl->last = elem->prev;
163
164     sl->nb_element--;
165
166     amfree(elem->name);
167     amfree(elem);
168 }
169
170
171 sl_t *duplicate_sl(sl)
172 sl_t *sl;
173 {
174     sl_t *new_sl = NULL;
175     sle_t *a;
176
177     if(!sl) return new_sl;
178
179     for(a = sl->first; a != NULL; a = a->next) {
180         new_sl = append_sl(new_sl, a->name);
181     }
182
183     return new_sl;
184 }
185
186 /*
187  * Return "true" iff sl is empty (i.e. contains no elements).
188  */
189 int is_empty_sl(sl)
190 sl_t *sl;
191 {
192     if (sl == NULL)
193         return 1;
194
195     return (sl->nb_element == 0);
196 }