version 1.1.1.1.2.1, 2008/09/30 15:03:56
|
version 1.2.2.2, 2009/09/09 20:42:07
|
Line 1
|
Line 1
|
|
/************************************************************************* |
|
* (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com> |
|
* by Michael Pounov <misho@openbsd-bg.org> |
|
* |
|
* $Author$ |
|
* $Id$ |
|
* |
|
*************************************************************************/ |
#include "global.h" |
#include "global.h" |
#include "aitcfg.h" |
#include "aitcfg.h" |
#include "tools.h" |
#include "tools.h" |
Line 16 static inline int cfgDbg(FILE *f, char *fmt, ...)
|
Line 24 static inline int cfgDbg(FILE *f, char *fmt, ...)
|
return ret; |
return ret; |
} |
} |
|
|
// ----------------------------------------- | /* |
| * InvertQueue() InvertQueue order //{cfg} list of elements for revert |
| * @cfg = Head list element for revert |
| */ |
| static inline void InvertQueue(sl_config * __restrict cfg) |
| { |
| struct tagPair *item, *next, *prev = NULL; |
|
|
|
for (item = cfg->slh_first; item; item = next) { |
|
next = item->sle_next; |
|
item->sle_next = prev; |
|
prev = item; |
|
} |
|
cfg->slh_first = prev; |
|
} |
|
|
|
// cfgWrite() Write to file from config list |
|
static inline int cfgWrite(FILE *f, sl_config * __restrict cfg, int whitespace) |
|
{ |
|
struct tagPair *av; |
|
time_t tim; |
|
char szTime[MAX_STR + 1]; |
|
u_char szSection[MAX_STR + 1]; |
|
|
|
bzero(szSection, MAX_STR + 1); |
|
|
|
bzero(szTime, MAX_STR + 1); |
|
time(&tim); |
|
strftime(szTime, MAX_STR, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim)); |
|
if (!cfgDbg(f, "## Write Config :: %s\n#\n", szTime)) { |
|
LOGERR; |
|
return -1; |
|
} |
|
|
|
InvertQueue(cfg); |
|
for (av = cfg->slh_first; av; av = av->sle_next) { |
|
if (av->psSection && strcmp((char*) av->psSection, (char*) szSection)) { |
|
strlcpy((char*) szSection, (char*) av->psSection, MAX_STR + 1); |
|
if (!cfgDbg(f, "\n[%s]\n", av->psSection)) { |
|
LOGERR; |
|
return -1; |
|
} |
|
} |
|
if (!av->psSection && *szSection) { |
|
bzero(szSection, MAX_STR + 1); |
|
if (!cfgDbg(f, "\n[]\n")) { |
|
LOGERR; |
|
return -1; |
|
} |
|
} |
|
|
|
if (whitespace) { |
|
if (!cfgDbg(f, "%s = %s\n", av->psAttribute, av->psValue)) { |
|
LOGERR; |
|
return -1; |
|
} |
|
} else { |
|
if (!cfgDbg(f, "%s=%s\n", av->psAttribute, av->psValue)) { |
|
LOGERR; |
|
return -1; |
|
} |
|
} |
|
} |
|
InvertQueue(cfg); |
|
|
|
bzero(szTime, MAX_STR + 1); |
|
time(&tim); |
|
strftime(szTime, MAX_STR, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim)); |
|
if (!cfgDbg(f, "\n#\n## Done. :: %s\n", szTime)) { |
|
LOGERR; |
|
return -1; |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
// --------------------------------------------------- |
|
|
/* |
/* |
* ReadConfig() Read from file and add new item to config list |
* ReadConfig() Read from file and add new item to config list |
* @f = file resource |
* @f = file resource |
Line 81 int ReadConfig(FILE *f, sl_config * __restrict cfg)
|
Line 165 int ReadConfig(FILE *f, sl_config * __restrict cfg)
|
LOGERR; |
LOGERR; |
return -1; |
return -1; |
} else { |
} else { |
|
memset(av, 0, sizeof(struct tagPair)); |
// added new element |
// added new element |
av->sle_next = cfg->slh_first; |
av->sle_next = cfg->slh_first; |
cfg->slh_first = av; |
cfg->slh_first = av; |
Line 93 int ReadConfig(FILE *f, sl_config * __restrict cfg)
|
Line 178 int ReadConfig(FILE *f, sl_config * __restrict cfg)
|
free(av); |
free(av); |
return -1; |
return -1; |
} else |
} else |
strcpy((char*) av->psSection, (char*) szSection); | strlcpy((char*) av->psSection, (char*) szSection, strlen((char*) szSection) + 1); |
} else |
} else |
av->psSection = NULL; |
av->psSection = NULL; |
|
|
Line 114 int ReadConfig(FILE *f, sl_config * __restrict cfg)
|
Line 199 int ReadConfig(FILE *f, sl_config * __restrict cfg)
|
free(av); |
free(av); |
return -1; |
return -1; |
} else |
} else |
strcpy((char*) av->psAttribute, (char*) psAttr); | strlcpy((char*) av->psAttribute, (char*) psAttr, strlen((char*) psAttr) + 1); |
// added value to element |
// added value to element |
av->psValue = malloc(strlen((char*) psVal) + 1); |
av->psValue = malloc(strlen((char*) psVal) + 1); |
if (!av->psValue) { |
if (!av->psValue) { |
Line 124 int ReadConfig(FILE *f, sl_config * __restrict cfg)
|
Line 209 int ReadConfig(FILE *f, sl_config * __restrict cfg)
|
free(av); |
free(av); |
return -1; |
return -1; |
} else |
} else |
strcpy((char*) av->psValue, (char*) psVal); | strlcpy((char*) av->psValue, (char*) psVal, strlen((char*) psVal) + 1); |
} |
} |
} |
} |
|
|
Line 139 int ReadConfig(FILE *f, sl_config * __restrict cfg)
|
Line 224 int ReadConfig(FILE *f, sl_config * __restrict cfg)
|
*/ |
*/ |
int WriteConfig(FILE *f, sl_config * __restrict cfg) |
int WriteConfig(FILE *f, sl_config * __restrict cfg) |
{ |
{ |
struct tagPair *av; | return cfgWrite(f, cfg, 1); |
time_t tim; | } |
char szTime[MAX_STR + 1]; | |
u_char szSection[MAX_STR + 1]; | |
|
|
bzero(szSection, MAX_STR + 1); | /* |
| * cfg_WriteConfig() Write to file from items in config list without whitespaces! |
bzero(szTime, MAX_STR + 1); | * @f = file resource |
time(&tim); | * @cfg = Head list element |
strftime(szTime, MAX_STR, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim)); | * return: 0 ok; -1 error:: can`t write to file |
if (!cfgDbg(f, "## Write Config :: %s\n#\n", szTime)) { | */ |
LOGERR; | int cfg_WriteConfig(FILE *f, sl_config * __restrict cfg) |
return -1; | { |
} | return cfgWrite(f, cfg, 0); |
| |
for (av = cfg->slh_first; av; av = av->sle_next) { | |
if (av->psSection && strcmp((char*) av->psSection, (char*) szSection)) { | |
bzero(szSection, MAX_STR + 1); | |
strcpy((char*) szSection, (char*) av->psSection); | |
if (!cfgDbg(f, "\n[%s]\n", av->psSection)) { | |
LOGERR; | |
return -1; | |
} | |
} | |
if (!av->psSection && *szSection) { | |
bzero(szSection, MAX_STR + 1); | |
if (!cfgDbg(f, "\n[]\n")) { | |
LOGERR; | |
return -1; | |
} | |
} | |
| |
if (!cfgDbg(f, "%s = %s\n", av->psAttribute, av->psValue)) { | |
LOGERR; | |
return -1; | |
} | |
} | |
| |
bzero(szTime, MAX_STR + 1); | |
time(&tim); | |
strftime(szTime, MAX_STR, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim)); | |
if (!cfgDbg(f, "\n#\n## Done. :: %s\n", szTime)) { | |
LOGERR; | |
return -1; | |
} | |
| |
return 0; | |
} |
} |