var.c

00001 /*
00002  * Copyright (c) 2005-2012 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.20 2008/05/16 15:04:47 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 
00029 u_string_t *var_get_name_s(var_t *v)
00030 {
00031     dbg_return_if (v == NULL, NULL);
00032 
00033     return v->sname; /* may be NULL */
00034 }
00035 
00046 u_string_t *var_get_value_s(var_t *v)
00047 {
00048     dbg_return_if (v == NULL, NULL);
00049 
00050     if(v->svalue == NULL)
00051         dbg_err_if(u_string_create(v->data, v->size, &v->svalue));
00052     
00053     return v->svalue;
00054 err:
00055     return NULL;
00056 }
00057 
00058 void var_set_opaque(var_t *v, void *opaque)
00059 {
00060     v->opaque = opaque;
00061 }
00062 
00063 void* var_get_opaque(var_t *v)
00064 {
00065     return v->opaque;
00066 }
00067 
00068 int var_bin_create(const char *name, const unsigned char *data, size_t size, 
00069         var_t **pv)
00070 {
00071     var_t *v = NULL;
00072 
00073     dbg_return_if (name == NULL, ~0);
00074     dbg_return_if (data == NULL, ~0);
00075     dbg_return_if (pv == NULL, ~0);
00076 
00077     v = u_zalloc(sizeof(var_t));
00078     dbg_err_if(v == NULL);
00079 
00080     dbg_err_if(u_string_create(name, strlen(name), &v->sname));
00081 
00082     dbg_err_if(var_set_bin_value(v, data, size));
00083 
00084     *pv = v;
00085 
00086     return 0;
00087 err:
00088     if(v)
00089         var_free(v);
00090     return ~0;
00091 }
00092 
00093 int var_create(const char* name, const char *value, var_t**pv)
00094 {
00095     dbg_return_if (name == NULL, ~0);
00096     dbg_return_if (value == NULL, ~0);
00097 
00098     return var_bin_create(name, (const unsigned char *) value, 
00099             1 + strlen(value), pv);
00100 }
00101 
00102 /*
00103  * \ingroup vars
00104  * \brief   Free a variable
00105  *
00106  * \return \c 0, always
00107  */
00108 int var_free(var_t *v)
00109 {
00110     if(v)
00111     {
00112         if(v->sname)
00113             u_string_free(v->sname);
00114 
00115         if(v->svalue)
00116             u_string_free(v->svalue);
00117 
00118         if(v->opaque)
00119             U_FREE(v->opaque);
00120 
00121         U_FREE(v->data);
00122         U_FREE(v);
00123     }
00124 
00125     return 0;
00126 }
00127 
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 
00155 const char *var_get_value(var_t *v)
00156 {
00157     dbg_return_if (v == NULL, NULL);
00158 
00159     return v->data;
00160 }
00161 
00172 size_t var_get_value_size(var_t *v)
00173 {
00174     dbg_return_if (v == NULL, 0);   /* XXX should be (ssize_t) '-1' */
00175 
00176     return v->size;
00177 }
00178 
00191 int var_set(var_t *var, const char *name, const char *value)
00192 {
00193     dbg_err_if (var == NULL);
00194     dbg_err_if (name == NULL);
00195     dbg_err_if (value == NULL);
00196     
00197     dbg_err_if(var_set_name(var, name));
00198     dbg_err_if(var_set_value(var, value));
00199 
00200     return 0;
00201 err:
00202     return ~0;
00203 }
00204 
00216 int var_set_name(var_t *v, const char *name)
00217 {
00218     dbg_err_if (v == NULL);
00219     dbg_err_if (name == NULL);
00220 
00221     dbg_err_if(u_string_set(v->sname, name, strlen(name)));
00222 
00223     return 0; 
00224 err:
00225     return ~0;
00226 }
00227 
00239 int var_set_value(var_t *v, const char *value)
00240 {
00241     dbg_return_if (v == NULL, ~0);
00242     dbg_return_if (value == NULL, ~0);
00243 
00244     /* copy the string and the trailing '\0' */
00245     return var_set_bin_value(v, (const unsigned char *) value, 
00246             1 + strlen(value));
00247 }
00248 
00261 int var_set_bin_value(var_t *v, const unsigned char *data, size_t size)
00262 {
00263     dbg_err_if (v == NULL);
00264     dbg_err_if (data == NULL);
00265     
00266     U_FREE(v->data);
00267 
00268     if(data && size)
00269     {
00270         v->size = size;
00271         v->data = u_malloc(size+1);
00272         dbg_err_if(v->data == NULL);
00273 
00274         memcpy(v->data, data, size);
00275         v->data[size] = 0; /* zero-term v->data so it can be used as a string */
00276     } else {
00277         v->size = 0;
00278         v->data = NULL;
00279     }
00280 
00281     if(v->svalue)
00282         dbg_err_if(u_string_set(v->svalue, v->data, v->size));
00283 
00284     return 0; 
00285 err:
00286     return ~0;
00287 }

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