GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
fpxdr.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 
7 #include <grass/raster.h>
8 
9 #include "raster3d_intern.h"
10 
11 /*---------------------------------------------------------------------------*/
12 
13 int Rast3d_is_xdr_null_num(const void *num, int isFloat)
14 {
15  static const char null_bytes[8] = {
16  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
17  };
18 
19  return memcmp(num, null_bytes, isFloat ? 4 : 8) == 0;
20 }
21 
22 /*---------------------------------------------------------------------------*/
23 
24 int Rast3d_is_xdr_null_float(const float *f)
25 {
26  return Rast3d_is_xdr_null_num(f, 1);
27 }
28 
29 /*---------------------------------------------------------------------------*/
30 
31 int Rast3d_is_xdr_null_double(const double *d)
32 {
33  return Rast3d_is_xdr_null_num(d, 0);
34 }
35 
36 /*---------------------------------------------------------------------------*/
37 
38 void Rast3d_set_xdr_null_num(void *num, int isFloat)
39 {
40  static const char null_bytes[8] = {
41  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
42  };
43 
44  memcpy(num, null_bytes, isFloat ? 4 : 8);
45 }
46 
47 /*---------------------------------------------------------------------------*/
48 
50 {
52 }
53 
54 /*---------------------------------------------------------------------------*/
55 
57 {
59 }
60 
61 /*---------------------------------------------------------------------------*/
62 
63 static size_t xdr_off;
64 
65 int Rast3d_init_fp_xdr(RASTER3D_Map * map, int misuseBytes)
66 
67 
68 
69  /* nof addtl bytes allocated for the xdr array so that */
70  /* the array can also be (mis)used for other purposes */
71 {
72  if (xdr == NULL) {
74  map->numLengthIntern) +
75  misuseBytes;
77  if (xdr == NULL) {
78  Rast3d_error("Rast3d_init_fp_xdr: error in Rast3d_malloc");
79  return 0;
80  }
81  }
82  else if (map->tileSize * RASTER3D_MAX(map->numLengthExtern,
83  map->numLengthIntern) + misuseBytes
84  > xdrLength) {
86  map->numLengthIntern) +
87  misuseBytes;
89  if (xdr == NULL) {
90  Rast3d_error("Rast3d_init_fp_xdr: error in Rast3d_realloc");
91  return 0;
92  }
93  }
94 
95  return 1;
96 }
97 
98 /*---------------------------------------------------------------------------*/
99 
100 static void *xdrTmp;
101 static int dstType, srcType, type, externLength, eltLength, isFloat, useXdr;
102 static double tmpValue, *tmp;
103 
105 {
106  xdrTmp = xdr;
107  useXdr = map->useXdr;
108  srcType = sType;
109 
110  if (map->useXdr == RASTER3D_USE_XDR)
111  xdr_off = 0;
112 
113  type = map->type;
114  isFloat = (type == FCELL_TYPE);
115  externLength = Rast3d_extern_length(type);
116  eltLength = Rast3d_length(srcType);
117  tmp = &tmpValue;
118 
119  return 1;
120 }
121 
122 /*---------------------------------------------------------------------------*/
123 
124 static int xdr_put(const void *src)
125 {
126  if (isFloat) {
127  if (xdr_off + RASTER3D_XDR_FLOAT_LENGTH > xdrLength)
128  return 0;
129  G_xdr_put_float((char*)xdr + xdr_off, src);
130  xdr_off += RASTER3D_XDR_FLOAT_LENGTH;
131  }
132  else {
133  if (xdr_off + RASTER3D_XDR_DOUBLE_LENGTH > xdrLength)
134  return 0;
135  G_xdr_put_double((char*)xdr + xdr_off, src);
136  xdr_off += RASTER3D_XDR_DOUBLE_LENGTH;
137  }
138  return 1;
139 }
140 
141 /*---------------------------------------------------------------------------*/
142 
143 int Rast3d_copy_to_xdr(const void *src, int nofNum)
144 {
145  int i;
146 
147  if (useXdr == RASTER3D_NO_XDR) {
148  Rast3d_copy_values(src, 0, srcType, xdrTmp, 0, type, nofNum);
149  xdrTmp = G_incr_void_ptr(xdrTmp, nofNum * Rast3d_extern_length(type));
150  return 1;
151  }
152 
153  for (i = 0; i < nofNum; i++, src = G_incr_void_ptr(src, eltLength)) {
154 
155  if (Rast3d_is_null_value_num(src, srcType)) {
156  Rast3d_set_xdr_null_num(xdrTmp, isFloat);
157  xdr_off += externLength;
158  }
159  else {
160  if (type == srcType) {
161  if (!xdr_put(src)) {
162  Rast3d_error("Rast3d_copy_to_xdr: writing xdr failed");
163  return 0;
164  }
165  }
166  else {
167  if (type == FCELL_TYPE)
168  *((float *)tmp) = (float)*((double *)src);
169  else
170  *((double *)tmp) = (double)*((float *)src);
171  if (!xdr_put(tmp)) {
172  Rast3d_error("Rast3d_copy_to_xdr: writing xdr failed");
173  return 0;
174  }
175  }
176  }
177 
178  xdrTmp = G_incr_void_ptr(xdrTmp, externLength);
179  }
180 
181  return 1;
182 }
183 
184 /*---------------------------------------------------------------------------*/
185 
187 {
188  xdrTmp = xdr;
189  useXdr = map->useXdr;
190  dstType = dType;
191 
192  if (useXdr == RASTER3D_USE_XDR)
193  xdr_off = 0;
194 
195  type = map->type;
196  isFloat = (type == FCELL_TYPE);
197  externLength = Rast3d_extern_length(type);
198  eltLength = Rast3d_length(dstType);
199  tmp = &tmpValue;
200 
201  return 1;
202 }
203 
204 /*---------------------------------------------------------------------------*/
205 
206 static int xdr_get(void *src)
207 {
208  if (isFloat) {
209  if (xdr_off + RASTER3D_XDR_FLOAT_LENGTH > xdrLength)
210  return 0;
211  G_xdr_get_float(src, (char*)xdr + xdr_off);
212  xdr_off += RASTER3D_XDR_FLOAT_LENGTH;
213  }
214  else {
215  if (xdr_off + RASTER3D_XDR_DOUBLE_LENGTH > xdrLength)
216  return 0;
217  G_xdr_get_double(src, (char*)xdr + xdr_off);
218  xdr_off += RASTER3D_XDR_DOUBLE_LENGTH;
219  }
220  return 1;
221 }
222 
223 /*---------------------------------------------------------------------------*/
224 
225 int Rast3d_copy_from_xdr(int nofNum, void *dst)
226 {
227  int i;
228 
229  if (useXdr == RASTER3D_NO_XDR) {
230  Rast3d_copy_values(xdrTmp, 0, type, dst, 0, dstType, nofNum);
231  xdrTmp = G_incr_void_ptr(xdrTmp, nofNum * Rast3d_extern_length(type));
232  return 1;
233  }
234 
235  for (i = 0; i < nofNum; i++, dst = G_incr_void_ptr(dst, eltLength)) {
236 
237  if (Rast3d_is_xdr_null_num(xdrTmp, isFloat)) {
238  Rast3d_set_null_value(dst, 1, dstType);
239  xdr_off += externLength;
240  }
241  else {
242  if (type == dstType) {
243  if (!xdr_get(dst)) {
244  Rast3d_error("Rast3d_copy_from_xdr: reading xdr failed");
245  return 0;
246  }
247  }
248  else {
249  if (!xdr_get(tmp)) {
250  Rast3d_error("Rast3d_copy_from_xdr: reading xdr failed");
251  return 0;
252  }
253  if (type == FCELL_TYPE)
254  *((double *)dst) = (double)*((float *)tmp);
255  else
256  *((float *)dst) = (float)*((double *)tmp);
257  }
258  }
259 
260  xdrTmp = G_incr_void_ptr(xdrTmp, externLength);
261  }
262 
263  return 1;
264 }
int numLengthIntern
Definition: raster3d.h:175
int Rast3d_is_xdr_null_double(const double *d)
Definition: fpxdr.c:31
int Rast3d_init_copy_to_xdr(RASTER3D_Map *map, int sType)
Definition: fpxdr.c:104
int numLengthExtern
Definition: raster3d.h:172
int Rast3d_is_xdr_null_float(const float *f)
Definition: fpxdr.c:24
int Rast3d_init_copy_from_xdr(RASTER3D_Map *map, int dType)
Definition: fpxdr.c:186
int xdrLength
void * Rast3d_malloc(int)
Same as malloc (nBytes), except that in case of error Rast3d_error() is invoked.
#define RASTER3D_XDR_DOUBLE_LENGTH
int Rast3d_is_xdr_null_num(const void *num, int isFloat)
Definition: fpxdr.c:13
char * dst
Definition: lz4.h:599
void Rast3d_set_null_value(void *, int, int)
Fills the vector pointed to by c with nofElts NULL-values of type.
Definition: null.c:35
#define G_incr_void_ptr(ptr, size)
Definition: defs/gis.h:100
#define NULL
Definition: ccmath.h:32
void Rast3d_set_xdr_null_double(double *d)
Definition: fpxdr.c:49
int Rast3d_extern_length(int)
Definition: raster3d/misc.c:90
void * xdr
void Rast3d_error(const char *,...) __attribute__((format(printf
int Rast3d_copy_from_xdr(int nofNum, void *dst)
Definition: fpxdr.c:225
#define RASTER3D_NO_XDR
void G_xdr_put_double(void *, const double *)
Definition: gis/xdr.c:88
void G_xdr_put_float(void *, const float *)
Definition: gis/xdr.c:78
void G_xdr_get_float(float *, const void *)
Definition: gis/xdr.c:73
void * Rast3d_realloc(void *, int)
Same as realloc (ptr, nBytes), except that in case of error Rast3d_error() is invoked.
void Rast3d_copy_values(const void *, int, int, void *, int, int, int)
Definition: raster3d/misc.c:53
int Rast3d_copy_to_xdr(const void *src, int nofNum)
Definition: fpxdr.c:143
int tileSize
Definition: raster3d.h:179
#define FCELL_TYPE
Definition: raster.h:12
int Rast3d_is_null_value_num(const void *, int)
Definition: null.c:12
int Rast3d_init_fp_xdr(RASTER3D_Map *map, int misuseBytes)
Definition: fpxdr.c:65
void Rast3d_set_xdr_null_num(void *num, int isFloat)
Definition: fpxdr.c:38
void Rast3d_set_xdr_null_float(float *f)
Definition: fpxdr.c:56
#define RASTER3D_MAX(a, b)
#define RASTER3D_USE_XDR
void G_xdr_get_double(double *, const void *)
Definition: gis/xdr.c:83
#define RASTER3D_XDR_FLOAT_LENGTH
int Rast3d_length(int)
Definition: raster3d/misc.c:78