xdrstring.c

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

Generated on Sun Apr 6 17:31:38 2008 for GRASS by  doxygen 1.5.5