Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
exnetwmsg.c
Go to the documentation of this file.
1 #include <nilorea.h>
2
3 NETW_MSG *test = NULL ;
4 N_STR *str = NULL;
5
6 double float_val = 0.0 ;
7 int it = 0 , int_val = 0 ;
8
9 size_t fsize = 0 ;
10
11 FILE *out = NULL;
12
13 int main( void )
14 {
15 set_log_level( LOG_DEBUG );
16
17 allegro_init();
18
19 create_msg( &test );
20
21 printf("\nAdding floating nb:");
22 for( it = 0 ; it < 5 ; it ++ )
23 {
24 float_val = (100.0-rand()%200)/3.0 ;
25 if( add_nb_to_msg( test , float_val ) == FALSE )
26 {
27 printf( "Error adding %g\n" , float_val );
28 }
29 else
30 {
31 printf(" %g" , float_val);
32 }
33 }
34
35 printf("\nAdding integer nb:");
36 for( it = 0 ; it < 5 ; it ++ )
37 {
38 int_val = (100-rand()%200)/3 ;
39 if( add_int_to_msg( test , int_val ) == FALSE )
40 {
41 printf( "Error adding %d\n" , int_val );
42 }
43 else
44 {
45 printf(" %d" , int_val);
46 }
47 }
48
49 printf("\nAdding str:");
50
51 for( it = 0 ; it < 10 ; it ++ )
52 {
53
54 int_val = 1+rand()%10;
55
56 str = new_str( int_val );
57
58 fill_str( str -> data , 'A' , int_val-1 ) ;
59
60 str->data[ int_val - 1 ] = 0;
61 str->written = int_val - 1 ;
62
63 add_strptr_to_msg( test , str );
64
65 printf("adding:%3d:%s\n", str -> length , str -> data );
66
67
68 }
69
70 printf("Making str from message\n");
71
72 str = make_str_from_msg( test );
73 printf("writing to test.log %d char\n",str -> length );
74 out = fopen( "test.log","wb" );
75 fwrite( str -> data , sizeof( char ) ,str -> length , out );
76 fclose( out);
77
78 puts(" ");
79
80 printf( "Killing the message now we have a string\n" );
81
82 delete_msg( &test );
83
84 printf("making a new msg from the string we have\n" );
85
86 test = make_msg_from_str( str );
87
88 printf("printing what we get from our msg\n");
89
90 while( get_nb_from_msg( test , &float_val ) != FALSE )
91 printf( "double:%g\n" , float_val );
92
93 while( get_int_from_msg( test , &int_val ) != FALSE )
94 printf( "int:%d\n" , int_val );
95
96 while( ( str = get_str_from_msg( test ) ) != NULL )
97 printf( "getting %d:%s\n" , str -> length , str -> data );
98
99
100 printf("delete_msg\n");
101
102 delete_msg( &test );
103
104 Malloc( str , N_STR , 1 );
105 out = fopen( "test.log" , "r+" );
106 fseek( out , 0 , SEEK_END );
107 fsize = ftell( out );
108 fseek( out , 0 , SEEK_SET);
109
110 Malloc( str -> data , char , fsize );
111 fread( str -> data , 1 , fsize , out );
112
113 fclose(out);
114
115 test = make_msg_from_str( str );
116
117 printf("printing what we get from our file msg\n");
118
119 while( get_nb_from_msg( test , &float_val ) )
120 printf( "double:%g\n" , float_val );
121
122 while( get_int_from_msg( test , &int_val ) )
123 printf( "int:%d\n" , int_val );
124
125 while( ( str = get_str_from_msg( test ) ) != NULL )
126 printf( "getting %d:%s\n" , str -> length , str -> data );
127
128 /*
129 * N_STR test
130 */
131 printf( "creating new str\n" );
132
133 N_STR *nstr = new_str( 10 );
134
135 for( int it = 0 ; it < 10 ; it ++ )
136 {
137 int result = nstrcat( nstr , "aaa" , 3 );
138 int result = nstrcat( nstr , nstr2->data , nstr2->length );
139
140 printf( "Result: %s string: %d/%d , %s\n" , (result==TRUE)?"TRUE":"FALSE" , nstr -> written , nstr -> length , _str( totonstr -> data ) );
141 }
142
143 free_str( &nstr );
144
145 nstr = file_to_nstr( "TESTFILE.TXT" );
146 if( !nstr )
147 {
148 printf( "Error loading TESTFILE.TXT" );
149 return FALSE ;
150 }
151
152 printf( "File:%d/%d\n%s\n" , nstr->written , nstr -> length , nstr -> data );
153
154 if( str_to_file( nstr , "TESTFILE.OUT" ) != TRUE )
155 {
156 printf( "Error writing to TESTFILE.OUT\n" );
157 }
158 else
159 {
160 printf( "SUCCESS !\n");
161 }
162
163 return 0;
164
165 }END_OF_MAIN()
166