-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjsox_parser.c
128 lines (118 loc) · 2.79 KB
/
jsox_parser.c
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
128
#include "jsox.h"
int level = 0;
void DumpMessage( PDATALIST pdl ) {
struct jsox_value_container *val;
INDEX idx;
int leader;
level++;
DATA_FORALL( pdl, idx, struct jsox_value_container *, val ) {
if( idx )
printf( ",\n" );
for( leader = 3; leader < level*3; leader++ ) {
putc( ' ', stdout );
}
if( val->name ) printf( "%s:", val->name );
if( val->value_type >= JSOX_VALUE_TYPED_ARRAY && val->value_type <= JSOX_VALUE_TYPED_ARRAY_MAX ) {
printf( "%s(%d)[%s]\n", val->className, val->value_type, val->string );
}
else switch( val->value_type ) {
case JSOX_VALUE_OBJECT:
if( val->className ) printf( "%s", val->className );
printf( "{ \n" );
DumpMessage( val->contains );
printf( " }" );
break;
case JSOX_VALUE_ARRAY:
printf( "[ " );
DumpMessage( val->contains );
printf( " ]" );
break;
case JSOX_VALUE_STRING:
printf( "\"%s\"", val->string );
break;
case JSOX_VALUE_DATE:
printf( "%s", val->string );
break;
case JSOX_VALUE_NUMBER:
if( val->float_result )
printf( "%g", val->result_d );
else
printf( "%d", val->result_n );
break;
case JSOX_VALUE_BIGINT:
printf( "%sn", val->string );
break;
case JSOX_VALUE_NAN:
printf( "NaN" );
break;
case JSOX_VALUE_INFINITY:
printf( "Infinity" );
break;
case JSOX_VALUE_NEG_INFINITY:
printf( "-Infinity" );
break;
case JSOX_VALUE_TRUE:
printf( "true" );
break;
case JSOX_VALUE_FALSE:
printf( "false" );
break;
case JSOX_VALUE_NULL:
printf( "null" );
break;
case JSOX_VALUE_EMPTY:
// array element that is ',,'
printf( "<EMPTY>" );
break;
case JSOX_VALUE_UNDEFINED:
printf( "undefined" );
break;
default :
printf( "\nunhandled value type: %d [%s]\n", val->value_type, val->string );
}
}
level--;
if( !level ) printf( "\n" );
}
void parse( char *fileName ) {
PDATALIST pdl;
FILE *file;
size_t size;
char *data;
int r;
struct jsox_parse_state *parser;
file = fopen( fileName, "rb" );
fseek( file, 0, SEEK_END );
size = ftell( file );
fseek( file, 0, SEEK_SET );
data = (char*)malloc( size );
fread( data, 1, size, file );
fclose( file );
parser = jsox_begin_parse();
do {
//r = jsox_parse_message( data, size, &pdl );
//if( r > 0 ) {
// DumpMessage( pdl );
// jsox_dispose_message( &pdl );
//}
//else if( r <= 0 )
// printf( "Error:%s", GetText( jsox_parse_get_error( NULL ) ) );
r = jsox_parse_add_data( parser, data, size );
free( data );
data = NULL;
size = 0;
if( r >= 0 ) {
pdl = jsox_parse_get_data( parser );
DumpMessage( pdl );
jsox_dispose_message( &pdl );
}
else if( r < 0 )
printf( "Error:%s", GetText( jsox_parse_get_error( NULL ) ) );
} while( r > 0 );
}
int main( int argc, char **argv) {
int n;
for( n = 1; n < argc; n++ ) {
parse( argv[n] );
}
}