robot_list.c 5.31 KB
Newer Older
Timothy Stack's avatar
Timothy Stack committed
1 2
/*
 * Copyright (c) 2005 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file 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 Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
Timothy Stack's avatar
Timothy Stack committed
22
 */
23

24 25 26 27 28
#include "config.h"

#include <stdlib.h>

#include "robot_list.h"
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

struct robot_list *robot_list_create() {
  struct robot_list *tmp = (struct robot_list *)malloc(sizeof(struct robot_list));
  if (tmp != NULL) {
	tmp->type = -1;
    tmp->item_count = 0;
    tmp->head = NULL;
    tmp->tail = NULL;
  }
  return tmp;
}

void robot_list_destroy(struct robot_list *l) {
  if (l->head != NULL) {
    struct robot_list_item *n = l->head;
    struct robot_list_item *nn = NULL;
    while (n != NULL) {
      nn = n->next;
      free(n);
      n = nn;
    }
  }
  free(l);
}

int robot_list_append(struct robot_list *l,int id,void *data) {
  if (l == NULL || data == NULL) {
    return 0;
  }
  else {
    struct robot_list_item *n = (struct robot_list_item *)malloc(sizeof(struct robot_list_item));
	n->id = id;
    n->data = data;
    n->next = NULL;
    if (n != NULL) {
      if (l->head == NULL) {
		l->head = n;
		l->tail = n;
      }
      else {
		l->tail->next = n;
		l->tail = n;
      }
      ++(l->item_count);
      return 1;
    }
    else {
      return 0;
    }
  }
}

int robot_list_insert(struct robot_list *l,int id,void *data) {
  if (l == NULL || data == NULL) {
    return 0;
  }
  else {
    struct robot_list_item *n = (struct robot_list_item *)malloc(sizeof(struct robot_list_item));
	n->id = id;
    n->data = data;
    n->next = NULL;
    if (n != NULL) {
      if (l->head == NULL) {
	l->head = n;
	l->tail = n;
      }
      else {
	n->next = l->head;
	l->head = n;
      }
      ++(l->item_count);
      return 1;
    }
    else {
      return 0;
    }
  }
}

void *robot_list_search(struct robot_list *l,int id) {
  if (l == NULL) {
	return NULL;
  }
  else {
	struct robot_list_item *h = NULL;
    struct robot_list_item *i = l->head;

    void *retval = NULL;

    while (i != NULL) {
      if (i->id == id) {
		retval = i->data;
		break;
      }
      h = i;
      i = i->next;
    }
    
    return retval;
  }
}
	

int robot_list_remove_by_data(struct robot_list *l,void *data) {
  if (l == NULL || data == NULL) {
    return -1;
  }
  else {
    struct robot_list_item *h = NULL;
    struct robot_list_item *i = l->head;
	
    int retval = -1;
	
    while (i != NULL) {
      if (i->data == data) {
		// if we're at head and there's only a single elm
		if (i == l->head && l->head == l->tail) {
		  l->head = l->tail = NULL;
		}
		else {
		  if (i == l->head) {
			l->head = i->next;
		  }
		  else if (i == l->tail) {
			h->next = NULL;
			l->tail = h;
		  }
		  else {
			h->next = i->next;
		  }
		}
		
		retval = i->id;
		free(i);
		--(l->item_count);
		
		break;
      }
      
      h = i;
      i = i->next;
    }
    
    return retval;
  }
}

void *robot_list_remove_by_id(struct robot_list *l,int id) {
  if (l == NULL) {
    return NULL;
  }
  else {
    struct robot_list_item *h = NULL;
    struct robot_list_item *i = l->head;

    void *retval = NULL;

    while (i != NULL) {
      if (i->id == id) {
		// if we're at head and there's only a single elm
		if (i == l->head && l->head == l->tail) {
		  l->head = l->tail = NULL;
		}
		else {
		  if (i == l->head) {
			l->head = i->next;
		  }
		  else if (i == l->tail) {
			h->next = NULL;
			l->tail = h;
		  }
		  else {
			h->next = i->next;
		  }
		}
		
		retval = i->data;
		free(i);
		--(l->item_count);
		
		break;
      }
      
      h = i;
      i = i->next;
    }
    
    return retval;
  }
}

struct robot_list_enum *robot_list_enum(struct robot_list *l) {
  if (l == NULL || l->item_count == 0) {
    return NULL;
  }
  else {
    struct robot_list_enum *e = (struct robot_list_enum *)malloc(sizeof(struct robot_list_enum));
    void **tmp = NULL;
    if (e == NULL) {
      return NULL;
    }
    else {
231 232 233
      struct robot_list_item *a;
      int i;
      
234 235 236 237 238 239 240 241 242 243 244
      tmp = (void *)malloc(sizeof(void *)*(l->item_count));
      if (tmp == NULL) {
		free(e);
		return NULL;
      }
	  
      e->data = tmp;
      e->current_index = 0;
      e->size = l->item_count;
	  
      /* scroll through list forwards and copy data into e->data */
245 246 247
      a = l->head;
      i = 0;
      
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
      while (a != NULL) {
		e->data[i] = a->data;
		a = a->next;
		++i;
      }
      return e;
    }
  }
}

void *robot_list_enum_next_element(struct robot_list_enum *e) {
  if (e != NULL) {
    if (e->current_index == e->size) {
      return NULL;
    }
    else {
      void *retval = e->data[e->current_index];
      ++(e->current_index);
      return retval;
    }
  }
  else {
    return NULL;
  }
}

void robot_list_enum_destroy(struct robot_list_enum *e) {
  if (e != NULL) {
    free(e->data);
    free(e);
  }
}