143 |
|
return(NULL); |
144 |
|
} |
145 |
|
|
146 |
< |
/* Allocate and load a matrix from the given file (or stdin if NULL) */ |
146 |
> |
/* Allocate and load a matrix from the given input (or stdin if NULL) */ |
147 |
|
CMATRIX * |
148 |
< |
cm_load(const char *fname, int nrows, int ncols, int dtype) |
148 |
> |
cm_load(const char *inspec, int nrows, int ncols, int dtype) |
149 |
|
{ |
150 |
|
FILE *fp = stdin; |
151 |
|
CMATRIX *cm; |
152 |
|
|
153 |
< |
if (fname == NULL) |
154 |
< |
fname = "<stdin>"; |
155 |
< |
else if ((fp = fopen(fname, "r")) == NULL) { |
156 |
< |
sprintf(errmsg, "cannot open file '%s'", fname); |
153 |
> |
if (inspec == NULL) |
154 |
> |
inspec = "<stdin>"; |
155 |
> |
else if (inspec[0] == '!') { |
156 |
> |
fp = popen(inspec+1, "r"); |
157 |
> |
if (fp == NULL) { |
158 |
> |
sprintf(errmsg, "cannot start command '%s'", inspec); |
159 |
> |
error(SYSTEM, errmsg); |
160 |
> |
} |
161 |
> |
} else if ((fp = fopen(inspec, "r")) == NULL) { |
162 |
> |
sprintf(errmsg, "cannot open file '%s'", inspec); |
163 |
|
error(SYSTEM, errmsg); |
164 |
|
} |
165 |
|
#ifdef getc_unlocked |
184 |
|
} |
185 |
|
if (nrows <= 0) { /* don't know length? */ |
186 |
|
int guessrows = 147; /* usually big enough */ |
187 |
< |
if ((dtype != DTascii) & (fp != stdin)) { |
187 |
> |
if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) { |
188 |
|
long startpos = ftell(fp); |
189 |
|
if (fseek(fp, 0L, SEEK_END) == 0) { |
190 |
|
long endpos = ftell(fp); |
194 |
|
if ((endpos - startpos) % (ncols*elemsiz)) { |
195 |
|
sprintf(errmsg, |
196 |
|
"improper length for binary file '%s'", |
197 |
< |
fname); |
197 |
> |
inspec); |
198 |
|
error(USER, errmsg); |
199 |
|
} |
200 |
|
guessrows = (endpos - startpos)/(ncols*elemsiz); |
201 |
|
if (fseek(fp, startpos, SEEK_SET) < 0) { |
202 |
|
sprintf(errmsg, |
203 |
|
"fseek() error on file '%s'", |
204 |
< |
fname); |
204 |
> |
inspec); |
205 |
|
error(SYSTEM, errmsg); |
206 |
|
} |
207 |
|
nrows = guessrows; /* we're confident */ |
231 |
|
while ((c = getc(fp)) != EOF) |
232 |
|
if (!isspace(c)) { |
233 |
|
sprintf(errmsg, |
234 |
< |
"unexpected data at end of ascii file %s", |
235 |
< |
fname); |
234 |
> |
"unexpected data at end of ascii input '%s'", |
235 |
> |
inspec); |
236 |
|
error(WARNING, errmsg); |
237 |
|
break; |
238 |
|
} |
286 |
|
} |
287 |
|
if (fgetc(fp) != EOF) { |
288 |
|
sprintf(errmsg, |
289 |
< |
"unexpected data at end of binary file %s", |
290 |
< |
fname); |
289 |
> |
"unexpected data at end of binary input '%s'", |
290 |
> |
inspec); |
291 |
|
error(WARNING, errmsg); |
292 |
|
} |
293 |
|
} |
294 |
< |
if (fp != stdin) |
295 |
< |
fclose(fp); |
294 |
> |
if (fp != stdin) { |
295 |
> |
if (inspec[0] != '!') |
296 |
> |
fclose(fp); |
297 |
> |
else if (pclose(fp)) { |
298 |
> |
sprintf(errmsg, "error running command '%s'", inspec); |
299 |
> |
error(WARNING, errmsg); |
300 |
> |
} |
301 |
> |
} |
302 |
|
#ifdef getc_unlocked |
303 |
|
else |
304 |
|
funlockfile(fp); |
305 |
|
#endif |
306 |
|
return(cm); |
307 |
|
EOFerror: |
308 |
< |
sprintf(errmsg, "unexpected EOF reading %s", fname); |
308 |
> |
sprintf(errmsg, "unexpected EOF reading %s", inspec); |
309 |
|
error(USER, errmsg); |
310 |
|
not_handled: |
311 |
|
error(INTERNAL, "unhandled data size or length in cm_load()"); |