/* ios_2.c -- Input/output service module with two parallel inputs */ #include #include #include #define ERR_OPEN_INPUT 10 #define ERR_OPEN_OUTPUT 11 #define ERR_LINE_SIZE 12 #define ERR_LINE_SYNTAX 13 #define ERR_LINE_MISSING 14 #define ERR_COMMAND_LINE 20 #define MAX_INPUT_CHANNELS 2 #define LINE_SIZE 102 static char Line_buffer [MAX_INPUT_CHANNELS] [LINE_SIZE]; static FILE * infile [MAX_INPUT_CHANNELS]; static FILE * outfile; static int input_channel; static int input_lines_count [MAX_INPUT_CHANNELS]; static void syntax_error ( void ); static FILE * open_input_file ( char filename [] ); static int get_line ( char ** start ); static int get_line_sure ( char ** start ); FILE * open_input_file1 ( char filename [] ) { input_channel = 0; return open_input_file ( filename ); } FILE * open_input_file2 ( char filename [] ) { input_channel = 1; return open_input_file ( filename ); } FILE * open_input_file ( char filename [] ) { input_lines_count [input_channel] = 0; infile [input_channel] = fopen ( filename, "rt" ); if ( infile [input_channel] ) return ( infile [input_channel] ); printf ( "error when opening the input file \"%s\"\n", filename ); printf ( " on channel %d\n", input_channel ); exit ( ERR_OPEN_INPUT ); } FILE * open_output_file ( char filename [] ) { outfile = fopen ( filename, "wt" ); if ( outfile ) return ( outfile ); printf ( "error when opening the output file \"%s\"\n", filename ); exit ( ERR_OPEN_OUTPUT ); } int get_line1 ( char ** start ) { input_channel = 0; return get_line ( start ); } int get_line2 ( char ** start ) { input_channel = 1; return get_line ( start ); } int get_line ( char ** start ) { char c; char * left; char * right; ++ input_lines_count [input_channel]; do { if ( NULL == fgets ( & Line_buffer [input_channel] [0], LINE_SIZE, infile [input_channel] ) ) { * start = NULL; return (-1); } c = * ( left = & Line_buffer [input_channel] [0] ); while ( ( c != 0 ) && ( c != '[' ) && ( c != ']' ) ) c = * ++left; if ( c == '[' ) break; if ( c == ']' ) syntax_error (); while ( left > & Line_buffer [input_channel] [0] ) { c = * (left-1); if ( (signed char) c < 0 || ! isascii ( c ) || ! isspace ( c ) ) break; --left; } if ( left > & Line_buffer [input_channel] [0] ) syntax_error (); } while ( 1 ); c = * ( right = ++left ); while ( ( c != 0 ) && ( c != '[' ) && ( c != ']' ) ) c = * ++right; if ( right >= & Line_buffer [input_channel] [ LINE_SIZE - 1 ] ) { printf ( "line %d of the %d-th input file: line length exceeds %d\n", LINE_SIZE - 2 ); printf ( " input channel %d\n", input_channel ); exit ( ERR_LINE_SIZE ); } if ( c != ']' ) syntax_error (); * right = 0; * start = left; return ( right - left ); } int get_line_sure1 ( char ** start ) { input_channel = 0; return get_line_sure ( start ); } int get_line_sure2 ( char ** start ) { input_channel = 1; return get_line_sure ( start ); } int get_line_sure ( char ** start ) { int n; n = get_line ( start ); if ( n >= 0 ) return (n); printf ( "missing line %d of the input file\n", input_lines_count [input_channel] ); printf ( " input channel %d\n", input_channel ); exit ( ERR_LINE_MISSING ); } void err_command_line ( char usage [] ) { printf ( "usage: %s\n", usage ); exit ( ERR_COMMAND_LINE ); } static void syntax_error ( void ) { printf ( "line %d of the input file is not of the form [...]\n", input_lines_count ); printf ( " input channel %d\n", input_channel ); exit ( ERR_LINE_SYNTAX ); } /* end of ios_2.c */