File: fpxdr.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (262 lines) | stat: -rw-r--r-- 7,571 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include <grass/raster.h>

#include "raster3d_intern.h"

/*---------------------------------------------------------------------------*/

int Rast3d_is_xdr_null_num(const void *num, int isFloat)
{
    static const char null_bytes[8] = {(char)0xFF, (char)0xFF, (char)0xFF,
                                       (char)0xFF, (char)0xFF, (char)0xFF,
                                       (char)0xFF, (char)0xFF};

    return memcmp(num, null_bytes, isFloat ? 4 : 8) == 0;
}

/*---------------------------------------------------------------------------*/

int Rast3d_is_xdr_null_float(const float *f)
{
    return Rast3d_is_xdr_null_num(f, 1);
}

/*---------------------------------------------------------------------------*/

int Rast3d_is_xdr_null_double(const double *d)
{
    return Rast3d_is_xdr_null_num(d, 0);
}

/*---------------------------------------------------------------------------*/

void Rast3d_set_xdr_null_num(void *num, int isFloat)
{
    static const char null_bytes[8] = {(char)0xFF, (char)0xFF, (char)0xFF,
                                       (char)0xFF, (char)0xFF, (char)0xFF,
                                       (char)0xFF, (char)0xFF};

    memcpy(num, null_bytes, isFloat ? 4 : 8);
}

/*---------------------------------------------------------------------------*/

void Rast3d_set_xdr_null_double(double *d)
{
    Rast3d_set_xdr_null_num(d, 0);
}

/*---------------------------------------------------------------------------*/

void Rast3d_set_xdr_null_float(float *f)
{
    Rast3d_set_xdr_null_num(f, 1);
}

/*---------------------------------------------------------------------------*/

static size_t xdr_off;

int Rast3d_init_fp_xdr(RASTER3D_Map *map, int misuseBytes)
/* nof addtl bytes allocated for the xdr array so that */
/* the array can also be (mis)used for other purposes */
{
    if (xdr == NULL) {
        xdrLength = map->tileSize * RASTER3D_MAX(map->numLengthExtern,
                                                 map->numLengthIntern) +
                    misuseBytes;
        xdr = Rast3d_malloc(xdrLength);
        if (xdr == NULL) {
            Rast3d_error("Rast3d_init_fp_xdr: error in Rast3d_malloc");
            return 0;
        }
    }
    else if (map->tileSize *
                     RASTER3D_MAX(map->numLengthExtern, map->numLengthIntern) +
                 misuseBytes >
             xdrLength) {
        xdrLength = map->tileSize * RASTER3D_MAX(map->numLengthExtern,
                                                 map->numLengthIntern) +
                    misuseBytes;
        xdr = Rast3d_realloc(xdr, xdrLength);
        if (xdr == NULL) {
            Rast3d_error("Rast3d_init_fp_xdr: error in Rast3d_realloc");
            return 0;
        }
    }

    return 1;
}

/*---------------------------------------------------------------------------*/

static void *xdrTmp;
static int dstType, srcType, type, externLength, eltLength, isFloat, useXdr;
static double tmpValue, *tmp;

int Rast3d_init_copy_to_xdr(RASTER3D_Map *map, int sType)
{
    xdrTmp = xdr;
    useXdr = map->useXdr;
    srcType = sType;

    if (map->useXdr == RASTER3D_USE_XDR)
        xdr_off = 0;

    type = map->type;
    isFloat = (type == FCELL_TYPE);
    externLength = Rast3d_extern_length(type);
    eltLength = Rast3d_length(srcType);
    tmp = &tmpValue;

    return 1;
}

/*---------------------------------------------------------------------------*/

static int xdr_put(const void *src)
{
    if (isFloat) {
        if (xdr_off + RASTER3D_XDR_FLOAT_LENGTH > (size_t)xdrLength)
            return 0;
        G_xdr_put_float((char *)xdr + xdr_off, src);
        xdr_off += RASTER3D_XDR_FLOAT_LENGTH;
    }
    else {
        if (xdr_off + RASTER3D_XDR_DOUBLE_LENGTH > (size_t)xdrLength)
            return 0;
        G_xdr_put_double((char *)xdr + xdr_off, src);
        xdr_off += RASTER3D_XDR_DOUBLE_LENGTH;
    }
    return 1;
}

/*---------------------------------------------------------------------------*/

int Rast3d_copy_to_xdr(const void *src, int nofNum)
{
    int i;

    if (useXdr == RASTER3D_NO_XDR) {
        Rast3d_copy_values(src, 0, srcType, xdrTmp, 0, type, nofNum);
        xdrTmp = G_incr_void_ptr(xdrTmp, nofNum * Rast3d_extern_length(type));
        return 1;
    }

    for (i = 0; i < nofNum; i++, src = G_incr_void_ptr(src, eltLength)) {

        if (Rast3d_is_null_value_num(src, srcType)) {
            Rast3d_set_xdr_null_num(xdrTmp, isFloat);
            xdr_off += externLength;
        }
        else {
            if (type == srcType) {
                if (!xdr_put(src)) {
                    Rast3d_error("Rast3d_copy_to_xdr: writing xdr failed");
                    return 0;
                }
            }
            else {
                if (type == FCELL_TYPE)
                    *((float *)tmp) = (float)*((double *)src);
                else
                    *((double *)tmp) = (double)*((float *)src);
                if (!xdr_put(tmp)) {
                    Rast3d_error("Rast3d_copy_to_xdr: writing xdr failed");
                    return 0;
                }
            }
        }

        xdrTmp = G_incr_void_ptr(xdrTmp, externLength);
    }

    return 1;
}

/*---------------------------------------------------------------------------*/

int Rast3d_init_copy_from_xdr(RASTER3D_Map *map, int dType)
{
    xdrTmp = xdr;
    useXdr = map->useXdr;
    dstType = dType;

    if (useXdr == RASTER3D_USE_XDR)
        xdr_off = 0;

    type = map->type;
    isFloat = (type == FCELL_TYPE);
    externLength = Rast3d_extern_length(type);
    eltLength = Rast3d_length(dstType);
    tmp = &tmpValue;

    return 1;
}

/*---------------------------------------------------------------------------*/

static int xdr_get(void *src)
{
    if (isFloat) {
        if (xdr_off + RASTER3D_XDR_FLOAT_LENGTH > (size_t)xdrLength)
            return 0;
        G_xdr_get_float(src, (char *)xdr + xdr_off);
        xdr_off += RASTER3D_XDR_FLOAT_LENGTH;
    }
    else {
        if (xdr_off + RASTER3D_XDR_DOUBLE_LENGTH > (size_t)xdrLength)
            return 0;
        G_xdr_get_double(src, (char *)xdr + xdr_off);
        xdr_off += RASTER3D_XDR_DOUBLE_LENGTH;
    }
    return 1;
}

/*---------------------------------------------------------------------------*/

int Rast3d_copy_from_xdr(int nofNum, void *dst)
{
    int i;

    if (useXdr == RASTER3D_NO_XDR) {
        Rast3d_copy_values(xdrTmp, 0, type, dst, 0, dstType, nofNum);
        xdrTmp = G_incr_void_ptr(xdrTmp, nofNum * Rast3d_extern_length(type));
        return 1;
    }

    for (i = 0; i < nofNum; i++, dst = G_incr_void_ptr(dst, eltLength)) {

        if (Rast3d_is_xdr_null_num(xdrTmp, isFloat)) {
            Rast3d_set_null_value(dst, 1, dstType);
            xdr_off += externLength;
        }
        else {
            if (type == dstType) {
                if (!xdr_get(dst)) {
                    Rast3d_error("Rast3d_copy_from_xdr: reading xdr failed");
                    return 0;
                }
            }
            else {
                if (!xdr_get(tmp)) {
                    Rast3d_error("Rast3d_copy_from_xdr: reading xdr failed");
                    return 0;
                }
                if (type == FCELL_TYPE)
                    *((double *)dst) = (double)*((float *)tmp);
                else
                    *((float *)dst) = (float)*((double *)tmp);
            }
        }

        xdrTmp = G_incr_void_ptr(xdrTmp, externLength);
    }

    return 1;
}