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

field.c

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: field.c,v 1.14 2007/10/26 08:57:59 tho Exp $
00009  */
00010 
00011 #include "klone_conf.h"
00012 #include <string.h>
00013 #include <klone/field.h>
00014 #include <klone/utils.h>
00015 #include <u/libu.h>
00016 
00017 int field_set(field_t*, const char *name, const char *value);
00018 int field_set_from_line(field_t*, const char *line);
00019 const char* field_get_name(field_t *f);
00020 const char* field_get_value(field_t *f);
00021 
00034 int field_set(field_t *f, const char *name, const char *value)
00035 {
00036     char *n = NULL, *v = NULL;
00037 
00038     dbg_err_if (f == NULL);
00039     dbg_err_if (name == NULL);
00040     dbg_err_if (value == NULL);
00041 
00042     n = u_strdup(name);
00043     dbg_err_if(n == NULL);
00044 
00045     v = u_strdup(value);
00046     dbg_err_if(v == NULL);
00047 
00048     U_FREE(f->name);
00049     U_FREE(f->value);
00050 
00051     f->name = n;
00052     f->value = v;
00053 
00054     return 0;
00055 err:
00056     U_FREE(n);
00057     U_FREE(v);
00058 
00059     return ~0;
00060 }
00061 
00074 int field_set_from_line(field_t *f, const char *ln)
00075 {
00076     enum { BUFSZ = 256 };
00077     char *p, *name = NULL;
00078 
00079     dbg_err_if (f == NULL);
00080     dbg_err_if (ln == NULL);
00081     dbg_err_if (!strlen(ln));
00082 
00083     dbg_err_if((p = strchr(ln, ':')) == NULL);
00084 
00085     name = u_strndup(ln, p-ln);
00086     dbg_err_if(name == NULL);
00087 
00088     /* eat blanks between ':' and value */
00089     for(++p; u_isblank(*p); ++p)
00090         ;
00091 
00092     dbg_err_if(field_set(f, name, p));
00093 
00094     U_FREE(name);
00095 
00096     return 0;
00097 err:
00098     U_FREE(name);
00099     return ~0;
00100 }
00101 
00112 const char* field_get_name(field_t *f)
00113 {
00114     dbg_return_if (f == NULL, NULL);
00115 
00116     return f->name; /* may be null */
00117 }
00118 
00129 const char* field_get_value(field_t *f)
00130 {
00131     dbg_return_if (f == NULL, NULL);
00132 
00133     return f->value; /* may be null */
00134 }
00135 
00148 int field_create(const char *name, const char *value, field_t **pf)
00149 {
00150     field_t *f = NULL;
00151 
00152     /* name and value may be NULL */
00153     dbg_err_if (pf == NULL);
00154 
00155     f = u_zalloc(sizeof(field_t));
00156     dbg_err_if(f == NULL);
00157 
00158     if(name)
00159         dbg_err_if((f->name = u_strdup(name)) == NULL);
00160 
00161     if(value)
00162         dbg_err_if((f->value = u_strdup(value)) == NULL);
00163 
00164     *pf = f;
00165 
00166     return 0;
00167 err:
00168     if(f)
00169         field_free(f);
00170     return ~0;
00171 }
00172 
00183 int field_free(field_t *f)
00184 {
00185     if(f)
00186     {
00187         U_FREE(f->name);
00188         U_FREE(f->value);
00189         U_FREE(f);
00190     }
00191 
00192     return 0;
00193 }