tomwalters@0: /* tomwalters@0: Copyright (c) Applied Psychology Unit, Medical Research Council. 1988, 1989 tomwalters@0: =========================================================================== tomwalters@0: tomwalters@0: Permission to use, copy, modify, and distribute this software without fee tomwalters@0: is hereby granted for research purposes, provided that this copyright tomwalters@0: notice appears in all copies and in all supporting documentation, and that tomwalters@0: the software is not redistributed for any fee (except for a nominal shipping tomwalters@0: charge). Anyone wanting to incorporate all or part of this software in a tomwalters@0: commercial product must obtain a license from the Medical Research Council. tomwalters@0: tomwalters@0: The MRC makes no representations about the suitability of this tomwalters@0: software for any purpose. It is provided "as is" without express or implied tomwalters@0: warranty. tomwalters@0: tomwalters@0: THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING tomwalters@0: ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE tomwalters@0: A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY tomwalters@0: DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN tomwalters@0: AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF tomwalters@0: OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. tomwalters@0: */ tomwalters@0: tomwalters@0: /* tomwalters@0: io.c tomwalters@0: ==== tomwalters@0: tomwalters@0: file i/o sources/sinks. tomwalters@0: tomwalters@0: tomwalters@0: */ tomwalters@0: tomwalters@0: #include tomwalters@0: tomwalters@0: #include "stitch.h" tomwalters@0: #include "source.h" tomwalters@0: #include "io.h" tomwalters@0: tomwalters@0: extern int fclose() ; tomwalters@0: tomwalters@0: /* for IBM pc's */ tomwalters@0: #ifdef PC tomwalters@0: char readBinary[] = "rb" ; tomwalters@0: char writeBinary[] = "wb" ; tomwalters@0: #else tomwalters@0: char readBinary[] = "r" ; tomwalters@0: char writeBinary[] = "w" ; tomwalters@0: #endif tomwalters@0: tomwalters@0: /* sources */ tomwalters@0: tomwalters@0: static void read_callback( state, bytes, buffer, end ) tomwalters@0: FILE *state ; tomwalters@0: ByteCount *bytes ; tomwalters@0: Pointer buffer ; tomwalters@0: { tomwalters@0: int actuall ; tomwalters@0: tomwalters@0: if( *bytes > 0 ) { tomwalters@0: actuall = fread( (char *) buffer, sizeof ( char ), *bytes, state ) ; tomwalters@0: tomwalters@0: if( actuall == EOF ) tomwalters@0: actuall = 0 ; tomwalters@0: tomwalters@0: stitch_bzero( (char *)buffer+actuall, *bytes-actuall ) ; tomwalters@0: tomwalters@0: *bytes = actuall ; tomwalters@0: } tomwalters@0: else if( *bytes < 0 ) tomwalters@0: (void) fseek( state, (long) -*bytes, 1 ) ; tomwalters@0: tomwalters@0: return ; tomwalters@0: } tomwalters@0: tomwalters@0: Source FileSource( fp ) tomwalters@0: FILE *fp ; tomwalters@0: { tomwalters@0: if( fp == ( FILE * ) 0 ) tomwalters@0: (void) fprintf( stderr, "Bad file pointer FileSource - unable to open file?\n" ) ; tomwalters@0: tomwalters@0: return ( NewExternalSource( (Pointer) fp, read_callback, (void ( * )()) 0, "io.c file pointer" ) ) ; tomwalters@0: } tomwalters@0: tomwalters@0: Source StdinSource() tomwalters@0: { tomwalters@0: return ( FileSource( stdin ) ) ; tomwalters@0: } tomwalters@0: tomwalters@0: Source FileNameSource( name ) tomwalters@0: char *name ; tomwalters@0: { tomwalters@0: FILE *fp = fopen( name, readBinary ) ; tomwalters@0: tomwalters@0: if( fp == (FILE *) 0 ) tomwalters@0: if( strcmp( name, "-" ) == 0 ) tomwalters@0: return ( StdinSource() ) ; tomwalters@0: else { tomwalters@0: (void) fprintf( stderr, "Bad file pointer FileSource - unable to open file?\n" ) ; tomwalters@0: return ( EmptySource ) ; tomwalters@0: } tomwalters@0: tomwalters@0: return ( NewExternalSource( (Pointer) fopen( name, readBinary ), read_callback, (void ( * )()) fclose, "io.c named file" ) ) ; tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: /* tap source into file on way past */ tomwalters@0: tomwalters@0: static void write_callback( state, bytes, buffer, end ) tomwalters@0: FILE *state ; tomwalters@0: ByteCount *bytes ; tomwalters@0: Pointer buffer ; tomwalters@0: { tomwalters@0: if( *bytes > 0 ) tomwalters@0: (void) fwrite( (char *) buffer, sizeof ( char ), *bytes, state ) ; tomwalters@0: else if( *bytes < 0 ) tomwalters@0: (void) fseek( state, (long) -*bytes, 1 ) ; tomwalters@0: tomwalters@0: return ; tomwalters@0: } tomwalters@0: tomwalters@0: Source FileTap( source, fp ) tomwalters@0: Source source ; tomwalters@0: FILE *fp ; tomwalters@0: { tomwalters@0: if( fp == ( FILE * ) 0 ) tomwalters@0: (void) fprintf( stderr, "Bad file pointer FileTap - unable to open file?\n" ) ; tomwalters@0: tomwalters@0: return ( NewTappingSource( (Pointer) fp, write_callback, (void (*)()) 0, source, "io.c file pointer tapping" ) ) ; tomwalters@0: } tomwalters@0: tomwalters@0: Source FileNameTap( source, name ) tomwalters@0: Source source ; tomwalters@0: char *name ; tomwalters@0: { tomwalters@0: (void) unlink( name ) ; tomwalters@0: tomwalters@0: return ( NewTappingSource( (Pointer) fopen( name, writeBinary ), write_callback, (void ( * )()) fclose, source, "io.c named file tapping" ) ) ; tomwalters@0: } tomwalters@0: tomwalters@0: Source StdoutTap( source ) tomwalters@0: Source source ; tomwalters@0: { tomwalters@0: return ( FileTap( source, stdout ) ) ; tomwalters@0: } tomwalters@0: