GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 1999-2009, 2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Joel Jones (CERL/UIUC), Radim Blazek, Brad Douglas, Markus Neteler
10 \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
11 */
12
13#include <string.h>
14#include "xdr.h"
15
16/*!
17 \brief Send string array
18
19 \param a
20 \param count
21
22 \return
23 */
25{
26 int i;
27 int stat;
28
30 for (i = 0; stat == DB_OK && i < count; i++)
31 stat = db__send_string(&a[i]);
32
33 return stat;
34}
35
36/*!
37 \brief Receive string array
38
39 \param a
40 \param n
41
42 \return
43 */
45{
46 int i, count;
47 int stat;
48 dbString *b;
49
50 *n = 0;
51 *a = NULL;
53 if (stat != DB_OK)
54 return stat;
55 if (count < 0) {
57 return DB_PROTOCOL_ERR;
58 }
59
61 if (b == NULL)
62 return DB_MEMORY_ERR;
63
64 for (i = 0; i < count; i++) {
65 stat = db__recv_string(&b[i]);
66 if (stat != DB_OK) {
68 return stat;
69 }
70 }
71 *n = count;
72 *a = b;
73
74 return DB_OK;
75}
76
77/*!
78 \brief Send string
79
80 \param x
81
82 \return
83 */
85{
86 int stat = DB_OK;
87 const char *s = db_get_string(x);
88 int len = s ? strlen(s) + 1 : 1;
89
90 if (!s)
91 s = ""; /* don't send a NULL string */
92
93 if (!db__send(&len, sizeof(len)))
95
96 if (!db__send(s, len))
98
99 if (stat == DB_PROTOCOL_ERR)
101
102 return stat;
103}
104
105/*!
106 \brief Reads a string from transport
107
108 Note: caller MUST initialize x by calling db_init_string()
109
110 \param x
111
112 \return DB_OK, DB_MEMORY_ERR, or DB_PROTOCOL_ERR
113 \return NULL if error
114 */
116{
117 int stat = DB_OK;
118 int len;
119 char *s;
120
121 if (!db__recv(&len, sizeof(len)))
123
124 if (len <= 0) /* len will include the null byte */
126
127 if (db_enlarge_string(x, len) != DB_OK)
129
130 s = db_get_string(x);
131
132 if (!db__recv(s, len))
134
135 if (stat == DB_PROTOCOL_ERR)
137
138 return stat;
139}
140
141/*!
142 \brief Send C string
143
144 \param s
145
146 \return
147 */
148int db__send_Cstring(const char *s)
149{
150 dbString x;
151
153 db_set_string_no_copy(&x, (char *)s);
154
155 return db__send_string(&x);
156}
#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:73
#define DB_OK
Definition dbmi.h:69
#define DB_MEMORY_ERR
Definition dbmi.h:72
int db__send_int(int)
Send integer.
Definition xdrint.c:22
int db_enlarge_string(dbString *, int)
Enlarge dbString.
Definition string.c:118
int db__recv_int(int *)
Receive integer.
Definition xdrint.c:42
void db_free_string_array(dbString *, int)
Free allocated dbString array.
Definition string.c:161
char * db_get_string(const dbString *)
Get string.
Definition string.c:138
dbString * db_alloc_string_array(int)
Allocate dbString array.
Definition string.c:179
int db_set_string_no_copy(dbString *, char *)
Inserts string to dbString (overwrite current value)
Definition string.c:53
void db_init_string(dbString *)
Initialize dbString.
Definition string.c:23
void db_protocol_error(void)
Report protocol error.
int count
double b
Definition r_raster.c:37
int db__send_Cstring(const char *s)
Send C string.
Definition xdrstring.c:148
int db__send_string(dbString *x)
Send string.
Definition xdrstring.c:84
int db__recv_string_array(dbString **a, int *n)
Receive string array.
Definition xdrstring.c:44
int db__send_string_array(dbString *a, int count)
Send string array.
Definition xdrstring.c:24
int db__recv_string(dbString *x)
Reads a string from transport.
Definition xdrstring.c:115
#define x