19 #include <grass/gis.h>
20 #include <grass/Vect.h>
36 extern unsigned char dbl_cnvrt[
sizeof(double)];
37 extern unsigned char flt_cnvrt[
sizeof(float)];
38 extern unsigned char lng_cnvrt[
sizeof(long)];
40 extern unsigned char shrt_cnvrt[
sizeof(short)];
44 static char *buffer =
NULL;
45 static int buf_alloced = 0;
47 static int buf_alloc(
int needed)
52 if (needed <= buf_alloced)
78 unsigned char *c1, *c2;
81 ret =
dig_fread(buf, PORT_DOUBLE, cnt, fp);
87 buf_alloc(cnt * PORT_DOUBLE);
88 ret =
dig_fread(buffer, PORT_DOUBLE, cnt, fp);
92 c1 = (
unsigned char *)buffer;
93 c2 = (
unsigned char *)buf;
94 for (i = 0; i < cnt; i++) {
95 for (j = 0; j < PORT_DOUBLE; j++) {
109 unsigned char *c1, *c2;
112 ret =
dig_fread(buf, PORT_FLOAT, cnt, fp);
118 buf_alloc(cnt * PORT_FLOAT);
119 ret =
dig_fread(buffer, PORT_FLOAT, cnt, fp);
123 c1 = (
unsigned char *)buffer;
124 c2 = (
unsigned char *)buf;
125 for (i = 0; i < cnt; i++) {
126 for (j = 0; j < PORT_FLOAT; j++) {
140 unsigned char *c1, *c2;
144 ret =
dig_fread(buf, PORT_LONG, cnt, fp);
150 buf_alloc(cnt * PORT_LONG);
151 ret =
dig_fread(buffer, PORT_LONG, cnt, fp);
155 memset(buf, 0, cnt *
sizeof(
long));
157 c1 = (
unsigned char *)buffer;
158 c2 = (
unsigned char *)buf;
159 for (i = 0; i < cnt; i++) {
162 if (c1[PORT_LONG - 1] & 0x80)
163 memset(c2, 0xff,
sizeof(
long));
164 memcpy(c2, c1, PORT_LONG);
168 memset(c2, 0xff,
sizeof(
long));
169 memcpy(c2 +
nat_lng - PORT_LONG, c1, PORT_LONG);
178 buf_alloc(cnt * PORT_LONG);
179 ret =
dig_fread(buffer, PORT_LONG, cnt, fp);
183 memset(buf, 0, cnt *
sizeof(
long));
185 c1 = (
unsigned char *)buffer;
186 c2 = (
unsigned char *)buf;
187 for (i = 0; i < cnt; i++) {
189 if (
Cur_Head->byte_order == ENDIAN_LITTLE) {
190 if (c1[PORT_LONG - 1] & 0x80)
191 memset(c2, 0xff,
sizeof(
long));
195 memset(c2, 0xff,
sizeof(
long));
197 for (j = 0; j < PORT_LONG; j++)
210 unsigned char *c1, *c2;
220 buf_alloc(cnt * PORT_INT);
221 ret =
dig_fread(buffer, PORT_INT, cnt, fp);
225 memset(buf, 0, cnt *
sizeof(
int));
227 c1 = (
unsigned char *)buffer;
228 c2 = (
unsigned char *)buf;
229 for (i = 0; i < cnt; i++) {
232 if (c1[PORT_INT - 1] & 0x80)
233 memset(c2, 0xff,
sizeof(
int));
234 memcpy(c2, c1, PORT_INT);
238 memset(c2, 0xff,
sizeof(
int));
239 memcpy(c2 +
nat_int - PORT_INT, c1, PORT_INT);
248 buf_alloc(cnt * PORT_INT);
249 ret =
dig_fread(buffer, PORT_INT, cnt, fp);
253 memset(buf, 0, cnt *
sizeof(
int));
255 c1 = (
unsigned char *)buffer;
256 c2 = (
unsigned char *)buf;
257 for (i = 0; i < cnt; i++) {
259 if (
Cur_Head->byte_order == ENDIAN_LITTLE) {
260 if (c1[PORT_INT - 1] & 0x80)
261 memset(c2, 0xff,
sizeof(
int));
265 memset(c2, 0xff,
sizeof(
int));
267 for (j = 0; j < PORT_INT; j++)
280 unsigned char *c1, *c2;
284 ret =
dig_fread(buf, PORT_SHORT, cnt, fp);
290 buf_alloc(cnt * PORT_SHORT);
291 if (0 >= (ret =
dig_fread(buffer, PORT_SHORT, cnt, fp)))
295 memset(buf, 0, cnt *
sizeof(
short));
297 c1 = (
unsigned char *)buffer;
298 c2 = (
unsigned char *)buf;
299 for (i = 0; i < cnt; i++) {
302 if (c1[PORT_SHORT - 1] & 0x80)
303 memset(c2, 0xff,
sizeof(
short));
304 memcpy(c2, c1, PORT_SHORT);
308 memset(c2, 0xff,
sizeof(
short));
309 memcpy(c2 +
nat_shrt - PORT_SHORT, c1, PORT_SHORT);
318 buf_alloc(cnt * PORT_SHORT);
319 ret =
dig_fread(buffer, PORT_SHORT, cnt, fp);
323 memset(buf, 0, cnt *
sizeof(
short));
325 c1 = (
unsigned char *)buffer;
326 c2 = (
unsigned char *)buf;
327 for (i = 0; i < cnt; i++) {
329 if (
Cur_Head->byte_order == ENDIAN_LITTLE) {
330 if (c1[PORT_SHORT - 1] & 0x80)
331 memset(c2, 0xff,
sizeof(
short));
335 memset(c2, 0xff,
sizeof(
short));
337 for (j = 0; j < PORT_SHORT; j++)
338 c2[
Cur_Head->shrt_cnvrt[j]] = c1[j];
351 ret =
dig_fread(buf, PORT_CHAR, cnt, fp);
371 int cnt, GVFILE * fp)
374 unsigned char *c1, *c2;
377 if (
dig_fwrite(buf, PORT_DOUBLE, cnt, fp) == cnt)
381 buf_alloc(cnt * PORT_DOUBLE);
382 c1 = (
unsigned char *)buf;
383 c2 = (
unsigned char *)buffer;
384 for (i = 0; i < cnt; i++) {
385 for (j = 0; j < PORT_DOUBLE; j++)
387 c1 +=
sizeof(
double);
390 if (
dig_fwrite(buffer, PORT_DOUBLE, cnt, fp) == cnt)
397 int cnt, GVFILE * fp)
400 unsigned char *c1, *c2;
403 if (
dig_fwrite(buf, PORT_FLOAT, cnt, fp) == cnt)
407 buf_alloc(cnt * PORT_FLOAT);
408 c1 = (
unsigned char *)buf;
409 c2 = (
unsigned char *)buffer;
410 for (i = 0; i < cnt; i++) {
411 for (j = 0; j < PORT_FLOAT; j++)
416 if (
dig_fwrite(buffer, PORT_FLOAT, cnt, fp) == cnt)
423 int cnt, GVFILE * fp)
426 unsigned char *c1, *c2;
430 if (
dig_fwrite(buf, PORT_LONG, cnt, fp) == cnt)
434 buf_alloc(cnt * PORT_LONG);
435 c1 = (
unsigned char *)buf;
436 c2 = (
unsigned char *)buffer;
437 for (i = 0; i < cnt; i++) {
439 memcpy(c2, c1, PORT_LONG);
441 memcpy(c2, c1 +
nat_lng - PORT_LONG, PORT_LONG);
445 if (
dig_fwrite(buffer, PORT_LONG, cnt, fp) == cnt)
450 buf_alloc(cnt * PORT_LONG);
451 c1 = (
unsigned char *)buf;
452 c2 = (
unsigned char *)buffer;
453 for (i = 0; i < cnt; i++) {
454 for (j = 0; j < PORT_LONG; j++)
459 if (
dig_fwrite(buffer, PORT_LONG, cnt, fp) == cnt)
466 int cnt, GVFILE * fp)
469 unsigned char *c1, *c2;
473 if (
dig_fwrite(buf, PORT_INT, cnt, fp) == cnt)
477 buf_alloc(cnt * PORT_INT);
478 c1 = (
unsigned char *)buf;
479 c2 = (
unsigned char *)buffer;
480 for (i = 0; i < cnt; i++) {
482 memcpy(c2, c1, PORT_INT);
484 memcpy(c2, c1 +
nat_int - PORT_INT, PORT_INT);
488 if (
dig_fwrite(buffer, PORT_INT, cnt, fp) == cnt)
493 buf_alloc(cnt * PORT_INT);
494 c1 = (
unsigned char *)buf;
495 c2 = (
unsigned char *)buffer;
496 for (i = 0; i < cnt; i++) {
497 for (j = 0; j < PORT_INT; j++)
502 if (
dig_fwrite(buffer, PORT_INT, cnt, fp) == cnt)
509 int cnt, GVFILE * fp)
512 unsigned char *c1, *c2;
516 if (
dig_fwrite(buf, PORT_SHORT, cnt, fp) == cnt)
520 buf_alloc(cnt * PORT_SHORT);
521 c1 = (
unsigned char *)buf;
522 c2 = (
unsigned char *)buffer;
523 for (i = 0; i < cnt; i++) {
525 memcpy(c2, c1, PORT_SHORT);
527 memcpy(c2, c1 +
nat_shrt - PORT_SHORT, PORT_SHORT);
531 if (
dig_fwrite(buffer, PORT_SHORT, cnt, fp) == cnt)
536 buf_alloc(cnt * PORT_SHORT);
537 c1 = (
unsigned char *)buf;
538 c2 = (
unsigned char *)buffer;
539 for (i = 0; i < cnt; i++) {
540 for (j = 0; j < PORT_SHORT; j++)
541 c2[j] = c1[
Cur_Head->shrt_cnvrt[j]];
545 if (
dig_fwrite(buffer, PORT_SHORT, cnt, fp) == cnt)
553 int cnt, GVFILE * fp)
559 int cnt, GVFILE * fp)
561 if (
dig_fwrite(buf, PORT_CHAR, cnt, fp) == cnt)
574 port->byte_order = byte_order;
577 port->dbl_quick =
TRUE;
579 port->dbl_quick =
FALSE;
581 for (i = 0; i < PORT_DOUBLE; i++) {
582 if (port->byte_order == ENDIAN_BIG)
585 port->dbl_cnvrt[i] =
dbl_cnvrt[PORT_DOUBLE - i - 1];
589 port->flt_quick =
TRUE;
591 port->flt_quick =
FALSE;
593 for (i = 0; i < PORT_FLOAT; i++) {
594 if (port->byte_order == ENDIAN_BIG)
597 port->flt_cnvrt[i] =
flt_cnvrt[PORT_FLOAT - i - 1];
601 port->lng_quick =
TRUE;
603 port->lng_quick =
FALSE;
605 for (i = 0; i < PORT_LONG; i++) {
606 if (port->byte_order == ENDIAN_BIG)
609 port->lng_cnvrt[i] =
lng_cnvrt[PORT_LONG - i - 1];
613 port->int_quick =
TRUE;
615 port->int_quick =
FALSE;
617 for (i = 0; i < PORT_INT; i++) {
618 if (port->byte_order == ENDIAN_BIG)
621 port->int_cnvrt[i] =
int_cnvrt[PORT_INT - i - 1];
625 port->shrt_quick =
TRUE;
627 port->shrt_quick =
FALSE;
629 for (i = 0; i < PORT_SHORT; i++) {
630 if (port->byte_order == ENDIAN_BIG)
633 port->shrt_cnvrt[i] =
shrt_cnvrt[PORT_SHORT - i - 1];
649 return (ENDIAN_LITTLE);
size_t dig_fwrite(void *ptr, size_t size, size_t nmemb, GVFILE *file)
Write GVFILE.
unsigned char lng_cnvrt[sizeof(long)]
int dig_set_cur_port(struct Port_info *port)
int dig__fread_port_P(plus_t *buf, int cnt, GVFILE *fp)
int dig__fread_port_S(short *buf, int cnt, GVFILE *fp)
int dig__fwrite_port_D(double *buf, int cnt, GVFILE *fp)
void dig_init_portable(struct Port_info *port, int byte_order)
int dig__fread_port_L(long *buf, int cnt, GVFILE *fp)
int dig__fwrite_port_I(int *buf, int cnt, GVFILE *fp)
int dig__fwrite_port_P(plus_t *buf, int cnt, GVFILE *fp)
int dig__fwrite_port_L(long *buf, int cnt, GVFILE *fp)
unsigned char dbl_cnvrt[sizeof(double)]
int dig__fread_port_D(double *buf, int cnt, GVFILE *fp)
struct Port_info * Cur_Head
int dig__fread_port_F(float *buf, int cnt, GVFILE *fp)
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
int dig__fread_port_C(char *buf, int cnt, GVFILE *fp)
unsigned char int_cnvrt[sizeof(int)]
unsigned char shrt_cnvrt[sizeof(short)]
void * dig__alloc_space(int n_wanted, int *n_elements, int chunk_size, void *ptr, int element_size)
int dig__fwrite_port_F(float *buf, int cnt, GVFILE *fp)
int dig__fwrite_port_C(char *buf, int cnt, GVFILE *fp)
int dig__byte_order_out()
int dig__fwrite_port_S(short *buf, int cnt, GVFILE *fp)
size_t dig_fread(void *ptr, size_t size, size_t nmemb, GVFILE *file)
Read GVFILE.
int dig__fread_port_I(int *buf, int cnt, GVFILE *fp)
unsigned char flt_cnvrt[sizeof(float)]