summaryrefslogtreecommitdiff
path: root/mtable.c
blob: 7d705e853b769dff78340e24e7ac63b09cc2e1ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 );
}