Actual source code: bread.c

      2: #include <stdio.h>
     3:  #include petscsys.h
     4:  #include src/sys/src/viewer/impls/socket/socket.h
      5: #include "mex.h"

      8: /*
      9:    TAKEN from src/sys/src/fileio/sysio.c The swap byte routines are
     10:   included here because the Matlab programs that use this do NOT
     11:   link to the PETSc libraries.
     12: */
     13: #include <errno.h>
     14: #if defined(PETSC_HAVE_UNISTD_H)
     15: #include <unistd.h>
     16: #endif

     18: #if !defined(PETSC_WORDS_BIGENDIAN)
     19: /*
     20:   SYByteSwapInt - Swap bytes in an integer
     21: */
     24: void SYByteSwapInt(int *buff,int n)
     25: {
     26:   int  i,j,tmp;
     27:   char *ptr1,*ptr2 = (char*)&tmp;
     28:   for (j=0; j<n; j++) {
     29:     ptr1 = (char*)(buff + j);
     30:     for (i=0; i<sizeof(int); i++) {
     31:       ptr2[i] = ptr1[sizeof(int)-1-i];
     32:     }
     33:     buff[j] = tmp;
     34:   }
     35: }
     36: /*
     37:   SYByteSwapShort - Swap bytes in a short
     38: */
     41: void SYByteSwapShort(short *buff,int n)
     42: {
     43:   int   i,j;
     44:   short tmp;
     45:   char  *ptr1,*ptr2 = (char*)&tmp;
     46:   for (j=0; j<n; j++) {
     47:     ptr1 = (char*)(buff + j);
     48:     for (i=0; i<sizeof(short); i++) {
     49:       ptr2[i] = ptr1[sizeof(int)-1-i];
     50:     }
     51:     buff[j] = tmp;
     52:   }
     53: }
     54: /*
     55:   SYByteSwapScalar - Swap bytes in a double
     56:   Complex is dealt with as if array of double twice as long.
     57: */
     60: void SYByteSwapScalar(PetscScalar *buff,int n)
     61: {
     62:   int    i,j;
     63:   double tmp,*buff1 = (double*)buff;
     64:   char   *ptr1,*ptr2 = (char*)&tmp;
     65: #if defined(PETSC_USE_COMPLEX)
     66:   n *= 2;
     67: #endif
     68:   for (j=0; j<n; j++) {
     69:     ptr1 = (char*)(buff1 + j);
     70:     for (i=0; i<sizeof(double); i++) {
     71:       ptr2[i] = ptr1[sizeof(double)-1-i];
     72:     }
     73:     buff1[j] = tmp;
     74:   }
     75: }
     76: #endif

     80: /*
     81:     PetscBinaryRead - Reads from a binary file.

     83:   Input Parameters:
     84: .   fd - the file
     85: .   n  - the number of items to read
     86: .   type - the type of items to read (PETSC_INT or PETSC_SCALAR)

     88:   Output Parameters:
     89: .   p - the buffer

     91:   Notes: does byte swapping to work on all machines.
     92: */
     93: PetscErrorCode PetscBinaryRead(int fd,void *p,int n,PetscDataType type)
     94: {

     96:   int  maxblock,wsize,err;
     97:   char *pp = (char*)p;
     98: #if !defined(PETSC_WORDS_BIGENDIAN)
     99:   int  ntmp = n;
    100:   void *ptmp = p;
    101: #endif

    103:   maxblock = 65536;
    104:   if (type == PETSC_INT)         n *= sizeof(int);
    105:   else if (type == PETSC_SCALAR) n *= sizeof(PetscScalar);
    106:   else if (type == PETSC_SHORT)  n *= sizeof(short);
    107:   else printf("PetscBinaryRead: Unknown type");
    108:
    109:   while (n) {
    110:     wsize = (n < maxblock) ? n : maxblock;
    111:     err = read(fd,pp,wsize);
    112: #if !defined(PETSC_MISSING_ERRNO_EINTR)
    113:     if (err < 0 && errno == EINTR) continue;
    114: #endif
    115:     if (!err && wsize > 0) return 1;
    116:     if (err < 0) {
    117:       perror("error reading");
    118:       return err;
    119:     }
    120:     n  -= err;
    121:     pp += err;
    122:   }
    123: #if !defined(PETSC_WORDS_BIGENDIAN)
    124:   if (type == PETSC_INT) SYByteSwapInt((int*)ptmp,ntmp);
    125:   else if (type == PETSC_SCALAR) SYByteSwapScalar((PetscScalar*)ptmp,ntmp);
    126:   else if (type == PETSC_SHORT) SYByteSwapShort((short*)ptmp,ntmp);
    127: #endif

    129:   return 0;
    130: }