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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/*
Generic linked list
$Id: list.c,v 1.14 2009/03/14 12:54:53 alexsisson Exp $
(C) Copyright 2004-2009 Alex Sisson (alexsisson@gmail.com)
This file is part of bosh.
bosh is free software; you can redistribute it and/or modify
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.
bosh 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 bosh; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
/* initialse list structure */
int list_init(struct list *l) {
/* the root is a pseudo node at the start which will never contain data */
l->root = malloc(sizeof(struct node));
l->root->data = NULL;
l->root->size = 0;
l->root->next = malloc(sizeof(struct node));
l->root->next->data = NULL;
l->root->next->size = 0;
l->root->next->next = NULL;
l->last = l->root->next;
l->items = 0;
l->next = NULL;
return 0;
}
/* free the list */
int list_free(struct list *l) {
l->iter = l->root;
while(l->iter) {
l->root = l->root->next;
if(l->iter->size)
free(l->iter->data);
free(l->iter);
l->iter = l->root;
}
return 0;
}
/* free and init the list */
int list_reinit(struct list *l) {
list_free(l);
list_init(l);
return 0;
}
/* add an item */
int list_add(struct list *l, void *data) {
l->last->data = data;
l->last->next = malloc(sizeof(struct node));
l->last->next->next = NULL;
l->last->next->data = NULL;
l->last->next->size = 0;
l->last = l->last->next;
l->items++;
return 0;
}
/* duplicate data and add */
int list_adddup(struct list *l, void *data, int size) {
l->last->data = malloc(size);
memcpy(l->last->data,data,size);
l->last->size = size;
l->last->next = malloc(sizeof(struct node));
l->last->next->next = NULL;
l->last->next->data = NULL;
l->last->next->size = 0;
l->last = l->last->next;
l->items++;
return 0;
}
/*
insert an item after index
NOT WORKING!
*/
int list_ins(struct list *l, int index, void *data)
{
struct node *temp;
if(index>=l->items)
return -1;
l->iter = l->root;
while(index) {
if(!l->iter->next)
return -1;
l->iter = l->iter->next;
index--;
}
l->iter->data = data;
l->iter->size = 0;
temp = l->iter->next;
l->iter->next = malloc(sizeof(struct node));
l->iter->next->next = temp;
l->iter->next->data = NULL;
l->last->next->size = 0;
l->items++;
return 0;
}
/*
insert an item after index
*/
int list_insdup(struct list *l, int index, void *data, int size)
{
struct node *temp;
if(index>=l->items)
return -1;
l->iter = l->root;
while(index) {
if(!l->iter->next)
return -1;
l->iter = l->iter->next;
index--;
}
temp = l->iter->next;
l->iter->next = malloc(sizeof(struct node));
l->iter->next->next = temp;
l->iter->next->data = malloc(size);
memcpy(l->iter->next->data,data,size);
l->iter->next->size = size;
l->items++;
return 0;
}
/*
delete item
*/
int list_del(struct list *l, int index) {
struct node *temp;
if(index>=l->items)
return -1;
if(index) {
index--;
l->iter = l->root->next;
while(index-- && l->iter->next->next) {
l->iter = l->iter->next;
// fprintf(stderr,"del: index=%d current=%s next=%s\n",index,l->iter?l->iter->data:"NULL",l->iter->next?l->iter->next->data:"NULL");
}
}
else {
l->iter = l->root;
}
//fprintf(stderr,"del: index=%d current=%s next=%s\n",index,l->iter?l->iter->data:"NULL",l->iter->next?l->iter->next->data:"NULL");
if(l->iter->next->size)
free(l->iter->next->data);
temp = l->iter->next;
l->iter->next = l->iter->next->next;
free(temp);
l->items--;
return 0;
}
/*
get item from list
*/
void *list_get(struct list *l, int index)
{
l->iter = l->root->next;
while(index-- && l->iter->next)
l->iter = l->iter->next;
return l->iter->data;
}
/*
get item and duplicate (caller needs to free) NOT FINISHED
*/
void *list_getdup(struct list *l, int index)
{
// void *data;
l->iter = l->root;
while(--index && l->iter->next->next)
l->iter = l->iter->next;
//l->iter->next->data;
return NULL;
}
/*
return number of items
*/
int list_items(struct list *l)
{
return l->items;
}
/*
was this data duplicated when added?
*/
int list_isdup(struct list *l, int index)
{
l->iter = l->root;
while(--index && l->iter->next->next)
l->iter = l->iter->next;
return l->iter->next->size>0;
}
/*
start iterating through loop
*/
int list_start(struct list *l, int index)
{
l->next = l->root->next;
while(index && l->next->next) {
l->next = l->next->next;
index--;
}
return 0;
}
/*
get next item
*/
void *list_next(struct list *l)
{
l->iter = l->next;
if(l->next->next)
l->next = l->next->next;
return l->iter->data;
}
/*
debug
*/
int list_debug(struct list *l)
{
int i = 0;
l->iter = l->root->next;
printf("items: %d\n",l->items);
for(i=0;i<l->items;i++) {
printf("%4d:contents=%s\n",i,(char*)l->iter->data);
l->iter = l->iter->next;
}
return 0;
}
#ifdef TEST
#include <ctype.h>
int main()
{
struct list l;
void *d;
int i,n;
char s[256];
list_init(&l);
while(1) {
fputc('>',stdout);
fgets(s,sizeof(s),stdin);
*strchr(s,'\n')=0;
fprintf(stderr,"%s\n",s);
if(s[0]=='x')
break;
if(isdigit(s[0])) {
if(s[1])
list_insdup(&l,s[0]-'0',s+1,strlen(s+1)+1);
else
list_del(&l,s[0]-'0');
}
else if(s[0]=='R')
list_reinit(&l);
else
list_adddup(&l,s,strlen(s)+1);
list_debug(&l);
// for(i=0;i<5;i++)
// list_adddup(&l,"hello",6);
// list_reinit(&l);
}
return 0;
}
#endif
|