aboutsummaryrefslogblamecommitdiffstats
path: root/mtable.c
blob: 0f81a27437449066b92891fb8db4b94cf57e8ccb (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14













                                                                               
                                      



























































































                                                                                        
#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_table));

	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 );
}