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