Main Page | Modules | Data Structures | File List | Data Fields

str.c

00001 /* 
00002  * Copyright (c) 2005, 2006 by KoanLogic s.r.l. - All rights reserved.  
00003  */
00004 
00005 static const char rcsid[] =
00006     "$Id: str.c,v 1.5 2006/01/09 12:38:38 tat Exp $";
00007 
00008 #include <stdlib.h>
00009 #include <errno.h>
00010 
00011 #include <u/str.h>
00012 #include <u/misc.h>
00013 #include <u/carpal.h>
00014 #include <u/memory.h>
00015 
00021 /* null strings will be bound to the null char* */
00022 static char null_cstr[1] = { 0 };
00023 static char* null = null_cstr;
00024 
00025 /* internal string struct */
00026 struct u_string_s
00027 {
00028     char *data;
00029     size_t data_sz, data_len, shift_cnt;
00030 };
00031 
00041 int u_string_trim(u_string_t *s)
00042 {
00043     if(s->data_len)
00044     {
00045         u_trim(s->data);
00046 
00047         s->data_len = strlen(s->data);
00048     }
00049 
00050     return 0;
00051 }
00052 
00062 int u_string_set_length(u_string_t *s, size_t len)
00063 {
00064     dbg_err_if(len > s->data_len);
00065 
00066     if(len < s->data_len)
00067     {
00068         s->data_len = len;
00069         s->data[len] = 0;
00070     }
00071 
00072     return 0;
00073 err:
00074     return ~0;
00075 }
00076 
00086 inline size_t u_string_len(u_string_t *s)
00087 {
00088     return s->data_len;
00089 }
00090 
00101 inline const char *u_string_c(u_string_t *s)
00102 {
00103     return s->data;
00104 }
00105 
00116 inline int u_string_copy(u_string_t *dst, u_string_t *src)
00117 {
00118     u_string_clear(dst);
00119     return u_string_append(dst, src->data, src->data_len);
00120 }
00121 
00131 int u_string_clear(u_string_t *s)
00132 {
00133     /* clear the string but not deallocate the buffer */
00134     if(s->data_sz)
00135     {
00136         s->data[0] = 0;
00137         s->data_len = 0;
00138     }
00139 
00140     return 0;
00141 }
00142 
00157 int u_string_create(const char *buf, size_t len, u_string_t **ps)
00158 {
00159     u_string_t *s = NULL;
00160 
00161     s = u_zalloc(sizeof(u_string_t));
00162     dbg_err_if(s == NULL);
00163 
00164     s->data = null;
00165 
00166     if(buf)
00167         dbg_err_if(u_string_append(s, buf, len));
00168 
00169     *ps = s;
00170 
00171     return 0;
00172 err:
00173     dbg_strerror(errno);
00174     return ~0;
00175 }
00176 
00177 
00187 int u_string_free(u_string_t *s)
00188 {
00189     if(s)
00190     {
00191         if(s->data_sz)
00192             U_FREE(s->data);
00193         U_FREE(s);
00194     }
00195     return 0;
00196 }
00197 
00198 
00210 int u_string_set(u_string_t *s, const char *buf, size_t len)
00211 {
00212     u_string_clear(s);
00213     return u_string_append(s, buf, len);
00214 }
00215 
00227 int u_string_append(u_string_t *s, const char *buf, size_t len)
00228 {
00229     char *ndata;
00230     size_t nsz, min;
00231 
00232     if(!len)
00233         return 0; /* nothing to do */
00234 
00235     /* if there's not enough space on pc->data alloc a bigger buffer */
00236     if(s->data_len + len + 1 > s->data_sz)
00237     {
00238         min = s->data_len + len + 1; /* min required buffer length */
00239         nsz = s->data_sz;
00240         while(nsz <= min)
00241             nsz += (BLOCK_SIZE << s->shift_cnt++);
00242         if(s->data == null)
00243             s->data = NULL;
00244         ndata = (char*) u_realloc(s->data, nsz);
00245         dbg_err_if(ndata == NULL);
00246         s->data = ndata;
00247         s->data_sz = nsz;
00248     }
00249 
00250     /* append this chunk to the data buffer */
00251     strncpy(s->data + s->data_len, buf, len);
00252     s->data_len += len;
00253     s->data[s->data_len] = 0;
00254     
00255     return 0;
00256 err:
00257     return ~0;
00258 }
00259 

←Products
© 2005-2006 - KoanLogic S.r.l. - All rights reserved