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