GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
g3dfpxdr.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 #include <rpc/types.h>
7 #include <rpc/xdr.h>
8 #include "G3d_intern.h"
9 
10 /*---------------------------------------------------------------------------*/
11 
12 int G3d_isXdrNullNum(const void *num, int isFloat)
13 {
14  static const char null_bytes[8] = {
15  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
16  };
17 
18  return memcmp(num, null_bytes, isFloat ? 4 : 8) == 0;
19 }
20 
21 /*---------------------------------------------------------------------------*/
22 
23 int G3d_isXdrNullFloat(const float *f)
24 {
25  return G3d_isXdrNullNum(f, 1);
26 }
27 
28 /*---------------------------------------------------------------------------*/
29 
30 int G3d_isXdrNullDouble(const double *d)
31 {
32  return G3d_isXdrNullNum(d, 0);
33 }
34 
35 /*---------------------------------------------------------------------------*/
36 
37 void G3d_setXdrNullNum(void *num, int isFloat)
38 {
39  static const char null_bytes[8] = {
40  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
41  };
42 
43  memcpy(num, null_bytes, isFloat ? 4 : 8);
44 }
45 
46 /*---------------------------------------------------------------------------*/
47 
48 void G3d_setXdrNullDouble(double *d)
49 {
50  G3d_setXdrNullNum(d, 0);
51 }
52 
53 /*---------------------------------------------------------------------------*/
54 
55 void G3d_setXdrNullFloat(float *f)
56 {
57  G3d_setXdrNullNum(f, 1);
58 }
59 
60 /*---------------------------------------------------------------------------*/
61 
62 XDR xdrEncodeStream, xdrDecodeStream; /* xdr support structures */
63 
64 int G3d_initFpXdr(G3D_Map * map, int misuseBytes)
65 
66 
67 
68  /* nof addtl bytes allocated for the xdr array so that */
69  /* the array can also be (mis)used for other purposes */
70 {
71  int doAlloc;
72 
73  doAlloc = 0;
74 
75  if (xdr == NULL) {
76  xdrLength = map->tileSize * G3D_MAX(map->numLengthExtern,
77  map->numLengthIntern) +
78  misuseBytes;
80  if (xdr == NULL) {
81  G3d_error("G3d_initFpXdr: error in G3d_malloc");
82  return 0;
83  }
84 
85  doAlloc = 1;
86  }
87  else if (map->tileSize * G3D_MAX(map->numLengthExtern,
88  map->numLengthIntern) + misuseBytes
89  > xdrLength) {
90  xdrLength = map->tileSize * G3D_MAX(map->numLengthExtern,
91  map->numLengthIntern) +
92  misuseBytes;
94  if (xdr == NULL) {
95  G3d_error("G3d_initFpXdr: error in G3d_realloc");
96  return 0;
97  }
98 
99  doAlloc = 1;
100  }
101 
102  if (doAlloc) {
103  xdrmem_create(&(xdrEncodeStream), xdr, (u_int) xdrLength, XDR_ENCODE);
104  xdrmem_create(&(xdrDecodeStream), xdr, (u_int) xdrLength, XDR_DECODE);
105  }
106 
107  return 1;
108 }
109 
110 /*---------------------------------------------------------------------------*/
111 
112 static void *xdrTmp;
113 static int dstType, srcType, type, externLength, eltLength, isFloat, useXdr;
114 static int (*xdrFun) ();
115 static XDR *xdrs;
116 static double tmpValue, *tmp;
117 
118 int G3d_initCopyToXdr(G3D_Map * map, int sType)
119 {
120  xdrTmp = xdr;
121  useXdr = map->useXdr;
122  srcType = sType;
123 
124  if (map->useXdr == G3D_USE_XDR) {
125  if (!xdr_setpos(&(xdrEncodeStream), 0)) {
126  G3d_error("G3d_InitCopyToXdr: positioning xdr failed");
127  return 0;
128  }
129  xdrs = &(xdrEncodeStream);
130  }
131 
132  type = map->type;
133  isFloat = (type == FCELL_TYPE);
134  externLength = G3d_externLength(type);
135  eltLength = G3d_length(srcType);
136  if (isFloat)
137  xdrFun = xdr_float;
138  else
139  xdrFun = xdr_double;
140  tmp = &tmpValue;
141 
142  return 1;
143 }
144 
145 /*---------------------------------------------------------------------------*/
146 
147 int G3d_copyToXdr(const void *src, int nofNum)
148 {
149  int i;
150 
151  if (useXdr == G3D_NO_XDR) {
152  G3d_copyValues(src, 0, srcType, xdrTmp, 0, type, nofNum);
153  xdrTmp = G_incr_void_ptr(xdrTmp, nofNum * G3d_externLength(type));
154  return 1;
155  }
156 
157  for (i = 0; i < nofNum; i++, src = G_incr_void_ptr(src, eltLength)) {
158 
159  if (G3d_isNullValueNum(src, srcType)) {
160  G3d_setXdrNullNum(xdrTmp, isFloat);
161  if (!xdr_setpos(xdrs, xdr_getpos(xdrs) + externLength)) {
162  G3d_error("G3d_copyToXdr: positioning xdr failed");
163  return 0;
164  }
165  }
166  else {
167  if (type == srcType) {
168  if (xdrFun(xdrs, src) < 0) {
169  G3d_error("G3d_copyToXdr: writing xdr failed");
170  return 0;
171  }
172  }
173  else {
174  if (type == FCELL_TYPE)
175  *((float *)tmp) = (float)*((double *)src);
176  else
177  *((double *)tmp) = (double)*((float *)src);
178  if (xdrFun(xdrs, tmp) < 0) {
179  G3d_error("G3d_copyToXdr: writing xdr failed");
180  return 0;
181  }
182  }
183  }
184 
185  xdrTmp = G_incr_void_ptr(xdrTmp, externLength);
186  }
187 
188  return 1;
189 }
190 
191 /*---------------------------------------------------------------------------*/
192 
193 int G3d_initCopyFromXdr(G3D_Map * map, int dType)
194 {
195  xdrTmp = xdr;
196  useXdr = map->useXdr;
197  dstType = dType;
198 
199  if (useXdr == G3D_USE_XDR) {
200  if (!xdr_setpos(&(xdrDecodeStream), 0)) {
201  G3d_error("G3d_initCopyFromXdr: positioning xdr failed");
202  return 0;
203  }
204  xdrs = &(xdrDecodeStream);
205  }
206 
207  type = map->type;
208  isFloat = (type == FCELL_TYPE);
209  externLength = G3d_externLength(type);
210  eltLength = G3d_length(dstType);
211  if (isFloat)
212  xdrFun = xdr_float;
213  else
214  xdrFun = xdr_double;
215  tmp = &tmpValue;
216 
217  return 1;
218 }
219 
220 /*---------------------------------------------------------------------------*/
221 
222 int G3d_copyFromXdr(int nofNum, void *dst)
223 {
224  int i;
225 
226  if (useXdr == G3D_NO_XDR) {
227  G3d_copyValues(xdrTmp, 0, type, dst, 0, dstType, nofNum);
228  xdrTmp = G_incr_void_ptr(xdrTmp, nofNum * G3d_externLength(type));
229  return 1;
230  }
231 
232  for (i = 0; i < nofNum; i++, dst = G_incr_void_ptr(dst, eltLength)) {
233 
234  if (G3d_isXdrNullNum(xdrTmp, isFloat)) {
235  G3d_setNullValue(dst, 1, dstType);
236  if (!xdr_setpos(xdrs, xdr_getpos(xdrs) + externLength)) {
237  G3d_error("G3d_copyFromXdr: positioning xdr failed");
238  return 0;
239  }
240  }
241  else {
242  if (type == dstType) {
243  if (xdrFun(xdrs, dst) < 0) {
244  G3d_error("G3d_copyFromXdr: reading xdr failed");
245  return 0;
246  }
247  }
248  else {
249  if (xdrFun(xdrs, tmp) < 0) {
250  G3d_error("G3d_copyFromXdr: 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 }
XDR xdrEncodeStream
Definition: g3dfpxdr.c:62
int G3d_externLength(int t)
Definition: g3dmisc.c:89
void G3d_setNullValue(void *c, int nofElts, int type)
Fills the vector pointed to by c with nofElts NULL-values of type.
Definition: g3dnull.c:32
void G3d_error(const char *msg,...)
Definition: g3derror.c:75
int G3d_isNullValueNum(const void *n, int type)
Definition: g3dnull.c:9
long num
Definition: g3dcats.c:93
void * G3d_realloc(void *ptr, int nBytes)
Same as realloc (ptr, nBytes), except that in case of error G3d_error() is invoked.
Definition: g3dalloc.c:50
#define G3D_MAX(a, b)
Definition: G3d_intern.h:26
void G3d_setXdrNullNum(void *num, int isFloat)
Definition: g3dfpxdr.c:37
void G3d_setXdrNullDouble(double *d)
Definition: g3dfpxdr.c:48
void * G_incr_void_ptr(const void *ptr, const size_t size)
Advance void pointer.
Definition: gis/raster.c:33
int G3d_isXdrNullNum(const void *num, int isFloat)
Definition: g3dfpxdr.c:12
#define G3D_NO_XDR
Definition: G3d_intern.h:32
void G3d_copyValues(const void *src, int offsSrc, int typeSrc, void *dst, int offsDst, int typeDst, int nElts)
Definition: g3dmisc.c:52
#define G3D_USE_XDR
Definition: G3d_intern.h:31
int G3d_copyFromXdr(int nofNum, void *dst)
Definition: g3dfpxdr.c:222
int G3d_copyToXdr(const void *src, int nofNum)
Definition: g3dfpxdr.c:147
int G3d_initCopyFromXdr(G3D_Map *map, int dType)
Definition: g3dfpxdr.c:193
int
Definition: g3dcolor.c:48
int G3d_isXdrNullFloat(const float *f)
Definition: g3dfpxdr.c:23
return NULL
Definition: dbfopen.c:1394
void * G3d_malloc(int nBytes)
Same as malloc (nBytes), except that in case of error G3d_error() is invoked.
Definition: g3dalloc.c:24
int G3d_isXdrNullDouble(const double *d)
Definition: g3dfpxdr.c:30
int G3d_length(int t)
Definition: g3dmisc.c:77
void * xdr
Definition: g3d/header.c:14
int G3d_initFpXdr(G3D_Map *map, int misuseBytes)
Definition: g3dfpxdr.c:64
void G3d_setXdrNullFloat(float *f)
Definition: g3dfpxdr.c:55
XDR xdrDecodeStream
Definition: g3dfpxdr.c:62
int xdrLength
Definition: g3d/header.c:15
int G3d_initCopyToXdr(G3D_Map *map, int sType)
Definition: g3dfpxdr.c:118