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>
|
|
|
|
|
2006-02-23 14:02:45 +00:00
|
|
|
#include <dc_internal.h>
|
2005-11-18 08:25:01 +00:00
|
|
|
|
|
|
|
/* Private functions */
|
2006-02-23 14:02:45 +00:00
|
|
|
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
|
|
|
|
2006-02-23 14:02:45 +00:00
|
|
|
struct pair
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
};
|
2005-11-18 08:25:01 +00:00
|
|
|
|
2006-02-23 14:02:45 +00:00
|
|
|
static struct pair *pairs;
|
2005-11-18 08:25:01 +00:00
|
|
|
static int n_pairs;
|
|
|
|
|
|
|
|
/* Public functions */
|
2006-02-23 14:02:45 +00:00
|
|
|
int _DC_parseCfg(const char *cfgfile)
|
2005-11-18 08:25:01 +00:00
|
|
|
{
|
2006-02-23 14:02:45 +00:00
|
|
|
FILE *cfg;
|
|
|
|
char line[1024];
|
|
|
|
char *str;
|
2005-11-18 08:25:01 +00:00
|
|
|
# define MAX_TOKEN 3
|
2006-02-23 14:02:45 +00:00
|
|
|
char *tokens[MAX_TOKEN];
|
|
|
|
int tokenCount;
|
|
|
|
|
|
|
|
if ((cfg = fopen(cfgfile, "r")) == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Config file %s cannot be opened: %s\n",
|
|
|
|
cfgfile, strerror(errno));
|
|
|
|
return -1;
|
2005-11-18 08:25:01 +00:00
|
|
|
}
|
2006-02-23 14:02:45 +00:00
|
|
|
|
|
|
|
while (fgets(line, 1024, cfg) != NULL)
|
|
|
|
{
|
|
|
|
/* Cut leading white space */
|
|
|
|
for (str = line; *str == ' ' || *str == '\t'; str++)
|
|
|
|
/* Nothing */;
|
|
|
|
|
|
|
|
/* Skip empty lines and comments */
|
|
|
|
if (!*str || *str == '\n' || *str == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tokenCount = tokenise(line, "=\n", tokens, MAX_TOKEN);
|
|
|
|
if (tokenCount == 2)
|
|
|
|
{
|
|
|
|
struct pair *tmp;
|
|
|
|
|
|
|
|
tmp = realloc(pairs, (n_pairs + 1) * sizeof(*tmp));
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Out of memory while parsing "
|
|
|
|
"config file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pairs = tmp;
|
|
|
|
pairs[n_pairs].name = strdup(tokens[0]);
|
|
|
|
pairs[n_pairs].value = strdup(tokens[1]);
|
|
|
|
n_pairs++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Cannot understand in config file %s "
|
|
|
|
"the line %s", cfgfile, line);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-11-18 08:25:01 +00:00
|
|
|
}
|
2006-02-23 14:02:45 +00:00
|
|
|
return 0;
|
2005-11-18 08:25:01 +00:00
|
|
|
}
|
|
|
|
|
2006-02-23 14:02:45 +00:00
|
|
|
const char *_DC_getCfgStr(const char *name)
|
2005-11-18 08:25:01 +00:00
|
|
|
{
|
2006-02-23 14:02:45 +00:00
|
|
|
int i;
|
2005-11-18 08:25:01 +00:00
|
|
|
|
2006-02-23 14:02:45 +00:00
|
|
|
if (!name)
|
|
|
|
return NULL;
|
2005-11-18 08:25:01 +00:00
|
|
|
|
2006-02-23 14:02:45 +00:00
|
|
|
for (i = 0; i < n_pairs; i++)
|
|
|
|
{
|
|
|
|
if (pairs[i].name && !strcmp(pairs[i].name, name))
|
|
|
|
return pairs[i].value;
|
|
|
|
}
|
|
|
|
return NULL;
|
2005-11-18 08:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dc_cfg_cutTrailingWhiteSpace(char *line)
|
|
|
|
{
|
2006-02-23 14:02:45 +00:00
|
|
|
/* 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--;
|
|
|
|
}
|
2005-11-18 08:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2006-02-23 14:02:45 +00:00
|
|
|
int i = 0;
|
|
|
|
char *pch;
|
|
|
|
|
|
|
|
pch = strtok(line, separators); /* get 1st token addr */
|
|
|
|
while (pch) /* repeat for each token */
|
|
|
|
{
|
|
|
|
if (i < max)
|
|
|
|
{
|
|
|
|
/* Cut leading white space */
|
|
|
|
while (*pch == ' ' || *pch == '\t')
|
|
|
|
pch++;
|
|
|
|
tokens[i] = pch;
|
|
|
|
dc_cfg_cutTrailingWhiteSpace(tokens[i]);
|
|
|
|
}
|
|
|
|
pch = strtok(NULL, separators); /* next token address */
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
2005-11-18 08:25:01 +00:00
|
|
|
}
|