2005-11-18 08:25:01 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2005-12-06 15:36:31 +00:00
|
|
|
#include <stdio.h>
|
2005-11-18 08:25:01 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "cfg.h"
|
|
|
|
#include "dc.h"
|
|
|
|
|
|
|
|
/* Private functions */
|
|
|
|
static char * dc_cfg_cutLeadingWhiteSpace(char *line);
|
|
|
|
void dc_cfg_cutTrailingWhiteSpace(char *line);
|
2005-12-06 15:36:31 +00:00
|
|
|
static int tokenise(char *line, char *separators, char *tokens[], int max);
|
2005-11-18 08:25:01 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
} NameValuePair;
|
|
|
|
|
|
|
|
static NameValuePair *pairs;
|
|
|
|
static int n_pairs;
|
|
|
|
|
|
|
|
/* Public functions */
|
|
|
|
int dc_cfg_parse(const char *cfgfile)
|
|
|
|
{
|
|
|
|
FILE *cfg;
|
|
|
|
char line[1024];
|
|
|
|
char *str;
|
|
|
|
# define MAX_TOKEN 3
|
|
|
|
char *tokens[MAX_TOKEN];
|
|
|
|
int tokenCount;
|
|
|
|
|
|
|
|
if ((cfg = fopen(cfgfile, "r")) == NULL) {
|
2005-12-06 15:36:31 +00:00
|
|
|
fprintf(stderr, "Config file %s cannot be opened\nErrno=%d %s",
|
2005-11-18 08:25:01 +00:00
|
|
|
cfgfile, errno, strerror(errno));
|
|
|
|
return DC_CFG_FILENOTEXISTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(line, 1024, cfg) != NULL) {
|
|
|
|
str = dc_cfg_cutLeadingWhiteSpace(line);
|
|
|
|
|
|
|
|
if (str[0] == '\0' || str[0] == '\n') continue; /* empty */
|
|
|
|
if (str[0] == '#') continue; /* comment */
|
|
|
|
|
2005-12-06 15:36:31 +00:00
|
|
|
tokenCount = tokenise(line, "=\n", tokens, MAX_TOKEN);
|
2005-11-18 08:25:01 +00:00
|
|
|
if (tokenCount == 2) {
|
|
|
|
NameValuePair *tmp;
|
|
|
|
|
|
|
|
tmp = realloc(pairs, (n_pairs + 1) * sizeof(*tmp));
|
|
|
|
if (!tmp) {
|
2005-12-06 15:36:31 +00:00
|
|
|
fprintf(stderr, "Out of memory while parsing config file\n");
|
2005-11-18 08:25:01 +00:00
|
|
|
return DC_CFG_OUTOFMEM;
|
|
|
|
}
|
|
|
|
pairs = tmp;
|
|
|
|
pairs[n_pairs].name = strdup(tokens[0]);
|
|
|
|
pairs[n_pairs].value = strdup(tokens[1]);
|
|
|
|
n_pairs++;
|
|
|
|
}
|
|
|
|
else {
|
2005-12-06 15:36:31 +00:00
|
|
|
fprintf(stderr,
|
2005-11-18 08:25:01 +00:00
|
|
|
"Cannot understand in config file %s the line %s",
|
|
|
|
cfgfile, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return DC_CFG_OK;
|
|
|
|
}
|
|
|
|
|
2005-12-06 15:36:31 +00:00
|
|
|
char *dc_cfg_get(char *name)
|
2005-11-18 08:25:01 +00:00
|
|
|
{
|
|
|
|
/* Return the value of the name=value pair */
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (name == NULL) return NULL;
|
|
|
|
|
|
|
|
while (i < n_pairs) {
|
|
|
|
if (pairs[i].name != NULL &&
|
|
|
|
!strcmp(pairs[i].name, name))
|
|
|
|
return pairs[i].value;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-12-06 15:36:31 +00:00
|
|
|
static char *dc_cfg_cutLeadingWhiteSpace(char *line)
|
2005-11-18 08:25:01 +00:00
|
|
|
{
|
|
|
|
/* go until white spaces are in front of the string */
|
|
|
|
char *str = line;
|
|
|
|
while (str[0] == ' ' || str[0] == '\t') str++;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dc_cfg_cutTrailingWhiteSpace(char *line)
|
|
|
|
{
|
|
|
|
/* go until white spaces are at the end of the string */
|
|
|
|
char *str = line;
|
|
|
|
int len = strlen(line);
|
|
|
|
if (len > 0) {
|
|
|
|
str = line+len-1;
|
|
|
|
while (len > 0 &&
|
|
|
|
(str[0] == ' ' || str[0] == '\t')) {
|
|
|
|
str[0]='\0';
|
|
|
|
str--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* break string into tokens and save tokens in arrays */
|
2005-12-06 15:36:31 +00:00
|
|
|
static int tokenise(char *line, char *separators, char *tokens[], int max)
|
2005-11-18 08:25:01 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
char *pch;
|
|
|
|
|
|
|
|
pch = strtok( line, separators ); /* get 1st token addr */
|
|
|
|
while( pch != NULL ) /* repeat for each token */
|
|
|
|
{
|
|
|
|
if ( i < max )
|
|
|
|
{
|
|
|
|
/* strip leading and trailing spaces and
|
|
|
|
save token in array
|
|
|
|
*/
|
|
|
|
tokens[i] = dc_cfg_cutLeadingWhiteSpace(pch);
|
|
|
|
dc_cfg_cutTrailingWhiteSpace(tokens[i]);
|
|
|
|
}
|
|
|
|
pch = strtok( NULL, separators ); /* next token address */
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return( i );
|
|
|
|
}
|