| 57 |
|
return(cm); |
| 58 |
|
} |
| 59 |
|
|
| 60 |
+ |
typedef struct { |
| 61 |
+ |
int dtype; /* data type */ |
| 62 |
+ |
int nrows, ncols; /* matrix size */ |
| 63 |
+ |
char *err; /* error message */ |
| 64 |
+ |
} CMINFO; /* header info record */ |
| 65 |
+ |
|
| 66 |
|
static int |
| 67 |
< |
getDT(char *s, void *p) |
| 67 |
> |
get_cminfo(char *s, void *p) |
| 68 |
|
{ |
| 69 |
+ |
CMINFO *ip = (CMINFO *)p; |
| 70 |
|
char fmt[32]; |
| 71 |
|
int i; |
| 72 |
< |
|
| 72 |
> |
|
| 73 |
> |
if (!strncmp(s, "NCOMP=", 6) && atoi(s+6) != 3) { |
| 74 |
> |
ip->err = "unexpected # components (must be 3)"; |
| 75 |
> |
return(-1); |
| 76 |
> |
} |
| 77 |
> |
if (!strncmp(s, "NROWS=", 6)) { |
| 78 |
> |
ip->nrows = atoi(s+6); |
| 79 |
> |
return(0); |
| 80 |
> |
} |
| 81 |
> |
if (!strncmp(s, "NCOLS=", 6)) { |
| 82 |
> |
ip->ncols = atoi(s+6); |
| 83 |
> |
return(0); |
| 84 |
> |
} |
| 85 |
|
if (!formatval(fmt, s)) |
| 86 |
|
return(0); |
| 87 |
|
for (i = 1; i < DTend; i++) |
| 88 |
|
if (!strcmp(fmt, cm_fmt_id[i])) |
| 89 |
< |
*((int *)p) = i; |
| 89 |
> |
ip->dtype = i; |
| 90 |
|
return(0); |
| 91 |
|
} |
| 92 |
|
|
| 93 |
< |
/* Load header to obtain data type */ |
| 94 |
< |
int |
| 95 |
< |
getDTfromHeader(FILE *fp) |
| 93 |
> |
/* Load header to obtain/check data type and number of columns */ |
| 94 |
> |
char * |
| 95 |
> |
cm_getheader(int *dt, int *nr, int *nc, FILE *fp) |
| 96 |
|
{ |
| 97 |
< |
int dt = DTfromHeader; |
| 98 |
< |
|
| 99 |
< |
if (getheader(fp, getDT, &dt) < 0) |
| 100 |
< |
error(SYSTEM, "header read error"); |
| 101 |
< |
if (dt == DTfromHeader) |
| 102 |
< |
error(USER, "missing data format in header"); |
| 103 |
< |
return(dt); |
| 97 |
> |
CMINFO cmi; |
| 98 |
> |
/* read header */ |
| 99 |
> |
cmi.dtype = DTfromHeader; |
| 100 |
> |
cmi.nrows = cmi.ncols = 0; |
| 101 |
> |
cmi.err = "unexpected EOF in header"; |
| 102 |
> |
if (getheader(fp, &get_cminfo, &cmi) < 0) |
| 103 |
> |
return(cmi.err); |
| 104 |
> |
if (dt != NULL) { /* get/check data type? */ |
| 105 |
> |
if (cmi.dtype == DTfromHeader) { |
| 106 |
> |
if (*dt == DTfromHeader) |
| 107 |
> |
return("missing/unknown data format in header"); |
| 108 |
> |
} else if (*dt == DTfromHeader) |
| 109 |
> |
*dt = cmi.dtype; |
| 110 |
> |
else if (*dt != cmi.dtype) |
| 111 |
> |
return("unexpected data format in header"); |
| 112 |
> |
} |
| 113 |
> |
if (nr != NULL) { /* get/check #rows? */ |
| 114 |
> |
if (*nr <= 0) |
| 115 |
> |
*nr = cmi.nrows; |
| 116 |
> |
else if ((cmi.nrows > 0) & (*nr != cmi.nrows)) |
| 117 |
> |
return("unexpected row count in header"); |
| 118 |
> |
} |
| 119 |
> |
if (nc != NULL) { /* get/check #columns? */ |
| 120 |
> |
if (*nc <= 0) |
| 121 |
> |
*nc = cmi.ncols; |
| 122 |
> |
else if ((cmi.ncols > 0) & (*nc != cmi.ncols)) |
| 123 |
> |
return("unexpected column count in header"); |
| 124 |
> |
} |
| 125 |
> |
return(NULL); |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
/* Allocate and load a matrix from the given file (or stdin if NULL) */ |
| 132 |
|
FILE *fp = stdin; |
| 133 |
|
CMATRIX *cm; |
| 134 |
|
|
| 94 |
– |
if (ncols <= 0) |
| 95 |
– |
error(USER, "Non-positive number of columns"); |
| 135 |
|
if (fname == NULL) |
| 136 |
|
fname = "<stdin>"; |
| 137 |
|
else if ((fp = fopen(fname, "r")) == NULL) { |
| 143 |
|
#endif |
| 144 |
|
if (dtype != DTascii) |
| 145 |
|
SET_FILE_BINARY(fp); /* doesn't really work */ |
| 146 |
< |
if (dtype == DTfromHeader) |
| 147 |
< |
dtype = getDTfromHeader(fp); |
| 146 |
> |
if (!dtype | !ncols) { /* expecting header? */ |
| 147 |
> |
char *err = cm_getheader(&dtype, &nrows, &ncols, fp); |
| 148 |
> |
if (err != NULL) |
| 149 |
> |
error(USER, err); |
| 150 |
> |
if (ncols <= 0) |
| 151 |
> |
error(USER, "unspecified number of columns"); |
| 152 |
> |
} |
| 153 |
|
switch (dtype) { |
| 154 |
|
case DTascii: |
| 155 |
|
case DTfloat: |