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

var.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005, 2006 by KoanLogic s.r.l. <http://www.koanlogic.com>
00003  * All rights reserved.
00004  *
00005  * This file is part of KLone, and as such it is subject to the license stated
00006  * in the LICENSE file which you have received as part of this distribution.
00007  *
00008  * $Id: var.c,v 1.15 2006/01/09 12:38:38 tat Exp $
00009  */
00010 
00011 #include "klone_conf.h"
00012 #include <sys/types.h>
00013 #include <stdlib.h>
00014 #include <u/libu.h>
00015 #include <klone/var.h>
00016 #include <klone/utils.h>
00017 #include <klone/varprv.h>
00018 
00034 u_string_t *var_get_name_s(var_t *v)
00035 {
00036     dbg_return_if (v == NULL, NULL);
00037 
00038     return v->sname; /* may be NULL */
00039 }
00040 
00050 u_string_t *var_get_value_s(var_t *v)
00051 {
00052     dbg_return_if (v == NULL, NULL);
00053 
00054     if(v->svalue == NULL)
00055         dbg_err_if(u_string_create(v->data, v->size, &v->svalue));
00056     
00057     return v->svalue;
00058 err:
00059     return NULL;
00060 }
00061 
00062 void var_set_opaque(var_t *v, void *opaque)
00063 {
00064     v->opaque = opaque;
00065 }
00066 
00067 void* var_get_opaque(var_t *v)
00068 {
00069     return v->opaque;
00070 }
00071 
00072 int var_bin_create(const char *name, const char *data, size_t size, var_t **pv)
00073 {
00074     var_t *v = NULL;
00075 
00076     dbg_return_if (name == NULL, ~0);
00077     dbg_return_if (data == NULL, ~0);
00078     dbg_return_if (pv == NULL, ~0);
00079 
00080     v = u_zalloc(sizeof(var_t));
00081     dbg_err_if(v == NULL);
00082 
00083     dbg_err_if(u_string_create(name, strlen(name), &v->sname));
00084 
00085     dbg_err_if(var_set_bin_value(v, data, size));
00086 
00087     *pv = v;
00088 
00089     return 0;
00090 err:
00091     if(v)
00092         var_free(v);
00093     return ~0;
00094 }
00095 
00096 int var_create(const char* name, const char *value, var_t**pv)
00097 {
00098     dbg_return_if (name == NULL, ~0);
00099     dbg_return_if (value == NULL, ~0);
00100 
00101     return var_bin_create(name, value, strlen(value), pv);
00102 }
00103 
00104 /*
00105  * \brief   Free a variable
00106  *
00107  * \return \c 0, always
00108  */
00109 int var_free(var_t *v)
00110 {
00111     if(v)
00112     {
00113         if(v->sname)
00114             u_string_free(v->sname);
00115 
00116         if(v->svalue)
00117             u_string_free(v->svalue);
00118 
00119         if(v->opaque)
00120             U_FREE(v->opaque);
00121 
00122         U_FREE(v->data);
00123         U_FREE(v);
00124     }
00125 
00126     return 0;
00127 }
00128 
00138 const char *var_get_name(var_t *v)
00139 {
00140     dbg_return_if (v == NULL, NULL);
00141 
00142     return u_string_c(v->sname);
00143 }
00144 
00154 const char *var_get_value(var_t *v)
00155 {
00156     dbg_return_if (v == NULL, NULL);
00157 
00158     return v->data;
00159 }
00160 
00170 size_t var_get_value_size(var_t *v)
00171 {
00172     dbg_return_if (v == NULL, 0);   /* XXX should be (ssize_t) '-1' */
00173 
00174     return v->size;
00175 }
00176 
00188 int var_set(var_t *var, const char *name, const char *value)
00189 {
00190     dbg_err_if (var == NULL);
00191     dbg_err_if (name == NULL);
00192     dbg_err_if (value == NULL);
00193     
00194     dbg_err_if(var_set_name(var, name));
00195     dbg_err_if(var_set_value(var, value));
00196 
00197     return 0;
00198 err:
00199     return ~0;
00200 }
00201 
00212 int var_set_name(var_t *v, const char *name)
00213 {
00214     dbg_err_if (v == NULL);
00215     dbg_err_if (name == NULL);
00216 
00217     dbg_err_if(u_string_set(v->sname, name, strlen(name)));
00218 
00219     return 0; 
00220 err:
00221     return ~0;
00222 }
00223 
00224 int var_set_value(var_t *v, const char *value)
00225 {
00226     dbg_return_if (v == NULL, ~0);
00227     dbg_return_if (value == NULL, ~0);
00228 
00229     /* copy the string and the trailing '\0' */
00230     return var_set_bin_value(v, value, strlen(value) + 1);
00231 }
00232 
00244 int var_set_bin_value(var_t *v, const char *data, size_t size)
00245 {
00246     dbg_err_if (v == NULL);
00247     dbg_err_if (data == NULL);
00248     
00249     U_FREE(v->data);
00250 
00251     if(data && size)
00252     {
00253         v->size = size;
00254         v->data = u_malloc(size+1);
00255         dbg_err_if(v->data == NULL);
00256 
00257         memcpy(v->data, data, size);
00258         v->data[size] = 0; /* zero-term v->data so it can be used as a string */
00259     } else {
00260         v->size = 0;
00261         v->data = NULL;
00262     }
00263 
00264     if(v->svalue)
00265         dbg_err_if(u_string_set(v->svalue, v->data, v->size));
00266 
00267     return 0; 
00268 err:
00269     return ~0;
00270 }
00271 

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