GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
xdrstring.c
Go to the documentation of this file.
1 #include <string.h>
2 #include "xdr.h"
3 
4 
5 int db__send_string_array(dbString * a, int count)
6 {
7  int i;
8  int stat;
9 
10  stat = db__send_int(count);
11  for (i = 0; stat == DB_OK && i < count; i++)
12  stat = db__send_string(&a[i]);
13 
14  return stat;
15 }
16 
17 /* note: dbString *a; ...(...,&a...) */
18 int db__recv_string_array(dbString ** a, int *n)
19 {
20  int i, count;
21  int stat;
22  dbString *b;
23 
24  *n = 0;
25  *a = NULL;
26  stat = db__recv_int(&count);
27  if (stat != DB_OK)
28  return stat;
29  if (count < 0) {
31  return DB_PROTOCOL_ERR;
32  }
33 
34  b = db_alloc_string_array(count);
35  if (b == NULL)
36  return DB_MEMORY_ERR;
37 
38  for (i = 0; i < count; i++) {
39  stat = db__recv_string(&b[i]);
40  if (stat != DB_OK) {
41  db_free_string_array(b, count);
42  return stat;
43  }
44  }
45  *n = count;
46  *a = b;
47 
48  return DB_OK;
49 }
50 
51 int db__send_string(dbString * x)
52 {
53  int stat = DB_OK;
54  const char *s = db_get_string(x);
55  int len = s ? strlen(s) + 1 : 1;
56 
57  if (!s)
58  s = ""; /* don't send a NULL string */
59 
60  if (!db__send(&len, sizeof(len)))
61  stat = DB_PROTOCOL_ERR;
62 
63  if (!db__send(s, len))
64  stat = DB_PROTOCOL_ERR;
65 
66  if (stat == DB_PROTOCOL_ERR)
68 
69  return stat;
70 }
71 
72 /*
73  * db__recv_string (dbString *x)
74  * reads a string from transport
75  *
76  * returns DB_OK, DB_MEMORY_ERR, or DB_PROTOCOL_ERR
77  * x.s will be NULL if error
78  *
79  * NOTE: caller MUST initialize x by calling db_init_string()
80  */
81 int db__recv_string(dbString * x)
82 {
83  int stat = DB_OK;
84  int len;
85  char *s;
86 
87  if (!db__recv(&len, sizeof(len)))
88  stat = DB_PROTOCOL_ERR;
89 
90  if (len <= 0) /* len will include the null byte */
91  stat = DB_PROTOCOL_ERR;
92 
93  if (db_enlarge_string(x, len) != DB_OK)
94  stat = DB_PROTOCOL_ERR;
95 
96  s = db_get_string(x);
97 
98  if (!db__recv(s, len))
99  stat = DB_PROTOCOL_ERR;
100 
101  if (stat == DB_PROTOCOL_ERR)
103 
104  return stat;
105 }
106 
107 int db__send_Cstring(const char *s)
108 {
109  dbString x;
110 
111  db_init_string(&x);
112  db_set_string_no_copy(&x, (char *)s);
113 
114  return db__send_string(&x);
115 }