diff options
| author | Tomas Chvatal (scarabeus) <tomas.chvatal@gmail.com> | 2008-10-22 22:50:54 +0200 |
|---|---|---|
| committer | Tomas Chvatal (scarabeus) <tomas.chvatal@gmail.com> | 2008-10-22 22:50:54 +0200 |
| commit | e12753d2ec9db83ec08bb9db63902a82fb245cdd (patch) | |
| tree | 6e4a63b790d6335f969f88b797a927e0fb25f37a /src/base/idManager.c~ | |
| parent | 0b4d9821c7333b8c4bdf7e0eeb750aef502a1137 (diff) | |
X
Diffstat (limited to 'src/base/idManager.c~')
| -rw-r--r-- | src/base/idManager.c~ | 209 |
1 files changed, 209 insertions, 0 deletions
diff --git a/src/base/idManager.c~ b/src/base/idManager.c~ new file mode 100644 index 0000000..fd4cac9 --- /dev/null +++ b/src/base/idManager.c~ @@ -0,0 +1,209 @@ + +#include <stdlib.h> +#include <stdio.h> +#include <assert.h> + +#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); +} + |