Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
calio.c
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2.1 of the GNU Lesser General Public License 
00007   as published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU Lesser General Public 
00021   License along with this program; if not, write the Free Software 
00022   Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, 
00023   USA.
00024 
00025   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00026   Mountain View, CA 94043, or:
00027 
00028   http://www.sgi.com
00029 
00030   For further information regarding this notice, see:
00031 
00032   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00033 
00034 */
00035 
00036 
00037 static char USMID[] = "@(#) libf/fio/c1/calio.c 92.0    10/08/98 14:30:10";
00038 
00039 /*
00040  *      Cal i/o drivers
00041  */
00042 
00043 #include <stdio.h>
00044 #include <stdlib.h>
00045 #include <string.h>
00046 
00047 /*
00048  *      Cal i/o completion status
00049  */
00050 
00051 #define CNT     1       /*  count exhausted             */
00052 #define EOR     0       /*  end-of-record               */
00053 #ifndef EOF
00054 #define EOF     -1      /*  end-of-file                 */
00055 #endif
00056 
00057 #define OK      0       
00058 #define IOERR   -1      /*  Cal I/O error               */
00059 
00060 /*
00061  *      Cal i/o request modes
00062  */
00063 
00064 #define PARTIAL 0       /*  partial record i/o          */
00065 #define FULL    1       /*  full record i/o             */
00066 
00067 #define BLANK   ((long) ' ')    /* ASCII blank                  */
00068 
00069 #define MYRMAX  256
00070 
00071 /*
00072  *      cal_rch - cal read characters
00073  */
00074 
00075 long
00076 cal_rch(fp, uda, chars, mode, status )   
00077 FILE *fp;
00078 long *uda, chars, mode, *status;
00079 {
00080 
00081         long nc, i, n, c;
00082         char *cp;
00083  
00084 /*
00085  *      If the number of characters to read is zero and full record
00086  *      mode has been requested, skip to the end of record.  If in
00087  *      partial record mode, the position remains as is.
00088  */
00089         if (chars == 0)
00090                 {
00091                 *status = CNT;
00092                 if (mode == FULL )
00093                         {
00094                         while (1)
00095                                 {
00096                                 if (fp->_cnt <= 0)
00097                                         {
00098                                         c = _filbuf(fp);
00099                                         /* EOF here means incomplete record. */
00100                                         if (c == EOF) return(0);
00101                                         fp->_cnt++;
00102                                         fp->_ptr--;
00103                                         }
00104                                 cp = memchr(fp->_ptr, '\n', fp->_cnt);
00105                                 if (cp != NULL)
00106                                         {
00107                                         cp++;
00108                                         fp->_cnt -= cp - (char *)fp->_ptr;
00109                                         fp->_ptr = (unsigned char *)cp;
00110                                         return(0);
00111                                         }
00112                                 else
00113                                         fp->_cnt = 0;
00114                                 }                     
00115                         }
00116                 }
00117 /*
00118  *      Loop until the character count has been exhausted,
00119  *      an end of file is encountered, or end of record.   
00120  */
00121         n = 0;
00122         while (n < chars)
00123                 {
00124                 if (fp->_cnt <= 0)
00125                         {
00126                         c = _filbuf(fp);
00127                         /* EOF here means incomplete record. */
00128                         if (c == EOF) {
00129                                 *status = EOF; 
00130                                 /* blank fill the record */
00131                                 (void) _memwset(uda, BLANK, chars - n);
00132                                 return(0);}
00133                         fp->_cnt++;
00134                         fp->_ptr--;
00135                         }
00136                 /* if not enough chars in buffer to satisfy request, */
00137                 if ((chars - n) >= fp->_cnt)
00138                         {
00139                         cp = memchr(fp->_ptr, '\n', fp->_cnt);
00140                         if (cp != NULL)
00141                                 {
00142                                 /* found end of record */
00143                                 nc = cp - (char *)fp->_ptr;
00144                                 n += nc;
00145                                 _unpack((char *)fp->_ptr, uda, nc);
00146                                 (void) _memwset(&uda[nc], BLANK, chars - n);
00147                                 fp->_cnt -= nc+1;
00148                                 fp->_ptr = (unsigned char *)++cp;
00149                                 *status = EOR;
00150                                 return(n);
00151                                 }
00152                         else
00153                                 {
00154                                 /* not found end of record */
00155                                 nc = fp->_cnt;
00156                                 _unpack((char *)fp->_ptr, uda, nc);
00157                                 uda += nc;
00158                                 fp->_cnt -= nc;
00159                                 n += nc;
00160                                 fp->_ptr += nc;
00161                                 /* go refill the buffer */
00162                                 }
00163                         }
00164                 else /* have enough characters to satisfy request */
00165                         {
00166                         cp = memchr(fp->_ptr, '\n', chars - n);
00167                         if (cp != NULL)
00168                                 {
00169                                 /* found end of record */
00170                                 nc = cp - (char *)fp->_ptr;
00171                                 n += nc;
00172                                 _unpack((char *)fp->_ptr, uda, nc);
00173                                 (void) _memwset(&uda[nc], BLANK, chars - n);
00174                                 fp->_cnt -= nc+1;
00175                                 fp->_ptr = (unsigned char *)++cp;
00176                                 *status = EOR;
00177                                 return(n);
00178                                 }
00179                         else
00180                                 {
00181                                 nc = chars - n;
00182                                 n += nc;
00183                                 _unpack((char *)fp->_ptr, uda, nc);
00184                                 (void) _memwset(&uda[nc], BLANK, chars - n);
00185                                 fp->_cnt -= nc;
00186                                 fp->_ptr += nc;
00187                                 uda += nc;
00188                                 }
00189                         }
00190                 }
00191 /*
00192  *      Get the next character to see if at end of record.  Set the
00193  *      user's status word accordingly.
00194  */
00195         c = getc(fp);
00196         if (c == '\n' ) {
00197                 *status = EOR;
00198                 return(n);
00199         }
00200         else
00201                 *status = CNT;
00202 /*
00203  *      We are not at end of record.  Thus if reading in full record 
00204  *      mode skip until EOR is found.  If reading in partial record
00205  *      mode, unget the last character read.
00206  */
00207         if (mode == FULL)
00208                 while (1)
00209                         {
00210                         if (fp->_cnt <= 0)
00211                                 {
00212                                 c = _filbuf(fp);
00213                                 /* EOF here means incomplete record. */
00214                                 if (c == EOF) return(n);
00215                                 fp->_cnt++;
00216                                 fp->_ptr--;
00217                                 }
00218                         cp = memchr(fp->_ptr, '\n', fp->_cnt);
00219                         if (cp != NULL)
00220                                 {
00221                                 cp++;
00222                                 fp->_cnt -= cp - (char *)fp->_ptr;
00223                                 fp->_ptr = (unsigned char *)cp;
00224                                 return(n);
00225                                 }
00226                         else
00227                                 fp->_cnt = 0;
00228                         }
00229         else {
00230                 (void) ungetc ((char) c, fp);
00231         }
00232 
00233         return(n);      /* return number of character read */
00234 
00235 }
00236 
00237 /*
00238  *      cal_wch - cal write characters
00239  */
00240 
00241 long
00242 cal_wch(fp, uda, chars, mode, status )   
00243 FILE *fp;
00244 long *uda, chars, mode, *status;
00245 {
00246         int  n, c, j, i, tpi, ist;
00247         char tp[MYRMAX+sizeof(long)];
00248 
00249         *status = OK;
00250 
00251         /* in case I/O has not been initialized... */
00252         if (fp->_base == NULL)
00253                 c = _findbuf(fp);       /* let stdio do its thing */
00254 
00255         j = 0;
00256         /* If the stream is unbuffered... */
00257         if (fp->_flag &(_IONBF | _IOLBF))
00258                 {
00259                 i = 0;
00260                 tpi = 0;
00261                 while (j < chars)
00262                         {
00263                         /* Pack chars into temp buffer and write em out */
00264                         i = chars - j;
00265                         if (chars - j > MYRMAX)
00266                                 i = MYRMAX;
00267                         _pack(&uda[j], &tp[tpi], i, -1);
00268                         tpi += i;
00269                         /* avoid syscalls, only write at EOL */
00270                         if (tpi >= MYRMAX)
00271                                 {
00272                                 ist = write(fileno(fp),tp,tpi);
00273                                 if( ist != tpi)
00274                                         {
00275                                         fp->_flag |= _IOERR;
00276                                         *status = IOERR;
00277                                         return(EOF);
00278                                         }
00279                                 else
00280                                         {
00281                                         tpi = 0;
00282                                         }
00283                                 }
00284                         j += i;
00285                         }
00286                 if (mode == FULL) 
00287                         {
00288                         tp[tpi++] = '\n';
00289                         chars++;
00290                         }
00291                 ist = write(fileno(fp),tp,tpi);
00292                 if( ist != tpi)
00293                         {
00294                         fp->_flag |= _IOERR;
00295                         *status = IOERR;
00296                         return(EOF);
00297                         }
00298                 }
00299         else
00300                 {
00301                 while (fp->_cnt < chars - j)
00302                         {
00303                         i = fp->_cnt;     /* we have this much room */
00304                         if (i == 0)
00305                                 {
00306                                 /* this one putc will init many things */
00307                                 /* and make the rest of this loop work */
00308                                 c = uda[j];
00309                                 c = putc(c, fp);
00310                                 i = 1;
00311                                 }
00312                         else
00313                                 {
00314                                 _pack(&uda[j], (char *)fp->_ptr, i, -1);
00315                                 fp->_ptr += i;
00316                                 fp->_cnt = -1;    /* dummy up count for _xflsbuf() */
00317                                 }
00318                         _xflsbuf(fp);
00319                         if (ferror(fp))
00320                                 {
00321                                 fp->_flag |= _IOERR;
00322                                 *status = IOERR;
00323                                 return(EOF);
00324                                 }
00325                         j += i;
00326                         }
00327                 if ((chars - j) > 0)
00328                         {
00329                         _pack(&uda[j], (char *)fp->_ptr, chars - j, -1);
00330                         fp->_cnt -= chars - j;
00331                         fp->_ptr += chars - j;
00332                         }
00333 
00334                 if (mode == FULL) 
00335                         {
00336                         putc('\n', fp);
00337                         chars++;
00338                         }
00339                 }
00340  
00341         return(chars);
00342 }
00343 
00344 /*
00345  *      cal_rwd - cal read words
00346  */
00347 
00348 long
00349 cal_rwd(fp, uda, words, mode, status )   
00350 FILE  *fp;
00351 long *uda, words, mode, *status;
00352 {
00353         long n, chars, c;
00354 
00355 /*
00356  *      If the number of words to read is zero return to caller. 
00357  *      Std. UNICOS binary files have no record structure.  Except
00358  *      for end of file, a read request always returns EOR status.
00359  */
00360         *status = EOR;
00361         if (words == 0)
00362                 return(0);
00363 
00364         n = fread((char *)uda, sizeof(long), words, fp );
00365 
00366         if (n == 0) {           /* if no data read */
00367                 *status = EOF;
00368                 return(0);
00369         }
00370  
00371         if (n < 0) {            /* if an error or end of file */
00372                 if (n != EOF)
00373                         return(IOERR);
00374                 *status = EOF;
00375                 return(0);
00376         }
00377 
00378         return(n);      /* return number of words read */
00379 
00380 }
00381 
00382 /*
00383  *      cal_wwd - cal write words
00384  */
00385 
00386 long
00387 cal_wwd(fp, uda, words, mode, status )   
00388 FILE  *fp;
00389 long *uda, words, mode, *status;
00390 {
00391         long n;
00392 /*
00393  *      If number of words to write is zero return to caller.  
00394  *      Std. UNICOS binary files have no record structures.
00395  */
00396         if (words == 0)
00397                 return(0);
00398 /*
00399  *      Use low-level binary i/o routine to write the requested
00400  *      amount of data.
00401  */
00402         if (n = fwrite((char *)uda, sizeof(long), words, fp) != words)
00403                 return(IOERR);
00404 
00405         return(words);  /* return number of words written */
00406 
00407 }
00408 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines