GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
xdrshort.c
Go to the documentation of this file.
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  */
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)
35 
36  return stat;
37 }
38 
39 /*!
40  \brief Receive short
41 
42  \param n
43 
44  \return
45  */
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)
55 
56  return stat;
57 }
58 
59 /*!
60  \brief Send short array
61 
62  \param x
63  \param n
64 
65  \return
66  */
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)
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  */
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)
112 
113  return stat;
114 }
#define NULL
Definition: ccmath.h:32
int db__recv(void *buf, size_t size)
int db__send(const void *buf, size_t size)
?
#define DB_PROTOCOL_ERR
Definition: dbmi.h:75
#define DB_OK
Definition: dbmi.h:71
void * db_calloc(int, int)
Allocate memory.
void db_protocol_error(void)
Report protocol error.
int count
int db__recv_short_array(short **x, int *n)
Receive short array.
Definition: xdrshort.c:94
int db__send_short_array(const short *x, int n)
Send short array.
Definition: xdrshort.c:67
int db__send_short(int n)
Send short.
Definition: xdrshort.c:25
int db__recv_short(short *n)
Receive short.
Definition: xdrshort.c:46
#define x