-
Notifications
You must be signed in to change notification settings - Fork 0
/
popenw.c
262 lines (235 loc) · 5.72 KB
/
popenw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* popenw.c
*
* popen with writing to pipes for matlab
*
* 2005-11-11 dpwe@ee.columbia.edu after popen
*
*
* $Header: /homes/drspeech/src/matlabpopen/RCS/popenw.c,v 1.2 2007/01/14 04:10:18 dpwe Exp $
*/
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include "mex.h"
#ifndef BIG_ENDIAN
#define BIG_ENDIAN 4321
#endif
#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN 1234
#endif
#ifndef BYTE_ORDER
#ifdef __DARWIN_UNIX03
#define BYTE_ORDER LITTLE_ENDIAN
#else
#define BYTE_ORDER BIG_ENDIAN
#endif
#endif
/* check if flags are working
#if BYTE_ORDER == BIG_ENDIAN
#error "byte order big endian"
#endif
#if BYTE_ORDER == LITTLE_ENDIAN
#error "byte order little endian"
#endif
*/
enum {
MXPO_INT16BE,
MXPO_INT16LE,
MXPO_INT16N,
MXPO_INT16R,
MXPO_FLOAT,
MXPO_DOUBLE,
MXPO_UINT8,
MXPO_CHAR,
};
#define FILETABSZ 16
static FILE *filetab[FILETABSZ];
static int filetabix = 0;
int findfreetab() {
/* find an open slot in the file table */
int i;
for (i = 0; i < filetabix; ++i) {
if ( filetab[i] == NULL ) {
/* NULL entries are currently unused */
return i;
}
}
if (filetabix < FILETABSZ) {
i = filetabix;
/* initialize it */
filetab[i] = NULL;
++filetabix;
return i;
}
/* out of space */
return -1;
}
void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int i, err, len;
long pvl, pvb[16];
if (nrhs < 1){
mexPrintf("popenw Y=popenw(X[,D[,F]]) Open and write an external process\n");
mexPrintf(" When X is a string, that string is executed as a new process\n");
mexPrintf(" and Y is returned as a handle (integer) to that stream.\n");
mexPrintf(" Subsequent calls to popen(Y,D[,F]) with that handle write\n");
mexPrintf(" the data in D to the the process, converted to binary\n");
mexPrintf(" according to the format string F (default: big endian int16).\n");
mexPrintf(" A call with D empty means to close the stream.\n");
return;
}
/* look at the data */
/* Check to be sure input argument is a string. */
if ((mxIsChar(prhs[0]))){
/* first argument is string - opening a new command */
FILE *f;
char *cmd;
int tabix = findfreetab();
if (tabix < 0) {
mexErrMsgTxt("Out of file table slots.");
} else {
cmd = mxArrayToString(prhs[0]);
/* fprintf(stderr, "cmd=%s\n", cmd); */
f = popen(cmd, "w");
mxFree(cmd);
if ( f == NULL ) {
mexErrMsgTxt("Error running external command.");
return;
}
/* else have a new command path - save the handle */
filetab[tabix] = f;
/* return the index */
if (nlhs > 0) {
double *pd;
mxArray *rslt = mxCreateDoubleMatrix(1,1, mxREAL);
plhs[0] = rslt;
pd = mxGetPr(rslt);
*pd = (double)tabix;
}
}
return;
}
if (nrhs < 2) {
mexErrMsgTxt("apparently accessing handle, but no D argument");
return;
}
/* get the handle */
{
int ix, rows, cols, npts, ngot;
int fmt = MXPO_INT16BE;
int sz = 2;
FILE *f = NULL;
double *pd;
char *data;
if (mxGetN(prhs[0]) == 0) {
mexErrMsgTxt("handle argument is empty");
return;
}
pd = mxGetPr(prhs[0]);
ix = (int)*pd;
if (ix < filetabix) {
f = filetab[ix];
}
if (f == NULL) {
mexErrMsgTxt("invalid handle");
return;
}
/* how many values to write */
npts = mxGetN(prhs[1]) * mxGetM(prhs[1]);
if (npts == 0) {
/* close */
pclose(f);
filetab[ix] = NULL;
return;
}
/* what is the format? */
if ( nrhs > 2 ) {
char *fmtstr;
if (!mxIsChar(prhs[2])) {
mexErrMsgTxt("format arg must be a string");
return;
}
fmtstr = mxArrayToString(prhs[2]);
if (strcmp(fmtstr, "int16n")==0 || strcmp(fmtstr, "int16") == 0) {
fmt = MXPO_INT16N;
} else if (strcmp(fmtstr, "int16r")==0) {
fmt = MXPO_INT16R;
} else if (strcmp(fmtstr, "int16be")==0) {
#if BYTE_ORDER == BIG_ENDIAN
fmt = MXPO_INT16N;
#else
fmt = MXPO_INT16R;
#endif
} else if (strcmp(fmtstr, "int16le")==0) {
#if BYTE_ORDER == BIG_ENDIAN
fmt = MXPO_INT16R;
#else
fmt = MXPO_INT16N;
#endif
} else if (strcmp(fmtstr, "uint8")==0) {
fmt = MXPO_UINT8;
sz = 1;
} else if (strcmp(fmtstr, "char")==0) {
fmt = MXPO_CHAR;
sz = 1;
} else {
mexErrMsgTxt("unrecognized format");
}
mxFree(fmtstr);
}
data = (char *)malloc(sz*npts+1);
/* format conversion */
if (sz == 1) {
if (mxIsChar(prhs[1])) {
if (mxGetString(prhs[1], data, npts+1) != 0) {
mxErrMsgTxt("Problems copying string data");
}
} else {
int i;
double *pd = mxGetPr(prhs[1]);
char *pc = (char *)data;
pd = pd + npts - 1;
pc = pc + npts - 1;
for(i = npts-1; i >= 0; --i) {
*pc-- = (char)*pd--;
}
}
} else if (fmt == MXPO_INT16N) {
int i;
double *pd = mxGetPr(prhs[1]);
short *ps = (short *)data;
pd = pd + npts - 1;
ps = ps + npts - 1;
for(i = npts-1; i >= 0; --i) {
*ps-- = (short)*pd--;
}
} else if (fmt == MXPO_INT16R) {
int i;
double *pd = mxGetPr(prhs[1]);
short *ps = (short *)data;
short v;
pd = pd + npts - 1;
ps = ps + npts - 1;
for(i = npts-1; i >= 0; --i) {
v = (short)*pd--;
*ps -- = (0xFF & (v >> 8)) + (0xFF00 & (v << 8));
}
} else {
mexErrMsgTxt("couldn't handle size");
}
/* write the data */
ngot = fwrite(data, sz, npts, f);
free(data);
/* return # bytes written */
if (nlhs > 0) {
double *pd;
mxArray *rslt = mxCreateDoubleMatrix(1,1, mxREAL);
plhs[0] = rslt;
pd = mxGetPr(rslt);
*pd = (double)ngot;
}
return;
}
}