summaryrefslogtreecommitdiff
path: root/test/test_save.c
blob: 6e3be63ed3c3dfa59007ef34f4d6657162a96792 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <stdio.h>
#include <stdlib.h>

#include <netbytes.h>

void pr_u8( nb_u8 *u8 )
{
	if ( !u8 )
	{
		printf("u8: NULL\n");
		return;
	} else
	{
		printf("u8:    t=0x%x v=0x%x\n", u8->type, u8->val );
	}
}

void pr_u8arr( nb_u8arr *u8arr )
{
	if (!u8arr)
	{
		printf("u8arr: NULL\n");
		return ;
	} else
	{
		printf("u8arr: t=0x%x l=0x%x v=0x%x\n",
			u8arr->type, u8arr->len, u8arr->val );
	}
}

void pr_store( netbyte_store *nb )
{
	int i;

	if (!nb)
	{
		printf("nb: NULL\n");
		return;
	}

	printf("nb: s=0x%x c=0x%x\n", nb->size, nb->count );
	for (i=0;i<nb->count;i++)
	{
		printf("\t[%x] -> ",i );
		if ( nb->types[i].type == NBT_U8 )
		{
			pr_u8( (nb_u8 *)nb->types[i].nb_val );
		} else if ( nb->types[i].type == NBT_U8ARRAY )
		{
			pr_u8arr( (nb_u8arr *)nb->types[i].nb_val );
		} else
		{
			printf("Unknown\n");
		}
	}
}

int main()
{
	FILE *f=NULL;
	int er;
	netbyte_store nb;
	netbyte_store nb2;
	nb_u8         u8;
	nb_u8arr      u8arr;
	const int len = 11;
	char *str = "0123456789";
	uint8_t *res=NULL;

	printf("Start test\n");

	nb_init( &nb );
	er = nb_u8_create(    &u8,    0x10 );
	if (er)
		printf("er create u8: %d\n",er);
	er = nb_u8arr_create( &u8arr, len, &str[0] );
	if (er)
		printf("er create u8arr: %d\n",er);


	pr_u8( &u8 );
	pr_u8arr( &u8arr );

	#if 1
	pr_store( &nb );
	er = nb_add_u8( &nb, &u8 );
	if (er)
		printf("er add u8: %d\n",er);
	#endif

	#if 1
	pr_store( &nb );
	er = nb_add_u8arr( &nb, &u8arr );
	if (er)
		printf("er add u8arr: %d\n",er);
	#endif

	pr_store( &nb );
	res = nb_create( &nb );
	pr_store( &nb );

	printf("res 0x%x\n", res);

	f = fopen("test.nb","w+");
	fwrite( res, 1, nb.size , f );
	fclose( f );

	printf("LOAD:-----\n");
	nb_init( &nb2 );
	pr_store( &nb2 );
	nb_load( &nb2, res );
	pr_store( &nb2 );

	printf( "->1 [%s]\n", ((nb_u8arr *)nb2.types[1].nb_val)->val );

	__NBT_TYPED *t=0xf8;
	nb_u8arr *v;
	nb_type( &nb2, 1, &t );
	printf("get type:  %02x\n", t );
	nb_val( &nb2, 1, (uint8_t **)&v );
	printf("get value: %02x\n", v->val );

	free( res );

	printf("End test\n");
	return 0;
}