annotate stitch/io.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
rev   line source
tomwalters@0 1 /*
tomwalters@0 2 Copyright (c) Applied Psychology Unit, Medical Research Council. 1988, 1989
tomwalters@0 3 ===========================================================================
tomwalters@0 4
tomwalters@0 5 Permission to use, copy, modify, and distribute this software without fee
tomwalters@0 6 is hereby granted for research purposes, provided that this copyright
tomwalters@0 7 notice appears in all copies and in all supporting documentation, and that
tomwalters@0 8 the software is not redistributed for any fee (except for a nominal shipping
tomwalters@0 9 charge). Anyone wanting to incorporate all or part of this software in a
tomwalters@0 10 commercial product must obtain a license from the Medical Research Council.
tomwalters@0 11
tomwalters@0 12 The MRC makes no representations about the suitability of this
tomwalters@0 13 software for any purpose. It is provided "as is" without express or implied
tomwalters@0 14 warranty.
tomwalters@0 15
tomwalters@0 16 THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
tomwalters@0 17 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE
tomwalters@0 18 A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
tomwalters@0 19 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
tomwalters@0 20 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
tomwalters@0 21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
tomwalters@0 22 */
tomwalters@0 23
tomwalters@0 24 /*
tomwalters@0 25 io.c
tomwalters@0 26 ====
tomwalters@0 27
tomwalters@0 28 file i/o sources/sinks.
tomwalters@0 29
tomwalters@0 30
tomwalters@0 31 */
tomwalters@0 32
tomwalters@0 33 #include <stdio.h>
tomwalters@0 34
tomwalters@0 35 #include "stitch.h"
tomwalters@0 36 #include "source.h"
tomwalters@0 37 #include "io.h"
tomwalters@0 38
tomwalters@0 39 extern int fclose() ;
tomwalters@0 40
tomwalters@0 41 /* for IBM pc's */
tomwalters@0 42 #ifdef PC
tomwalters@0 43 char readBinary[] = "rb" ;
tomwalters@0 44 char writeBinary[] = "wb" ;
tomwalters@0 45 #else
tomwalters@0 46 char readBinary[] = "r" ;
tomwalters@0 47 char writeBinary[] = "w" ;
tomwalters@0 48 #endif
tomwalters@0 49
tomwalters@0 50 /* sources */
tomwalters@0 51
tomwalters@0 52 static void read_callback( state, bytes, buffer, end )
tomwalters@0 53 FILE *state ;
tomwalters@0 54 ByteCount *bytes ;
tomwalters@0 55 Pointer buffer ;
tomwalters@0 56 {
tomwalters@0 57 int actuall ;
tomwalters@0 58
tomwalters@0 59 if( *bytes > 0 ) {
tomwalters@0 60 actuall = fread( (char *) buffer, sizeof ( char ), *bytes, state ) ;
tomwalters@0 61
tomwalters@0 62 if( actuall == EOF )
tomwalters@0 63 actuall = 0 ;
tomwalters@0 64
tomwalters@0 65 stitch_bzero( (char *)buffer+actuall, *bytes-actuall ) ;
tomwalters@0 66
tomwalters@0 67 *bytes = actuall ;
tomwalters@0 68 }
tomwalters@0 69 else if( *bytes < 0 )
tomwalters@0 70 (void) fseek( state, (long) -*bytes, 1 ) ;
tomwalters@0 71
tomwalters@0 72 return ;
tomwalters@0 73 }
tomwalters@0 74
tomwalters@0 75 Source FileSource( fp )
tomwalters@0 76 FILE *fp ;
tomwalters@0 77 {
tomwalters@0 78 if( fp == ( FILE * ) 0 )
tomwalters@0 79 (void) fprintf( stderr, "Bad file pointer FileSource - unable to open file?\n" ) ;
tomwalters@0 80
tomwalters@0 81 return ( NewExternalSource( (Pointer) fp, read_callback, (void ( * )()) 0, "io.c file pointer" ) ) ;
tomwalters@0 82 }
tomwalters@0 83
tomwalters@0 84 Source StdinSource()
tomwalters@0 85 {
tomwalters@0 86 return ( FileSource( stdin ) ) ;
tomwalters@0 87 }
tomwalters@0 88
tomwalters@0 89 Source FileNameSource( name )
tomwalters@0 90 char *name ;
tomwalters@0 91 {
tomwalters@0 92 FILE *fp = fopen( name, readBinary ) ;
tomwalters@0 93
tomwalters@0 94 if( fp == (FILE *) 0 )
tomwalters@0 95 if( strcmp( name, "-" ) == 0 )
tomwalters@0 96 return ( StdinSource() ) ;
tomwalters@0 97 else {
tomwalters@0 98 (void) fprintf( stderr, "Bad file pointer FileSource - unable to open file?\n" ) ;
tomwalters@0 99 return ( EmptySource ) ;
tomwalters@0 100 }
tomwalters@0 101
tomwalters@0 102 return ( NewExternalSource( (Pointer) fopen( name, readBinary ), read_callback, (void ( * )()) fclose, "io.c named file" ) ) ;
tomwalters@0 103 }
tomwalters@0 104
tomwalters@0 105
tomwalters@0 106 /* tap source into file on way past */
tomwalters@0 107
tomwalters@0 108 static void write_callback( state, bytes, buffer, end )
tomwalters@0 109 FILE *state ;
tomwalters@0 110 ByteCount *bytes ;
tomwalters@0 111 Pointer buffer ;
tomwalters@0 112 {
tomwalters@0 113 if( *bytes > 0 )
tomwalters@0 114 (void) fwrite( (char *) buffer, sizeof ( char ), *bytes, state ) ;
tomwalters@0 115 else if( *bytes < 0 )
tomwalters@0 116 (void) fseek( state, (long) -*bytes, 1 ) ;
tomwalters@0 117
tomwalters@0 118 return ;
tomwalters@0 119 }
tomwalters@0 120
tomwalters@0 121 Source FileTap( source, fp )
tomwalters@0 122 Source source ;
tomwalters@0 123 FILE *fp ;
tomwalters@0 124 {
tomwalters@0 125 if( fp == ( FILE * ) 0 )
tomwalters@0 126 (void) fprintf( stderr, "Bad file pointer FileTap - unable to open file?\n" ) ;
tomwalters@0 127
tomwalters@0 128 return ( NewTappingSource( (Pointer) fp, write_callback, (void (*)()) 0, source, "io.c file pointer tapping" ) ) ;
tomwalters@0 129 }
tomwalters@0 130
tomwalters@0 131 Source FileNameTap( source, name )
tomwalters@0 132 Source source ;
tomwalters@0 133 char *name ;
tomwalters@0 134 {
tomwalters@0 135 (void) unlink( name ) ;
tomwalters@0 136
tomwalters@0 137 return ( NewTappingSource( (Pointer) fopen( name, writeBinary ), write_callback, (void ( * )()) fclose, source, "io.c named file tapping" ) ) ;
tomwalters@0 138 }
tomwalters@0 139
tomwalters@0 140 Source StdoutTap( source )
tomwalters@0 141 Source source ;
tomwalters@0 142 {
tomwalters@0 143 return ( FileTap( source, stdout ) ) ;
tomwalters@0 144 }
tomwalters@0 145