GRASS Programmer's Manual
6.4.2(2012)
|
00001 #include <string.h> 00002 #include "xdr.h" 00003 00004 00005 int db__send_string_array(dbString * a, int count) 00006 { 00007 int i; 00008 int stat; 00009 00010 stat = db__send_int(count); 00011 for (i = 0; stat == DB_OK && i < count; i++) 00012 stat = db__send_string(&a[i]); 00013 00014 return stat; 00015 } 00016 00017 /* note: dbString *a; ...(...,&a...) */ 00018 int db__recv_string_array(dbString ** a, int *n) 00019 { 00020 int i, count; 00021 int stat; 00022 dbString *b; 00023 00024 *n = 0; 00025 *a = NULL; 00026 stat = db__recv_int(&count); 00027 if (stat != DB_OK) 00028 return stat; 00029 if (count < 0) { 00030 db_protocol_error(); 00031 return DB_PROTOCOL_ERR; 00032 } 00033 00034 b = db_alloc_string_array(count); 00035 if (b == NULL) 00036 return DB_MEMORY_ERR; 00037 00038 for (i = 0; i < count; i++) { 00039 stat = db__recv_string(&b[i]); 00040 if (stat != DB_OK) { 00041 db_free_string_array(b, count); 00042 return stat; 00043 } 00044 } 00045 *n = count; 00046 *a = b; 00047 00048 return DB_OK; 00049 } 00050 00051 int db__send_string(dbString * x) 00052 { 00053 int stat = DB_OK; 00054 const char *s = db_get_string(x); 00055 int len = s ? strlen(s) + 1 : 1; 00056 00057 if (!s) 00058 s = ""; /* don't send a NULL string */ 00059 00060 if (!db__send(&len, sizeof(len))) 00061 stat = DB_PROTOCOL_ERR; 00062 00063 if (!db__send(s, len)) 00064 stat = DB_PROTOCOL_ERR; 00065 00066 if (stat == DB_PROTOCOL_ERR) 00067 db_protocol_error(); 00068 00069 return stat; 00070 } 00071 00072 /* 00073 * db__recv_string (dbString *x) 00074 * reads a string from transport 00075 * 00076 * returns DB_OK, DB_MEMORY_ERR, or DB_PROTOCOL_ERR 00077 * x.s will be NULL if error 00078 * 00079 * NOTE: caller MUST initialize x by calling db_init_string() 00080 */ 00081 int db__recv_string(dbString * x) 00082 { 00083 int stat = DB_OK; 00084 int len; 00085 char *s; 00086 00087 if (!db__recv(&len, sizeof(len))) 00088 stat = DB_PROTOCOL_ERR; 00089 00090 if (len <= 0) /* len will include the null byte */ 00091 stat = DB_PROTOCOL_ERR; 00092 00093 if (db_enlarge_string(x, len) != DB_OK) 00094 stat = DB_PROTOCOL_ERR; 00095 00096 s = db_get_string(x); 00097 00098 if (!db__recv(s, len)) 00099 stat = DB_PROTOCOL_ERR; 00100 00101 if (stat == DB_PROTOCOL_ERR) 00102 db_protocol_error(); 00103 00104 return stat; 00105 } 00106 00107 int db__send_Cstring(const char *s) 00108 { 00109 dbString x; 00110 00111 db_init_string(&x); 00112 db_set_string_no_copy(&x, (char *)s); 00113 00114 return db__send_string(&x); 00115 }