diff options
| author | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-06-28 16:38:35 +0000 |
|---|---|---|
| committer | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-06-28 16:38:35 +0000 |
| commit | 2d776fcd61be168e1ff0ff2e4a5e71835e7cea19 (patch) | |
| tree | 7cf274897bf540d5332d446f583ffd066e4e2e1d /src/base/idManager.c | |
| parent | b877ae23ac5d380ab3aa48d7724045cf4883b186 (diff) | |
- prekopanie adresarovej struktury, prva cast
git-svn-id: http://opensvn.csie.org/tuxanci_ng@77 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
Diffstat (limited to 'src/base/idManager.c')
| -rw-r--r-- | src/base/idManager.c | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/base/idManager.c b/src/base/idManager.c new file mode 100644 index 0000000..fb4a928 --- /dev/null +++ b/src/base/idManager.c @@ -0,0 +1,107 @@ + +#include <stdlib.h> +#include <stdio.h> +#include <assert.h> + +#include "base/main.h" +#include "base/list.h" +#include "base/idManager.h" + +static list_t *listID; +static int lastID; + +void initListID() +{ + listID = newList(); + lastID = 0; + printf("init ID manger..\n"); +} + +int isRegisterID(int id) +{ + int i; + + assert( listID != NULL ); + + for( i = 0 ; i < listID->count ; i++ ) + { + int *z; + + z = (int *) listID->list[i]; + + if( *z == id ) + { + return i; + } + } + + return -1; +} + +int getNewID() +{ + int ret; + + assert( listID != NULL ); + + do{ + ret = random() % MAX_ID; +/* + ret = ++lastID; + + if( lastID > MAX_ID ) + { + lastID = 0; + } +*/ + }while( isRegisterID(ret) != -1 ); + + addList(listID, newInt(ret) ); + + //printf("new ID -> %d\n", ret); + return ret; +} + +void delID(int id) +{ + int index; + + assert( listID != NULL ); + + index = isRegisterID(id); + + if( index == -1 ) + { + assert( ! "takyto ID nebol nikdy registrovany !" ); + return; // ha ha ha + } + + delListItem(listID, index, free); + + return; +} + +void replaceID(int old_id, int new_id) +{ + if( old_id == new_id ) + { + return; + } + + delID(old_id); + + if( new_id != -1 ) + { + assert( isRegisterID(new_id) == -1 ); + addList(listID, newInt(new_id) ); + } +} + +void quitListID() +{ + printf("quit ID manger..\n"); + + assert( listID != NULL ); + destroyListItem(listID, free); +} + |