#include #include #include #include "main.h" #include "list.h" #include "idManager.h" static list_t *listID; static int lastID; typedef struct id_item_struct { int id; int count; } id_item_t; static id_item_t * newIdItem(int id, int count) { id_item_t *new; new = malloc(sizeof(id_item_t)); new->id = id; new->count = count; return new; } static void destroyIdItem(id_item_t * p) { assert(p != NULL); free(p); } void initListID() { listID = newList(); lastID = 0; #ifdef DEBUG printf(_("Starting ID manger\n")); #endif } int isRegisterID(int id) { int i; assert(listID != NULL); for (i = 0; i < listID->count; i++) { id_item_t *this; this = listID->list[i]; if (this->id == id) { return i; } } return -1; } static int findNewID() { int ret; assert(listID != NULL); if (listID->count >= MAX_ID - 1) { assert(!_("No free ID left!")); } do { //ret = ( random() % (listID->count + 8 ) ) + 1; ret = random() % MAX_ID + 1; } while (isRegisterID(ret) != -1); //printf("new ID %d\n", ret); return ret; } int getNewIDcount(int count) { int id; id = findNewID(); addList(listID, newIdItem(id, count)); return id; } int getNewID() { return getNewIDcount(1); } void incID(int id) { id_item_t *this; int index; assert(listID != NULL); index = isRegisterID(id); if (index == -1) { assert(!_("This kind of ID was never registered!")); return; // ha ha ha } this = listID->list[index]; this->count++; //printf("inc ID %d %d\n", this->id, this->count); return; } void delID(int id) { id_item_t *this; int index; assert(listID != NULL); index = isRegisterID(id); if (index == -1) { assert(!_("This kind of ID was never registered!")); return; // ha ha ha } this = listID->list[index]; this->count--; //printf("dec ID %d %d\n", this->id, this->count); if (this->count <= 0) { delListItem(listID, index, free); //printf("listID->count = %d\n", listID->count); } return; } void replaceID(int old_id, int new_id) { int index_old_id; int index_new_id; id_item_t *this; if (old_id == new_id) { return; } index_old_id = isRegisterID(old_id); assert(index_old_id != -1); index_new_id = isRegisterID(new_id); assert(index_new_id == -1); this = listID->list[index_old_id]; this->id = new_id; } void infoID(int id) { id_item_t *this; int index; assert(listID != NULL); index = isRegisterID(id); if (index == -1) { #ifdef DEBUG printf(_("ID %d does not exist\n"), id); #endif return; } this = listID->list[index]; #ifdef DEBUG printf(_("ID %d (count %d)\n"), this->id, this->count); #endif return; } void quitListID() { #ifdef DEBUG printf(_("Quitting ID manger\n")); #endif assert(listID != NULL); destroyListItem(listID, destroyIdItem); }