00001 #include <string.h>
00002 #include <stdlib.h>
00003 #include "dbmi.h"
00004
00011 void
00012 db_init_string (x)
00013 dbString *x;
00014 {
00015 x->string = "";
00016 x->nalloc = 0;
00017 }
00018
00019
00020
00027
00028
00029
00030
00031
00032
00033 static int set_string();
00034
00035 int
00036 db_set_string (x, s)
00037 dbString *x;
00038 char *s;
00039 {
00040 return set_string (x, s, 1);
00041 }
00042
00049 int
00050 db_set_string_no_copy (x, s)
00051 dbString *x;
00052 char *s;
00053 {
00054 return set_string (x, s, 0);
00055 }
00056
00063 unsigned int
00064 db_sizeof_string (x)
00065 dbString *x;
00066 {
00067 if (x->nalloc < 0) return 0;
00068 return (unsigned int) x->nalloc;
00069 }
00070
00077 void
00078 db_zero_string (x)
00079 dbString *x;
00080 {
00081 db_zero ((void *)db_get_string(x), db_sizeof_string(x));
00082 }
00083
00090 static int
00091 set_string (x, s, copy)
00092 dbString *x;
00093 char *s;
00094 {
00095 int len;
00096 int stat;
00097
00098 if (s == NULL)
00099 {
00100 s = "";
00101 copy = 1;
00102 }
00103
00104 len = strlen(s)+1;
00105
00106 if (copy)
00107 {
00108 stat = db_enlarge_string (x, len);
00109 if (stat != DB_OK)
00110 return stat;
00111 strcpy (x->string, s);
00112 }
00113 else
00114 {
00115 db_free_string(x);
00116 x->string = s;
00117 x->nalloc = -1;
00118 }
00119 return DB_OK;
00120 }
00121
00128 int
00129 db_enlarge_string (x, len)
00130 dbString *x;
00131 int len;
00132 {
00133 if (x->nalloc < len)
00134 {
00135 if (x->nalloc <= 0)
00136 x->string = db_store("");
00137 x->string = db_realloc ((void *)x->string, len);
00138 if (x->string == NULL)
00139 return DB_MEMORY_ERR;
00140 x->nalloc = len;
00141 }
00142 return DB_OK;
00143 }
00144
00145 char *
00146 db_get_string(x)
00147 dbString *x;
00148 {
00149 return x->string;
00150 }
00151
00158 void
00159 db_free_string(x)
00160 dbString *x;
00161 {
00162 if (x->nalloc > 0)
00163 free(x->string);
00164 db_init_string (x);
00165 }
00166
00173 void
00174 db_free_string_array (a, n)
00175 dbString *a;
00176 {
00177 int i;
00178
00179 if (a)
00180 {
00181 for (i = 0; i<n; i++)
00182 db_free_string(&a[i]);
00183 free (a);
00184 }
00185 }
00186
00193 dbString *
00194 db_alloc_string_array (count)
00195 int count;
00196 {
00197 int i;
00198 dbString *a;
00199
00200 if (count < 0) count = 0;
00201 a = (dbString *) db_calloc (count, sizeof(dbString));
00202 if (a)
00203 {
00204 for (i = 0; i < count; i++)
00205 db_init_string(&a[i]);
00206 }
00207 return a;
00208 }
00209
00216 int
00217 db_append_string (x, s)
00218 dbString *x;
00219 char *s;
00220 {
00221 int len;
00222 int stat;
00223
00224 len = strlen (db_get_string(x)) + strlen(s) + 1;
00225 stat = db_enlarge_string (x, len);
00226 if (stat != DB_OK)
00227 return stat;
00228 strcat (db_get_string(x), s);
00229 return DB_OK;
00230 }
00231
00238 int
00239 db_copy_string (dst, src)
00240 dbString *dst, *src;
00241 {
00242 return db_set_string (dst, db_get_string(src));
00243 }
00244
00251 void
00252 db_double_quote_string (src)
00253 dbString *src;
00254 {
00255 char *ptra, *ptrb, buf[2];
00256 dbString tmp;
00257
00258 db_init_string (&tmp);
00259 buf[1] = 0;
00260
00261 ptrb = db_get_string(src);
00262 while ( (ptra = strchr( ptrb, '\'') ) != NULL ) {
00263 for ( ; ptrb <= ptra; ptrb++ ) { buf[0] = ptrb[0]; db_append_string (&tmp, buf); }
00264 db_append_string (&tmp, "'");
00265 }
00266 db_append_string (&tmp, ptrb );
00267 db_set_string ( src, db_get_string(&tmp));
00268 db_free_string( &tmp );
00269 }
00270