File: portable.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (652 lines) | stat: -rw-r--r-- 15,564 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/*
 ****************************************************************************
 *
 * MODULE:       Vector library 
 *              
 * AUTHOR(S):    Original author CERL, probably Dave Gerdes.
 *               Update to GRASS 5.7 Radim Blazek.
 *
 * PURPOSE:      Lower level functions for reading/writing/manipulating vectors.
 *
 * COPYRIGHT:    (C) 2001 by the GRASS Development Team
 *
 *               This program is free software under the GNU General Public
 *              License (>=v2). Read the file COPYING that comes with GRASS
 *              for details.
 *
 *****************************************************************************/
#include <string.h>
#include <grass/gis.h>
#include <grass/Vect.h>

extern void port_init(void);

extern int nat_dbl;
extern int nat_flt;
extern int nat_lng;
extern int nat_int;
extern int nat_shrt;

extern int dbl_order;
extern int flt_order;
extern int lng_order;
extern int int_order;
extern int shrt_order;

extern unsigned char dbl_cnvrt[sizeof(double)];
extern unsigned char flt_cnvrt[sizeof(float)];
extern unsigned char lng_cnvrt[sizeof(long)];
extern unsigned char int_cnvrt[sizeof(int)];
extern unsigned char shrt_cnvrt[sizeof(short)];

struct Port_info *Cur_Head;

static char *buffer = NULL;
static int buf_alloced = 0;

static int buf_alloc(int needed)
{
    char *p;
    int cnt;

    if (needed <= buf_alloced)
	return (0);
    cnt = buf_alloced;
    p = dig__alloc_space(needed, &cnt, 100, buffer, 1);
    if (p == NULL)
	return (dig_out_of_memory());
    buffer = p;
    buf_alloced = cnt;
    return (0);
}

/***************************** READ ************************************/
/*  These are the routines to read from the Portable Vector Format.
   These routines must handle any type size conversions between the
   portable format and the native machine.

   Return:  0 error
   1 OK

 */


/* read doubles from the PVF file */
int dig__fread_port_D(double *buf, int cnt, GVFILE * fp)
{
    int i, j, ret;
    unsigned char *c1, *c2;

    if (Cur_Head->dbl_quick) {
	ret = dig_fread(buf, PORT_DOUBLE, cnt, fp);
	if (ret != cnt)
	    return 0;
    }
    else {
	/* read into buffer */
	buf_alloc(cnt * PORT_DOUBLE);
	ret = dig_fread(buffer, PORT_DOUBLE, cnt, fp);
	if (ret != cnt)
	    return 0;
	/* read from buffer in changed order */
	c1 = (unsigned char *)buffer;
	c2 = (unsigned char *)buf;
	for (i = 0; i < cnt; i++) {
	    for (j = 0; j < PORT_DOUBLE; j++) {
		c2[Cur_Head->dbl_cnvrt[j]] = c1[j];
	    }
	    c1 += PORT_DOUBLE;
	    c2 += sizeof(double);
	}
    }
    return 1;
}

/* read floats from the PVF file */
int dig__fread_port_F(float *buf, int cnt, GVFILE * fp)
{
    int i, j, ret;
    unsigned char *c1, *c2;

    if (Cur_Head->flt_quick) {
	ret = dig_fread(buf, PORT_FLOAT, cnt, fp);
	if (ret != cnt)
	    return 0;
    }
    else {
	/* read into buffer */
	buf_alloc(cnt * PORT_FLOAT);
	ret = dig_fread(buffer, PORT_FLOAT, cnt, fp);
	if (ret != cnt)
	    return 0;
	/* read from buffer in changed order */
	c1 = (unsigned char *)buffer;
	c2 = (unsigned char *)buf;
	for (i = 0; i < cnt; i++) {
	    for (j = 0; j < PORT_FLOAT; j++) {
		c2[Cur_Head->flt_cnvrt[j]] = c1[j];
	    }
	    c1 += PORT_FLOAT;
	    c2 += sizeof(float);
	}
    }
    return 1;
}

/* read longs from the PVF file */
int dig__fread_port_L(long *buf, int cnt, GVFILE * fp)
{
    int i, j, ret;
    unsigned char *c1, *c2;

    if (Cur_Head->lng_quick) {
	if (nat_lng == PORT_LONG) {
	    ret = dig_fread(buf, PORT_LONG, cnt, fp);
	    if (ret != cnt)
		return 0;
	}
	else {
	    /* read into buffer */
	    buf_alloc(cnt * PORT_LONG);
	    ret = dig_fread(buffer, PORT_LONG, cnt, fp);
	    if (ret != cnt)
		return 0;
	    /* set buffer to zero (positive numbers) */
	    memset(buf, 0, cnt * sizeof(long));
	    /* read from buffer in changed order */
	    c1 = (unsigned char *)buffer;
	    c2 = (unsigned char *)buf;
	    for (i = 0; i < cnt; i++) {
		/* set to FF if the value is negative */
		if (lng_order == ENDIAN_LITTLE) {
		    if (c1[PORT_LONG - 1] & 0x80)
			memset(c2, 0xff, sizeof(long));
		    memcpy(c2, c1, PORT_LONG);
		}
		else {
		    if (c1[0] & 0x80)
			memset(c2, 0xff, sizeof(long));
		    memcpy(c2 + nat_lng - PORT_LONG, c1, PORT_LONG);
		}
		c1 += PORT_LONG;
		c2 += sizeof(long);
	    }
	}
    }
    else {
	/* read into buffer */
	buf_alloc(cnt * PORT_LONG);
	ret = dig_fread(buffer, PORT_LONG, cnt, fp);
	if (ret != cnt)
	    return 0;
	/* set buffer to zero (positive numbers) */
	memset(buf, 0, cnt * sizeof(long));
	/* read from buffer in changed order */
	c1 = (unsigned char *)buffer;
	c2 = (unsigned char *)buf;
	for (i = 0; i < cnt; i++) {
	    /* set to FF if the value is negative */
	    if (Cur_Head->byte_order == ENDIAN_LITTLE) {
		if (c1[PORT_LONG - 1] & 0x80)
		    memset(c2, 0xff, sizeof(long));
	    }
	    else {
		if (c1[0] & 0x80)
		    memset(c2, 0xff, sizeof(long));
	    }
	    for (j = 0; j < PORT_LONG; j++)
		c2[Cur_Head->lng_cnvrt[j]] = c1[j];
	    c1 += PORT_LONG;
	    c2 += sizeof(long);
	}
    }
    return 1;
}

/* read ints from the PVF file */
int dig__fread_port_I(int *buf, int cnt, GVFILE * fp)
{
    int i, j, ret;
    unsigned char *c1, *c2;

    if (Cur_Head->int_quick) {
	if (nat_int == PORT_INT) {
	    ret = dig_fread(buf, PORT_INT, cnt, fp);
	    if (ret != cnt)
		return 0;
	}
	else {
	    /* read into buffer */
	    buf_alloc(cnt * PORT_INT);
	    ret = dig_fread(buffer, PORT_INT, cnt, fp);
	    if (ret != cnt)
		return 0;
	    /* set buffer to zero (positive numbers) */
	    memset(buf, 0, cnt * sizeof(int));
	    /* read from buffer in changed order */
	    c1 = (unsigned char *)buffer;
	    c2 = (unsigned char *)buf;
	    for (i = 0; i < cnt; i++) {
		/* set to FF if the value is negative */
		if (int_order == ENDIAN_LITTLE) {
		    if (c1[PORT_INT - 1] & 0x80)
			memset(c2, 0xff, sizeof(int));
		    memcpy(c2, c1, PORT_INT);
		}
		else {
		    if (c1[0] & 0x80)
			memset(c2, 0xff, sizeof(int));
		    memcpy(c2 + nat_int - PORT_INT, c1, PORT_INT);
		}
		c1 += PORT_INT;
		c2 += sizeof(int);
	    }
	}
    }
    else {
	/* read into buffer */
	buf_alloc(cnt * PORT_INT);
	ret = dig_fread(buffer, PORT_INT, cnt, fp);
	if (ret != cnt)
	    return 0;
	/* set buffer to zero (positive numbers) */
	memset(buf, 0, cnt * sizeof(int));
	/* read from buffer in changed order */
	c1 = (unsigned char *)buffer;
	c2 = (unsigned char *)buf;
	for (i = 0; i < cnt; i++) {
	    /* set to FF if the value is negative */
	    if (Cur_Head->byte_order == ENDIAN_LITTLE) {
		if (c1[PORT_INT - 1] & 0x80)
		    memset(c2, 0xff, sizeof(int));
	    }
	    else {
		if (c1[0] & 0x80)
		    memset(c2, 0xff, sizeof(int));
	    }
	    for (j = 0; j < PORT_INT; j++)
		c2[Cur_Head->int_cnvrt[j]] = c1[j];
	    c1 += PORT_INT;
	    c2 += sizeof(int);
	}
    }
    return 1;
}

/* read shorts from the PVF file */
int dig__fread_port_S(short *buf, int cnt, GVFILE * fp)
{
    int i, j, ret;
    unsigned char *c1, *c2;

    if (Cur_Head->shrt_quick) {
	if (nat_shrt == PORT_SHORT) {
	    ret = dig_fread(buf, PORT_SHORT, cnt, fp);
	    if (ret != cnt)
		return 0;
	}
	else {
	    /* read into buffer */
	    buf_alloc(cnt * PORT_SHORT);
	    if (0 >= (ret = dig_fread(buffer, PORT_SHORT, cnt, fp)))
		if (ret != cnt)
		    return 0;
	    /* set buffer to zero (positive numbers) */
	    memset(buf, 0, cnt * sizeof(short));
	    /* read from buffer in changed order */
	    c1 = (unsigned char *)buffer;
	    c2 = (unsigned char *)buf;
	    for (i = 0; i < cnt; i++) {
		/* set to FF if the value is negative */
		if (shrt_order == ENDIAN_LITTLE) {
		    if (c1[PORT_SHORT - 1] & 0x80)
			memset(c2, 0xff, sizeof(short));
		    memcpy(c2, c1, PORT_SHORT);
		}
		else {
		    if (c1[0] & 0x80)
			memset(c2, 0xff, sizeof(short));
		    memcpy(c2 + nat_shrt - PORT_SHORT, c1, PORT_SHORT);
		}
		c1 += PORT_SHORT;
		c2 += sizeof(short);
	    }
	}
    }
    else {
	/* read into buffer */
	buf_alloc(cnt * PORT_SHORT);
	ret = dig_fread(buffer, PORT_SHORT, cnt, fp);
	if (ret != cnt)
	    return 0;
	/* set buffer to zero (positive numbers) */
	memset(buf, 0, cnt * sizeof(short));
	/* read from buffer in changed order */
	c1 = (unsigned char *)buffer;
	c2 = (unsigned char *)buf;
	for (i = 0; i < cnt; i++) {
	    /* set to FF if the value is negative */
	    if (Cur_Head->byte_order == ENDIAN_LITTLE) {
		if (c1[PORT_SHORT - 1] & 0x80)
		    memset(c2, 0xff, sizeof(short));
	    }
	    else {
		if (c1[0] & 0x80)
		    memset(c2, 0xff, sizeof(short));
	    }
	    for (j = 0; j < PORT_SHORT; j++)
		c2[Cur_Head->shrt_cnvrt[j]] = c1[j];
	    c1 += PORT_SHORT;
	    c2 += sizeof(short);
	}
    }
    return 1;
}

/* read chars from the PVF file */
int dig__fread_port_C(char *buf, int cnt, GVFILE * fp)
{
    int ret;

    ret = dig_fread(buf, PORT_CHAR, cnt, fp);
    if (ret != cnt)
	return 0;
    return 1;
}

/* read plus_t from the PVF file */
/* plus_t is defined as int so we only retype pointer and use int function */
int dig__fread_port_P(plus_t * buf, int cnt, GVFILE * fp)
{
    int *ibuf;

    ibuf = (int *)buf;

    return (dig__fread_port_I(ibuf, cnt, fp));
}

/***************************** WRITE ************************************/

int dig__fwrite_port_D(double *buf,	/* DOUBLE */
		       int cnt, GVFILE * fp)
{
    int i, j;
    unsigned char *c1, *c2;

    if (Cur_Head->dbl_quick) {
	if (dig_fwrite(buf, PORT_DOUBLE, cnt, fp) == cnt)
	    return 1;
    }
    else {
	buf_alloc(cnt * PORT_DOUBLE);
	c1 = (unsigned char *)buf;
	c2 = (unsigned char *)buffer;
	for (i = 0; i < cnt; i++) {
	    for (j = 0; j < PORT_DOUBLE; j++)
		c2[j] = c1[Cur_Head->dbl_cnvrt[j]];
	    c1 += sizeof(double);
	    c2 += PORT_DOUBLE;
	}
	if (dig_fwrite(buffer, PORT_DOUBLE, cnt, fp) == cnt)
	    return 1;
    }
    return 0;
}

int dig__fwrite_port_F(float *buf,	/* FLOAT */
		       int cnt, GVFILE * fp)
{
    int i, j;
    unsigned char *c1, *c2;

    if (Cur_Head->flt_quick) {
	if (dig_fwrite(buf, PORT_FLOAT, cnt, fp) == cnt)
	    return 1;
    }
    else {
	buf_alloc(cnt * PORT_FLOAT);
	c1 = (unsigned char *)buf;
	c2 = (unsigned char *)buffer;
	for (i = 0; i < cnt; i++) {
	    for (j = 0; j < PORT_FLOAT; j++)
		c2[j] = c1[Cur_Head->flt_cnvrt[j]];
	    c1 += sizeof(float);
	    c2 += PORT_FLOAT;
	}
	if (dig_fwrite(buffer, PORT_FLOAT, cnt, fp) == cnt)
	    return 1;
    }
    return 0;
}

int dig__fwrite_port_L(long *buf,	/* LONG */
		       int cnt, GVFILE * fp)
{
    int i, j;
    unsigned char *c1, *c2;

    if (Cur_Head->lng_quick) {
	if (nat_lng == PORT_LONG) {
	    if (dig_fwrite(buf, PORT_LONG, cnt, fp) == cnt)
		return 1;
	}
	else {
	    buf_alloc(cnt * PORT_LONG);
	    c1 = (unsigned char *)buf;
	    c2 = (unsigned char *)buffer;
	    for (i = 0; i < cnt; i++) {
		if (lng_order == ENDIAN_LITTLE)
		    memcpy(c2, c1, PORT_LONG);
		else
		    memcpy(c2, c1 + nat_lng - PORT_LONG, PORT_LONG);
		c1 += sizeof(long);
		c2 += PORT_LONG;
	    }
	    if (dig_fwrite(buffer, PORT_LONG, cnt, fp) == cnt)
		return 1;
	}
    }
    else {
	buf_alloc(cnt * PORT_LONG);
	c1 = (unsigned char *)buf;
	c2 = (unsigned char *)buffer;
	for (i = 0; i < cnt; i++) {
	    for (j = 0; j < PORT_LONG; j++)
		c2[j] = c1[Cur_Head->lng_cnvrt[j]];
	    c1 += sizeof(long);
	    c2 += PORT_LONG;
	}
	if (dig_fwrite(buffer, PORT_LONG, cnt, fp) == cnt)
	    return 1;
    }
    return 0;
}

int dig__fwrite_port_I(int *buf,	/* INT */
		       int cnt, GVFILE * fp)
{
    int i, j;
    unsigned char *c1, *c2;

    if (Cur_Head->int_quick) {
	if (nat_int == PORT_INT) {
	    if (dig_fwrite(buf, PORT_INT, cnt, fp) == cnt)
		return 1;
	}
	else {
	    buf_alloc(cnt * PORT_INT);
	    c1 = (unsigned char *)buf;
	    c2 = (unsigned char *)buffer;
	    for (i = 0; i < cnt; i++) {
		if (int_order == ENDIAN_LITTLE)
		    memcpy(c2, c1, PORT_INT);
		else
		    memcpy(c2, c1 + nat_int - PORT_INT, PORT_INT);
		c1 += sizeof(int);
		c2 += PORT_INT;
	    }
	    if (dig_fwrite(buffer, PORT_INT, cnt, fp) == cnt)
		return 1;
	}
    }
    else {
	buf_alloc(cnt * PORT_INT);
	c1 = (unsigned char *)buf;
	c2 = (unsigned char *)buffer;
	for (i = 0; i < cnt; i++) {
	    for (j = 0; j < PORT_INT; j++)
		c2[j] = c1[Cur_Head->int_cnvrt[j]];
	    c1 += sizeof(int);
	    c2 += PORT_INT;
	}
	if (dig_fwrite(buffer, PORT_INT, cnt, fp) == cnt)
	    return 1;
    }
    return 0;
}

int dig__fwrite_port_S(short *buf,	/* SHORT */
		       int cnt, GVFILE * fp)
{
    int i, j;
    unsigned char *c1, *c2;

    if (Cur_Head->shrt_quick) {
	if (nat_shrt == PORT_SHORT) {
	    if (dig_fwrite(buf, PORT_SHORT, cnt, fp) == cnt)
		return 1;
	}
	else {
	    buf_alloc(cnt * PORT_SHORT);
	    c1 = (unsigned char *)buf;
	    c2 = (unsigned char *)buffer;
	    for (i = 0; i < cnt; i++) {
		if (shrt_order == ENDIAN_LITTLE)
		    memcpy(c2, c1, PORT_SHORT);
		else
		    memcpy(c2, c1 + nat_shrt - PORT_SHORT, PORT_SHORT);
		c1 += sizeof(short);
		c2 += PORT_SHORT;
	    }
	    if (dig_fwrite(buffer, PORT_SHORT, cnt, fp) == cnt)
		return 1;
	}
    }
    else {
	buf_alloc(cnt * PORT_SHORT);
	c1 = (unsigned char *)buf;
	c2 = (unsigned char *)buffer;
	for (i = 0; i < cnt; i++) {
	    for (j = 0; j < PORT_SHORT; j++)
		c2[j] = c1[Cur_Head->shrt_cnvrt[j]];
	    c1 += sizeof(short);
	    c2 += PORT_SHORT;
	}
	if (dig_fwrite(buffer, PORT_SHORT, cnt, fp) == cnt)
	    return 1;
    }
    return 0;
}

/* plus_t is defined as int so we only retype pointer and use int function */
int dig__fwrite_port_P(plus_t * buf,	/* PLUS_T->INT */
		       int cnt, GVFILE * fp)
{
    return (dig__fwrite_port_I((int *)buf, cnt, fp));
}

int dig__fwrite_port_C(char *buf,	/* CHAR */
		       int cnt, GVFILE * fp)
{
    if (dig_fwrite(buf, PORT_CHAR, cnt, fp) == cnt)
	return 1;

    return 0;
}

/* set portable info structure to byte order of file */
void dig_init_portable(struct Port_info *port, int byte_order)
{
    int i;

    port_init();

    port->byte_order = byte_order;

    if (port->byte_order == dbl_order)
	port->dbl_quick = TRUE;
    else
	port->dbl_quick = FALSE;

    for (i = 0; i < PORT_DOUBLE; i++) {
	if (port->byte_order == ENDIAN_BIG)
	    port->dbl_cnvrt[i] = dbl_cnvrt[i];
	else
	    port->dbl_cnvrt[i] = dbl_cnvrt[PORT_DOUBLE - i - 1];
    }

    if (port->byte_order == flt_order)
	port->flt_quick = TRUE;
    else
	port->flt_quick = FALSE;

    for (i = 0; i < PORT_FLOAT; i++) {
	if (port->byte_order == ENDIAN_BIG)
	    port->flt_cnvrt[i] = flt_cnvrt[i];
	else
	    port->flt_cnvrt[i] = flt_cnvrt[PORT_FLOAT - i - 1];
    }

    if (port->byte_order == lng_order)
	port->lng_quick = TRUE;
    else
	port->lng_quick = FALSE;

    for (i = 0; i < PORT_LONG; i++) {
	if (port->byte_order == ENDIAN_BIG)
	    port->lng_cnvrt[i] = lng_cnvrt[i];
	else
	    port->lng_cnvrt[i] = lng_cnvrt[PORT_LONG - i - 1];
    }

    if (port->byte_order == int_order)
	port->int_quick = TRUE;
    else
	port->int_quick = FALSE;

    for (i = 0; i < PORT_INT; i++) {
	if (port->byte_order == ENDIAN_BIG)
	    port->int_cnvrt[i] = int_cnvrt[i];
	else
	    port->int_cnvrt[i] = int_cnvrt[PORT_INT - i - 1];
    }

    if (port->byte_order == shrt_order)
	port->shrt_quick = TRUE;
    else
	port->shrt_quick = FALSE;

    for (i = 0; i < PORT_SHORT; i++) {
	if (port->byte_order == ENDIAN_BIG)
	    port->shrt_cnvrt[i] = shrt_cnvrt[i];
	else
	    port->shrt_cnvrt[i] = shrt_cnvrt[PORT_SHORT - i - 1];
    }

    return;
}

/* set current portable info */
int dig_set_cur_port(struct Port_info *port)
{
    Cur_Head = port;
    return 0;
}

int dig__byte_order_out()
{
    if (dbl_order == ENDIAN_LITTLE)
	return (ENDIAN_LITTLE);
    else
	return (ENDIAN_BIG);
}