/* This file contains simple binary read/write routines. */ #include #include #include #include #if defined(PETSC_HAVE_UNISTD_H) #include #endif #if defined(PETSC_HAVE_IO_H) #include #endif #if !defined(PETSC_HAVE_O_BINARY) #define O_BINARY 0 #endif const char *const PetscFileModes[] = {"READ","WRITE","APPEND","UPDATE","APPEND_UPDATE","PetscFileMode","PETSC_FILE_",NULL}; /* --------------------------------------------------------- */ /* PetscByteSwapEnum - Swap bytes in a PETSc Enum */ PetscErrorCode PetscByteSwapEnum(PetscEnum *buff,PetscInt n) { PetscInt i,j; PetscEnum tmp = ENUM_DUMMY; char *ptr1,*ptr2 = (char*)&tmp; PetscFunctionBegin; for (j=0; j 0) break; /* Proxy for EOF */ if (ret < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Error reading from file, errno %d",errno); m -= ret; p += ret; n += ret; } if (m && !count) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Read past end of file"); num = (PetscInt)(n/typesize); /* Should we require `n % typesize == 0` ? */ if (count) *count = num; /* TODO: This is most likely wrong for PETSC_BIT_LOGICAL */ #if defined(PETSC_USE_REAL___FLOAT128) if ((type == PETSC_REAL || type == PETSC_COMPLEX) && readdouble) { PetscInt i, cnt = num * ((type == PETSC_REAL) ? 1 : 2); PetscReal *preal = (PetscReal*)data; if (!PetscBinaryBigEndian()) {ierr = PetscByteSwapDouble(pdouble,cnt);CHKERRQ(ierr);} for (i=0; i 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only binary view a single function at a time"); ierr = PetscFPTFind(*(void**)p,&fnametmp);CHKERRQ(ierr); ierr = PetscStrncpy(fname,fnametmp,m);CHKERRQ(ierr); #else ierr = PetscStrncpy(fname,"",m);CHKERRQ(ierr); #endif wtype = PETSC_CHAR; pp = (char*)fname; ptmp = (void*)fname; } #if defined(PETSC_USE_REAL___FLOAT128) ierr = PetscOptionsGetBool(NULL,NULL,"-binary_write_double",&writedouble,NULL);CHKERRQ(ierr); /* If using __float128 precision we still write in doubles to file */ if ((type == PETSC_SCALAR || type == PETSC_REAL || type == PETSC_COMPLEX) && writedouble) { wtype = PETSC_DOUBLE; ierr = PetscMalloc1(n,&ppp);CHKERRQ(ierr); pv = (PetscReal*)pp; for (i=0; i