GRASS GIS 7 Programmer's Manual  7.7.svn(2018)-r73574
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
xdrstring.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_base/xdrstring.c
3 
4  \brief DBMI Library (base) - external data representation (string)
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 <string.h>
16 #include "xdr.h"
17 
18 /*!
19  \brief Send string array
20 
21  \param a
22  \param count
23 
24  \return
25 */
27 {
28  int i;
29  int stat;
30 
31  stat = db__send_int(count);
32  for (i = 0; stat == DB_OK && i < count; i++)
33  stat = db__send_string(&a[i]);
34 
35  return stat;
36 }
37 
38 /*!
39  \brief Receive string array
40 
41  \param a
42  \param n
43 
44  \return
45 */
47 {
48  int i, count;
49  int stat;
50  dbString *b;
51 
52  *n = 0;
53  *a = NULL;
54  stat = db__recv_int(&count);
55  if (stat != DB_OK)
56  return stat;
57  if (count < 0) {
59  return DB_PROTOCOL_ERR;
60  }
61 
62  b = db_alloc_string_array(count);
63  if (b == NULL)
64  return DB_MEMORY_ERR;
65 
66  for (i = 0; i < count; i++) {
67  stat = db__recv_string(&b[i]);
68  if (stat != DB_OK) {
69  db_free_string_array(b, count);
70  return stat;
71  }
72  }
73  *n = count;
74  *a = b;
75 
76  return DB_OK;
77 }
78 
79 /*!
80  \brief Send string
81 
82  \param x
83 
84  \return
85 */
87 {
88  int stat = DB_OK;
89  const char *s = db_get_string(x);
90  int len = s ? strlen(s) + 1 : 1;
91 
92  if (!s)
93  s = ""; /* don't send a NULL string */
94 
95  if (!db__send(&len, sizeof(len)))
96  stat = DB_PROTOCOL_ERR;
97 
98  if (!db__send(s, len))
99  stat = DB_PROTOCOL_ERR;
100 
101  if (stat == DB_PROTOCOL_ERR)
103 
104  return stat;
105 }
106 
107 /*!
108  \brief Reads a string from transport
109 
110  Note: caller MUST initialize x by calling db_init_string()
111 
112  \param x
113 
114  \return DB_OK, DB_MEMORY_ERR, or DB_PROTOCOL_ERR
115  \return NULL if error
116  */
118 {
119  int stat = DB_OK;
120  int len;
121  char *s;
122 
123  if (!db__recv(&len, sizeof(len)))
124  stat = DB_PROTOCOL_ERR;
125 
126  if (len <= 0) /* len will include the null byte */
127  stat = DB_PROTOCOL_ERR;
128 
129  if (db_enlarge_string(x, len) != DB_OK)
130  stat = DB_PROTOCOL_ERR;
131 
132  s = db_get_string(x);
133 
134  if (!db__recv(s, len))
135  stat = DB_PROTOCOL_ERR;
136 
137  if (stat == DB_PROTOCOL_ERR)
139 
140  return stat;
141 }
142 
143 /*!
144  \brief Send C string
145 
146  \param s
147 
148  \return
149 */
150 int db__send_Cstring(const char *s)
151 {
152  dbString x;
153 
154  db_init_string(&x);
155  db_set_string_no_copy(&x, (char *)s);
156 
157  return db__send_string(&x);
158 }
int db_enlarge_string(dbString *x, int len)
Enlarge dbString.
Definition: string.c:120
int db__send_string_array(dbString *a, int count)
Send string array.
Definition: xdrstring.c:26
#define DB_PROTOCOL_ERR
Definition: dbmi.h:75
int db_set_string_no_copy(dbString *x, char *s)
Inserts string to dbString (overwrite current value)
Definition: string.c:55
int count
int db__recv_string(dbString *x)
Reads a string from transport.
Definition: xdrstring.c:117
#define NULL
Definition: ccmath.h:32
#define x
char * db_get_string(const dbString *x)
Get string.
Definition: string.c:140
double b
Definition: r_raster.c:39
int db__send_int(int n)
Send integer.
Definition: xdrint.c:24
int db__recv_string_array(dbString **a, int *n)
Receive string array.
Definition: xdrstring.c:46
void db_free_string_array(dbString *a, int n)
Free allocated dbString array.
Definition: string.c:163
void db_protocol_error(void)
Report protocol error.
dbString * db_alloc_string_array(int count)
Allocate dbString array.
Definition: string.c:181
int db__recv(void *buf, size_t size)
int db__send(const void *buf, size_t size)
?
#define DB_MEMORY_ERR
Definition: dbmi.h:74
int db__send_string(dbString *x)
Send string.
Definition: xdrstring.c:86
int db__recv_int(int *n)
Receive integer.
Definition: xdrint.c:44
void db_init_string(dbString *x)
Initialize dbString.
Definition: string.c:25
int db__send_Cstring(const char *s)
Send C string.
Definition: xdrstring.c:150
#define DB_OK
Definition: dbmi.h:71