Actual source code: bread.c

  1: /*$Id: bread.c,v 1.8 2001/08/06 21:13:30 bsmith Exp $*/

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


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

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

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

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

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

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

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

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

128:   return 0;
129: }