81 |
|
|
82 |
|
typedef struct { |
83 |
|
int dtype; /* data type */ |
84 |
+ |
int need2swap; /* need byte swap? */ |
85 |
|
int nrows, ncols; /* matrix size */ |
86 |
|
char *err; /* error message */ |
87 |
|
} CMINFO; /* header info record */ |
105 |
|
ip->ncols = atoi(s+6); |
106 |
|
return(0); |
107 |
|
} |
108 |
+ |
if ((i = isbigendian(s)) >= 0) { |
109 |
+ |
ip->need2swap = (nativebigendian() != i); |
110 |
+ |
return(0); |
111 |
+ |
} |
112 |
|
if (!formatval(fmt, s)) |
113 |
|
return(0); |
114 |
|
for (i = 1; i < DTend; i++) |
119 |
|
|
120 |
|
/* Load header to obtain/check data type and number of columns */ |
121 |
|
char * |
122 |
< |
cm_getheader(int *dt, int *nr, int *nc, FILE *fp) |
122 |
> |
cm_getheader(int *dt, int *nr, int *nc, int *swp, FILE *fp) |
123 |
|
{ |
124 |
|
CMINFO cmi; |
125 |
|
/* read header */ |
126 |
|
cmi.dtype = DTfromHeader; |
127 |
+ |
cmi.need2swap = 0; |
128 |
|
cmi.nrows = cmi.ncols = 0; |
129 |
|
cmi.err = "unexpected EOF in header"; |
130 |
|
if (getheader(fp, get_cminfo, &cmi) < 0) |
150 |
|
else if ((cmi.ncols > 0) & (*nc != cmi.ncols)) |
151 |
|
return("unexpected column count in header"); |
152 |
|
} |
153 |
+ |
if (swp) /* get/check swap? */ |
154 |
+ |
*swp = cmi.need2swap; |
155 |
|
return(NULL); |
156 |
|
} |
157 |
|
|
161 |
|
{ |
162 |
|
const int ROWINC = 2048; |
163 |
|
FILE *fp = stdin; |
164 |
+ |
int swap = 0; |
165 |
|
CMATRIX *cm; |
166 |
|
|
167 |
|
if (!inspec) |
182 |
|
if (dtype != DTascii) |
183 |
|
SET_FILE_BINARY(fp); /* doesn't really work */ |
184 |
|
if (!dtype | !ncols) { /* expecting header? */ |
185 |
< |
char *err = cm_getheader(&dtype, &nrows, &ncols, fp); |
185 |
> |
char *err = cm_getheader(&dtype, &nrows, &ncols, &swap, fp); |
186 |
|
if (err) |
187 |
|
error(USER, err); |
188 |
|
if (ncols <= 0) |
305 |
|
inspec); |
306 |
|
error(WARNING, errmsg); |
307 |
|
} |
308 |
+ |
} |
309 |
+ |
if (swap) { |
310 |
+ |
if (dtype == DTfloat) |
311 |
+ |
swap32((char *)cm->cmem, 3*cm->nrows*cm->ncols); |
312 |
+ |
else if (dtype == DTdouble) |
313 |
+ |
swap64((char *)cm->cmem, 3*cm->nrows*cm->ncols); |
314 |
|
} |
315 |
|
if (fp != stdin) { |
316 |
|
if (inspec[0] != '!') |