1*b75c6efcSBarry Smith /* 2*b75c6efcSBarry Smith 3*b75c6efcSBarry Smith This is the equivalent of MATLAB's fread() only on sockets instead of 4*b75c6efcSBarry Smith binary files. 5*b75c6efcSBarry Smith */ 6*b75c6efcSBarry Smith 7*b75c6efcSBarry Smith #include <petscsys.h> 8*b75c6efcSBarry Smith #include <../src/sys/classes/viewer/impls/socket/socket.h> 9*b75c6efcSBarry Smith #include <mex.h> 10*b75c6efcSBarry Smith 11*b75c6efcSBarry Smith PetscErrorCode PetscBinaryRead(int, void *p, int, int *, PetscDataType); 12*b75c6efcSBarry Smith 13*b75c6efcSBarry Smith #define PETSC_MEX_ERROR(a) \ 14*b75c6efcSBarry Smith { \ 15*b75c6efcSBarry Smith fprintf(stdout, "sread: %s \n", a); \ 16*b75c6efcSBarry Smith return; \ 17*b75c6efcSBarry Smith } 18*b75c6efcSBarry Smith /*-----------------------------------------------------------------*/ 19*b75c6efcSBarry Smith /* */ 20*b75c6efcSBarry Smith /*-----------------------------------------------------------------*/ 21*b75c6efcSBarry Smith PETSC_EXTERN void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 22*b75c6efcSBarry Smith { 23*b75c6efcSBarry Smith int fd, cnt, dt; 24*b75c6efcSBarry Smith PetscErrorCode ierr; 25*b75c6efcSBarry Smith 26*b75c6efcSBarry Smith /* check output parameters */ 27*b75c6efcSBarry Smith if (nlhs != 1) PETSC_MEX_ERROR("Receive requires one output argument."); 28*b75c6efcSBarry Smith if (nrhs != 3) PETSC_MEX_ERROR("Receive requires three input arguments."); 29*b75c6efcSBarry Smith fd = (int)mxGetScalar(prhs[0]); 30*b75c6efcSBarry Smith cnt = (int)mxGetScalar(prhs[1]); 31*b75c6efcSBarry Smith dt = (PetscDataType)mxGetScalar(prhs[2]); 32*b75c6efcSBarry Smith 33*b75c6efcSBarry Smith if (dt == PETSC_DOUBLE) { 34*b75c6efcSBarry Smith plhs[0] = mxCreateDoubleMatrix(1, cnt, mxREAL); 35*b75c6efcSBarry Smith ierr = PetscBinaryRead(fd, mxGetPr(plhs[0]), cnt, NULL, (PetscDataType)dt); 36*b75c6efcSBarry Smith if (ierr) PETSC_MEX_ERROR("Unable to receive double items."); 37*b75c6efcSBarry Smith } else if (dt == PETSC_INT) { 38*b75c6efcSBarry Smith plhs[0] = mxCreateNumericMatrix(1, cnt, mxINT32_CLASS, mxREAL); 39*b75c6efcSBarry Smith ierr = PetscBinaryRead(fd, mxGetPr(plhs[0]), cnt, NULL, (PetscDataType)dt); 40*b75c6efcSBarry Smith if (ierr) PETSC_MEX_ERROR("Unable to receive int items."); 41*b75c6efcSBarry Smith } else if (dt == PETSC_CHAR) { 42*b75c6efcSBarry Smith char *tmp = (char *)mxMalloc(cnt * sizeof(char)); 43*b75c6efcSBarry Smith ierr = PetscBinaryRead(fd, tmp, cnt, NULL, (PetscDataType)dt); 44*b75c6efcSBarry Smith if (ierr) PETSC_MEX_ERROR("Unable to receive char items."); 45*b75c6efcSBarry Smith plhs[0] = mxCreateStringFromNChars(tmp, cnt); 46*b75c6efcSBarry Smith mxFree(tmp); 47*b75c6efcSBarry Smith } else PETSC_MEX_ERROR("Unknown datatype."); 48*b75c6efcSBarry Smith return; 49*b75c6efcSBarry Smith } 50*b75c6efcSBarry Smith 51*b75c6efcSBarry Smith int main(int argc, char **argv) 52*b75c6efcSBarry Smith { 53*b75c6efcSBarry Smith return 0; 54*b75c6efcSBarry Smith } 55