Mercurial > hg > aim92
diff saitools/inout.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 diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/saitools/inout.c Fri May 20 15:19:45 2011 +0100 @@ -0,0 +1,150 @@ +/* + Copyright (c) Applied Psychology Unit, Medical Research Council. 1993 + =========================================================================== + + 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. +*/ + +/*--------------------------------------------------------------------------*/ + +/* inout.c +* ---------- +* +* Some of the data in/out functions. +* +* +* M. Akeroyd. 26th May 1993. Revised Winter 1994. +*/ + + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "tip.h" + + +extern short outputfiguredata[MAX_DATA]; +extern short outputgrounddata[MAX_DATA]; + +extern int verboseflag; +extern char progname[MAX_STRING_LENGTH]; + + +/*---------------------------------------------------------------------------*/ + + +FILE *open_file(char filefn[], FILE *dir_default, int streamtype) +{ + /* Opens a file stream associated with filefn[]. + * If that's null, then the stream is a copy of the dir_default: + * stdin / stdout / stderr. + * 'streamtype' is either READ (for "rb") or WRITE ("wb") + */ + + char tempfilefn[MAX_STRING_LENGTH]; + FILE *filefp = NULL; + + if (strcmp(filefn, "") == 0) + filefp = dir_default; + else + if (streamtype == READ) + filefp = fopen(filefn, "rb"); + else /* WRITE */ + filefp = fopen(filefn, "wb"); + + if (filefp == NULL) { + fprintf(stderr, " %s : unable to open file %s\n", progname, filefn); + exit(-1);} + + + return filefp; + +} + + + +/*---------------------------------------------------------------------------*/ + + + + +void writedata_output(FILE *outputfp, int samples_to_write) +{ + /* writes an array shorts to outputfp + */ + + fwrite(outputfiguredata, 2, samples_to_write, outputfp); + + if (ferror(outputfp)) { + fprintf(stderr, " %s : something's gone wrong: error writing output \n", progname); + exit(-1);} +} + + + +/*---------------------------------------------------------------------------*/ + + + +void writedata_output_fg(FILE *figurefp, FILE *groundfp, int samples_to_write) +{ + /* similar to writedata_output, but uses both figure and ground + * arrays + */ + + fwrite(outputfiguredata, 2, samples_to_write, figurefp); + fwrite(outputgrounddata, 2, samples_to_write, groundfp); + + if (ferror(figurefp)!=0) { + fprintf(stderr, " %s : something's gone wrong: error writing figure \n", progname); + exit(-1);} + + if (ferror(groundfp)!=0) { + fprintf(stderr, " %s : something's gone wrong: error writing ground \n", progname); + exit(-1);} +} + + + +/*---------------------------------------------------------------------------*/ + + + +void close_files(FILE *fp) +{ + + if (verboseflag == ON) + fprintf(stderr, "\n"); + + fclose(fp); + + if ( ferror(fp) != 0) { + fprintf(stderr, " %s : error closing file.\n", progname); + exit(-1);} +} + + +/* The End */ +/*---------------------------------------------------------------------------*/ + + + +