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