-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_io.c
435 lines (382 loc) · 11.9 KB
/
file_io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* ----------------------------- MNI Header -----------------------------------
@NAME : file_io.c
@DESCRIPTION: Routines for doing io from acr_nema code.
@METHOD :
@GLOBALS :
@CREATED : November 9, 1993 (Peter Neelin)
@MODIFIED : $Log: file_io.c,v $
@MODIFIED : Revision 1.2 2005-03-02 13:14:15 rotor
@MODIFIED : * Changes for autobuild process
@MODIFIED : * removed public/private syntactic sugar
@MODIFIED : * removed minc_def ugliness - replaced with equally ugly FREE and MALLOC
@MODIFIED :
@MODIFIED : Revision 1.1 2005/02/26 14:08:32 rotor
@MODIFIED : * Initial checkin to CVS for autoconf build
@MODIFIED :
* Revision 1.4 93/11/25 10:36:14 neelin
* Added file free and ungetc
*
* Revision 1.3 93/11/23 16:02:12 neelin
* Corrected header comment in acr_file_flush.
*
* Revision 1.2 93/11/23 13:21:42 neelin
* Added fflush for stdio write routine.
*
* Revision 1.1 93/11/10 10:33:22 neelin
* Initial revision
*
@COPYRIGHT :
Copyright 1993 Peter Neelin, McConnell Brain Imaging Centre,
Montreal Neurological Institute, McGill University.
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies. The author and McGill University
make no representations about the suitability of this
software for any purpose. It is provided "as is" without
express or implied warranty.
---------------------------------------------------------------------------- */
#include <stdlib.h>
#include <stdio.h>
#include "file_io.h"
/* defs */
#define MALLOC(size) ((void *) malloc(size))
#define FREE(ptr) free(ptr)
/* Define some constants */
#define ACR_MAX_BUFFER_LENGTH (8*1024)
#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif
/* Stream type */
#define ACR_UNKNOWN_STREAM 0
#define ACR_READ_STREAM 1
#define ACR_WRITE_STREAM 2
/* Stuff for input and output tracing */
static char *Input_trace_file = "acr_file.input";
static char *Output_trace_file = "acr_file.output";
static int Do_input_trace = FALSE;
static int Do_output_trace = FALSE;
static FILE *Input_trace = NULL;
static FILE *Output_trace = NULL;
void acr_enable_input_trace(void)
{
Do_input_trace = TRUE;
return;
}
void acr_disable_input_trace(void)
{
Do_input_trace = FALSE;
return;
}
void acr_enable_output_trace(void)
{
Do_output_trace = TRUE;
return;
}
void acr_disable_output_trace(void)
{
Do_output_trace = FALSE;
return;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : acr_file_initialize
@INPUT : user_data - pointer to data for read and write routines
maxlength - maximum length for a single read or write
(zero or negative means use internal maximum).
io_routine - routine to read or write data
@OUTPUT : (none)
@RETURNS : pointer to Acr_File structure created
@DESCRIPTION: Sets up the routines for reading/writing from an input
or output stream. A given Acr_File can only do input or
output, not both.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : November 9, 1993 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
Acr_File *acr_file_initialize(void *user_data,
int maxlength,
Acr_Io_Routine io_routine)
{
Acr_File *afp;
/* Check that an io routine is given */
if (io_routine == NULL) {
return NULL;
}
/* Allocate the strcture */
afp = MALLOC(sizeof(*afp));
/* Initialize fields */
afp->user_data = user_data;
afp->io_routine = io_routine;
if ((maxlength < ACR_MAX_BUFFER_LENGTH) && (maxlength > 0))
afp->maxlength = maxlength;
else
afp->maxlength = ACR_MAX_BUFFER_LENGTH;
afp->stream_type = ACR_UNKNOWN_STREAM;
/* Allocate the buffer */
afp->start = MALLOC((size_t) afp->maxlength);
afp->length = afp->maxlength;
/* Set ptr and end to start so that we can detect beginning of i/o */
afp->end = afp->start;
afp->ptr = afp->end;
return afp;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : acr_file_free
@INPUT : afp
@OUTPUT : (none)
@RETURNS : (nothing)
@DESCRIPTION: Frees the Acr_File structure, flushing the buffer if needed.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : November 9, 1993 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
void acr_file_free(Acr_File *afp)
{
if (afp != NULL) {
if (afp->stream_type == ACR_WRITE_STREAM) {
(void) acr_file_flush(afp);
}
if (afp->start != NULL) {
FREE(afp->start);
}
FREE(afp);
}
return;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : acr_file_read_more
@INPUT : afp - Acr_File pointer
@OUTPUT : (none)
@RETURNS : (nothing)
@DESCRIPTION: Gets more input.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : November 9, 1993 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
int acr_file_read_more(Acr_File *afp)
{
int nread;
/* Check the pointer */
if (afp == NULL) {
return EOF;
}
/* Check the stream type */
switch (afp->stream_type) {
case ACR_UNKNOWN_STREAM:
afp->stream_type = ACR_READ_STREAM; break;
case ACR_READ_STREAM:
break;
case ACR_WRITE_STREAM:
default:
return EOF;
}
/* Read in another buffer-full */
nread=afp->io_routine(afp->user_data, afp->start, afp->maxlength);
/* Do tracing */
if (Do_input_trace) {
if (Input_trace == NULL) {
Input_trace = fopen(Input_trace_file, "w");
if (Input_trace != NULL) {
(void) fprintf(stderr, "Opened input trace file %s.\n",
Input_trace_file);
(void) fflush(stderr);
}
else {
(void) fprintf(stderr, "Error opening input trace file %s.\n",
Input_trace_file);
(void) fflush(stderr);
}
}
(void) fwrite(afp->start, sizeof(char), nread, Input_trace);
(void) fflush(Input_trace);
}
/* Check for EOF */
if (nread <= 0) {
afp->end = afp->start + afp->maxlength;
afp->ptr = afp->end;
return EOF;
}
/* Set up variables */
afp->ptr = afp->start;
afp->length = nread;
afp->end = afp->start + afp->length;
return (int) *(afp->ptr++);
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : acr_file_write_more
@INPUT : afp - Acr_File pointer
@OUTPUT : (none)
@RETURNS : (nothing)
@DESCRIPTION: Writes out the buffer and the character.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : November 9, 1993 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
int acr_file_write_more(Acr_File *afp, int character)
{
/* Check the pointer */
if (afp == NULL) {
return EOF;
}
/* Flush the buffer */
if (acr_file_flush(afp) == EOF) {
return EOF;
}
/* Write the character into the buffer */
return (int) (*(afp->ptr++) = (unsigned char) character);
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : acr_file_flush
@INPUT : afp - Acr_File pointer
@OUTPUT : (none)
@RETURNS : EOF if an error occurs, otherwise 0.
@DESCRIPTION: Flushes the buffer.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : November 9, 1993 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
int acr_file_flush(Acr_File *afp)
{
int length;
/* Check the pointer */
if (afp == NULL) {
return EOF;
}
/* Check the stream type */
switch (afp->stream_type) {
case ACR_UNKNOWN_STREAM:
afp->stream_type = ACR_WRITE_STREAM; break;
case ACR_WRITE_STREAM:
break;
case ACR_READ_STREAM:
default:
return EOF;
}
/* Check for something to write */
length = afp->ptr - afp->start;
if (length > 0) {
if (afp->io_routine(afp->user_data, afp->start, length) != length) {
return EOF;
}
/* Do trace, if needed */
if (Do_output_trace) {
if (Output_trace == NULL) {
Output_trace = fopen(Output_trace_file, "w");
if (Output_trace != NULL) {
(void) fprintf(stderr, "Opened output trace file %s.\n",
Output_trace_file);
(void) fflush(stderr);
}
else {
(void) fprintf(stderr, "Error opening output trace file %s.\n",
Output_trace_file);
(void) fflush(stderr);
}
}
(void) fwrite(afp->start, sizeof(char), length, Output_trace);
(void) fflush(Output_trace);
}
}
/* Reset the buffer */
afp->ptr = afp->start;
afp->length = afp->maxlength;
afp->end = afp->start + afp->length;
return 0;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : acr_ungetc
@INPUT : c - character to unget
afp - Acr_File pointer
@OUTPUT : (none)
@RETURNS : (nothing)
@DESCRIPTION: Puts a character back into the input stream. This command is
fragile - it will only work if it can back up on the current
buffer.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : November 25, 1993 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
int acr_ungetc(int c, Acr_File *afp)
{
/* Check the pointer */
if (afp == NULL) {
return EOF;
}
/* Check to see if we can put the character back */
if (afp->ptr > afp->start) {
afp->ptr--;
*afp->ptr = c;
}
else {
return EOF;
}
return (int) *afp->ptr;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : acr_stdio_read
@INPUT : user_data - should be a FILE * pointer
nbytes - number of bytes to read
@OUTPUT : buffer - buffer into which we will read
@RETURNS : Number of bytes read.
@DESCRIPTION: Acr io routine for reading from a stdio FILE pointer
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : November 9, 1993 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
int acr_stdio_read(void *user_data, void *buffer, int nbytes)
{
FILE *fp;
int nread;
/* Get file pointer */
if (user_data == NULL) return 0;
fp = (FILE *) user_data;
/* Read the data */
nread = fread(buffer, sizeof(char), (size_t) nbytes, fp);
if (nread < 0) nread = 0;
return nread;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : acr_stdio_write
@INPUT : user_data - should be a FILE * pointer
buffer - buffer from which we will write
nbytes - number of bytes to write
@OUTPUT : (nothing)
@RETURNS : Number of bytes written.
@DESCRIPTION: Acr io routine for writing to a stdio FILE pointer
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : November 9, 1993 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
int acr_stdio_write(void *user_data, void *buffer, int nbytes)
{
FILE *fp;
int nwritten;
/* Get file pointer */
if (user_data == NULL) return 0;
fp = (FILE *) user_data;
/* Write the data */
nwritten = fwrite(buffer, sizeof(char), (size_t) nbytes, fp);
if (nwritten < 0) nwritten = 0;
/* Flush the buffer */
(void) fflush(fp);
return nwritten;
}