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 */
db__send_string_array(dbString * a,int count)26 int db__send_string_array(dbString * a, int count)
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 */
db__recv_string_array(dbString ** a,int * n)46 int db__recv_string_array(dbString ** a, int *n)
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) {
58 	db_protocol_error();
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 */
db__send_string(dbString * x)86 int db__send_string(dbString * x)
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)
102 	db_protocol_error();
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  */
db__recv_string(dbString * x)117 int db__recv_string(dbString * x)
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)
138 	db_protocol_error();
139 
140     return stat;
141 }
142 
143 /*!
144   \brief Send C string
145 
146   \param s
147 
148   \return
149 */
db__send_Cstring(const char * s)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 }
159