home *** CD-ROM | disk | FTP | other *** search
- /*
- *
- * file i/o routines
- *
- */
-
- /*
- * This software may be freely distributed under the following conditions:
- * 1. It is distributed in its current form, without alterations.
- * 2. No monetary profit is made, under any circumstances.
- *
- * Marc A. Murison
- * Smithsonian Astrophysical Observatory
- * 60 Garden Street, MS 63
- * Cambridge, MA 02138
- *
- * (617) 495-7079 murison@cfacx2.harvard.edu
- */
-
-
- #define global extern
- #define FILEIO_C
- #include "fileio.h"
-
-
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- /* Boolean fnext_word( FILE *fp, char *word, int *n, char *cp ) */
- /* */
- /* retrieve next word from a file */
- /* */
- /* A word is assumed to consist of sequential alphanumeric characters. */
- /* Anything else is a word delimiter. */
- /* */
- /* entry: fp file pointer */
- /* */
- /* exit: word contents of next word */
- /* n number of characters in word, excluding the NULL */
- /* fp points to 2nd char after word */
- /* cp contains the 1st char after word */
- /* */
- /* returns: 1 successful */
- /* 0 EOF encountered */
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- Boolean fnext_word( FILE *fp, char *word, int *n, char *cp )
- {
- char *wp;
- int c;
-
- /* skip to next word */
- c = getc(fp);
- while( !WORD_CHAR(c) && c != EOF ) c = getc(fp);
- if( c == EOF ) return FALSE;
-
- /* transfer a word */
- wp = word;
- *n = 0;
- *wp = NULL;
- while( WORD_CHAR(c) ) {
- *wp++ = c;
- ++(*n);
- c = getc(fp);
- }
- *wp = NULL;
- *cp = c;
- if( c == EOF )
- return FALSE;
- else
- return TRUE;
- }
-
-
-
-
-
-
-
-
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- /* Boolean fnext_line( FILE *file_ptr ) */
- /* */
- /* advance file pointer to character after next newline character */
- /* */
- /* returns: 1 successful */
- /* 0 EOF encountered */
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- Boolean fnext_line( FILE *file_ptr )
- {
- int c;
-
- do {
- c = getc( file_ptr );
- if( c == EOF ) return FALSE;
- } while( c != CR && c != LF );
-
- return TRUE;
- }
-
-
-
-
-
-
-
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- /* int fget_line( FILE *file_ptr, char *buffer ) */
- /* */
- /* Read 1 line of text from a file and put it into a buffer. */
- /* */
- /* Advances file pointer to character after next newline character. */
- /* */
- /* returns: number of bytes read (minus newline char) if successful */
- /* EOF if EOF encountered */
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- int fget_line( FILE *file_ptr, char *buffer )
- {
- int N, c;
- char *bp;
-
- bp = buffer; N = 0;
- while( (c=getc(file_ptr)) != NEWLINE && c != EOF ) {
- *bp++ = c;
- ++N;
- }
- *bp = NULL;
-
- if( c == EOF )
- return EOF;
- else
- return N;
- }
-
-
-
-
-
-
-
- /*
- *
- * open file function
- *
- */
-
- Boolean openfile( File_Def *file, File_Type ftype, File_Mode mode )
- {
- char *errstr;
-
- if( ftype == FTEXT ) {
- if( mode == FREAD )
- file->fp = fopen( file->name, "r" );
- else if( mode == FWRITE )
- file->fp = fopen( file->name, "w" );
- else if( mode == FAPPEND )
- file->fp = fopen( file->name, "a" );
- else {
- ferrout( ON, stdout,
- "\n* * * openfile: Illegal read/write request! * * *\n\n" );
- return FALSE;
- }
- } else if( ftype == FBINARY ) {
- if( mode == FREAD )
- file->fp = fopen( file->name, "rb" );
- else if( mode == FWRITE )
- file->fp = fopen( file->name, "wb" );
- else if( mode == FAPPEND )
- file->fp = fopen( file->name, "ab" );
- else {
- ferrout( ON, stdout,
- "\n* * * openfile: Illegal mode request! * * *\n\n" );
- return FALSE;
- }
- } else {
- ferrout( ON, stdout, "\n* * * openfile: Illegal file type! * * *\n\n" );
- return FALSE;
- }
-
- if( file->fp == NULL ) {
- errstr = cvector( 0, 80 );
- strcpy( errstr, "\n* * * openfile: Unable to open " );
- strcat( errstr, file->name );
- strcat( errstr, " * * * \n\n" );
- ferrout( ON, stdout, errstr );
- free_cvector( errstr, 0 );
- return FALSE;
- }
-
- return TRUE;
- }
-
-
-
-
-
-
-
-
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- /* cl_in_out( int min_args, char *usage, char *help, int file_mode, */
- /* int *argc_ptr, char *argv[], File_Def *infile, */
- /* File_Def *outfile_ptr ) */
- /* */
- /* Function to parse the command line for an input file and an output */
- /* file. Both files will be opened, the input file for read only and */
- /* the output file for write only. */
- /* */
- /* */
- /* caller must provide */
- /* argc command line argument count */
- /* *argv[] command line arguments */
- /* min_args minimum number of command line args needed (in */
- /* addition to program invocation) */
- /* usage usage string printed to screen if usage error */
- /* help help string printed to screen if cl arg is "?" */
- /* */
- /* Returns 1 if successful, 0 if error. */
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- Boolean cl_in_out( int min_args, char *usage, char *help, File_Mode mode,
- int argc, char *argv[], File_Def *infile,
- File_Def *outfile )
- {
- int status;
- File_Def tmpfile;
-
- /* help */
- if( argc == 2 ) {
- if( STREQ( argv[1], "\?" ) ) {
- #if __TURBOC__
- if( !browse( help, "* Help *" ) ) moreprint( help );
- #else
- moreprint( help );
- #endif
- return FALSE;
- }
- if( STREQ( argv[1], "\?\?" ) ) {
- print_msg( YES, YES, 1, "Writing file \"help\"..." );
- strcpy( tmpfile.name, "help" );
- status = openfile( &tmpfile, FTEXT, FWRITE );
- if( !status ) return FALSE;
- fprintf( tmpfile.fp, "%s", help );
- fclose( tmpfile.fp );
- print_msg( NO, YES, 1, "Done" );
- return FALSE;
- }
- }
-
- /* too few cl args */
- if ( argc <= min_args ) {
- ferrout( ON, stdout, usage );
- return FALSE;
- }
-
-
- /* open the input file */
- strcpy( infile->name, argv[1] );
- status = openfile( infile, mode, FREAD );
- if( !status ) return FALSE;
-
-
- /* open the output file */
- strcpy( outfile->name, argv[2] );
- status = openfile( outfile, mode, FWRITE );
- if( !status ) return FALSE;
-
- return TRUE;
- }
-
-
-
-
-
-
-
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- /* cl_dir_in_out command line parser */
- /* */
- /* gets input and output file names, opens input and output files, checks */
- /* input file for a directory of files and sets *dirflag_ptr accordingly */
- /* */
- /* valid cl: program infile[.in] [outfile[.out]] [more args...] */
- /* program infile[.in] [.out] [more args...] */
- /* program dirfile [.out] [more args...] */
- /* program dirfile [.in .out] [more args...] */
- /* */
- /* caller must provide */
- /* nargs command line argument count */
- /* *argv[] command line arguments */
- /* default_in_ext[] default input file extension (e.g., ".dat") */
- /* default_out_ext[] default output file extension (e.g., ".out") */
- /* min_args minimum number of command line args needed (in */
- /* addition to program invocation) */
- /* usage[] usage string printed to screen if usage error */
- /* help help string printed to screen if cl arg is "?" */
- /* file_mode must be BINARY or TEXT */
- /* */
- /* returns 0 if error, 1 if no error */
- /* */
- /* The pieces of the input and output file names are put into */
- /* infile->name[] entire file name */
- /* in_name[] file name root only */
- /* in_ext[] file name extension only */
- /* and similar for the output file name. */
- /* */
- /* function dependences: upcase, get_filename, fnext_line */
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- Boolean cl_dir_in_out( int nargs, char *argv[],
- int min_args, char *usage,
- char *help, File_Mode mode,
- char default_in_ext[], char default_out_ext[],
- char in_name[], char in_ext[],
- char out_name[], char out_ext[],
- File_Def *infile, File_Def *outfile,
- File_Def *dirfile, int *dirflag )
- {
- int i, status;
- char c, *errstr;
- char def_ext[15];
- char *name_ptr, *arg_ptr;
- char *char_ptr, *scratch_ptr;
- char dirtest[10];
- File_Def tmpfile;
-
-
- /* help */
- if( nargs == 2 ) {
- if( STREQ( argv[1], "\?" ) ) {
- #if __TURBOC__
- if( !browse( help, "* Help *" ) ) moreprint( help );
- #else
- moreprint( help );
- #endif
- return FALSE;
- }
- if( STREQ( argv[1], "\?\?" ) ) {
- print_msg( YES, YES, 1, "Writing file \"help\"..." );
- strcpy( tmpfile.name, "help" );
- status = openfile( &tmpfile, FTEXT, FWRITE );
- if( !status ) return FALSE;
- fprintf( tmpfile.fp, "%s", help );
- fclose( tmpfile.fp );
- print_msg( NO, YES, 1, "Done" );
- return FALSE;
- }
- }
-
- /* wrong number of command line arguments */
- if( nargs <= min_args ) {
- ferrout( ON, stdout, usage );
- return FALSE;
- }
-
-
- /* parse the input file name */
- char_ptr = (char *) strchr( argv[1], DOT );
- if( char_ptr == NULL ) { /* name only */
- strcpy( in_name, argv[1] );
- strcpy( in_ext, default_in_ext );
- } else { /* full file name given */
- arg_ptr = argv[1];
- name_ptr = in_name;
- while( strcmp( arg_ptr, char_ptr ) != 0 ) /* copy name */
- *name_ptr++ = *arg_ptr++;
- *name_ptr = NULL;
- strcpy( in_ext, char_ptr ); /* copy ext */
- }
-
-
- /* open input file */
- strcpy( infile->name, in_name ); /* form input file name */
- strcat( infile->name, in_ext );
- status = openfile( infile, mode, FREAD );
- if( !status ) return FALSE;
-
-
- /* check infile for directory option */
-
- dircheck:
- /* skip leading white space */
- do {
- c = getc( infile->fp );
- if( c == EOF ) {
- errstr = cvector( 0, 80 );
- strcpy( errstr, "\nInput file " );
- strcat( errstr, infile->name );
- strcat( errstr, " is empty!\n\n" );
- ferrout( ON, stdout, errstr );
- free_cvector( errstr, 0 );
- fclose( infile->fp );
- return FALSE;
- }
- } while( isspace(c) );
-
- /* now get first 9 characters ... */
- scratch_ptr = dirtest;
- *scratch_ptr++ = c; /* 1st character */
- for( i=1; i < 9; i++ ) { /* next 8 */
- c = getc( infile->fp );
- if( c == EOF ) {
- errstr = cvector( 0, 80 );
- strcpy( errstr, "\nInput file " );
- strcat( errstr, infile->name );
- strcat( errstr, " is empty!\n\n" );
- ferrout( ON, stdout, errstr );
- free_cvector( errstr, 0 );
- fclose( infile->fp );
- return FALSE;
- }
- *scratch_ptr++ = c;
- }
- *(++scratch_ptr) = NULL;
-
- /* ...and check for "directory" */
- if( strcmp( upcase(dirtest), "DIRECTORY" ) == 0 ) {
- *dirflag = 1;
- *dirfile = *infile;
-
- /* read past the rest of the "directory" line */
- if( !fnext_line(infile->fp) ) {
- errstr = cvector( 0, 80 );
- strcpy( errstr, "\nInput file " );
- strcat( errstr, infile->name );
- strcat( errstr, " is empty!\n\n" );
- ferrout( ON, stdout, errstr );
- free_cvector( errstr, 0 );
- fclose( infile->fp );
- return FALSE;
- }
-
- /* a result of DOS dir (volume name)--skip to next line */
- } else if( strcmp( dirtest, "VOLUME IN" ) == 0 ) {
- if( !fnext_line(infile->fp) ) {
- errstr = cvector( 0, 80 );
- strcpy( errstr, "\nInput file " );
- strcat( errstr, infile->name );
- strcat( errstr, " is empty!\n\n" );
- ferrout( ON, stdout, errstr );
- free_cvector( errstr, 0 );
- fclose( infile->fp );
- return FALSE;
- }
- goto dircheck;
-
- /* another DOS dir result (file count)--skip it, too */
- } else if( isdigit( dirtest[0] ) ) {
- scratch_ptr = dirtest;
- while( *scratch_ptr && !isspace( *scratch_ptr++ ) );
- if( strcmp( scratch_ptr, "FILE(" ) == 0 ||
- strcmp( scratch_ptr, "FILE(S" ) == 0 ||
- strcmp( scratch_ptr, "FILE(S)" ) == 0 ) {
-
- if( !fnext_line(infile->fp) ) {
- errstr = cvector( 0, 80 );
- strcpy( errstr, "\nInput file " );
- strcat( errstr, infile->name );
- strcat( errstr, " is empty!\n\n" );
- ferrout( ON, stdout, errstr );
- free_cvector( errstr, 0 );
- fclose( infile->fp );
- return FALSE;
- }
- goto dircheck;
- } else {
- *dirflag = 0; /* not a dirfile */
- rewind( infile->fp );
- }
-
- } else { /* infile not a directory -- reset infile position */
- *dirflag = 0;
- rewind( infile->fp );
- }
-
-
-
- /* set output file name and extension if infile is not a dirfile */
- if( *dirflag == 0 ) {
-
- if( nargs == 2 ) { /* only infile specified */
- strcpy( out_name, in_name );
- strcpy( out_ext, default_out_ext );
- } else { /* output file name was specified */
- if( *argv[2] == DOT ) { /* extension only */
- strcpy( out_name, in_name );
- strcpy( out_ext, argv[2] );
- } else {
- char_ptr = (char *) strchr( argv[2], DOT );
- if( char_ptr == NULL ) { /* name only */
- strcpy( out_name, argv[2] );
- strcpy( out_ext, default_out_ext );
- } else { /* full file name given */
- arg_ptr = argv[2];
- name_ptr = out_name;
- while( strcmp( arg_ptr, char_ptr ) != 0 ) /* copy name */
- *name_ptr++ = *arg_ptr++;
- *name_ptr = NULL;
- strcpy( out_ext, char_ptr ); /* copy ext */
- }
- }
- }
-
- /* form full file name */
- strcpy( outfile->name, out_name );
- strcat( outfile->name, out_ext );
- }
-
-
-
- /* get infile and outfile names if infile is a dirfile */
- if( *dirflag ) {
-
- /* set file extensions */
- if( nargs == 2 ) {
- strcpy( def_ext, default_in_ext );
- strcpy( out_ext, default_out_ext );
- } else if( nargs == 3 ) {
- strcpy( def_ext, default_in_ext );
- if( *argv[2] == DOT )
- strcpy( out_ext, argv[2] );
- else
- strcpy( out_ext, default_out_ext );
- } else { /* 4 or more cl args present */
- if( *argv[2] != DOT ) {
- strcpy( def_ext, default_in_ext );
- strcpy( out_ext, default_out_ext );
- } else {
- if( *argv[3] == DOT ) {
- strcpy( def_ext, argv[2] );
- strcpy( default_in_ext, argv[2] );
- strcpy( out_ext, argv[3] );
- strcpy( default_out_ext, argv[3] );
- } else {
- strcpy( def_ext, default_in_ext );
- strcpy( out_ext, argv[2] );
- strcpy( default_out_ext, argv[2] );
- }
- }
- }
-
-
- /* input file name */
- i = get_filename( dirfile, def_ext, infile->name, in_name, in_ext );
- strcpy( infile->name, in_name ); /* override get_filename */
- strcat( infile->name, def_ext );
-
- /* error--end program now */
- if( ! i ) {
- errstr = cvector( 0, 80 );
- strcpy( errstr, "\nDirectory file " );
- strcat( errstr, infile->name );
- strcat( errstr, " is empty or doesn't contain a valid input " );
- strcat( errstr, "file name!\n\n" );
- ferrout( ON, stdout, errstr );
- free_cvector( errstr, 0 );
- fclose( dirfile->fp );
- return FALSE;
- }
-
- /* output file name */
- strcpy( outfile->name, in_name );
- strcat( outfile->name, out_ext );
-
-
- /* open input file */
- status = openfile( infile, mode, FREAD );
- if( !status ) return FALSE;
-
- }
-
-
- /* check for outfile = infile, then open outfile */
- if( *dirflag == 0 && strcmp( infile->name, outfile->name ) == 0 ) {
- ferrout( ON, stdout, "\nCannot allow infile = outfile\n\n");
- fclose( infile->fp );
- return FALSE;
- }
-
- status = openfile( outfile, mode, FWRITE );
- if( !status ) return FALSE;
-
- return TRUE;
- }
-
-
-
-
-
-
-
-
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- /* */
- /* Function to get the next file name from the list in dirfile, parse it, */
- /* and return the parts of the input and output file names. The input and */
- /* output files will be opened for read-only and write-only. */
- /* */
- /* On exit, */
- /* *infile = File_Def structure pointer for input file */
- /* *outfile = File_Def structure pointer for output file */
- /* in_name[] = character array containing the infile_name */
- /* minus the file extension */
- /* in_ext[] = character array containing the input file */
- /* extension */
- /* out_name[] = character array containing the outfile_name */
- /* minus the file extension */
- /* out_ext[] = character array containing the output file */
- /* extension */
- /* *dirfile = closed if encountered EOF, left alone */
- /* otherwise */
- /* */
- /* */
- /* get_next_file returns 0 if error, 1 if no error */
- /* */
- /* function dependencies: get_filename */
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- Boolean get_next_file( File_Def *dirfile, File_Def *infile,
- File_Def *outfile, File_Mode mode,
- char default_in_ext[], char in_name[],
- char in_ext[], char out_name[], char out_ext[] )
- {
- int i;
-
-
- /* get the next name in the list */
- i = get_filename( dirfile, default_in_ext, infile->name,
- in_name, in_ext );
-
- /* no more in list */
- if( !i ) {
- fclose( dirfile->fp );
- return FALSE;
- }
-
- /* open input file for reading */
- i = openfile( infile, mode, FREAD );
- if( !i ) return FALSE;
-
- /* form output file name and open it for writing */
- strcpy( outfile->name, in_name );
- strcpy( out_name, in_name );
- strcat( outfile->name, out_ext );
- i = openfile( outfile, mode, FWRITE );
- if( !i ) return FALSE;
-
- return TRUE;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- /* */
- /* Function to extract next file name from a list of file names. */
- /* The name to be extracted is assumed to be on the current line (i.e.,*/
- /* before the next end-of-line character if the line is not blank). */
- /* */
- /* on entry: */
- /* dirfile = pointer to directory file, which must */
- /* already be opened and waiting to be read */
- /* default_ext[] = character array containing default input */
- /* file extension */
- /* */
- /* on exit: */
- /* infile_name[] = character array containing full input */
- /* file name */
- /* in_name[] = character array containing the infile_name */
- /* minus the file extension */
- /* in_ext[] = character array containing the input file */
- /* extension */
- /* */
- /* get_filename returns: */
- /* 1 if attempt to read file name from dirfile was successful */
- /* 0 if attempt encountered EOF */
- /* */
- /* function dependencies: fnext_line, dncase */
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- Boolean get_filename( File_Def *dirfile, char default_ext[],
- char infile_name[], char in_name[], char in_ext[] )
- {
- char c, *name_ptr, *dot_ptr;
- char test[MAX_FNAME];
-
-
- extract:
-
- /* check to make sure we're not already at end of file */
- if( feof( dirfile->fp ) ) return FALSE;
-
- /* extract input file name */
- name_ptr = infile_name;
- do { /* skip over leading white space */
- c = getc( dirfile->fp );
- if( c == EOF ) return FALSE;
- } while( isspace(c) );
-
- *name_ptr++ = c; /* put first character to infile_name */
- do { /* get the rest of the characters */
- c = getc( dirfile->fp );
- *name_ptr++ = c;
- } while( c == DOT || isalnum(c) || c == '_' || c == '$' && c != EOF );
- *(--name_ptr) = NULL; /* put a NULL after last char */
-
-
-
- /* check for DOS dir dreck and skip it */
- if( strcmp( infile_name, "Volume" ) == 0 ) { /* volume name */
- if( !fnext_line(dirfile->fp) ) return FALSE;
- goto extract;
- } else if( isdigit( infile_name[0] ) ) { /* file count */
- /* skip white */
- while( (c = getc(dirfile->fp)) != EOF && c != '\n' && isspace(c) );
- if( c == EOF ) return FALSE;
-
- /* look for "File(s)" */
- name_ptr = test;
- *name_ptr++ = c;
- while( (c = getc(dirfile->fp)) != EOF && c != '\n' && !isspace(c) )
- *name_ptr++ = c;
- *name_ptr = NULL;
- if( strcmp( test, "File(s)" ) == 0 ) {
- if( !fnext_line(dirfile->fp) ) return FALSE;
- goto extract;
- }
- }
-
-
- /* skip to end of line in preparation for next time */
- fnext_line( dirfile->fp );
-
-
- /* parse file name */
- dot_ptr = (char *) strchr( infile_name, DOT );
- if( dot_ptr == NULL ) { /* name does not contain a "." */
- strcpy( in_name, infile_name );
- strcpy( in_ext, default_ext );
- strcat( infile_name, in_ext );
- } else { /* name contains a "." */
- strcpy( in_ext, dot_ptr ); /* get file extension */
- strcpy( in_name, infile_name ); /* get name */
- dot_ptr = (char *) strchr( in_name, DOT );
- *dot_ptr = NULL;
- }
-
-
- /* lower case */
- infile_name = dncase( infile_name );
- in_name = dncase( in_name );
- in_ext = dncase( in_ext );
-
-
- return TRUE;
- }
-
-
-
-
-
-
-
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- /* unsigned long fill_file_buff( FILE *infile, File_Type ftype, long Nmax, */
- /* char *buff_ptr, int *EOF_ptr ) */
- /* */
- /* Function to fill a buffer from infile. Nmax is max number of bytes */
- /* to read. Filling begins at the place pointed to by buff_ptr. If the end */
- /* of the file is reached before the buffer is full, the flag pointed to */
- /* by EOF_ptr is set to 1; otherwise, it is set to 0 on entry to this */
- /* routine. ftype must be either TEXT or BINARY. */
- /* */
- /* fill_file_buff returns the number of bytes read from infile, not */
- /* counting the EOF if encountered or the NULL placed at the end of the */
- /* buffer. */
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- unsigned long fill_file_buff( FILE *infile, File_Type ftype, ulong Nmax,
- char *buff_ptr, int *EOF_ptr )
- {
- ulong i;
- int c;
- char *p;
-
-
- if( ftype != FTEXT && ftype != FBINARY ) {
- ferrout( ON, stdout, "\nfill_file_buff: Invalid ftype!\n\n" );
- *EOF_ptr = 1;
- return 0;
- }
-
-
- *EOF_ptr = 0;
- p = buff_ptr;
- for( i = 0; i < Nmax; i++ ) {
- c = getc( infile );
- if( c == EOF ) {
- *EOF_ptr = 1;
- break;
- } else if( c == NEWLINE )
- if( ftype == FTEXT ) i++; /* '\n' == cr + lf */
- *p++ = c;
- }
-
- *p = NULL;
-
- return i;
- }
-
-
-
-
-
-
-
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- /* unsigned long append_buff( char *source_buff, unsigned long N, */
- /* char *dest_buff ) */
- /* */
- /* Function to append a destination buffer pointed to by dest_buff */
- /* with N bytes from a source buffer pointed to by source_buff. N is */
- /* the number of bytes to copy. Source buffer must contain a NULL- */
- /* terminated string. Bytes from *source_buff will be transferred starting */
- /* from the entry position of source_buff and continuing until either N */
- /* bytes have been copied or the NULL at the end of *source_buff has been */
- /* reached. Copied bytes are placed starting at the entry position pointed */
- /* to by dest_buff. *dest_buff will contain a NULL-terminated string upon */
- /* exit. */
- /* */
- /* append_buff returns the actual number of bytes copied. */
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- unsigned long append_buff( char *source_buff, ulong N, char *dest_buff )
- {
- ulong i;
- char *s, *d;
-
- s = source_buff;
- d = dest_buff;
- for( i = 0; *s && i < N; i++ )
- *d++ = *s++;
-
- *d = NULL;
- return i;
- }
-
-
-
-
-
-
-
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- /* unsigned int fill_line_buff( FILE *infile, File_Type ftype, */
- /* char file_buffer[], */
- /* char **file_buff_ptr_ptr, */
- /* ulong Fbytes, unsigned int Lbytes, */
- /* char *line_buff_ptr, */
- /* int msg_flag, int *EOF_ptr ) */
- /* */
- /* Function to fill line buffer from file buffer. */
- /* */
- /* Fill line buffer with Lbytes bytes, beginning from the element pointed */
- /* to by line_buff_ptr, from the file buffer, starting from the element */
- /* pointed to by the pointer pointed to by file_buff_ptr_ptr. If the file */
- /* buffer runs out of bytes, read Fbytes more from infile. If the input */
- /* file gets read and runs into the end-of-file marker, or if the EOF has */
- /* already been encountered, *EOF_flag is set to 1; if not, it is set to */
- /* zero. ftype must be either FTEXT or FBINARY. */
- /* */
- /* If msg_flag = 1, then if bytes have to be read from the input file a */
- /* "reading..." message is written to (and erased from) the screen. */
- /* */
- /* fill_line_buff returns the number of bytes actually put into the line */
- /* buffer. The pointer pointed to by file_buff_ptr_ptr is updated to point */
- /* to the first unread element of file_buffer. */
- /* */
- /* function dependencies: append_buff, fill_file_buff */
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- uint fill_line_buff( FILE *infile, File_Type ftype, char file_buffer[],
- char **file_buff_ptr_ptr, ulong Fbytes,
- uint Lbytes, char *line_buff_ptr, int msg_flag,
- int *EOF_ptr )
- {
- uint n;
- ulong k;
- char *file_buff_ptr;
-
- /* try to move Lbytes bytes */
- file_buff_ptr = *file_buff_ptr_ptr;
- k = append_buff( file_buff_ptr, (unsigned long) Lbytes, line_buff_ptr );
- n = (unsigned int) k;
- *file_buff_ptr_ptr += n;
-
- /* if not enough bytes were copied, get more from the input file */
- if( n != Lbytes ) {
- *file_buffer = NULL;
- file_buff_ptr = file_buffer;
- *file_buff_ptr_ptr = file_buff_ptr;
- if( !feof(infile) ) {
- if( msg_flag ) {
- printf( "\r % % % % % Reading input file " );
- printf( "% % % % % \r" );
- }
- k = fill_file_buff( infile, ftype, Fbytes, file_buff_ptr,
- EOF_ptr );
- if( msg_flag ) {
- printf( "\r " );
- printf( " \r" );
- }
- /* finish filling the line buffer */
- line_buff_ptr += n;
- k = append_buff( file_buff_ptr, (unsigned long) (Lbytes - n),
- line_buff_ptr );
- n += (unsigned int) k;
- *file_buff_ptr_ptr += (unsigned int) k;
- } else /* already at end of file on entry into this routine */
- *EOF_ptr = 1;
- }
-
- return n;
- }
-
-
-
- /*
- *
- * error output routine
- *
- */
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- /* void ferrout( int bell, FILE *device, char *errstring ) */
- /* */
- /* error output */
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- void ferrout( Boolean bell, FILE *device, char *errstring )
- {
- if( bell ) fprintf( device, "\a" );
- fprintf( device, "%s", errstring );
- return;
- }
-
-
-
-
-
-
-
-
-