From 2d776fcd61be168e1ff0ff2e4a5e71835e7cea19 Mon Sep 17 00:00:00 2001 From: oroborus Date: Sat, 28 Jun 2008 16:38:35 +0000 Subject: - prekopanie adresarovej struktury, prva cast git-svn-id: http://opensvn.csie.org/tuxanci_ng@77 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e --- src/base/idManager.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/base/idManager.c (limited to 'src/base/idManager.c') 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 +#include +#include + +#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); +} + -- cgit v1.2.3