From 2081be0c64376204776c27671cc221401b56cc04 Mon Sep 17 00:00:00 2001 From: Dan Dennedy Date: Sun, 17 Apr 2011 13:05:18 -0700 Subject: [PATCH] Add mvcp_parser_run_file(). --- src/mvcp/mvcp_parser.c | 67 ++++++++++++++++++++++++++++-------------------- src/mvcp/mvcp_parser.h | 1 + 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/mvcp/mvcp_parser.c b/src/mvcp/mvcp_parser.c index 5135d98..b199dad 100644 --- a/src/mvcp/mvcp_parser.c +++ b/src/mvcp/mvcp_parser.c @@ -79,50 +79,61 @@ mvcp_response mvcp_parser_executef( mvcp_parser parser, const char *format, ... return response; } -/** Execute the contents of a file. Note the special case mvcp_response returned. +/** Execute the contents of a file descriptor. */ -mvcp_response mvcp_parser_run( mvcp_parser parser, char *filename ) +mvcp_response mvcp_parser_run_file( mvcp_parser parser, FILE *file ) { mvcp_response response = mvcp_response_init( ); if ( response != NULL ) { - FILE *file = fopen( filename, "r" ); - if ( file != NULL ) + char command[ 1024 ]; + mvcp_response_set_error( response, 201, "OK" ); + while ( mvcp_response_get_error_code( response ) == 201 && fgets( command, 1024, file ) ) { - char command[ 1024 ]; - mvcp_response_set_error( response, 201, "OK" ); - while ( mvcp_response_get_error_code( response ) == 201 && fgets( command, 1024, file ) ) + mvcp_util_trim( mvcp_util_chomp( command ) ); + if ( strcmp( command, "" ) && command[ 0 ] != '#' ) { - mvcp_util_trim( mvcp_util_chomp( command ) ); - if ( strcmp( command, "" ) && command[ 0 ] != '#' ) + mvcp_response temp = NULL; + mvcp_response_printf( response, 1024, "%s\n", command ); + temp = mvcp_parser_execute( parser, command ); + if ( temp != NULL ) { - mvcp_response temp = NULL; - mvcp_response_printf( response, 1024, "%s\n", command ); - temp = mvcp_parser_execute( parser, command ); - if ( temp != NULL ) - { - int index = 0; - for ( index = 0; index < mvcp_response_count( temp ); index ++ ) - mvcp_response_printf( response, 10240, "%s\n", mvcp_response_get_line( temp, index ) ); - mvcp_response_close( temp ); - } - else - { - mvcp_response_set_error( response, 500, "Batch execution failed" ); - } + int index = 0; + for ( index = 0; index < mvcp_response_count( temp ); index ++ ) + mvcp_response_printf( response, 10240, "%s\n", mvcp_response_get_line( temp, index ) ); + mvcp_response_close( temp ); + } + else + { + mvcp_response_set_error( response, 500, "Batch execution failed" ); } } - fclose( file ); - } - else - { - mvcp_response_set_error( response, 404, "File not found." ); } } return response; } +/** Execute the contents of a file. Note the special case mvcp_response returned. +*/ + +mvcp_response mvcp_parser_run( mvcp_parser parser, char *filename ) +{ + mvcp_response response; + FILE *file = fopen( filename, "r" ); + if ( file != NULL ) + { + response = mvcp_parser_run_file( parser, file ); + fclose( file ); + } + else + { + response = mvcp_response_init( ); + mvcp_response_set_error( response, 404, "File not found." ); + } + return response; +} + /** Get the notifier associated to the parser. */ diff --git a/src/mvcp/mvcp_parser.h b/src/mvcp/mvcp_parser.h index 3e2f465..67d2bba 100644 --- a/src/mvcp/mvcp_parser.h +++ b/src/mvcp/mvcp_parser.h @@ -65,6 +65,7 @@ extern mvcp_response mvcp_parser_push( mvcp_parser, char *, mlt_service ); extern mvcp_response mvcp_parser_received( mvcp_parser, char *, char * ); extern mvcp_response mvcp_parser_execute( mvcp_parser, char * ); extern mvcp_response mvcp_parser_executef( mvcp_parser, const char *, ... ); +extern mvcp_response mvcp_parser_run_file( mvcp_parser parser, FILE *file ); extern mvcp_response mvcp_parser_run( mvcp_parser, char * ); extern mvcp_notifier mvcp_parser_get_notifier( mvcp_parser ); extern void mvcp_parser_close( mvcp_parser ); -- 1.7.4.4