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