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

field.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: field.c,v 1.11 2006/01/09 12:38:38 tat 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 
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 
00073 int field_set_from_line(field_t *f, const char *ln)
00074 {
00075     enum { BUFSZ = 256 };
00076     char *p, *name = NULL;
00077 
00078     dbg_err_if (f == NULL);
00079     dbg_err_if (ln == NULL);
00080     dbg_err_if (!strlen(ln));
00081 
00082     dbg_err_if((p = strchr(ln, ':')) == NULL);
00083 
00084     name = u_strndup(ln, p-ln);
00085     dbg_err_if(name == NULL);
00086 
00087     /* eat blanks between ':' and value */
00088     for(++p; u_isblank(*p); ++p)
00089         ;
00090 
00091     dbg_err_if(field_set(f, name, p));
00092 
00093     U_FREE(name);
00094 
00095     return 0;
00096 err:
00097     U_FREE(name);
00098     return ~0;
00099 }
00100 
00110 const char* field_get_name(field_t *f)
00111 {
00112     dbg_return_if (f == NULL, NULL);
00113 
00114     return f->name; /* may be null */
00115 }
00116 
00126 const char* field_get_value(field_t *f)
00127 {
00128     dbg_return_if (f == NULL, NULL);
00129 
00130     return f->value; /* may be null */
00131 }
00132 
00144 int field_create(const char *name, const char *value, field_t **pf)
00145 {
00146     field_t *f = NULL;
00147 
00148     /* name and value may be NULL */
00149     dbg_err_if (pf == NULL);
00150 
00151     f = u_zalloc(sizeof(field_t));
00152     dbg_err_if(f == NULL);
00153 
00154     if(name)
00155         dbg_err_if((f->name = u_strdup(name)) == NULL);
00156 
00157     if(value)
00158         dbg_err_if((f->value = u_strdup(value)) == NULL);
00159 
00160     *pf = f;
00161 
00162     return 0;
00163 err:
00164     if(f)
00165         field_free(f);
00166     return ~0;
00167 }
00168 
00178 int field_free(field_t *f)
00179 {
00180     if(f)
00181     {
00182         U_FREE(f->name);
00183         U_FREE(f->value);
00184         U_FREE(f);
00185     }
00186 
00187     return 0;
00188 }
00189 

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