taskrambler  0.1.9
Web server and task management solution.
config/value.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 
28 #include "class.h"
29 #include "config/value.h"
30 #include "utils/memory.h"
31 #include "utils/hash.h"
32 
33 static
34 int
35 configValueCtor(void * _this, va_list * params)
36 {
37  ConfigValue this = _this;
38 
39  char * key = va_arg(*params, char *);
40  size_t nkey = va_arg(*params, size_t);
41  char * value = va_arg(*params, char *);
42  size_t nvalue = va_arg(*params, size_t);
43 
44  this->hash = sdbm((unsigned char *)key, nkey);
45 
46  /**
47  * if we find the value enclosed by single or double
48  * quotes we take it as a string, else we take it
49  * as a numeric which is treated as a long long
50  * right now.
51  */
52  if (('"' == value[0] && '"' == value[nvalue-1])
53  || ('\'' == value[0] && '\'' == value[nvalue-1]))
54  {
55  this->type = CONFIG_VALUE_STRING;
56  (this->value).string = memMalloc(nvalue-1);
57  (this->value).string[nvalue-2] = '\0';
58  memcpy((this->value).string, value+1, nvalue-2);
59  this->nvalue = nvalue;
60  } else {
61  this->type = CONFIG_VALUE_NUMERIC;
62  (this->value).number = atoll(value);
63  this->nvalue = sizeof(long long);
64  }
65 
66  return 0;
67 }
68 
69 static
70 void
71 configValueDtor(void * _this)
72 {
73  ConfigValue this = _this;
74 
75  if (CONFIG_VALUE_STRING == this->type) {
76  MEM_FREE((this->value).string);
77  }
78 }
79 
80 static
81 unsigned long
82 configValueGetHash(void * _this)
83 {
84  ConfigValue this = _this;
85 
86  return this->hash;
87 }
88 
89 static
90 void
91 configValueHandleDouble(void * _this, void * _double)
92 {
93  /* right now I do nothing...but I could :D */
94 }
95 
98 CREATE_CLASS(ConfigValue, NULL, IFACE(Class), IFACE(Hashable));
99 
100 // vim: set ts=4 sw=4:
#define MEM_FREE(seg)
Definition: memory.h:28
INIT_IFACE(Class, configValueCtor, configValueDtor, NULL)
static int configValueCtor(void *_this, va_list *params)
Definition: config/value.c:35
unsigned long sdbm(const unsigned char *, size_t)
Definition: utils/hash.c:45
static void configValueDtor(void *_this)
Definition: config/value.c:71
void * memMalloc(size_t)
Definition: memory.c:783
static void configValueHandleDouble(void *_this, void *_double)
Definition: config/value.c:91
#define IFACE(name)
Definition: interface.h:34
CREATE_CLASS(ConfigValue, NULL, IFACE(Class), IFACE(Hashable))
static unsigned long configValueGetHash(void *_this)
Definition: config/value.c:82