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