GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-0824e8842e
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 {
22  unsigned char *dst = (unsigned char *)dstp;
23  const unsigned char *src = (const unsigned char *)srcp;
24 
25  if (G__.little_endian) {
26  dst[0] = src[3];
27  dst[1] = src[2];
28  dst[2] = src[1];
29  dst[3] = src[0];
30  }
31  else
32  memcpy(dst, src, 4);
33 }
34 
35 static void swap_float(void *dstp, const void *srcp)
36 {
37  unsigned char *dst = (unsigned char *)dstp;
38  const unsigned char *src = (const unsigned char *)srcp;
39 
40  if (G__.little_endian) {
41  dst[0] = src[3];
42  dst[1] = src[2];
43  dst[2] = src[1];
44  dst[3] = src[0];
45  }
46  else
47  memcpy(dst, src, 4);
48 }
49 
50 static void swap_double(void *dstp, const void *srcp)
51 {
52  unsigned char *dst = (unsigned char *)dstp;
53  const unsigned char *src = (const unsigned char *)srcp;
54 
55  if (G__.little_endian) {
56  dst[0] = src[7];
57  dst[1] = src[6];
58  dst[2] = src[5];
59  dst[3] = src[4];
60  dst[4] = src[3];
61  dst[5] = src[2];
62  dst[6] = src[1];
63  dst[7] = src[0];
64  }
65  else
66  memcpy(dst, src, 8);
67 }
68 
69 void G_xdr_get_int(int *dst, const void *src)
70 {
71  swap_int(dst, src);
72 }
73 
74 void G_xdr_put_int(void *dst, const int *src)
75 {
76  swap_int(dst, src);
77 }
78 
79 void G_xdr_get_float(float *dst, const void *src)
80 {
81  swap_float(dst, src);
82 }
83 
84 void G_xdr_put_float(void *dst, const float *src)
85 {
86  swap_float(dst, src);
87 }
88 
89 void G_xdr_get_double(double *dst, const void *src)
90 {
91  swap_double(dst, src);
92 }
93 
94 void G_xdr_put_double(void *dst, const double *src)
95 {
96  swap_double(dst, src);
97 }
void G_xdr_put_int(void *dst, const int *src)
Definition: gis/xdr.c:74
void G_xdr_put_float(void *dst, const float *src)
Definition: gis/xdr.c:84
void G_xdr_get_double(double *dst, const void *src)
Definition: gis/xdr.c:89
void G_xdr_put_double(void *dst, const double *src)
Definition: gis/xdr.c:94
void G_xdr_get_int(int *dst, const void *src)
Definition: gis/xdr.c:69
void G_xdr_get_float(float *dst, const void *src)
Definition: gis/xdr.c:79
Definition: G.h:5
int little_endian
Definition: G.h:8