taskrambler  0.1.8
Web server and task management solution.
config.c File Reference
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include "class.h"
#include "config/config.h"
#include "config/value.h"
#include "utils/memory.h"
+ Include dependency graph for config.c:

Go to the source code of this file.

Functions

static int configCtor (void *_this, va_list *params)
 
static void configDtor (void *_this)
 
 INIT_IFACE (Class, configCtor, configDtor, NULL)
 
 CREATE_CLASS (Config, NULL, IFACE(Class))
 

Detailed Description

Author
Georg Hopp

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file config.c.

Function Documentation

static int configCtor ( void *  _this,
va_list *  params 
)
static

Definition at line 36 of file config.c.

References config, hashAdd(), MAX_CONFIG_LINE, MEM_FREE, and memMalloc().

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 }
#define MEM_FREE(seg)
Definition: memory.h:28
#define MAX_CONFIG_LINE
Definition: config.h:30
void * memMalloc(size_t)
Definition: memory.c:783
Config config
Definition: taskrambler.c:67
void * hashAdd(Hash, void *)
Definition: add.c:48

+ Here is the call graph for this function:

static void configDtor ( void *  _this)
static

Definition at line 111 of file config.c.

References config, and MEM_FREE.

112 {
113  Config this = _this;
114 
115  MEM_FREE(this->cnf_file);
116  delete(this->config);
117 }
#define MEM_FREE(seg)
Definition: memory.h:28
Config config
Definition: taskrambler.c:67
CREATE_CLASS ( Config  ,
NULL  ,
IFACE(Class)   
)
INIT_IFACE ( Class  ,
configCtor  ,
configDtor  ,
NULL   
)