GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71769
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
xdrint.c
Go to the documentation of this file.
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 */
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)
33 
34  return stat;
35 }
36 
37 /*!
38  \brief Receive integer
39 
40  \param n
41 
42  \return
43 */
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)
53 
54  return stat;
55 }
56 
57 /*!
58  \brief Send integer array
59 
60  \param x
61  \param n
62 
63  \return
64 */
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)
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 */
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)
110 
111  return stat;
112 }
#define DB_PROTOCOL_ERR
Definition: dbmi.h:75
int count
int db__send_int_array(const int *x, int n)
Send integer array.
Definition: xdrint.c:65
#define NULL
Definition: ccmath.h:32
#define x
int db__recv_int_array(int **x, int *n)
Receive integer array.
Definition: xdrint.c:92
void * db_calloc(int n, int m)
Allocate memory.
int db__send_int(int n)
Send integer.
Definition: xdrint.c:24
void db_protocol_error(void)
Report protocol error.
int db__recv(void *buf, size_t size)
int db__send(const void *buf, size_t size)
?
int db__recv_int(int *n)
Receive integer.
Definition: xdrint.c:44
#define DB_OK
Definition: dbmi.h:71