File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / parse.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Wed Sep 9 09:07:31 2009 UTC (14 years, 9 months ago) by misho
Branches: MAIN
CVS tags: cfg3_1, HEAD, CFG3_0
prepare ver

    1: /*************************************************************************
    2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: parse.c,v 1.2 2009/09/09 09:07:31 misho Exp $
    7: *
    8: *************************************************************************/
    9: #include "global.h"
   10: #include "aitcfg.h"
   11: #include "tools.h"
   12: 
   13: 
   14: // cfgDbg() Debug/Log operation
   15: static inline int cfgDbg(FILE *f, char *fmt, ...)
   16: {
   17: 	int ret = 0;
   18: 	va_list lst;
   19: 
   20: 	va_start(lst, fmt);
   21: 	ret = vfprintf(f, fmt, lst);
   22: 	va_end(lst);
   23: 
   24: 	return ret;
   25: }
   26: 
   27: // -----------------------------------------
   28: 
   29: /*
   30:  * ReadConfig() Read from file and add new item to config list
   31:  * @f = file resource
   32:  * @cfg = Head list element
   33:  * return: 0 ok; -1 error:: can`t allocate memory
   34: */
   35: int ReadConfig(FILE *f, sl_config * __restrict cfg)
   36: {
   37: 	u_char szLine[MAX_STR + 1];
   38: 	u_char szSection[MAX_STR + 1], *psAttr, *psVal;
   39: 	int pos;
   40: 	struct tagPair *av;
   41: 
   42: 	memset(szSection, 0, MAX_STR + 1);
   43: 	while (!feof(f)) {
   44: 		memset(szLine, 0, MAX_STR + 1);
   45: 		fgets((char*) szLine, MAX_STR, f);
   46: 		trim(szLine);
   47: #ifdef __DEBUG
   48: 		cfgDbg(stdout, "DEBUG:: RAW |%s|\n", szLine);
   49: #endif
   50: 
   51: 		// End of config
   52: 		if (*szLine == '.')
   53: 			break;
   54: 		// Comment
   55: 		if (*szLine == '#' || *szLine == ';' || !*szLine)
   56: 			continue;
   57: 
   58: #ifdef __DEBUG
   59: 		cfgDbg(stdout, "DEBUG:: Clean |%s|\n", szLine);
   60: #endif
   61: 
   62: 		// Section
   63: 		if (*szLine == '[') {
   64: 			pos = strlen((char*) szLine) - 1;
   65: 			if (szLine[pos] != ']') {
   66: #ifdef __DEBUG
   67: 				cfgDbg(stdout, "WARNING:: Ignore section %s ... not closed breket\n", szLine);
   68: #endif
   69: 			} else {
   70: 				szLine[pos] = 0; 
   71: 				strncpy((char*) szSection, (char*) szLine + 1, MAX_STR);
   72: #ifdef __DEBUG
   73: 				cfgDbg(stdout, "DEBUG:: Section %s\n", szSection);
   74: #endif
   75: 			}
   76: 			continue;
   77: 		}
   78: 
   79: 		// Devide pairs
   80: 		pos = strchr((char*) szLine, '=') ? strchr((char*) szLine, '=') - (char*) szLine : 0;
   81: 		if (!pos) {
   82: #ifdef __DEBUG
   83: 			cfgDbg(stdout, "WARNING:: Ignore a/v %s ... format error!\n", szLine);
   84: #endif
   85: 			continue;
   86: 		} else {
   87: 			av = malloc(sizeof(struct tagPair));
   88: 			if (!av) {
   89: 				LOGERR;
   90: 				return -1;
   91: 			} else {
   92: 				memset(av, 0, sizeof(struct tagPair));
   93: 				// added new element
   94: 				av->sle_next = cfg->slh_first;
   95: 				cfg->slh_first = av;
   96: 			}
   97: 			// added section name to element
   98: 			if (*szSection) {
   99: 				av->psSection = malloc(strlen((char*) szSection) + 1);
  100: 				if (!av->psSection) {
  101: 					LOGERR;
  102: 					free(av);
  103: 					return -1;
  104: 				} else
  105: 					strlcpy((char*) av->psSection, (char*) szSection, strlen((char*) szSection) + 1);
  106: 			} else
  107: 				av->psSection = NULL;
  108: 
  109: 			psAttr = szLine;
  110: 			psVal = (szLine + pos + 1);
  111: 			szLine[pos] = 0;
  112: 			rtrim(psAttr);
  113: 			ltrim(psVal);
  114: #ifdef __DEBUG
  115: 			cfgDbg(stdout, "DEBUG:: Attr(%p) ->%s size=%d Value(%p) ->%s size=%d\n", 
  116: 					psAttr, psAttr, strlen((char*) psAttr), psVal, psVal, strlen((char*) psVal));
  117: #endif
  118: 			// added attribute to element
  119: 			av->psAttribute = malloc(strlen((char*) psAttr) + 1);
  120: 			if (!av->psAttribute) {
  121: 				LOGERR;
  122: 				free(av->psSection);
  123: 				free(av);
  124: 				return -1;
  125: 			} else
  126: 				strlcpy((char*) av->psAttribute, (char*) psAttr, strlen((char*) psAttr) + 1);
  127: 			// added value to element
  128: 			av->psValue = malloc(strlen((char*) psVal) + 1);
  129: 			if (!av->psValue) {
  130: 				LOGERR;
  131: 				free(av->psAttribute);
  132: 				free(av->psSection);
  133: 				free(av);
  134: 				return -1;
  135: 			} else
  136: 				strlcpy((char*) av->psValue, (char*) psVal, strlen((char*) psVal) + 1);
  137: 		}
  138: 	}
  139: 
  140: 	return 0;
  141: }
  142: 
  143: /*
  144:  * WriteConfig() Write to file from items in config list
  145:  * @f = file resource
  146:  * @cfg = Head list element
  147:  * return: 0 ok; -1 error:: can`t write to file
  148: */
  149: int WriteConfig(FILE *f, sl_config * __restrict cfg)
  150: {
  151: 	struct tagPair *av;
  152: 	time_t tim;
  153: 	char szTime[MAX_STR + 1];
  154: 	u_char szSection[MAX_STR + 1];
  155: 
  156: 	bzero(szSection, MAX_STR + 1);
  157: 
  158: 	bzero(szTime, MAX_STR + 1);
  159: 	time(&tim);
  160: 	strftime(szTime, MAX_STR, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
  161: 	if (!cfgDbg(f, "## Write Config :: %s\n#\n", szTime)) {
  162: 		LOGERR;
  163: 		return -1;
  164: 	}
  165: 
  166: 	for (av = cfg->slh_first; av; av = av->sle_next) {
  167: 		if (av->psSection && strcmp((char*) av->psSection, (char*) szSection)) {
  168: 			strlcpy((char*) szSection, (char*) av->psSection, MAX_STR + 1);
  169: 			if (!cfgDbg(f, "\n[%s]\n", av->psSection)) {
  170: 				LOGERR;
  171: 				return -1;
  172: 			}
  173: 		}
  174: 		if (!av->psSection && *szSection) {
  175: 			bzero(szSection, MAX_STR + 1);
  176: 			if (!cfgDbg(f, "\n[]\n")) {
  177: 				LOGERR;
  178: 				return -1;
  179: 			}
  180: 		}
  181: 
  182: 		if (!cfgDbg(f, "%s = %s\n", av->psAttribute, av->psValue)) {
  183: 			LOGERR;
  184: 			return -1;
  185: 		}
  186: 	}
  187: 
  188: 	bzero(szTime, MAX_STR + 1);
  189: 	time(&tim);
  190: 	strftime(szTime, MAX_STR, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
  191: 	if (!cfgDbg(f, "\n#\n## Done. :: %s\n", szTime)) {
  192: 		LOGERR;
  193: 		return -1;
  194: 	}
  195: 
  196: 	return 0;
  197: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>