1 /*!
2   \file lib/db/dbmi_base/xdrdouble.c
3 
4   \brief DBMI Library (base) - external data representation (double)
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 double
19 
20   \param d
21 
22   \return
23 */
db__send_double(double d)24 int db__send_double(double 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)
32 	db_protocol_error();
33 
34     return stat;
35 }
36 
37 /*!
38   \brief Receive double
39 
40   \param d
41 */
db__recv_double(double * d)42 int db__recv_double(double *d)
43 {
44     int stat = DB_OK;
45 
46     if (!db__recv(d, sizeof(*d)))
47 	stat = DB_PROTOCOL_ERR;
48 
49     if (stat == DB_PROTOCOL_ERR)
50 	db_protocol_error();
51 
52     return stat;
53 }
54 
55 /*!
56   \brief Send double array
57 
58   \param x
59   \param n
60 
61   \return
62 */							\
db__send_double_array(const double * x,int n)63 int db__send_double_array(const double *x, int n)
64 {
65     int stat = DB_OK;
66 
67     if (!db__send(&n, sizeof(n)))
68 	stat = DB_PROTOCOL_ERR;
69 
70     if (!db__send(x, n * sizeof(*x)))
71 	stat = DB_PROTOCOL_ERR;
72 
73     if (stat == DB_PROTOCOL_ERR)
74 	db_protocol_error();
75 
76     return stat;
77 }
78 
79 /*!
80   \brief Receive double array
81 
82   Returns an allocated array of doubles
83   Caller is responsible for free()
84 
85   \param x
86   \param n
87 
88   \return
89 */
db__recv_double_array(double ** x,int * n)90 int db__recv_double_array(double **x, int *n)
91 {
92     int stat = DB_OK;
93     int count = 0;
94     double *a = NULL;
95 
96     if (!db__recv(&count, sizeof(count)))
97 	stat = DB_PROTOCOL_ERR;
98 
99     *n = count;
100 
101     *x = a = (double *)db_calloc(count, sizeof(*a));
102 
103     if (!db__recv(a, count * sizeof(*a)))
104 	stat = DB_PROTOCOL_ERR;
105 
106     if (stat == DB_PROTOCOL_ERR)
107 	db_protocol_error();
108 
109     return stat;
110 }
111