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