taskrambler  0.1.9
Web server and task management solution.
config.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 <string.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <sys/types.h>
28 
29 #include "class.h"
30 #include "config/config.h"
31 #include "config/value.h"
32 #include "utils/memory.h"
33 
34 static
35 int
36 configCtor(void * _this, va_list * params)
37 {
38  Config this = _this;
39 
40  FILE * handle;
41  char line[MAX_CONFIG_LINE];
42  char * cnf_file = va_arg(*params, char *);
43  size_t ncnf_file = strlen(cnf_file);
44 
45  this->cnf_file = memMalloc(ncnf_file + 1);
46  memcpy(this->cnf_file, cnf_file, ncnf_file);
47  this->cnf_file[ncnf_file] = '\0';
48 
49  handle = fopen(this->cnf_file, "r");
50  if (NULL == handle) {
51  MEM_FREE(this->cnf_file);
52  return -1;
53  }
54 
55  this->config = new(Hash);
56 
57  line[MAX_CONFIG_LINE] = '\0';
58 
59  while(NULL != fgets(line, MAX_CONFIG_LINE, handle)) {
60  char * key = line;
61  size_t nkey = 0;
62  size_t nvalue = 0;
63  size_t nspaces = 0;
64  char * value;
65 
66  while (isspace(*key)) {
67  key++;
68  }
69 
70  if ('#' == *key || 0 == *key) {
71  continue;
72  }
73 
74  while (! isspace(key[nkey])) {
75  nkey++;
76  }
77 
78  value = &(key[nkey+1]);
79  while (isspace(*value)) {
80  value++;
81  }
82 
83  while ('\0' != value[nvalue+nspaces]
84  && '\n' != value[nvalue+nspaces])
85  {
86  if (isspace(value[nvalue+nspaces])) {
87  nspaces++;
88  } else {
89  if (0 != nspaces) {
90  nvalue += nspaces;
91  nspaces = 0;
92  }
93  nvalue++;
94  }
95  }
96 
97  value[nvalue] = '\0';
98 
99  if (0 != nkey && 0 != nvalue) {
100  hashAdd(
101  this->config,
102  new(ConfigValue, key, nkey, value, nvalue));
103  }
104  }
105 
106  fclose(handle);
107 
108  return 0;
109 }
110 
111 static void configDtor(void * _this)
112 {
113  Config this = _this;
114 
115  MEM_FREE(this->cnf_file);
116  delete(this->config);
117 }
118 
119 INIT_IFACE(Class, configCtor, configDtor, NULL);
120 CREATE_CLASS(Config, NULL, IFACE(Class));
121 
122 // vim: set ts=4 sw=4:
#define MEM_FREE(seg)
Definition: memory.h:28
static int configCtor(void *_this, va_list *params)
Definition: config.c:36
#define MAX_CONFIG_LINE
Definition: config.h:30
INIT_IFACE(Class, configCtor, configDtor, NULL)
void * memMalloc(size_t)
Definition: memory.c:783
#define IFACE(name)
Definition: interface.h:34
static void configDtor(void *_this)
Definition: config.c:111
CREATE_CLASS(Config, NULL, IFACE(Class))
Config config
Definition: taskrambler.c:67
void * hashAdd(Hash, void *)
Definition: add.c:48