configstoreadapter.c 5.07 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 13 14 15 16 17 18 19
#include <assert.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#include <string.h>
#include "configstoreadapter.h"
#include "configstore.h"

Jonathan Jenkins's avatar
Jonathan Jenkins committed
20 21 22 23 24 25 26 27 28 29
/* unused attribute is only used here, so use directly in source */
#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif /* UNUSED */

30 31 32 33 34 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
static int cfsa_getKey (void *  handle, SectionHandle section, const char * name,
      char * buf, size_t bufsize)
{
   mcs_entry * key = mcs_findkey ((mcs_entry*) (section ? section : handle), name);
   int count;
   char * tmp;
   unsigned int dcount;
   int ret = 1;

   if (!key)
      return -1;


   count = mcs_valuecount (key);
   
   if (count < 0)
      return count;

   /* error because key is multival */
   if (count > 1)
      return -2;

   /* if bufsize == 0 the user only wants to know the size and so we
    * ignore buf */
   if (bufsize == 0)
   {
      return mcs_getvaluesingle (key, 0, 0);
   }


   dcount = 1; 
   mcs_getvaluemultiple (key, &tmp, &dcount);

   if (!dcount)
   {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
65
      assert(buf);
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
      *buf = 0;
      ret = 0;
      /* tmp was not modified no need to free */
   }
   else
   {
      ret = strlen (tmp);
      strncpy (buf, tmp, bufsize);
      if (bufsize > 0)
         buf[bufsize-1]=0;
      free (tmp);
   }
   return ret;
}

static int cfsa_getMultiKey (void * handle, SectionHandle section, const char *name,
      char *** buf, size_t * e)
{
   mcs_entry * key = mcs_findkey ((mcs_entry*) (section ? section : handle), 
         name);
   int count; 
   unsigned int dcount;

   *e = 0; 
   if (!key)
      return -1;

   count = mcs_valuecount (key);

   if (count < 0)
      return -2;

98
   *buf = (char **) malloc (sizeof (char **) * count);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
   dcount = count;
   mcs_getvaluemultiple (key, *buf, &dcount);
   *e = dcount;

   return 1;
}

static int cfsa_listSection (void * handle, SectionHandle section, 
      SectionEntry * entries, size_t * maxentries)
{
   mcs_section_entry * out;
   int count;
   int ret = 0;
   int i;

   if (!section)
      section = handle;


118
   count = mcs_childcount ((mcs_entry *)section);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
   if (count < 0)
   {
      *maxentries = 0;
      return count;
   }
   

   if (count == 0)
   {
      *maxentries = 0;
      return count;
   }

   ret = count;
   
134
   out = (mcs_section_entry*) malloc (sizeof(mcs_section_entry)* *maxentries);
135

136
   count = mcs_listsection ((mcs_entry*)section, (mcs_section_entry*)out,  *maxentries);
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
   if (count < 0)
   {
      *maxentries = 0;
      ret = count;
   }
   else
   {
      for (i=0; i<count; ++i)
      {
         entries[i].name = out[i].name;
         if (out[i].is_section)
         {
            entries[i].type = SE_SECTION;
         }
         else
         {
            entries[i].type = (out[i].childcount <= 1 ? SE_KEY : SE_MULTIKEY);
         }
      }
      *maxentries = count;
   }
   free (out);

   return ret;
}

static int cfsa_openSection (void * handle, SectionHandle section, const char *
      sectionname, SectionHandle * newsection)
{
   mcs_entry * e = mcs_findsubsection ((mcs_entry *) (section ? section : handle),
         sectionname);
   *newsection = e;
   return (e != 0 ? 1 : -1);
}

static int cfsa_closeSection (void * UNUSED(handle), SectionHandle UNUSED(section))
{
   return 1;
}

static int cfsa_createSection (void * handle, SectionHandle section, const
      char * name, SectionHandle * newsection)
{
   mcs_entry * news = mcs_addsection ( (mcs_entry*) (section ? section : handle),
         name);
   *newsection = news;
   return (news != 0);
}

static int cfsa_createKey (void * handle, SectionHandle section, const char * key,
      const char ** data, unsigned int count)
{
   return (mcs_addkey ((mcs_entry *) (section ? section : handle), key, data, count) != 0 ? 1 : -1);
}

static void cfsa_free (void * handle)
{
   mcs_freeroot ((mcs_entry*) handle);
}

static int cfsa_getSectionSize (void * handle, SectionHandle section,
      unsigned int * count)
{
   int c = mcs_childcount ((mcs_entry *) (section ? section : handle));
   if (c >= 0)
   {
      *count = c;
      return 1;
   }
   else
   {
      return c;
   }
}



214
static struct ConfigVTable cfsa_template = {
215 216 217 218 219 220 221 222 223 224 225 226
   .getKey = cfsa_getKey,
   .getMultiKey = cfsa_getMultiKey,
   .listSection = cfsa_listSection,
   .openSection = cfsa_openSection,
   .getSectionSize = cfsa_getSectionSize,
   .closeSection = cfsa_closeSection,
   .createSection = cfsa_createSection,
   .createKey = cfsa_createKey,
   .free = cfsa_free,
   .data = 0
};

227
struct ConfigVTable * cfsa_create (mcs_entry * e)
228
{
Elsa Gonsiorowski (Uranus)'s avatar
Elsa Gonsiorowski (Uranus) committed
229
    struct ConfigVTable * newh = (struct ConfigVTable *) malloc (sizeof (struct ConfigVTable));
230 231 232 233 234
   *newh = cfsa_template;
   newh->data = e;
   return newh; 
}

235
struct ConfigVTable * cfsa_create_empty ()
236 237 238
{
   return cfsa_create (mcs_initroot ());
}
Philip Carns's avatar
Philip Carns committed
239 240 241 242 243 244 245 246 247

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