00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <string.h>
00021 #include <grass/gis.h>
00022 #include <grass/portable.h>
00023 #include <grass/Vect.h>
00024
00025 struct Port_info *Cur_Head;
00026
00027 static char *buffer = NULL;
00028 static int buf_alloced = 0;
00029
00030 static int
00031 buf_alloc (int needed)
00032 {
00033 char *p;
00034 int cnt;
00035
00036 if (needed <= buf_alloced)
00037 return (0);
00038 cnt = buf_alloced;
00039 p = dig__alloc_space (needed, &cnt, 100, buffer, 1);
00040 if (p == NULL)
00041 return (dig_out_of_memory ());
00042 buffer = p;
00043 buf_alloced = cnt;
00044 return (0);
00045 }
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 int
00060 dig__fread_port_D ( double *buf,
00061 int cnt,
00062 GVFILE * fp)
00063 {
00064 int i, j, ret;
00065 unsigned char *c1, *c2;
00066
00067 if ( Cur_Head->dbl_quick)
00068 {
00069 ret = dig_fread (buf, PORT_DOUBLE, cnt, fp);
00070 if ( ret != cnt ) return 0;
00071 }
00072 else
00073 {
00074
00075 buf_alloc (cnt * PORT_DOUBLE);
00076 ret = dig_fread (buffer, PORT_DOUBLE, cnt, fp);
00077 if ( ret != cnt ) return 0;
00078
00079 c1 = (unsigned char *) buffer;
00080 c2 = (unsigned char *) buf;
00081 for (i = 0; i < cnt; i++)
00082 {
00083 for (j = 0; j < PORT_DOUBLE; j++)
00084 {
00085 c2[Cur_Head->dbl_cnvrt[j]] = c1[j];
00086 }
00087 c1 += PORT_DOUBLE;
00088 c2 += sizeof (double);
00089 }
00090 }
00091 return 1;
00092 }
00093
00094
00095 int
00096 dig__fread_port_F (
00097 float *buf,
00098 int cnt,
00099 GVFILE * fp)
00100 {
00101 int i, j, ret;
00102 unsigned char *c1, *c2;
00103
00104 if ( Cur_Head->flt_quick)
00105 {
00106 ret = dig_fread (buf, PORT_FLOAT, cnt, fp);
00107 if ( ret != cnt ) return 0;
00108 }
00109 else
00110 {
00111
00112 buf_alloc (cnt * PORT_FLOAT);
00113 ret = dig_fread (buffer, PORT_FLOAT, cnt, fp);
00114 if ( ret != cnt ) return 0;
00115
00116 c1 = (unsigned char *) buffer;
00117 c2 = (unsigned char *) buf;
00118 for (i = 0; i < cnt; i++)
00119 {
00120 for (j = 0; j < PORT_FLOAT; j++)
00121 {
00122 c2[Cur_Head->flt_cnvrt[j]] = c1[j];
00123 }
00124 c1 += PORT_FLOAT;
00125 c2 += sizeof (float);
00126 }
00127 }
00128 return 1;
00129 }
00130
00131
00132 int
00133 dig__fread_port_L (
00134 long *buf,
00135 int cnt,
00136 GVFILE * fp)
00137 {
00138 int i, j, ret;
00139 unsigned char *c1, *c2;
00140
00141 if ( Cur_Head->lng_quick )
00142 {
00143 #if NATIVE_LONG == PORT_LONG
00144 ret = dig_fread (buf, PORT_LONG, cnt, fp);
00145 if ( ret != cnt ) return 0;
00146 #else
00147
00148 buf_alloc (cnt * PORT_LONG);
00149 ret = dig_fread (buffer, PORT_LONG, cnt, fp);
00150 if ( ret != cnt ) return 0;
00151
00152 memset (buf, 0, cnt * sizeof(long));
00153
00154 c1 = (unsigned char *) buffer;
00155 #if LONG_ORDER == ENDIAN_LITTLE
00156 c2 = (unsigned char *) buf;
00157 #else
00158 c2 = (unsigned char *) buf + NATIVE_LONG - PORT_LONG;
00159 #endif
00160 for (i = 0; i < cnt; i++)
00161 {
00162
00163 #if LONG_ORDER == ENDIAN_LITTLE
00164 if ( c1[PORT_LONG-1] & 0x80 )
00165 memset (c2, 0xff, sizeof(long));
00166 #else
00167 if ( c1[0] & 0x80 )
00168 memset (c2, 0xff, sizeof(long));
00169 #endif
00170 memcpy (c2, c1, PORT_LONG);
00171 c1 += PORT_LONG;
00172 c2 += sizeof (long);
00173 }
00174 #endif
00175 }
00176 else
00177 {
00178
00179 buf_alloc (cnt * PORT_LONG);
00180 ret = dig_fread (buffer, PORT_LONG, cnt, fp);
00181 if ( ret != cnt ) return 0;
00182
00183 memset (buf, 0, cnt * sizeof(long));
00184
00185 c1 = (unsigned char *) buffer;
00186 c2 = (unsigned char *) buf;
00187 for (i = 0; i < cnt; i++)
00188 {
00189
00190 if ( Cur_Head->byte_order == ENDIAN_LITTLE ) {
00191 if ( c1[PORT_LONG-1] & 0x80 )
00192 memset (c2, 0xff, sizeof(long));
00193 } else {
00194 if ( c1[0] & 0x80 )
00195 memset (c2, 0xff, sizeof(long));
00196 }
00197 for (j = 0; j < PORT_LONG; j++)
00198 c2[Cur_Head->lng_cnvrt[j]] = c1[j];
00199 c1 += PORT_LONG;
00200 c2 += sizeof (long);
00201 }
00202 }
00203 return 1;
00204 }
00205
00206
00207 int
00208 dig__fread_port_I (
00209 int *buf,
00210 int cnt,
00211 GVFILE * fp)
00212 {
00213 int i, j, ret;
00214 unsigned char *c1, *c2;
00215
00216 if ( Cur_Head->int_quick )
00217 {
00218 #if NATIVE_INT == PORT_INT
00219 ret = dig_fread (buf, PORT_INT, cnt, fp);
00220 if ( ret != cnt ) return 0;
00221 #else
00222
00223 buf_alloc (cnt * PORT_INT);
00224 ret = dig_fread (buffer, PORT_INT, cnt, fp);
00225 if ( ret != cnt ) return 0;
00226
00227 memset (buf, 0, cnt * sizeof(int));
00228
00229 c1 = (unsigned char *) buffer;
00230 #if INT_ORDER == ENDIAN_LITTLE
00231 c2 = (unsigned char *) buf;
00232 #else
00233 c2 = (unsigned char *) buf + NATIVE_INT - PORT_INT;
00234 #endif
00235 for (i = 0; i < cnt; i++)
00236 {
00237
00238 #if INT_ORDER == ENDIAN_LITTLE
00239 if ( c1[PORT_INT-1] & 0x80 )
00240 memset (c2, 0xff, sizeof(int));
00241 #else
00242 if ( c1[0] & 0x80 )
00243 memset (c2, 0xff, sizeof(int));
00244 #endif
00245 memcpy (c2, c1, PORT_INT);
00246 c1 += PORT_INT;
00247 c2 += sizeof (int);
00248 }
00249 #endif
00250 }
00251 else
00252 {
00253
00254 buf_alloc (cnt * PORT_INT);
00255 ret = dig_fread (buffer, PORT_INT, cnt, fp);
00256 if ( ret != cnt ) return 0;
00257
00258 memset (buf, 0, cnt * sizeof(int));
00259
00260 c1 = (unsigned char *) buffer;
00261 c2 = (unsigned char *) buf;
00262 for (i = 0; i < cnt; i++)
00263 {
00264
00265 if ( Cur_Head->byte_order == ENDIAN_LITTLE ) {
00266 if ( c1[PORT_INT-1] & 0x80 )
00267 memset (c2, 0xff, sizeof(int));
00268 } else {
00269 if ( c1[0] & 0x80 )
00270 memset (c2, 0xff, sizeof(int));
00271 }
00272 for (j = 0; j < PORT_INT; j++)
00273 c2[Cur_Head->int_cnvrt[j]] = c1[j];
00274 c1 += PORT_INT;
00275 c2 += sizeof (int);
00276 }
00277 }
00278 return 1;
00279 }
00280
00281
00282 int
00283 dig__fread_port_S (
00284 short *buf,
00285 int cnt,
00286 GVFILE * fp)
00287 {
00288 int i, j, ret;
00289 unsigned char *c1, *c2;
00290
00291 if ( Cur_Head->shrt_quick )
00292 {
00293 #if NATIVE_SHORT == PORT_SHORT
00294 ret = dig_fread (buf, PORT_SHORT, cnt, fp);
00295 if ( ret != cnt ) return 0;
00296 #else
00297
00298 buf_alloc (cnt * PORT_SHORT);
00299 if (0 >= (ret = dig_fread (buffer, PORT_SHORT, cnt, fp)))
00300 if ( ret != cnt ) return 0;
00301
00302 memset (buf, 0, cnt * sizeof(short));
00303
00304 c1 = (unsigned char *) buffer;
00305 #if SHORT_ORDER == ENDIAN_LITTLE
00306 c2 = (unsigned char *) buf;
00307 #else
00308 c2 = (unsigned char *) buf + NATIVE_SHORT - PORT_SHORT;
00309 #endif
00310 for (i = 0; i < cnt; i++)
00311 {
00312
00313 #if SHORT_ORDER == ENDIAN_LITTLE
00314 if ( c1[PORT_SHORT-1] & 0x80 )
00315 memset (c2, 0xff, sizeof(short));
00316 #else
00317 if ( c1[0] & 0x80 )
00318 memset (c2, 0xff, sizeof(short));
00319 #endif
00320 memcpy (c2, c1, PORT_SHORT);
00321 c1 += PORT_SHORT;
00322 c2 += sizeof (short);
00323 }
00324 #endif
00325 }
00326 else
00327 {
00328
00329 buf_alloc (cnt * PORT_SHORT);
00330 ret = dig_fread (buffer, PORT_SHORT, cnt, fp);
00331 if ( ret != cnt ) return 0;
00332
00333 memset (buf, 0, cnt * sizeof(short));
00334
00335 c1 = (unsigned char *) buffer;
00336 c2 = (unsigned char *) buf;
00337 for (i = 0; i < cnt; i++)
00338 {
00339
00340 if ( Cur_Head->byte_order == ENDIAN_LITTLE ) {
00341 if ( c1[PORT_SHORT-1] & 0x80 )
00342 memset (c2, 0xff, sizeof(short));
00343 } else {
00344 if ( c1[0] & 0x80 )
00345 memset (c2, 0xff, sizeof(short));
00346 }
00347 for (j = 0; j < PORT_SHORT; j++)
00348 c2[Cur_Head->shrt_cnvrt[j]] = c1[j];
00349 c1 += PORT_SHORT;
00350 c2 += sizeof (short);
00351 }
00352 }
00353 return 1;
00354 }
00355
00356
00357 int
00358 dig__fread_port_C ( char *buf,
00359 int cnt,
00360 GVFILE * fp)
00361 {
00362 int ret;
00363 ret = dig_fread (buf, PORT_CHAR, cnt, fp);
00364 if ( ret != cnt ) return 0;
00365 return 1;
00366 }
00367
00368
00369
00370 int
00371 dig__fread_port_P ( plus_t * buf,
00372 int cnt,
00373 GVFILE * fp)
00374 {
00375 int *ibuf;
00376
00377 ibuf = (int *) buf;
00378
00379 return ( dig__fread_port_I (ibuf, cnt, fp) );
00380 }
00381
00382
00383
00384 int
00385 dig__fwrite_port_D ( double *buf,
00386 int cnt,
00387 GVFILE * fp)
00388 {
00389 int i,j;
00390 unsigned char *c1, *c2;
00391
00392 if ( Cur_Head->dbl_quick )
00393 {
00394 if ( dig_fwrite (buf, PORT_DOUBLE, cnt, fp) == cnt )
00395 return 1;
00396 }
00397 else
00398 {
00399 buf_alloc (cnt * PORT_DOUBLE);
00400 c1 = (unsigned char *) buf;
00401 c2 = (unsigned char *) buffer;
00402 for (i = 0; i < cnt; i++)
00403 {
00404 for (j = 0; j < PORT_DOUBLE; j++)
00405 c2[j] = c1[Cur_Head->dbl_cnvrt[j]];
00406 c1 += sizeof (double);
00407 c2 += PORT_DOUBLE;
00408 }
00409 if ( dig_fwrite (buffer, PORT_DOUBLE, cnt, fp) == cnt )
00410 return 1;
00411 }
00412 return 0;
00413 }
00414
00415 int
00416 dig__fwrite_port_F ( float *buf,
00417 int cnt,
00418 GVFILE * fp)
00419 {
00420 int i,j;
00421 unsigned char *c1, *c2;
00422 if ( Cur_Head->flt_quick )
00423 {
00424 if ( dig_fwrite (buf, PORT_FLOAT, cnt, fp) == cnt )
00425 return 1;
00426 }
00427 else
00428 {
00429 buf_alloc (cnt * PORT_FLOAT);
00430 c1 = (unsigned char *) buf;
00431 c2 = (unsigned char *) buffer;
00432 for (i = 0; i < cnt; i++)
00433 {
00434 for (j = 0; j < PORT_FLOAT; j++)
00435 c2[j] = c1[Cur_Head->flt_cnvrt[j]];
00436 c1 += sizeof (float);
00437 c2 += PORT_FLOAT;
00438 }
00439 if ( dig_fwrite (buffer, PORT_FLOAT, cnt, fp) == cnt )
00440 return 1;
00441 }
00442 return 0;
00443 }
00444
00445 int
00446 dig__fwrite_port_L ( long *buf,
00447 int cnt,
00448 GVFILE * fp)
00449 {
00450 int i,j;
00451 unsigned char *c1, *c2;
00452 if ( Cur_Head->lng_quick )
00453 {
00454 #if NATIVE_LONG == PORT_LONG
00455 if ( dig_fwrite (buf, PORT_LONG, cnt, fp) == cnt )
00456 return 1;
00457 #else
00458 buf_alloc (cnt * PORT_LONG);
00459 #if LONG_ORDER == ENDIAN_LITTLE
00460 c1 = (unsigned char *) buf;
00461 #else
00462 c1 = (unsigned char *) buf + NATIVE_LONG - PORT_LONG;
00463 #endif
00464 c2 = (unsigned char *) buffer;
00465 for (i = 0; i < cnt; i++)
00466 {
00467 memcpy (c2, c1, PORT_LONG);
00468 c1 += PORT_LONG;
00469 c2 += sizeof (long);
00470 }
00471 if ( dig_fwrite (buffer, PORT_LONG, cnt, fp) == cnt )
00472 return 1;
00473 #endif
00474 }
00475 else
00476 {
00477 buf_alloc (cnt * PORT_LONG);
00478 c1 = (unsigned char *) buf;
00479 c2 = (unsigned char *) buffer;
00480 for (i = 0; i < cnt; i++)
00481 {
00482 for (j = 0; j < PORT_LONG; j++)
00483 c2[j] = c1[Cur_Head->lng_cnvrt[j]];
00484 c1 += sizeof (long);
00485 c2 += PORT_LONG;
00486 }
00487 if ( dig_fwrite (buffer, PORT_LONG, cnt, fp) == cnt )
00488 return 1;
00489 }
00490 return 0;
00491 }
00492
00493 int
00494 dig__fwrite_port_I ( int *buf,
00495 int cnt,
00496 GVFILE * fp)
00497 {
00498 int i,j;
00499 unsigned char *c1, *c2;
00500
00501 if ( Cur_Head->int_quick )
00502 {
00503 #if NATIVE_INT == PORT_INT
00504 if ( dig_fwrite (buf, PORT_INT, cnt, fp) == cnt )
00505 return 1;
00506 #else
00507 buf_alloc (cnt * PORT_INT);
00508 #if INT_ORDER == ENDIAN_LITTLE
00509 c1 = (unsigned char *) buf;
00510 #else
00511 c1 = (unsigned char *) buf + NATIVE_INT - PORT_INT;
00512 #endif
00513 c2 = (unsigned char *) buffer;
00514 for (i = 0; i < cnt; i++)
00515 {
00516 memcpy (c2, c1, PORT_INT);
00517 c1 += PORT_INT;
00518 c2 += sizeof (int);
00519 }
00520 if ( dig_fwrite (buffer, PORT_INT, cnt, fp) == cnt )
00521 return 1;
00522 #endif
00523 }
00524 else
00525 {
00526 buf_alloc (cnt * PORT_INT);
00527 c1 = (unsigned char *) buf;
00528 c2 = (unsigned char *) buffer;
00529 for (i = 0; i < cnt; i++)
00530 {
00531 for (j = 0; j < PORT_INT; j++)
00532 c2[j] = c1[Cur_Head->int_cnvrt[j]];
00533 c1 += sizeof (int);
00534 c2 += PORT_INT;
00535 }
00536 if ( dig_fwrite (buffer, PORT_INT, cnt, fp) == cnt )
00537 return 1;
00538 }
00539 return 0;
00540 }
00541
00542 int
00543 dig__fwrite_port_S ( short *buf,
00544 int cnt,
00545 GVFILE * fp)
00546 {
00547 int i,j;
00548 unsigned char *c1, *c2;
00549
00550 if ( Cur_Head->shrt_quick )
00551 {
00552 #if NATIVE_SHORT == PORT_SHORT
00553 if ( dig_fwrite (buf, PORT_SHORT, cnt, fp) == cnt )
00554 return 1;
00555 #else
00556 buf_alloc (cnt * PORT_SHORT);
00557 #if SHORT_ORDER == ENDIAN_LITTLE
00558 c1 = (unsigned char *) buf;
00559 #else
00560 c1 = (unsigned char *) buf + NATIVE_SHORT - PORT_SHORT;
00561 #endif
00562 c2 = (unsigned char *) buffer;
00563 for (i = 0; i < cnt; i++)
00564 {
00565 memcpy (c2, c1, PORT_SHORT);
00566 c1 += PORT_SHORT;
00567 c2 += sizeof (short);
00568 }
00569 if ( dig_fwrite (buffer, PORT_SHORT, cnt, fp) == cnt )
00570 return 1;
00571 #endif
00572 }
00573 else
00574 {
00575 buf_alloc (cnt * PORT_SHORT);
00576 c1 = (unsigned char *) buf;
00577 c2 = (unsigned char *) buffer;
00578 for (i = 0; i < cnt; i++)
00579 {
00580 for (j = 0; j < PORT_SHORT; j++)
00581 c2[j] = c1[Cur_Head->shrt_cnvrt[j]];
00582 c1 += sizeof (short);
00583 c2 += PORT_SHORT;
00584 }
00585 if ( dig_fwrite (buffer, PORT_SHORT, cnt, fp) == cnt )
00586 return 1;
00587 }
00588 return 0;
00589 }
00590
00591
00592 int
00593 dig__fwrite_port_P ( plus_t * buf,
00594 int cnt,
00595 GVFILE * fp)
00596 {
00597 return ( dig__fwrite_port_I ( (int *) buf, cnt, fp) );
00598 }
00599
00600 int
00601 dig__fwrite_port_C ( char *buf,
00602 int cnt,
00603 GVFILE * fp)
00604 {
00605 if ( dig_fwrite (buf, PORT_CHAR, cnt, fp) == cnt )
00606 return 1;
00607
00608 return 0;
00609 }
00610
00611
00612 void
00613 dig_init_portable ( struct Port_info *port, int byte_order )
00614 {
00615 int i;
00616
00617 port->byte_order = byte_order;
00618
00619 if ( port->byte_order == DOUBLE_ORDER )
00620 port->dbl_quick = TRUE;
00621 else
00622 port->dbl_quick = FALSE;
00623
00624 for ( i = 0; i < PORT_DOUBLE; i++ )
00625 {
00626 if ( port->byte_order == ENDIAN_BIG )
00627 port->dbl_cnvrt[i] = dbl_cnvrt[i];
00628 else
00629 port->dbl_cnvrt[i] = dbl_cnvrt[PORT_DOUBLE - i - 1];
00630 }
00631
00632 if ( port->byte_order == FLOAT_ORDER )
00633 port->flt_quick = TRUE;
00634 else
00635 port->flt_quick = FALSE;
00636
00637 for ( i = 0; i < PORT_FLOAT; i++ )
00638 {
00639 if ( port->byte_order == ENDIAN_BIG )
00640 port->flt_cnvrt[i] = flt_cnvrt[i];
00641 else
00642 port->flt_cnvrt[i] = flt_cnvrt[PORT_FLOAT - i - 1];
00643 }
00644
00645 if ( port->byte_order == LONG_ORDER )
00646 port->lng_quick = TRUE;
00647 else
00648 port->lng_quick = FALSE;
00649
00650 for ( i = 0; i < PORT_LONG; i++ )
00651 {
00652 if ( port->byte_order == ENDIAN_BIG )
00653 port->lng_cnvrt[i] = lng_cnvrt[i];
00654 else
00655 port->lng_cnvrt[i] = lng_cnvrt[PORT_LONG - i - 1];
00656 }
00657
00658 if ( port->byte_order == INT_ORDER )
00659 port->int_quick = TRUE;
00660 else
00661 port->int_quick = FALSE;
00662
00663 for ( i = 0; i < PORT_INT; i++ )
00664 {
00665 if ( port->byte_order == ENDIAN_BIG )
00666 port->int_cnvrt[i] = int_cnvrt[i];
00667 else
00668 port->int_cnvrt[i] = int_cnvrt[PORT_INT - i - 1];
00669 }
00670
00671 if ( port->byte_order == SHORT_ORDER )
00672 port->shrt_quick = TRUE;
00673 else
00674 port->shrt_quick = FALSE;
00675
00676 for ( i = 0; i < PORT_SHORT; i++ )
00677 {
00678 if ( port->byte_order == ENDIAN_BIG )
00679 port->shrt_cnvrt[i] = shrt_cnvrt[i];
00680 else
00681 port->shrt_cnvrt[i] = shrt_cnvrt[PORT_SHORT - i - 1];
00682 }
00683
00684 return;
00685 }
00686
00687
00688 int
00689 dig_set_cur_port ( struct Port_info *port)
00690 {
00691 Cur_Head = port;
00692 return 0;
00693 }
00694
00695 int
00696 dig__byte_order_out()
00697 {
00698 if ( DOUBLE_ORDER == ENDIAN_LITTLE )
00699 return ( ENDIAN_LITTLE );
00700 else
00701 return ( ENDIAN_BIG );
00702 }
00703