GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
xdrfloat.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_base/xdrfloat.c
3 
4  \brief DBMI Library (base) - external data representation (float)
5 
6  (C) 1999-2009, 2011 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 Joel Jones (CERL/UIUC), Radim Blazek, Brad Douglas, Markus Neteler
12  \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
13 */
14 
15 #include "xdr.h"
16 
17 /*!
18  \brief Send float
19 
20  \param d
21 
22  \return
23 */
24 int db__send_float(float d)
25 {
26  int stat = DB_OK;
27 
28  if (!db__send(&d, sizeof(d)))
29  stat = DB_PROTOCOL_ERR;
30 
31  if (stat == DB_PROTOCOL_ERR)
33 
34  return stat;
35 }
36 
37 /*!
38  \brief Receive float
39 
40  \param d
41 
42  \return
43 */
44 int db__recv_float(float *d)
45 {
46  int stat = DB_OK;
47 
48  if (!db__recv(d, sizeof(*d)))
49  stat = DB_PROTOCOL_ERR;
50 
51  if (stat == DB_PROTOCOL_ERR)
53 
54  return stat;
55 }
56 
57 /*!
58  \brief Send float array
59 
60  \param x
61  \param n
62 
63  \return
64 */
65 int db__send_float_array(const float *x, int n)
66 {
67  int stat = DB_OK;
68 
69  if (!db__send(&n, sizeof(n)))
70  stat = DB_PROTOCOL_ERR;
71 
72  if (!db__send(x, n * sizeof(*x)))
73  stat = DB_PROTOCOL_ERR;
74 
75  if (stat == DB_PROTOCOL_ERR)
77 
78  return stat;
79 }
80 
81 /*!
82  \brief Receive float array
83 
84  Returns an allocated array of floats
85  Caller is responsible for free()
86 
87  \param x
88  \param n
89 
90  \return
91 */
92 int db__recv_float_array(float **x, int *n)
93 {
94  int stat = DB_OK;
95  int count = 0;
96  float *a = NULL;
97 
98  if (!db__recv(&count, sizeof(count)))
99  stat = DB_PROTOCOL_ERR;
100 
101  *n = count;
102 
103  *x = a = (float *)db_calloc(count, sizeof(*a));
104 
105  if (!db__recv(a, count * sizeof(*a)))
106  stat = DB_PROTOCOL_ERR;
107 
108  if (stat == DB_PROTOCOL_ERR)
110 
111  return stat;
112 }
#define DB_PROTOCOL_ERR
Definition: dbmi.h:75
int count
#define NULL
Definition: ccmath.h:32
#define x
int db__send_float(float d)
Send float.
Definition: xdrfloat.c:24
int db__send_float_array(const float *x, int n)
Send float array.
Definition: xdrfloat.c:65
int db__recv_float(float *d)
Receive float.
Definition: xdrfloat.c:44
void * db_calloc(int, int)
Allocate memory.
int db__recv_float_array(float **x, int *n)
Receive float array.
Definition: xdrfloat.c:92
int db__recv(void *buf, size_t size)
void db_protocol_error(void)
Report protocol error.
int db__send(const void *buf, size_t size)
?
#define DB_OK
Definition: dbmi.h:71