ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/cmatrix.c
(Generate patch)

Comparing ray/src/util/cmatrix.c (file contents):
Revision 2.7 by greg, Tue Jul 8 16:39:41 2014 UTC vs.
Revision 2.15 by greg, Wed Feb 17 23:26:06 2016 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
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[] = {
18 <                        "unknown", "ascii", "float", "double",
19 <                        COLRFMT, CIEFMT
18 >                        "unknown", "ascii", COLRFMT, CIEFMT,
19 >                        "float", "double"
20                  };
21  
22   const int       cm_elem_size[] = {
# Line 143 | Line 144 | cm_getheader(int *dt, int *nr, int *nc, FILE *fp)
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;
151 >        const int       ROWINC = 2048;
152 >        FILE            *fp = stdin;
153 >        CMATRIX         *cm;
154  
155 <        if (fname == NULL)
156 <                fname = "<stdin>";
157 <        else if ((fp = fopen(fname, "r")) == NULL) {
158 <                sprintf(errmsg, "cannot open file '%s'", fname);
155 >        if (inspec == NULL)
156 >                inspec = "<stdin>";
157 >        else if (inspec[0] == '!') {
158 >                fp = popen(inspec+1, "r");
159 >                if (fp == NULL) {
160 >                        sprintf(errmsg, "cannot start command '%s'", inspec);
161 >                        error(SYSTEM, errmsg);
162 >                }
163 >        } else if ((fp = fopen(inspec, "r")) == NULL) {
164 >                sprintf(errmsg, "cannot open file '%s'", inspec);
165                  error(SYSTEM, errmsg);
166          }
167   #ifdef getc_unlocked
# Line 178 | Line 186 | cm_load(const char *fname, int nrows, int ncols, int d
186          }
187          if (nrows <= 0) {                       /* don't know length? */
188                  int     guessrows = 147;        /* usually big enough */
189 <                if ((dtype != DTascii) & (fp != stdin)) {
189 >                if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) {
190                          long    startpos = ftell(fp);
191                          if (fseek(fp, 0L, SEEK_END) == 0) {
192                                  long    endpos = ftell(fp);
# Line 188 | Line 196 | cm_load(const char *fname, int nrows, int ncols, int d
196                                  if ((endpos - startpos) % (ncols*elemsiz)) {
197                                          sprintf(errmsg,
198                                          "improper length for binary file '%s'",
199 <                                                        fname);
199 >                                                        inspec);
200                                          error(USER, errmsg);
201                                  }
202                                  guessrows = (endpos - startpos)/(ncols*elemsiz);
203                                  if (fseek(fp, startpos, SEEK_SET) < 0) {
204                                          sprintf(errmsg,
205                                                  "fseek() error on file '%s'",
206 <                                                        fname);
206 >                                                        inspec);
207                                          error(SYSTEM, errmsg);
208                                  }
209                                  nrows = guessrows;      /* we're confident */
# Line 211 | Line 219 | cm_load(const char *fname, int nrows, int ncols, int d
219                  int     r, c;
220                  for (r = 0; r < maxrow; r++) {
221                      if (r >= cm->nrows)                 /* need more space? */
222 <                        cm = cm_resize(cm, 2*cm->nrows);
222 >                        cm = cm_resize(cm, cm->nrows+ROWINC);
223                      for (c = 0; c < ncols; c++) {
224                          COLORV  *cv = cm_lval(cm,r,c);
225 <                        if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3)
225 >                        if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) {
226                                  if ((nrows <= 0) & (r > 0) & !c) {
227                                          cm = cm_resize(cm, maxrow=r);
228                                          break;
229                                  } else
230                                          goto EOFerror;
231 +                        }
232                      }
233                  }
234                  while ((c = getc(fp)) != EOF)
235                          if (!isspace(c)) {
236                                  sprintf(errmsg,
237 <                                "unexpected data at end of ascii file %s",
238 <                                                fname);
237 >                                "unexpected data at end of ascii input '%s'",
238 >                                                inspec);
239                                  error(WARNING, errmsg);
240                                  break;
241                          }
# Line 241 | Line 250 | cm_load(const char *fname, int nrows, int ncols, int d
250                                  if (nrows <= 0) {       /* unknown length */
251                                          if (nread == cm->nrows*cm->ncols)
252                                                          /* need more space? */
253 <                                                cm = cm_resize(cm, 2*cm->nrows);
253 >                                                cm = cm_resize(cm, cm->nrows+ROWINC);
254                                          else if (nread && !(nread % cm->ncols))
255                                                          /* seem to be  done */
256                                                  cm = cm_resize(cm, nread/cm->ncols);
# Line 280 | Line 289 | cm_load(const char *fname, int nrows, int ncols, int d
289                  }
290                  if (fgetc(fp) != EOF) {
291                                  sprintf(errmsg,
292 <                                "unexpected data at end of binary file %s",
293 <                                                fname);
292 >                                "unexpected data at end of binary input '%s'",
293 >                                                inspec);
294                                  error(WARNING, errmsg);
295                  }
296          }
297 <        if (fp != stdin)
298 <                fclose(fp);
297 >        if (fp != stdin) {
298 >                if (inspec[0] != '!')
299 >                        fclose(fp);
300 >                else if (pclose(fp)) {
301 >                        sprintf(errmsg, "error running command '%s'", inspec);
302 >                        error(WARNING, errmsg);
303 >                }
304 >        }
305   #ifdef getc_unlocked
306          else
307                  funlockfile(fp);
308   #endif
309          return(cm);
310   EOFerror:
311 <        sprintf(errmsg, "unexpected EOF reading %s", fname);
311 >        sprintf(errmsg, "unexpected EOF reading %s", inspec);
312          error(USER, errmsg);
313   not_handled:
314          error(INTERNAL, "unhandled data size or length in cm_load()");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines