diff options
Diffstat (limited to 'mtable.c')
-rw-r--r-- | mtable.c | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/mtable.c b/mtable.c new file mode 100644 index 0000000..7d705e8 --- /dev/null +++ b/mtable.c @@ -0,0 +1,107 @@ +#include "mtable.h" + + +/*****************************************************************************/ +mt_table* mt_create() +{ + mt_table *mt = NULL; + + mt = malloc( sizeof(mt_table) ); + if (mt == NULL) + { + printf("Cannot allocate mem for mt_table\n"); + return NULL; + } + memset(mt,0,sizeof(mt)); + + mt->table = darr_create( sizeof(mt_range), 100 ); + if (mt->table == NULL) + { + printf("Cannot allocate ->table\n"); + goto error; + } + + return mt; + +error: + free( mt ); + return NULL; + +} + + +/*****************************************************************************/ +int mt_add( mt_table *mt, mt_range *rng ) +{ + if (rng == NULL) + { + return -1; + } + + if ( darr_push( mt->table, rng ) != 0 ) + { + printf("Cannot add new table row\n"); + return -1; + } + + return 0; +} + +/*****************************************************************************/ +mt_range* mt_search( mt_table *mt, int pos ) +{ + int i; + mt_range *el = NULL; + + for (i=0; i<darr_end(mt->table); i++) + { + el = darr_get( mt->table, i ); + if ((pos >= el->start) && (pos <= el->end)) + { + return el; + } + } + + return NULL; +} + + +/*****************************************************************************/ +int mt_print( mt_table *mt ) +{ + int i; + mt_range *rng = NULL; + + printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); + printf("+ START + END + CMP + SIZE + VAL + SIZE +\n"); + printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); + for (i=0; i<darr_end(mt->table); i++) + { + rng = mt_get(mt,i); + if (rng == NULL) + continue; + printf("+ %8d + %8d + %08x + %8d + %08x + %8d +\n", + rng->start, rng->end, rng->cmp, rng->cmp_sz, + rng->val, rng->val_sz); + } + printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); + + return 0; +} + + +/*****************************************************************************/ +void mt_destroy( mt_table *mt ) +{ + if ( mt == NULL ) + { + return; + } + + if ( mt->table != NULL) + { + darr_destroy( mt->table ); + } + + free( mt ); +} |