diff options
author | Adrian Thurston <thurston@colm.net> | 2018-06-18 12:48:00 +0700 |
---|---|---|
committer | Adrian Thurston <thurston@colm.net> | 2018-06-18 12:48:00 +0700 |
commit | 922038be0f1924e7fe6a52a843d090c47bf7d29c (patch) | |
tree | 2c62eec1ba9bc83a662d6b25cf04690308c3ccd4 /src | |
parent | 7edc5a222a767b2be6b610029ecb71dbd2818e9a (diff) | |
download | colm-922038be0f1924e7fe6a52a843d090c47bf7d29c.tar.gz |
added stream funcs for collect and flush
Diffstat (limited to 'src')
-rw-r--r-- | src/bytecode.c | 26 | ||||
-rw-r--r-- | src/ctinput.cc | 4 | ||||
-rw-r--r-- | src/input.c | 34 | ||||
-rw-r--r-- | src/input.h | 8 | ||||
-rw-r--r-- | src/print.c | 20 | ||||
-rw-r--r-- | src/tree.h | 22 |
6 files changed, 76 insertions, 38 deletions
diff --git a/src/bytecode.c b/src/bytecode.c index 938393e0..b9510ed2 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -134,11 +134,15 @@ static void make_stderr( program_t *prg ) static void flush_streams( program_t *prg ) { - if ( prg->stdout_val != 0 ) - fflush( prg->stdout_val->impl->file ); + if ( prg->stdout_val != 0 ) { + struct stream_impl *si = prg->stdout_val->impl; + si->funcs->flush_stream( si ); + } - if ( prg->stderr_val != 0 ) - fflush( prg->stderr_val->impl->file ); + if ( prg->stderr_val != 0 ) { + struct stream_impl *si = prg->stderr_val->impl; + si->funcs->flush_stream( si ); + } } void colm_parser_set_context( program_t *prg, tree_t **sp, parser_t *parser, struct_t *val ) @@ -149,7 +153,7 @@ void colm_parser_set_context( program_t *prg, tree_t **sp, parser_t *parser, str static head_t *tree_to_str( program_t *prg, tree_t **sp, tree_t *tree, int trim, int attrs ) { /* Collect the tree data. */ - StrCollect collect; + str_collect_t collect; init_str_collect( &collect ); if ( attrs ) @@ -168,7 +172,7 @@ static head_t *tree_to_str( program_t *prg, tree_t **sp, tree_t *tree, int trim, static head_t *tree_to_str_xml( program_t *prg, tree_t **sp, tree_t *tree, int trim, int attrs ) { /* Collect the tree data. */ - StrCollect collect; + str_collect_t collect; init_str_collect( &collect ); colm_print_tree_collect_xml( prg, sp, &collect, tree, trim ); @@ -184,7 +188,7 @@ static head_t *tree_to_str_xml( program_t *prg, tree_t **sp, tree_t *tree, int t static head_t *tree_to_str_xml_ac( program_t *prg, tree_t **sp, tree_t *tree, int trim, int attrs ) { /* Collect the tree data. */ - StrCollect collect; + str_collect_t collect; init_str_collect( &collect ); colm_print_tree_collect_xml_ac( prg, sp, &collect, tree, trim ); @@ -200,7 +204,7 @@ static head_t *tree_to_str_xml_ac( program_t *prg, tree_t **sp, tree_t *tree, in static head_t *tree_to_str_postfix( program_t *prg, tree_t **sp, tree_t *tree, int trim, int attrs ) { /* Collect the tree data. */ - StrCollect collect; + str_collect_t collect; init_str_collect( &collect ); colm_postfix_tree_collect( prg, sp, &collect, tree, trim ); @@ -224,7 +228,7 @@ static word_t stream_append_text( program_t *prg, tree_t **sp, stream_t *dest, t } else { /* Collect the tree data. */ - StrCollect collect; + str_collect_t collect; init_str_collect( &collect ); colm_print_tree_collect( prg, sp, &collect, input, false ); @@ -247,7 +251,7 @@ static word_t stream_append_tree( program_t *prg, tree_t **sp, stream_t *dest, t } else if ( input->id == LEL_ID_STR ) { /* Collect the tree data. */ - StrCollect collect; + str_collect_t collect; init_str_collect( &collect ); colm_print_tree_collect( prg, sp, &collect, input, false ); @@ -321,7 +325,7 @@ static long stream_push( program_t *prg, tree_t **sp, struct stream_impl *in, tr assert( !ignore ); /* Collect the tree data. */ - StrCollect collect; + str_collect_t collect; init_str_collect( &collect ); colm_print_tree_collect( prg, sp, &collect, tree, false ); diff --git a/src/ctinput.cc b/src/ctinput.cc index 23ee53be..7e9b1b33 100644 --- a/src/ctinput.cc +++ b/src/ctinput.cc @@ -245,6 +245,8 @@ stream_funcs_ct patternFuncs = 0, 0, 0, 0, 0, &inputStreamPatternDestructor, + + 0, 0, }; @@ -462,6 +464,8 @@ stream_funcs_ct replFuncs = 0, 0, 0, 0, 0, &inputStreamConsDestructor, + + 0, 0, }; void pushBinding( pda_run *pdaRun, parse_tree_t *parseTree ) diff --git a/src/input.c b/src/input.c index 47f97685..fda79789 100644 --- a/src/input.c +++ b/src/input.c @@ -326,6 +326,17 @@ static void data_destructor( program_t *prg, tree_t **sp, struct stream_impl_dat free( si ); } +static str_collect_t *data_get_collect( struct stream_impl_data *si ) +{ + return 0; +} + +static void data_flush_stream( struct stream_impl_data *si ) +{ + if ( si->file != 0 ) + fflush( si->file ); +} + static int data_get_parse_block( struct stream_impl_data *ss, int skip, char **pdp, int *copied ) { int ret = 0; @@ -602,6 +613,16 @@ static void stream_destructor( program_t *prg, tree_t **sp, struct stream_impl_s free( si ); } +static str_collect_t *stream_get_collect( struct stream_impl_seq *si ) +{ + return si->collect; +} + +static void stream_flush_stream( struct stream_impl_seq *si ) +{ +} + + static int stream_get_parse_block( struct stream_impl_seq *is, int skip, char **pdp, int *copied ) { int ret = 0; @@ -1166,7 +1187,9 @@ struct stream_funcs_seq stream_funcs = .undo_append_tree = &stream_undo_append_tree, .undo_append_stream = &stream_undo_append_stream, - .destructor = &stream_destructor, + .destructor = &stream_destructor, + .get_collect = &stream_get_collect, + .flush_stream = &stream_flush_stream, }; struct stream_funcs_data file_funcs = @@ -1177,6 +1200,8 @@ struct stream_funcs_data file_funcs = .undo_consume_data = &data_undo_consume_data, .get_data_source = &file_get_data_source, .destructor = &data_destructor, + .get_collect = &data_get_collect, + .flush_stream = &data_flush_stream, }; struct stream_funcs_data text_funcs = @@ -1187,6 +1212,8 @@ struct stream_funcs_data text_funcs = .undo_consume_data = &data_undo_consume_data, .get_data_source = &text_get_data_source, .destructor = &data_destructor, + .get_collect = &data_get_collect, + .flush_stream = &data_flush_stream, }; static struct stream_impl *colm_impl_new_file( char *name, FILE *file ) @@ -1235,7 +1262,7 @@ struct stream_impl *colm_impl_new_collect( char *name ) struct stream_impl_seq *ss = (struct stream_impl_seq*)malloc(sizeof(struct stream_impl_seq)); init_stream_impl_seq( ss, name ); ss->funcs = (struct stream_funcs*)&stream_funcs; - ss->collect = (StrCollect*) malloc( sizeof( StrCollect ) ); + ss->collect = (struct colm_str_collect*) malloc( sizeof( struct colm_str_collect ) ); init_str_collect( ss->collect ); return (struct stream_impl*)ss; } @@ -1304,7 +1331,8 @@ stream_t *colm_stream_new( program_t *prg ) str_t *collect_string( program_t *prg, stream_t *s ) { - head_t *head = string_alloc_full( prg, s->impl->collect->data, s->impl->collect->length ); + str_collect_t *collect = s->impl->funcs->get_collect( s->impl ); + head_t *head = string_alloc_full( prg, collect->data, collect->length ); str_t *str = (str_t*)construct_string( prg, head ); return str; } diff --git a/src/input.h b/src/input.h index cb46d998..a40b61f6 100644 --- a/src/input.h +++ b/src/input.h @@ -102,6 +102,8 @@ struct stream_funcs \ struct colm_tree *(*undo_append_tree)( struct stream_impl *si ); \ struct colm_tree *(*undo_append_stream)( struct stream_impl *si ); \ void (*destructor)( struct colm_program *prg, struct colm_tree **sp, struct stream_impl *si ); \ + struct colm_str_collect *(*get_collect)( struct stream_impl *si ); \ + void (*flush_stream)( struct stream_impl *si ); \ } DEF_STREAM_FUNCS( stream_funcs, stream_impl ); @@ -129,7 +131,7 @@ struct stream_impl char *name; FILE *file; - struct _StrCollect *collect; + struct colm_str_collect *collect; int consumed; @@ -161,7 +163,7 @@ struct stream_impl_seq char *name; FILE *file; - struct _StrCollect *collect; + struct colm_str_collect *collect; int consumed; @@ -193,7 +195,7 @@ struct stream_impl_data char *name; FILE *file; - struct _StrCollect *collect; + struct colm_str_collect *collect; int consumed; diff --git a/src/print.c b/src/print.c index 471bfbd1..02e09f87 100644 --- a/src/print.c +++ b/src/print.c @@ -57,19 +57,19 @@ static void xml_escape_data( struct colm_print_args *print_args, const char *dat } } -void init_str_collect( StrCollect *collect ) +void init_str_collect( str_collect_t *collect ) { collect->data = (char*) malloc( BUFFER_INITIAL_SIZE ); collect->allocated = BUFFER_INITIAL_SIZE; collect->length = 0; } -void str_collect_destroy( StrCollect *collect ) +void str_collect_destroy( str_collect_t *collect ) { free( collect->data ); } -void str_collect_append( StrCollect *collect, const char *data, long len ) +void str_collect_append( str_collect_t *collect, const char *data, long len ) { long new_len = collect->length + len; if ( new_len > collect->allocated ) { @@ -80,7 +80,7 @@ void str_collect_append( StrCollect *collect, const char *data, long len ) collect->length += len; } -void str_collect_clear( StrCollect *collect ) +void str_collect_clear( str_collect_t *collect ) { collect->length = 0; } @@ -94,7 +94,7 @@ void print_str( struct colm_print_args *print_args, head_t *str ) void append_collect( struct colm_print_args *args, const char *data, int length ) { - str_collect_append( (StrCollect*) args->arg, data, length ); + str_collect_append( (str_collect_t*) args->arg, data, length ); } void append_file( struct colm_print_args *args, const char *data, int length ) @@ -485,7 +485,7 @@ void colm_print_term_tree( program_t *prg, tree_t **sp, } void colm_print_tree_collect( program_t *prg, tree_t **sp, - StrCollect *collect, tree_t *tree, int trim ) + str_collect_t *collect, tree_t *tree, int trim ) { struct colm_print_args print_args = { collect, true, false, trim, &append_collect, @@ -496,7 +496,7 @@ void colm_print_tree_collect( program_t *prg, tree_t **sp, } void colm_print_tree_collect_a( program_t *prg, tree_t **sp, - StrCollect *collect, tree_t *tree, int trim ) + str_collect_t *collect, tree_t *tree, int trim ) { struct colm_print_args print_args = { collect, true, true, trim, &append_collect, @@ -721,7 +721,7 @@ static void postfix_close( program_t *prg, tree_t **sp, } void colm_postfix_tree_collect( program_t *prg, tree_t **sp, - StrCollect *collect, tree_t *tree, int trim ) + str_collect_t *collect, tree_t *tree, int trim ) { struct colm_print_args print_args = { collect, false, false, false, &append_collect, @@ -746,7 +746,7 @@ void colm_postfix_tree_file( program_t *prg, tree_t **sp, struct stream_impl *im } void colm_print_tree_collect_xml( program_t *prg, tree_t **sp, - StrCollect *collect, tree_t *tree, int trim ) + str_collect_t *collect, tree_t *tree, int trim ) { struct colm_print_args print_args = { collect, false, false, trim, &append_collect, @@ -757,7 +757,7 @@ void colm_print_tree_collect_xml( program_t *prg, tree_t **sp, } void colm_print_tree_collect_xml_ac( program_t *prg, tree_t **sp, - StrCollect *collect, tree_t *tree, int trim ) + str_collect_t *collect, tree_t *tree, int trim ) { struct colm_print_args print_args = { collect, true, true, trim, &append_collect, @@ -295,24 +295,24 @@ tree_t *tree_iter_prev_repeat( struct colm_program *prg, tree_t ***psp, tree_ite /* An automatically grown buffer for collecting tokens. Always reuses space; * never down resizes. */ -typedef struct _StrCollect +typedef struct colm_str_collect { char *data; int allocated; int length; -} StrCollect; +} str_collect_t; -void init_str_collect( StrCollect *collect ); -void str_collect_destroy( StrCollect *collect ); -void str_collect_append( StrCollect *collect, const char *data, long len ); -void str_collect_clear( StrCollect *collect ); +void init_str_collect( str_collect_t *collect ); +void str_collect_destroy( str_collect_t *collect ); +void str_collect_append( str_collect_t *collect, const char *data, long len ); +void str_collect_clear( str_collect_t *collect ); tree_t *tree_trim( struct colm_program *prg, tree_t **sp, tree_t *tree ); void colm_print_tree_collect( struct colm_program *prg, tree_t **sp, - StrCollect *collect, tree_t *tree, int trim ); + str_collect_t *collect, tree_t *tree, int trim ); void colm_print_tree_collect_a( struct colm_program *prg, tree_t **sp, - StrCollect *collect, tree_t *tree, int trim ); + str_collect_t *collect, tree_t *tree, int trim ); void colm_print_tree_file( struct colm_program *prg, tree_t **sp, struct stream_impl *impl, tree_t *tree, int trim ); @@ -320,7 +320,7 @@ void colm_print_xml_stdout( struct colm_program *prg, tree_t **sp, struct stream_impl *impl, tree_t *tree, int comm_attr, int trim ); void colm_postfix_tree_collect( struct colm_program *prg, tree_t **sp, - StrCollect *collect, tree_t *tree, int trim ); + str_collect_t *collect, tree_t *tree, int trim ); void colm_postfix_tree_file( struct colm_program *prg, tree_t **sp, struct stream_impl *impl, tree_t *tree, int trim ); @@ -381,10 +381,10 @@ tree_t *construct_string( struct colm_program *prg, head_t *s ); void free_kid_list( program_t *prg, kid_t *kid ); void colm_print_tree_collect_xml( program_t *prg, tree_t **sp, - StrCollect *collect, tree_t *tree, int trim ); + str_collect_t *collect, tree_t *tree, int trim ); void colm_print_tree_collect_xml_ac( program_t *prg, tree_t **sp, - StrCollect *collect, tree_t *tree, int trim ); + str_collect_t *collect, tree_t *tree, int trim ); #if defined(__cplusplus) } |