GRASS Programmer's Manual  6.5.svn(2014)-r66266
 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 }
int db_enlarge_string(dbString *x, int len)
Definition: string.c:111
int db__send_string_array(dbString *a, int count)
Definition: xdrstring.c:5
float b
Definition: named_colr.c:8
int db__send(const void *buf, size_t size)
Definition: xdr.c:77
int db_set_string_no_copy(dbString *x, char *s)
Definition: string.c:44
int count
int db__recv_string(dbString *x)
Definition: xdrstring.c:81
void db_protocol_error(void)
int stat
Definition: g3dcolor.c:369
int db__send_int(int n)
Definition: xdrint.c:4
int db__recv(void *buf, size_t size)
Definition: xdr.c:88
int db__recv_string_array(dbString **a, int *n)
Definition: xdrstring.c:18
void db_free_string_array(dbString *a, int n)
Definition: string.c:155
return NULL
Definition: dbfopen.c:1394
dbString * db_alloc_string_array(int count)
Definition: string.c:172
char * db_get_string(dbString *x)
Definition: string.c:131
int db__send_string(dbString *x)
Definition: xdrstring.c:51
int n
Definition: dataquad.c:291
int db__recv_int(int *n)
Definition: xdrint.c:17
void db_init_string(dbString *x)
Definition: string.c:11
int db__send_Cstring(const char *s)
Definition: xdrstring.c:107