syck_.c

filter: Statementstmt 77.7% 206 / 265 Branchbran 73.6% 53 / 72 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.8% 259 / 337 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« emitter.c ↑ Summary perl_syck.h »
1 /*
2 * syck.c
3 *
4 * $Author: why $
5 * $Date: 2005-01-01 10:06:25 +0800 (六, 01 1 2005) $
6 *
7 * Copyright (C) 2003 why the lucky stiff
8 */
9 #include <stdio.h>
10 #include <string.h>
11  
12 #include "syck.h"
13  
14 void syck_parser_pop_level( SyckParser * );
15  
16 /*
17 * Custom assert
18 */
19 void
20 0 syck_assert( char *file_name, unsigned line_num )
21 {
22 0 fflush( NULL );
23 0 fprintf( stderr, "\nAssertion failed: %s, line %u\n",
24 file_name, line_num );
25 0 fflush( stderr );
26 0 abort();
27 }
28  
29 /*
30 * Allocates and copies a string
31 */
32 char *
33 18748416 syck_strndup( char *buf, long len )
34 {
35 18748416 char *new = S_ALLOC_N( char, len + 1 );
36 18748416 S_MEMZERO( new, char, len + 1 );
37 18748416 S_MEMCPY( new, buf, char, len );
38 18748416 return new;
39 }
40  
41 /*
42 * Default FILE IO function
43 */
44 long
45 0 syck_io_file_read( char *buf, SyckIoFile *file, long max_size, long skip )
46 {
47 0 long len = 0;
48  
49 ASSERT( file != NULL );
50  
51 0 max_size -= skip;
52 0 len = fread( buf + skip, sizeof( char ), max_size, file->ptr );
53 0 len += skip;
54 0 buf[len] = '\0';
55  
56 0 return len;
57 }
58  
59 /*
60 * Default string IO function
61 */
62 long
63 248930 syck_io_str_read( char *buf, SyckIoStr *str, long max_size, long skip )
64 {
65 char *beg;
66 248930 long len = 0;
67  
68 ASSERT( str != NULL );
69 248930 beg = str->ptr;
70 248930 ▶ if ( max_size >= 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( max_size >= 0 ) 248930 0 248930
71 {
72 248930 max_size -= skip;
73 248930 ▶ if ( max_size <= 0 ) max_size = 0;
Branch: 1 branch
BranchTrueFalseTotal
if ( max_size <= 0 ) max_size = 0; 0 248930 248930
74 248930 else str->ptr += max_size;
75  
76 248930 ▶ if ( str->ptr > str->end )
Branch: 1 branch
BranchTrueFalseTotal
if ( str->ptr > str->end ) 9930 239000 248930
77 {
78 9930 str->ptr = str->end;
79 }
80 }
81 else
82 {
83 /* Use exact string length */
84 0 ▶ while ( str->ptr < str->end ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( str->ptr < str->end ) { 0 0 0
85 0 ▶ if (*(str->ptr++) == '\n') break;
Branch: 1 branch
BranchTrueFalseTotal
if (*(str->ptr++) == '\n') break; 0 0 0
86 }
87 }
88 248930 ▶ if ( beg < str->ptr )
Branch: 1 branch
BranchTrueFalseTotal
if ( beg < str->ptr ) 241214 7716 248930
89 {
90 241214 len = ( str->ptr - beg );
91 241214 S_MEMCPY( buf + skip, beg, char, len );
92 }
93 248930 len += skip;
94 248930 buf[len] = '\0';
95  
96 248930 return len;
97 }
98  
99 void
100 6870 syck_parser_reset_levels( SyckParser *p )
101 {
102 6881 ▶ while ( p->lvl_idx > 1 )
Branch: 1 branch
BranchTrueFalseTotal
while ( p->lvl_idx > 1 ) 11 6870 6881
103 {
104 11 syck_parser_pop_level( p );
105 }
106  
107 6870 ▶ if ( p->lvl_idx < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->lvl_idx < 1 ) 2214 4656 6870
108 {
109 2214 p->lvl_idx = 1;
110 2214 p->levels[0].spaces = -1;
111 2214 p->levels[0].ncount = 0;
112 2214 p->levels[0].domain = syck_strndup( "", 0 );
113 }
114 6870 p->levels[0].status = syck_lvl_header;
115 6870 }
116  
117 void
118 2214 syck_parser_reset_cursor( SyckParser *p )
119 {
120 2214 ▶ if ( p->buffer == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->buffer == NULL ) 2214 0 2214
121 {
122 2214 p->buffer = S_ALLOC_N( char, p->bufsize );
123 2214 S_MEMZERO( p->buffer, char, p->bufsize );
124 }
125 2214 p->buffer[0] = '\0';
126  
127 2214 p->cursor = NULL;
128 2214 p->lineptr = NULL;
129 2214 p->linectptr = NULL;
130 2214 p->token = NULL;
131 2214 p->toktmp = NULL;
132 2214 p->marker = NULL;
133 2214 p->limit = NULL;
134  
135 2214 p->root = 0;
136 2214 p->root_on_error = 0;
137 2214 p->linect = 0;
138 2214 p->eof = 0;
139 2214 p->last_token = 0;
140 2214 p->force_token = 0;
141 2214 }
142  
143 /*
144 * Value to return on a parse error
145 */
146 void
147 0 syck_parser_set_root_on_error( SyckParser *p, SYMID roer )
148 {
149 0 p->root_on_error = roer;
150 0 }
151  
152 /*
153 * Allocate the parser
154 */
155 SyckParser *
156 2214 syck_new_parser(void)
157 {
158 SyckParser *p;
159 2214 p = S_ALLOC( SyckParser );
160 2214 S_MEMZERO( p, SyckParser, 1 );
161 2214 p->lvl_capa = ALLOC_CT;
162 2214 p->levels = S_ALLOC_N( SyckLevel, p->lvl_capa );
163 2214 p->input_type = syck_yaml_utf8;
164 2214 p->io_type = syck_io_str;
165 2214 p->io.str = NULL;
166 2214 p->syms = NULL;
167 2214 p->anchors = NULL;
168 2214 p->bad_anchors = NULL;
169 2214 p->implicit_typing = 1;
170 2214 p->taguri_expansion = 0;
171 2214 p->bufsize = SYCK_BUFFERSIZE;
172 2214 p->buffer = NULL;
173 2214 p->lvl_idx = 0;
174 2214 syck_parser_reset_levels( p );
175 2214 return p;
176 }
177  
178 int
179 18724210 syck_add_sym( SyckParser *p, char *data )
180 {
181 18724210 SYMID id = 0;
182 18724210 ▶ if ( p->syms == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->syms == NULL ) 2203 18722007 18724210
183 {
184 2203 p->syms = st_init_numtable();
185 }
186 18724210 id = p->syms->num_entries + 1;
187 18724210 st_insert( p->syms, id, (st_data_t)data );
188 18724210 return id;
189 }
190  
191 int
192 18724229 syck_lookup_sym( SyckParser *p, SYMID id, char **data )
193 {
194 18724229 ▶ if ( p->syms == NULL ) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ( p->syms == NULL ) return 0; 0 18724229 18724229
195 18724229 return st_lookup( p->syms, id, (st_data_t *)data );
196 }
197  
198 enum st_retval
199 37 syck_st_free_nodes( st_data_t key, st_data_t value, st_data_t arg )
200 {
201 37 SyckNode *n = (SyckNode *)value;
202 37 ▶ if ( n != (void *)1 ) syck_free_node( n );
Branch: 1 branch
BranchTrueFalseTotal
if ( n != (void *)1 ) syck_free_node( n ); 37 0 37
203 37 return ST_CONTINUE;
204 }
205  
206 void
207 4656 syck_st_free( SyckParser *p )
208 {
209 /*
210 * Free the anchor tables
211 */
212 4656 ▶ if ( p->anchors != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->anchors != NULL ) 22 4634 4656
213 {
214 22 st_foreach( p->anchors, syck_st_free_nodes, 0 );
215 22 st_free_table( p->anchors );
216 22 p->anchors = NULL;
217 }
218  
219 4656 ▶ if ( p->bad_anchors != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->bad_anchors != NULL ) 6 4650 4656
220 {
221 6 st_foreach( p->bad_anchors, syck_st_free_nodes, 0 );
222 6 st_free_table( p->bad_anchors );
223 6 p->bad_anchors = NULL;
224 }
225 4656 }
226  
227 void
228 2214 syck_free_parser( SyckParser *p )
229 {
230 /*
231 * Free the adhoc symbol table
232 */
233 2214 ▶ if ( p->syms != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->syms != NULL ) 2203 11 2214
234 {
235 2203 st_free_table( p->syms );
236 2203 p->syms = NULL;
237 }
238  
239 /*
240 * Free tables, levels
241 */
242 2214 syck_st_free( p );
243 2214 syck_parser_reset_levels( p );
244 2214 S_FREE( p->levels[0].domain );
245 2214 S_FREE( p->levels );
246  
247 2214 ▶ if ( p->buffer != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->buffer != NULL ) 2214 0 2214
248 {
249 2214 S_FREE( p->buffer );
250 }
251 2214 free_any_io( p );
252 2214 S_FREE( p );
253 2214 }
254  
255 void
256 2214 syck_parser_handler( SyckParser *p, SyckNodeHandler hdlr )
257 {
258 ASSERT( p != NULL );
259 2214 p->handler = hdlr;
260 2214 }
261  
262 void
263 2214 syck_parser_implicit_typing( SyckParser *p, int flag )
264 {
265 2214 p->implicit_typing = ( flag == 0 ? 0 : 1 );
266 2214 }
267  
268 void
269 2214 syck_parser_taguri_expansion( SyckParser *p, int flag )
270 {
271 2214 p->taguri_expansion = ( flag == 0 ? 0 : 1 );
272 2214 }
273  
274 void
275 2214 syck_parser_error_handler( SyckParser *p, SyckErrorHandler hdlr )
276 {
277 ASSERT( p != NULL );
278 2214 p->error_handler = hdlr;
279 2214 }
280  
281 void
282 2214 syck_parser_bad_anchor_handler( SyckParser *p, SyckBadAnchorHandler hdlr )
283 {
284 ASSERT( p != NULL );
285 2214 p->bad_anchor_handler = hdlr;
286 2214 }
287  
288 void
289 0 syck_parser_set_input_type( SyckParser *p, enum syck_parser_input input_type )
290 {
291 ASSERT( p != NULL );
292 0 p->input_type = input_type;
293 0 }
294  
295 void
296 0 syck_parser_file( SyckParser *p, FILE *fp, SyckIoFileRead read )
297 {
298 ASSERT( p != NULL );
299 0 free_any_io( p );
300 0 syck_parser_reset_cursor( p );
301 0 p->io_type = syck_io_file;
302 0 p->io.file = S_ALLOC( SyckIoFile );
303 0 p->io.file->ptr = fp;
304 0 ▶ if ( read != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( read != NULL ) 0 0 0
305 {
306 0 p->io.file->read = read;
307 }
308 else
309 {
310 0 p->io.file->read = syck_io_file_read;
311 }
312 0 }
313  
314 void
315 2214 syck_parser_str( SyckParser *p, char *ptr, long len, SyckIoStrRead read )
316 {
317 ASSERT( p != NULL );
318 2214 free_any_io( p );
319 2214 syck_parser_reset_cursor( p );
320 2214 p->io_type = syck_io_str;
321 2214 p->io.str = S_ALLOC( SyckIoStr );
322 2214 p->io.str->beg = ptr;
323 2214 p->io.str->ptr = ptr;
324 2214 p->io.str->end = ptr + len;
325 2214 ▶ if ( read != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( read != NULL ) 0 2214 2214
326 {
327 0 p->io.str->read = read;
328 }
329 else
330 {
331 2214 p->io.str->read = syck_io_str_read;
332 }
333 2214 }
334  
335 void
336 2214 syck_parser_str_auto( SyckParser *p, char *ptr, SyckIoStrRead read )
337 {
338 2214 syck_parser_str( p, ptr, strlen( ptr ), read );
339 2214 }
340  
341 SyckLevel *
342 74909457 syck_parser_current_level( SyckParser *p )
343 {
344 74909457 return &p->levels[p->lvl_idx-1];
345 }
346  
347 void
348 2608 syck_parser_pop_level( SyckParser *p )
349 {
350 ASSERT( p != NULL );
351  
352 /* The root level should never be popped */
353 2608 ▶ if ( p->lvl_idx <= 1 ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( p->lvl_idx <= 1 ) return; 0 2608 2608
354  
355 2608 p->lvl_idx -= 1;
356 2608 free( p->levels[p->lvl_idx].domain );
357 }
358  
359 void
360 2608 syck_parser_add_level( SyckParser *p, int len, enum syck_level_status status )
361 {
362 ASSERT( p != NULL );
363 2608 ▶ if ( p->lvl_idx + 1 > p->lvl_capa )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->lvl_idx + 1 > p->lvl_capa ) 1 2607 2608
364 {
365 1 p->lvl_capa += ALLOC_CT;
366 1 S_REALLOC_N( p->levels, SyckLevel, p->lvl_capa );
367 }
368  
369 ASSERT( len > p->levels[p->lvl_idx-1].spaces );
370 2608 p->levels[p->lvl_idx].spaces = len;
371 2608 p->levels[p->lvl_idx].ncount = 0;
372 2608 p->levels[p->lvl_idx].domain = syck_strndup( p->levels[p->lvl_idx-1].domain, strlen( p->levels[p->lvl_idx-1].domain ) );
373 2608 p->levels[p->lvl_idx].status = status;
374 2608 p->lvl_idx += 1;
375 2608 }
376  
377 void
378 4428 free_any_io( SyckParser *p )
379 {
380 ASSERT( p != NULL );
381 4428 switch ( p->io_type )
382 {
383 4428 case syck_io_str:
384 4428 ▶ if ( p->io.str != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->io.str != NULL ) 2214 2214 4428
385 {
386 2214 S_FREE( p->io.str );
387 2214 p->io.str = NULL;
388 }
389 4428 break;
390  
391 0 case syck_io_file:
392 0 ▶ if ( p->io.file != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->io.file != NULL ) 0 0 0
393 {
394 0 S_FREE( p->io.file );
395 0 p->io.file = NULL;
396 }
397 0 break;
398 }
399 4428 }
400  
401 long
402 248930 syck_move_tokens( SyckParser *p )
403 {
404 long count, skip;
405 ASSERT( p->buffer != NULL );
406  
407 248930 ▶ if ( p->token == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->token == NULL ) 2214 246716 248930
408 2214 return 0;
409  
410 246716 skip = p->limit - p->token;
411 246716 ▶ if ( skip < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( skip < 0 ) 0 246716 246716
412 0 return 0;
413  
414 246716 ▶ if ( ( count = p->token - p->buffer ) )
Branch: 1 branch
BranchTrueFalseTotal
if ( ( count = p->token - p->buffer ) ) 243148 3568 246716
415 {
416 243148 S_MEMMOVE( p->buffer, p->token, char, skip );
417 243148 p->token = p->buffer;
418 243148 p->marker -= count;
419 243148 p->cursor -= count;
420 243148 p->toktmp -= count;
421 243148 p->limit -= count;
422 243148 p->lineptr -= count;
423 243148 p->linectptr -= count;
424 }
425 246716 return skip;
426 }
427  
428 void
429 248930 syck_check_limit( SyckParser *p, long len )
430 {
431 248930 ▶ if ( p->cursor == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( p->cursor == NULL ) 2214 246716 248930
432 {
433 2214 p->cursor = p->buffer;
434 2214 p->lineptr = p->buffer;
435 2214 p->linectptr = p->buffer;
436 2214 p->marker = p->buffer;
437 }
438 248930 p->limit = p->buffer + len;
439 248930 }
440  
441 long
442 248930 syck_parser_read( SyckParser *p )
443 {
444 248930 long len = 0;
445 248930 long skip = 0;
446 ASSERT( p != NULL );
447 248930 switch ( p->io_type )
448 {
449 248930 case syck_io_str:
450 248930 skip = syck_move_tokens( p );
451 248930 len = (p->io.str->read)( p->buffer, p->io.str, SYCK_BUFFERSIZE - 1, skip );
452 248930 break;
453  
454 0 case syck_io_file:
455 0 skip = syck_move_tokens( p );
456 0 len = (p->io.file->read)( p->buffer, p->io.file, SYCK_BUFFERSIZE - 1, skip );
457 0 break;
458 }
459 248930 syck_check_limit( p, len );
460 248930 return len;
461 }
462  
463 long
464 0 syck_parser_readlen( SyckParser *p, long max_size )
465 {
466 0 long len = 0;
467 0 long skip = 0;
468 ASSERT( p != NULL );
469 0 switch ( p->io_type )
470 {
471 0 case syck_io_str:
472 0 skip = syck_move_tokens( p );
473 0 len = (p->io.str->read)( p->buffer, p->io.str, max_size, skip );
474 0 break;
475  
476 0 case syck_io_file:
477 0 skip = syck_move_tokens( p );
478 0 len = (p->io.file->read)( p->buffer, p->io.file, max_size, skip );
479 0 break;
480 }
481 0 syck_check_limit( p, len );
482 0 return len;
483 }
484  
485 SYMID
486 2442 syck_parse( SyckParser *p )
487 {
488 ASSERT( p != NULL );
489  
490 2442 syck_st_free( p );
491 2442 syck_parser_reset_levels( p );
492 2442 syckparse( p );
493 2420 return p->root;
494 }
495  
496 void
497 0 syck_default_error_handler( SyckParser *p, char *msg )
498 {
499 0 printf( "Error at [Line %d, Col %ld]: %s\n",
500 p->linect,
501 0 p->cursor - p->lineptr,
502 msg );
503 0 }
504  
505 814 int syck_str_is_unquotable_integer(char* str, long len) {
506 int idx;
507  
508 814 ▶ if(!str) return 0; /* Don't parse null strings */
Branch: 1 branch
BranchTrueFalseTotal
if(!str) return 0; /* Don't parse null strings */ 0 814 814
509 814 ▶ if(len < 1) return 0; /* empty strings can't be numbers */
Branch: 1 branch
BranchTrueFalseTotal
if(len < 1) return 0; /* empty strings can't be numbers */ 0 814 814
510 814 ▶ if(len > 9) return 0; /* Ints larger than 9 digits (32bit) might not portable. Force a string. */
Branch: 1 branch
BranchTrueFalseTotal
if(len > 9) return 0; /* Ints larger than 9 digits (32bit) might not portable. Force a string. */ 5 809 814
511  
512 809 ▶ if(str[0] == '0' && len == 1) return 1; /* 0 is unquoted. */
Branch: 2 branches
BranchTrueFalseTotal
if(str[0] == '0' && len == 1) return 1; /* 0 is unquoted. */ 13 796 809
if(str[0] == '0' && len == 1) return 1; /* 0 is unquoted. */ 4 9 13
513 805 ▶ if(str[0] == '+') return 0; /* +N must be quoted or the '+' is lost on roundtrip. */
Branch: 1 branch
BranchTrueFalseTotal
if(str[0] == '+') return 0; /* +N must be quoted or the '+' is lost on roundtrip. */ 5 800 805
514 800 ▶ if(str[0] == '-') {str++; len --;} /* supress the leading '-' sign if detected for testing purposes only. */
Branch: 1 branch
BranchTrueFalseTotal
if(str[0] == '-') {str++; len --;} /* supress the leading '-' sign if detected for testing purposes only. */ 14 786 800
515 800 ▶ if(str[0] == '0') return 0; /* Octals need to be quoted or you lose data converting them to an integer. This also accidentally blocks -0 which probably needs to be quoted. */
Branch: 1 branch
BranchTrueFalseTotal
if(str[0] == '0') return 0; /* Octals need to be quoted or you lose data converting them to an integer. This also accidentally blocks -0 which probably needs to be quoted. */ 9 791 800
516  
517 /* Look for illegal characters */
518 834 ▶ for ( idx = 1; idx < len; idx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( idx = 1; idx < len; idx++ ) { 51 783 834
519 51 ▶ if(!isdigit(str[idx])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(!isdigit(str[idx])) return 0; 8 43 51
520 }
521  
522 783 return 1;
523 }
« emitter.c ↑ Summary perl_syck.h »