1 /*!
2   \file lib/db/dbmi_base/xdrint.c
3 
4   \brief DBMI Library (base) - external data representation (integer)
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 integer
19 
20   \param n
21 
22   \return
23 */
db__send_int(int n)24 int db__send_int(int n)
25 {
26     int stat = DB_OK;
27 
28     if (!db__send(&n, sizeof(n)))
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 integer
39 
40   \param n
41 
42   \return
43 */
db__recv_int(int * n)44 int db__recv_int(int *n)
45 {
46     int stat = DB_OK;
47 
48     if (!db__recv(n, sizeof(*n)))
49 	stat = DB_PROTOCOL_ERR;
50 
51     if (stat == DB_PROTOCOL_ERR)
52 	db_protocol_error();
53 
54     return stat;
55 }
56 
57 /*!
58   \brief Send integer array
59 
60   \param x
61   \param n
62 
63   \return
64 */
db__send_int_array(const int * x,int n)65 int db__send_int_array(const int *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)
76 	db_protocol_error();
77 
78     return stat;
79 }
80 
81 /*!
82   \brief Receive integer array
83 
84   Returns an allocated array of ints
85   Caller is responsible for free()
86 
87   \param x
88   \param n
89 
90   \return
91 */
db__recv_int_array(int ** x,int * n)92 int db__recv_int_array(int **x, int *n)
93 {
94     int stat = DB_OK;
95     int count = 0;
96     int *a = NULL;
97 
98     if (!db__recv(&count, sizeof(count)))
99 	stat = DB_PROTOCOL_ERR;
100 
101     *n = count;
102 
103     *x = a = (int *)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)
109 	db_protocol_error();
110 
111     return stat;
112 }
113