configstore.c 6.91 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

7
#include "codes_config.h"
8 9 10 11 12
#include <string.h>
#include <assert.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
13 14 15
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
16
#include "configstore.h"
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33


struct  mcs_entry
{
   const char * name;
   unsigned int is_section;
   struct mcs_entry * next;
   union
   {
      const char        * value;
      struct mcs_entry  * child;
   };
};


static inline mcs_entry * mcs_allocentry ()
{
34
    mcs_entry * n = (mcs_entry *) malloc (sizeof (mcs_entry));
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
   memset (n, 0, sizeof (mcs_entry));
   return n;
}

static inline void mcs_addtail (mcs_entry * e, mcs_entry * newe)
{
   assert (e);
   while (e->next)
   {
      e = e->next;
   }
   e->next = newe;
}

/* Add child to entry */
static void mcs_addchild (mcs_entry * section, mcs_entry * child)
{
   mcs_entry * p;

   p = section->child;
   if (!p)
   {
      section->child = child;
   }
   else
   {
      mcs_addtail (p, child);
   }
}


/* Create empty subsection */
static mcs_entry * mcs_createsection (const char * name)
{
   mcs_entry * n = mcs_allocentry ();
   n->is_section = 1;
   n->name = (name ? strdup (name) : 0);
   return n;
}

/* Create text entry */
static mcs_entry * mcs_createvalue (const char * name, const char ** values,
      unsigned int count)
{
   mcs_entry * n = mcs_allocentry ();
   unsigned int i;

   n->is_section = 0;
   n->next = 0;
   n->name = (name ? strdup (name) : 0);

   for (i=0; i<count; ++i)
   {
      mcs_entry * v = mcs_allocentry ()  ;
      v->is_section = 0;
      v->next = 0;
      v->name = 0;
      v->value = (values[i] ? strdup (values[i]) : 0);
      mcs_addchild (n, v);
   }

   return n;
}

/* Recursively free entry and all following entries and children */
static void mcs_freechain (mcs_entry * t)
{
   if (!t)
      return;

   while (t)
   {
      mcs_entry * old = t;

      free ((void*) t->name);
      if (t->is_section)
      {
         /* assert (!t->value); */ /* t->value and t->child are one...
                                      (union)*/
         mcs_freechain (t->child);
      }
      else
      {
         /* free all child values */
         mcs_entry * v = t->child;
         while (v)
         {
            mcs_entry * valentry = v;
            assert (!v->name);
            free ((void *) v->value);
            v = v->next;
            free (valentry);
         }
      }
      t = t->next;
      free (old);
   }
}

/* Remove (and free) named entry from section */
int mcs_removechild (mcs_entry * section, const char * child)
{
   mcs_entry * e;
   mcs_entry * prev;

   prev = 0;
   e = section->child;

   assert (e->is_section);
   if (!e->child)
      return 0;


   while (e)
   {
      if (!strcmp (child, e->name))
      {
         /* found entry */
         if (!prev)
         {
            /* entry is first child */
            section->child = e->next;
         }
         else
         {
            prev->next = e->next;
         }

         /* isolate e so we don't free the whole chain */
         e->next = 0;
         mcs_freechain (e);
         return 1;
      }
      e = e->next;
   }
   return 0;
}

/* Free (recursively) section */
int mcs_freeroot (mcs_entry * section)
{
   mcs_freechain (section);
   return 1;
}

static unsigned int mcs_chaincount (const mcs_entry * e)
{
   unsigned int ret = 0;

   while (e)
   {
      ++ret;
      e = e->next;
   }

   return ret;
}

/* ============= high level functions ======================== */

/* Return empty tree */
mcs_entry * mcs_initroot ()
{
   return mcs_createsection (0);
}
/* Add child section to section; returns child section */
mcs_entry * mcs_addsection (mcs_entry * e, const char * name)
{
   mcs_entry * newsec = mcs_createsection (name);
   mcs_addchild (e, newsec);
   return newsec;
}

/* Add (multi-)key to section */
mcs_entry * mcs_addkey (mcs_entry * e, const char * name, const char **
      values, unsigned int count)
{
   mcs_entry * newkey = mcs_createvalue (name, values, count);
   mcs_addchild (e, newkey);
   return newkey;
}


/* Returns true if entry is a subsection */
int mcs_issection (const mcs_entry * e)
{
   return e->is_section;
}

/* Returns number of children in section */
int mcs_childcount (const mcs_entry * e)
{
   assert (e->is_section);
   return mcs_chaincount (e->child);
}

/* Return the number of values in the key */
int mcs_valuecount (const mcs_entry * e)
{
   assert (!e->is_section);
   return mcs_chaincount (e->child);
}

/*
 * If buf & bufsize == 0, returns the length of the key value (without
 * terminating 0) or < 0 if error.
 * Returns len of key value without ending '\0'
 */
int mcs_getvaluesingle (const mcs_entry * e, char * buf, unsigned int bufsize)
{
   if (e->is_section)
      return MCS_WRONGTYPE;

   if (buf)
      *buf = 0;

   if (!e->child)
      return -1;

   if (!e->child->value)
      return 0;

   if (bufsize)
   {
      strncpy (buf, e->child->value, bufsize-1);
      buf[bufsize-1] = 0;
   }


   return strlen (e->child->value);
}

/* Retrieve the values for this key */
int mcs_getvaluemultiple (const mcs_entry * e, char ** buf, unsigned int * maxcount)
{
   unsigned int i = 0;
   mcs_entry * t = e->child;

   assert (!e->is_section);

   while (t)
   {
      if (i == *maxcount)
         return 0;

      assert (!t->name);

      buf[i] = (t->value ? strdup (t->value) : 0);
      ++i;
      t = t->next;
   }
   *maxcount = i;
   return 1;
}

/* Move to the next entry on this level */
mcs_entry * mcs_next (const mcs_entry * e)
{
   return e->next;
}

/* Move to the first child of this section */
mcs_entry * mcs_child (const mcs_entry * e)
{
   assert (e->is_section);
   return e->child;
}

/* Look for a child node with the specified name */
static mcs_entry * mcs_findchild (const mcs_entry * e, const char * name)
{
   mcs_entry * curchild = e->child;

   while (curchild)
   {
      if (!strcmp (curchild->name, name))
         break;
      curchild = curchild->next;
   }
   return curchild;
}


mcs_entry * mcs_findsubsection (const mcs_entry * e, const char * name)
{
   mcs_entry * ret = mcs_findchild (e, name);
   if (!ret)
      return 0;
   if (!ret->is_section)
      return 0;
   return ret;
}

mcs_entry * mcs_findkey (const mcs_entry * e, const char * name)
{
   mcs_entry * ret = mcs_findchild (e, name);
   if (!ret)
      return 0;
   if (ret->is_section)
      return 0;
   return ret;
}

int mcs_listsection (const mcs_entry * e, mcs_section_entry * out, unsigned int maxcount)
{
   const mcs_entry * cur;
   unsigned int i = 0;

   if (!e)
      return 0;
   if (!e->is_section)
      return -1;

   cur = e->child;

   i=0;
   while (cur && i < maxcount)
   {
      out[i].name = (cur->name ? strdup (cur->name) : 0);
      out[i].is_section = cur->is_section;
      out[i].childcount = mcs_chaincount (cur->child);
      ++i;
      cur = cur->next;
   }
   return i;
}
Philip Carns's avatar
Philip Carns committed
361 362 363 364 365 366 367 368 369

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */