view 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
line wrap: on
line source
/*
    Copyright (c) Applied Psychology Unit, Medical Research Council. 1988, 1989
    ===========================================================================

    Permission to use, copy, modify, and distribute this software without fee 
    is hereby granted for research purposes, provided that this copyright 
    notice appears in all copies and in all supporting documentation, and that 
    the software is not redistributed for any fee (except for a nominal shipping 
    charge). Anyone wanting to incorporate all or part of this software in a
    commercial product must obtain a license from the Medical Research Council.

    The MRC makes no representations about the suitability of this 
    software for any purpose.  It is provided "as is" without express or implied 
    warranty.
 
    THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
    ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE
    A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY 
    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 
    AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/*
    io.c
    ====

    file i/o sources/sinks.


*/

#include <stdio.h>

#include "stitch.h"
#include "source.h"
#include "io.h"

extern int fclose() ;

/* for IBM pc's */
#ifdef PC
char  readBinary[] = "rb" ;
char writeBinary[] = "wb" ;
#else
char  readBinary[] = "r" ;
char writeBinary[] = "w" ;
#endif

/* sources */

static void read_callback( state, bytes, buffer, end )
FILE *state ;
ByteCount *bytes ;
Pointer buffer ;
{
    int actuall ;

    if( *bytes > 0 ) {
	actuall = fread( (char *) buffer, sizeof ( char ), *bytes, state ) ;

	if( actuall == EOF )
	    actuall  =  0  ;

	stitch_bzero( (char *)buffer+actuall, *bytes-actuall ) ;

	*bytes = actuall ;
    }
    else if( *bytes < 0 )
	(void) fseek( state, (long) -*bytes, 1 ) ;

    return ;
}

Source FileSource( fp )
FILE *fp ;
{
    if( fp == ( FILE * ) 0 )
	(void) fprintf( stderr, "Bad file pointer FileSource - unable to open file?\n" ) ;

    return ( NewExternalSource( (Pointer) fp, read_callback, (void ( * )()) 0, "io.c file pointer" ) ) ;
}

Source StdinSource()
{
    return ( FileSource( stdin ) ) ;
}

Source FileNameSource( name )
char *name ;
{
    FILE *fp = fopen( name, readBinary ) ;

    if( fp == (FILE *) 0 )
	if( strcmp( name, "-" ) == 0 )
	    return ( StdinSource() ) ;
	else {
	    (void) fprintf( stderr, "Bad file pointer FileSource - unable to open file?\n" ) ;
	    return ( EmptySource ) ;
	}

    return ( NewExternalSource( (Pointer) fopen( name, readBinary ), read_callback, (void ( * )()) fclose, "io.c named file" ) ) ;
}


/* tap source into file on way past */

static void write_callback( state, bytes, buffer, end )
FILE *state ;
ByteCount *bytes ;
Pointer buffer ;
{
    if( *bytes > 0 )
	(void) fwrite( (char *) buffer, sizeof ( char ), *bytes, state ) ;
    else if( *bytes < 0 )
	(void) fseek( state, (long) -*bytes, 1 ) ;

    return ;
}

Source FileTap( source, fp )
Source source ;
FILE *fp ;
{
    if( fp == ( FILE * ) 0 )
	(void) fprintf( stderr, "Bad file pointer FileTap - unable to open file?\n" ) ;

    return ( NewTappingSource( (Pointer) fp, write_callback, (void (*)()) 0, source, "io.c file pointer tapping" ) ) ;
}

Source FileNameTap( source, name )
Source source ;
char *name ;
{
    (void) unlink( name ) ;

    return ( NewTappingSource( (Pointer) fopen( name, writeBinary ), write_callback, (void ( * )()) fclose, source, "io.c named file tapping" ) ) ;
}

Source StdoutTap( source )
Source source ;
{
    return ( FileTap( source, stdout ) ) ;
}