GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
gis/xdr.c
Go to the documentation of this file.
1 /*!
2  * \file lib/gis/xdr.c
3  *
4  * \brief GIS Library - XDR related functions.
5  *
6  * (C) 2012-2014 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public License
9  * (>=v2). Read the file COPYING that comes with GRASS for details.
10  *
11  * \author Glynn Clements
12  */
13 
14 #include <stdlib.h>
15 #include <string.h>
16 #include <grass/gis.h>
17 
18 #include "G.h"
19 
20 static void swap_int(void *dstp, const void *srcp) {
21  unsigned char *dst = (unsigned char *) dstp;
22  const unsigned char *src = (const unsigned char *) srcp;
23  if (G__.little_endian) {
24  dst[0] = src[3];
25  dst[1] = src[2];
26  dst[2] = src[1];
27  dst[3] = src[0];
28  }
29  else
30  memcpy(dst, src, 4);
31 }
32 
33 static void swap_float(void *dstp, const void *srcp) {
34  unsigned char *dst = (unsigned char *) dstp;
35  const unsigned char *src = (const unsigned char *) srcp;
36  if (G__.little_endian) {
37  dst[0] = src[3];
38  dst[1] = src[2];
39  dst[2] = src[1];
40  dst[3] = src[0];
41  }
42  else
43  memcpy(dst, src, 4);
44 }
45 
46 static void swap_double(void *dstp, const void *srcp) {
47  unsigned char *dst = (unsigned char *) dstp;
48  const unsigned char *src = (const unsigned char *) srcp;
49  if (G__.little_endian) {
50  dst[0] = src[7];
51  dst[1] = src[6];
52  dst[2] = src[5];
53  dst[3] = src[4];
54  dst[4] = src[3];
55  dst[5] = src[2];
56  dst[6] = src[1];
57  dst[7] = src[0];
58  }
59  else
60  memcpy(dst, src, 8);
61 }
62 
63 void G_xdr_get_int(int *dst, const void *src)
64 {
65  swap_int(dst, src);
66 }
67 
68 void G_xdr_put_int(void *dst, const int *src)
69 {
70  swap_int(dst, src);
71 }
72 
73 void G_xdr_get_float(float *dst, const void *src)
74 {
75  swap_float(dst, src);
76 }
77 
78 void G_xdr_put_float(void *dst, const float *src)
79 {
80  swap_float(dst, src);
81 }
82 
83 void G_xdr_get_double(double *dst, const void *src)
84 {
85  swap_double(dst, src);
86 }
87 
88 void G_xdr_put_double(void *dst, const double *src)
89 {
90  swap_double(dst, src);
91 }
92 
char * dst
Definition: lz4.h:599
int little_endian
Definition: G.h:8
void G_xdr_put_float(void *dst, const float *src)
Definition: gis/xdr.c:78
Definition: G.h:4
void G_xdr_put_double(void *dst, const double *src)
Definition: gis/xdr.c:88
void G_xdr_put_int(void *dst, const int *src)
Definition: gis/xdr.c:68
void G_xdr_get_float(float *dst, const void *src)
Definition: gis/xdr.c:73
void G_xdr_get_double(double *dst, const void *src)
Definition: gis/xdr.c:83
void G_xdr_get_int(int *dst, const void *src)
Definition: gis/xdr.c:63