Actual source code: swrite.c
1: /*
3: This is the equivalent of MATLAB's fwrite() 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: #define PETSC_MEX_ERROR(a) \
12: { \
13: fprintf(stdout, "sread: %s \n", a); \
14: return; \
15: }
17: PETSC_EXTERN void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
18: {
19: int i, fd, cnt, dt;
20: PetscErrorCode ierr;
22: /* check output parameters */
23: if (nrhs != 3) PETSC_MEX_ERROR("Receive requires three input arguments.");
24: fd = (int)mxGetScalar(prhs[0]);
25: cnt = (int)mxGetNumberOfElements(prhs[1]);
26: dt = (PetscDataType)mxGetScalar(prhs[2]);
28: if (dt == PETSC_DOUBLE) {
29: ierr = PetscBinaryWrite(fd, mxGetPr(prhs[1]), cnt, (PetscDataType)dt);
30: if (ierr) PETSC_MEX_ERROR("Unable to send double items.");
31: } else if (dt == PETSC_INT) {
32: int *tmp = (int *)mxMalloc((cnt + 5) * sizeof(int));
33: double *t = mxGetPr(prhs[1]);
34: for (i = 0; i < cnt; i++) tmp[i] = (int)t[i];
35: ierr = PetscBinaryWrite(fd, tmp, cnt, (PetscDataType)dt);
36: if (ierr) PETSC_MEX_ERROR("Unable to send int items.");
37: mxFree(tmp);
38: } else if (dt == PETSC_CHAR) {
39: char *tmp = (char *)mxMalloc((cnt + 5) * sizeof(char));
40: mxGetNChars(prhs[1], tmp, cnt + 1);
41: ierr = PetscBinaryWrite(fd, tmp, cnt, (PetscDataType)dt);
42: if (ierr) PETSC_MEX_ERROR("Unable to send char items.");
43: mxFree(tmp);
44: } else PETSC_MEX_ERROR("Unknown datatype.");
45: return;
46: }
48: int main(int argc, char **argv)
49: {
50: return 0;
51: }