home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************\
- *
- * Module: FIOLib.C
- *
- * Purpose: Library of functions to handle Fortran formatted files.
- *
- * Copyright (c) 1993 by David Cook
- *
- * Permission to use, copy, and distribute this software and its
- * documentation for any purpose with or without fee is hereby granted,
- * provided that the above copyright notice appears in all copies and
- * that both that copyright notice and this permission notice appear
- * in supporting documentation.
- *
- * Permission to modify the software is granted, but not the right to
- * distribute the modified code. Modifications are to be distributed
- * as patches to released version.
- *
- * This software is provided "as is" without express or implied warranty.
- *
- * David Cook
- * Dave Cook Consulting
- * 206-230-0239
- * CIS 72124,3725
- *
- * Revision History:
- *
- * 24-Jun-93 D. Cook
- * Original version.
- *
- \******************************************************************************/
-
- #include <stdio.h>
- #include <fcntl.h>
- #include <sys\types.h>
- #include <sys\stat.h>
- #include <io.h>
- #include "fiolib.h"
-
- #ifndef TRUE
- #define FALSE 0
- #define TRUE !FALSE
- #endif
-
- static int first = TRUE ;
- static int bufcnt = 0,
- bufidx = 0 ;
-
-
- /******************************************************************************\
- *
- * Function to initialize for I/O
- *
- \******************************************************************************/
-
- /* Function */ void ForRewind()
-
- {
- first = TRUE ;
- bufcnt = bufidx = 0 ;
- }
-
- /******************************************************************************\
- *
- * Function to fetch data item from an unformatted record
- *
- \******************************************************************************/
-
- static char buf[128] ;
-
- /* Function */ int ForGetData ( int fd, char bp[], int size )
-
- {
- int cnt ;
-
- for ( cnt = 0 ; cnt < size ; cnt++ )
- {
- if ( bufidx >= bufcnt )
- {
- if ( ForGetBuf ( fd ) == EOF )
- return ( EOF ) ;
- }
- bp[cnt] = buf[bufidx++] ;
- }
- return ( cnt ) ;
-
- }
-
- /******************************************************************************\
- *
- * Function to load a buffer of data from an unformatted file
- *
- \******************************************************************************/
-
- static unsigned char RecHead, RecTail ; // Record Size: BOR, EOR
- #define BOF_MARK 75
- #define EOF_MARK 130
- #define MULTIREC 129 // Multiple record mark
-
-
- /* Function */ int ForGetBuf ( int fd )
-
- {
- if ( first )
- {
- read ( fd, &RecHead, 1 ) ;
- if ( RecHead != BOF_MARK )
- {
- fprintf ( stderr, "GetBuf - BOF mark mismatch.\n" ) ;
- return ( EOF ) ;
- }
- first = FALSE ;
- }
-
- read ( fd, &RecHead, 1 ) ;
- switch ( RecHead )
- {
- case EOF_MARK:
- return ( EOF ) ;
-
- case MULTIREC:
- read ( fd, buf, 128 ) ;
- bufcnt = 128 ;
- break ;
-
- default:
- read ( fd, buf, RecHead ) ;
- bufcnt = RecHead ;
- }
- read ( fd, &RecTail, 1 ) ;
- if ( RecTail != RecHead )
- {
- fprintf ( stderr, "GetBuf - BOR != EOR\n" ) ;
- return ( EOF ) ;
- }
- bufidx = 0 ;
- return ( RecHead ) ;
-
- }
-