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