f84ebb3883c55b3babe96c036aa50506a6bb26d5
[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.6 2006/05/25 01:47:12 johnfranks 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(
38     sl_t *sl)
39 {
40     sl->first = NULL;
41     sl->last  = NULL;
42     sl->nb_element = 0;
43 }
44
45
46 sl_t *
47 new_sl(void)
48 {
49     sl_t *sl;
50     sl = alloc(SIZEOF(sl_t));
51     init_sl(sl);
52     return(sl);
53 }
54
55
56 sl_t *
57 insert_sl(
58     sl_t *sl,
59     char *name)
60 {
61     sle_t *a;
62
63     if(!sl) {
64         sl = new_sl();
65     }
66     a = alloc(SIZEOF(sle_t));
67     a->name = stralloc(name);
68     a->next = sl->first;
69     a->prev = NULL;
70     if(a->next)
71         a->next->prev = a;
72     else
73         sl->last = a;
74     sl->first = a;
75     sl->nb_element++;
76     return(sl);
77 }
78
79
80 sl_t *
81 append_sl(
82     sl_t *      sl,
83     char *      name)
84 {
85     sle_t *a;
86
87     if(!sl) {
88         sl = new_sl();
89     }
90     a = alloc(SIZEOF(sle_t));
91     a->name = stralloc(name);
92     a->prev = sl->last;
93     a->next = NULL;
94     if(a->prev)
95         a->prev->next = a;
96     else
97         sl->first = a;
98     sl->last = a;
99     sl->nb_element++;
100     return(sl);
101 }
102
103
104 sl_t *
105 insert_sort_sl(
106     sl_t *      sl,
107     char *      name)
108 {
109     sle_t *a, *b;
110
111     if(!sl) {
112         sl = new_sl();
113     }
114
115     for(b=sl->first; b != NULL; b=b->next) {
116         int i = strcmp(b->name, name);
117         if(i==0) return(sl); /* already there, no need to insert */
118         if(i>0) break;
119     }
120
121     if(b == sl->first) return insert_sl(sl, name);
122     if(b == NULL)      return append_sl(sl, name);
123
124     a = alloc(SIZEOF(sle_t));
125     a->name = stralloc(name);
126
127     /* insert before b */
128     a->next = b;
129     a->prev = b->prev;
130     b->prev->next = a;
131     b->prev = a;
132     sl->nb_element++;
133     return(sl);
134 }
135
136
137 void
138 free_sl(
139     sl_t *      sl)
140 {
141     sle_t *a, *b;
142
143     if(!sl) return;
144
145     a = sl->first;
146     while(a != NULL) {
147         b = a;
148         a = a->next;
149         amfree(b->name);
150         amfree(b);
151     }
152     amfree(sl);
153 }
154
155
156 void
157 remove_sl(
158     sl_t *      sl,
159     sle_t *     elem)
160 {
161     if(elem->prev)
162         elem->prev->next = elem->next;
163     else
164         sl->first = elem->next;
165
166     if(elem->next)
167         elem->next->prev = elem->prev;
168     else
169         sl->last = elem->prev;
170
171     sl->nb_element--;
172
173     amfree(elem->name);
174     amfree(elem);
175 }
176
177
178 sl_t *
179 duplicate_sl(
180     sl_t *      sl)
181 {
182     sl_t *new_sl = NULL;
183     sle_t *a;
184
185     if(!sl) return new_sl;
186
187     for(a = sl->first; a != NULL; a = a->next) {
188         new_sl = append_sl(new_sl, a->name);
189     }
190
191     return new_sl;
192 }
193
194 /*
195  * Return "true" iff sl is empty (i.e. contains no elements).
196  */
197 int
198 is_empty_sl(
199     sl_t *      sl)
200 {
201     if (sl == NULL)
202         return 1;
203
204     return (sl->nb_element == 0);
205 }