Actual source code: sread.c
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, int *, PetscDataType);
13: #define PETSC_MEX_ERROR(a) \
14: { \
15: fprintf(stdout, "sread: %s \n", a); \
16: return; \
17: }
18: /*-----------------------------------------------------------------*/
19: /* */
20: /*-----------------------------------------------------------------*/
21: PETSC_EXTERN void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
22: {
23: int fd, cnt, dt;
26: /* check output parameters */
27: if (nlhs != 1) PETSC_MEX_ERROR("Receive requires one output argument.");
28: if (nrhs != 3) PETSC_MEX_ERROR("Receive requires three input arguments.");
29: fd = (int)mxGetScalar(prhs[0]);
30: cnt = (int)mxGetScalar(prhs[1]);
31: dt = (PetscDataType)mxGetScalar(prhs[2]);
33: if (dt == PETSC_DOUBLE) {
34: plhs[0] = mxCreateDoubleMatrix(1, cnt, mxREAL);
35: PetscBinaryRead(fd, mxGetPr(plhs[0]), cnt, NULL, (PetscDataType)dt);
36: if (ierr) PETSC_MEX_ERROR("Unable to receive double items.");
37: } else if (dt == PETSC_INT) {
38: plhs[0] = mxCreateNumericMatrix(1, cnt, mxINT32_CLASS, mxREAL);
39: PetscBinaryRead(fd, mxGetPr(plhs[0]), cnt, NULL, (PetscDataType)dt);
40: if (ierr) PETSC_MEX_ERROR("Unable to receive int items.");
41: } else if (dt == PETSC_CHAR) {
42: char *tmp = (char *)mxMalloc(cnt * sizeof(char));
43: PetscBinaryRead(fd, tmp, cnt, NULL, (PetscDataType)dt);
44: if (ierr) PETSC_MEX_ERROR("Unable to receive char items.");
45: plhs[0] = mxCreateStringFromNChars(tmp, cnt);
46: mxFree(tmp);
47: } else PETSC_MEX_ERROR("Unknown datatype.");
48: return;
49: }
51: int main(int argc, char **argv)
52: {
53: return 0;
54: }