atom.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: atom.c,v 1.12 2006/01/09 12:38:38 tat Exp $
00009  */
00010 
00011 #include "klone_conf.h"
00012 #include <u/libu.h>
00013 #include <klone/utils.h>
00014 #include <klone/atom.h>
00015 
00016 int atom_create(const char *id, const char *data, size_t size, void *arg, 
00017     atom_t **patom)
00018 {
00019     atom_t *atom = NULL;
00020 
00021     dbg_err_if (id == NULL);
00022     dbg_err_if (data == NULL);
00023     dbg_err_if (patom == NULL);
00024 
00025     atom = u_zalloc(sizeof(atom_t));
00026     dbg_err_if(atom == NULL);
00027 
00028     atom->id = u_strdup(id);
00029     dbg_err_if(atom->id == NULL);
00030 
00031     atom->data = u_memdup(data, size);
00032     dbg_err_if(atom->data == NULL);
00033 
00034     atom->size = size;
00035     atom->arg = arg;
00036 
00037     *patom = atom;
00038 
00039     return 0;
00040 err:
00041     if(atom)
00042         atom_free(atom);
00043     return ~0;
00044 }
00045 
00046 int atom_free(atom_t *atom)
00047 {
00048     if (atom)
00049     {
00050         U_FREE(atom->id);
00051         U_FREE(atom->data);
00052         U_FREE(atom);
00053     }
00054 
00055     return 0;
00056 }
00057 
00058 /* sum of atoms size field */
00059 size_t atoms_size(atoms_t *as)
00060 {
00061     dbg_err_if (as == NULL);
00062 
00063     return as->size;
00064 err:
00065     return -1;
00066 }
00067 
00068 /* # of atoms */
00069 size_t atoms_count(atoms_t *as)
00070 {
00071     dbg_err_if (as == NULL);
00072 
00073     return as->count;
00074 err:
00075     return -1;
00076 }
00077 
00078 /* return the n-th atom */
00079 int atoms_getn(atoms_t *as, size_t n, atom_t **patom)
00080 {
00081     atom_t *atom;
00082     size_t i = 0;
00083 
00084     dbg_err_if (as == NULL);
00085     dbg_err_if (n >= as->count);
00086     dbg_err_if (patom == NULL);
00087 
00088     LIST_FOREACH(atom, &as->list, np)
00089     {
00090         if(i++ == n)
00091         {
00092             *patom = atom;
00093             return 0;
00094         }
00095     }
00096 
00097 err:
00098     return ~0; /* out of bounds */
00099 }
00100 
00101 /* return the atom whose ID is id */
00102 int atoms_get(atoms_t *as, const char *id, atom_t **patom)
00103 {
00104     atom_t *atom;
00105 
00106     dbg_err_if (as == NULL);
00107     dbg_err_if (id == NULL);
00108     dbg_err_if (patom == NULL);
00109 
00110     LIST_FOREACH(atom, &as->list, np)
00111     {
00112         if(strcmp(id, atom->id) == 0)
00113         {
00114             *patom = atom;
00115             return 0;
00116         }
00117     }
00118 
00119 err:
00120     return ~0; /* not found */
00121 }
00122 
00123 /* add an atom to the list */
00124 int atoms_add(atoms_t *as, atom_t *atom)
00125 {
00126     dbg_err_if (as == NULL);
00127     dbg_err_if (atom == NULL);
00128 
00129     LIST_INSERT_HEAD(&as->list, atom, np);
00130 
00131     as->count++;
00132     as->size += atom->size;
00133 
00134     return 0;
00135 err:
00136     return ~0;
00137 }
00138 
00139 /* add an atom to the list */
00140 int atoms_remove(atoms_t *as, atom_t *atom)
00141 {
00142     dbg_err_if (as == NULL);
00143     dbg_err_if (atom == NULL);
00144 
00145     LIST_REMOVE(atom, np);
00146 
00147     as->count--;
00148     as->size -= atom->size;
00149 
00150     return 0;
00151 err:
00152     return ~0;
00153 
00154 }
00155 
00156 /* create an atom list */
00157 int atoms_create(atoms_t **pas)
00158 {
00159     atoms_t *as = NULL;
00160 
00161     dbg_err_if (pas == NULL);
00162 
00163     as = u_zalloc(sizeof(atoms_t));
00164     dbg_err_if(as == NULL);
00165 
00166     LIST_INIT(&as->list);
00167 
00168     *pas = as;
00169 
00170     return 0;
00171 err:
00172     U_FREE(as);
00173     return ~0;
00174 }
00175 
00176 /* free an atom list */
00177 int atoms_free(atoms_t *as)
00178 {
00179     atom_t *atom;
00180 
00181     if (as)
00182     {
00183         while(atoms_count(as))
00184         {
00185             dbg_err_if(atoms_getn(as, 0, &atom));
00186             dbg_err_if(atoms_remove(as, atom));
00187         }
00188 
00189         U_FREE(as);
00190     }
00191 
00192     return 0;
00193 err:
00194     return ~0;
00195 }

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