GRASS Programmer's Manual 6.4.1(2011)
string.c
Go to the documentation of this file.
00001 #include <string.h>
00002 #include <stdlib.h>
00003 #include <grass/dbmi.h>
00004 
00011 void db_init_string(dbString * x)
00012 {
00013     x->string = "";
00014     x->nalloc = 0;
00015 }
00016 
00017 
00018 
00025 /* db_set_string(dbString *x, char *s, int copy)
00026  *  inserts 's' into 'x'
00027  *   if 'copy' is true, then memory is allocated to copy into
00028  *   else 'x' is made to point to 's'
00029  * returns DB_OK or DB_MEMORY_ERR
00030  */
00031 static int set_string(dbString * x, char *s, int copy);
00032 
00033 int db_set_string(dbString * x, const char *s)
00034 {
00035     return set_string(x, (char *)s, 1);
00036 }
00037 
00044 int db_set_string_no_copy(dbString * x, char *s)
00045 {
00046     return set_string(x, s, 0);
00047 }
00048 
00055 unsigned int db_sizeof_string(dbString * x)
00056 {
00057     if (x->nalloc < 0)
00058         return 0;
00059     return (unsigned int)x->nalloc;
00060 }
00061 
00068 void db_zero_string(dbString * x)
00069 {
00070     db_zero((void *)db_get_string(x), db_sizeof_string(x));
00071 }
00072 
00079 static int set_string(dbString * x, char *s, int copy)
00080 {
00081     int len;
00082     int stat;
00083 
00084     if (s == NULL) {
00085         s = "";
00086         copy = 1;
00087     }
00088 
00089     len = strlen(s) + 1;
00090 
00091     if (copy) {
00092         stat = db_enlarge_string(x, len);
00093         if (stat != DB_OK)
00094             return stat;
00095         strcpy(x->string, s);
00096     }
00097     else {
00098         db_free_string(x);
00099         x->string = s;
00100         x->nalloc = -1;
00101     }
00102     return DB_OK;
00103 }
00104 
00111 int db_enlarge_string(dbString * x, int len)
00112 {
00113     if (x->nalloc < len) {
00114         if (x->nalloc <= 0)
00115             x->string = db_store("");
00116         x->string = db_realloc((void *)x->string, len);
00117         if (x->string == NULL)
00118             return DB_MEMORY_ERR;
00119         x->nalloc = len;
00120     }
00121     return DB_OK;
00122 }
00123 
00131 char *db_get_string(dbString * x)
00132 {
00133     return x->string;
00134 }
00135 
00142 void db_free_string(dbString * x)
00143 {
00144     if (x->nalloc > 0)
00145         db_free(x->string);
00146     db_init_string(x);
00147 }
00148 
00155 void db_free_string_array(dbString * a, int n)
00156 {
00157     int i;
00158 
00159     if (a) {
00160         for (i = 0; i < n; i++)
00161             db_free_string(&a[i]);
00162         db_free(a);
00163     }
00164 }
00165 
00172 dbString *db_alloc_string_array(int count)
00173 {
00174     int i;
00175     dbString *a;
00176 
00177     if (count < 0)
00178         count = 0;
00179     a = (dbString *) db_calloc(count, sizeof(dbString));
00180     if (a) {
00181         for (i = 0; i < count; i++)
00182             db_init_string(&a[i]);
00183     }
00184     return a;
00185 }
00186 
00193 int db_append_string(dbString * x, const char *s)
00194 {
00195     int len;
00196     int stat;
00197 
00198     len = strlen(db_get_string(x)) + strlen(s) + 1;
00199     stat = db_enlarge_string(x, len);
00200     if (stat != DB_OK)
00201         return stat;
00202     strcat(db_get_string(x), s);
00203     return DB_OK;
00204 }
00205 
00212 int db_copy_string(dbString * dst, dbString * src)
00213 {
00214     return db_set_string(dst, db_get_string(src));
00215 }
00216 
00223 void db_double_quote_string(dbString * src)
00224 {
00225     char *ptra, *ptrb, buf[2];
00226     dbString tmp;
00227 
00228     db_init_string(&tmp);
00229     buf[1] = 0;
00230 
00231     ptrb = db_get_string(src);
00232     while ((ptra = strchr(ptrb, '\'')) != NULL) {
00233         for (; ptrb <= ptra; ptrb++) {
00234             buf[0] = ptrb[0];
00235             db_append_string(&tmp, buf);
00236         }
00237         db_append_string(&tmp, "'");
00238     }
00239     db_append_string(&tmp, ptrb);
00240     db_set_string(src, db_get_string(&tmp));
00241     db_free_string(&tmp);
00242 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines