Annotation of libaitcfg/src/parse.c, revision 1.1.1.1.2.2

1.1.1.1.2.2! misho       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: global.h,v 4.0.2.3 2008/03/06 10:08:48 misho Exp $
        !             7: *
        !             8: *************************************************************************/
1.1       misho       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] != ']') {
1.1.1.1.2.1  misho      66: #ifdef __DEBUG
1.1       misho      67:                                cfgDbg(stdout, "WARNING:: Ignore section %s ... not closed breket\n", szLine);
1.1.1.1.2.1  misho      68: #endif
1.1       misho      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) {
1.1.1.1.2.1  misho      82: #ifdef __DEBUG
1.1       misho      83:                        cfgDbg(stdout, "WARNING:: Ignore a/v %s ... format error!\n", szLine);
1.1.1.1.2.1  misho      84: #endif
1.1       misho      85:                        continue;
                     86:                } else {
                     87:                        av = malloc(sizeof(struct tagPair));
                     88:                        if (!av) {
                     89:                                LOGERR;
                     90:                                return -1;
                     91:                        } else {
                     92:                                // added new element
                     93:                                av->sle_next = cfg->slh_first;
                     94:                                cfg->slh_first = av;
                     95:                        }
                     96:                        // added section name to element
                     97:                        if (*szSection) {
                     98:                                av->psSection = malloc(strlen((char*) szSection) + 1);
                     99:                                if (!av->psSection) {
                    100:                                        LOGERR;
                    101:                                        free(av);
                    102:                                        return -1;
                    103:                                } else
                    104:                                        strcpy((char*) av->psSection, (char*) szSection);
                    105:                        } else
                    106:                                av->psSection = NULL;
                    107: 
                    108:                        psAttr = szLine;
                    109:                        psVal = (szLine + pos + 1);
                    110:                        szLine[pos] = 0;
                    111:                        rtrim(psAttr);
                    112:                        ltrim(psVal);
                    113: #ifdef __DEBUG
                    114:                        cfgDbg(stdout, "DEBUG:: Attr(%p) ->%s size=%d Value(%p) ->%s size=%d\n", 
                    115:                                        psAttr, psAttr, strlen((char*) psAttr), psVal, psVal, strlen((char*) psVal));
                    116: #endif
                    117:                        // added attribute to element
                    118:                        av->psAttribute = malloc(strlen((char*) psAttr) + 1);
                    119:                        if (!av->psAttribute) {
                    120:                                LOGERR;
                    121:                                free(av->psSection);
                    122:                                free(av);
                    123:                                return -1;
                    124:                        } else
                    125:                                strcpy((char*) av->psAttribute, (char*) psAttr);
                    126:                        // added value to element
                    127:                        av->psValue = malloc(strlen((char*) psVal) + 1);
                    128:                        if (!av->psValue) {
                    129:                                LOGERR;
                    130:                                free(av->psAttribute);
                    131:                                free(av->psSection);
                    132:                                free(av);
                    133:                                return -1;
                    134:                        } else
                    135:                                strcpy((char*) av->psValue, (char*) psVal);
                    136:                }
                    137:        }
                    138: 
                    139:        return 0;
                    140: }
                    141: 
                    142: /*
                    143:  * WriteConfig() Write to file from items in config list
                    144:  * @f = file resource
                    145:  * @cfg = Head list element
                    146:  * return: 0 ok; -1 error:: can`t write to file
                    147: */
                    148: int WriteConfig(FILE *f, sl_config * __restrict cfg)
                    149: {
                    150:        struct tagPair *av;
                    151:        time_t tim;
                    152:        char szTime[MAX_STR + 1];
                    153:        u_char szSection[MAX_STR + 1];
                    154: 
                    155:        bzero(szSection, MAX_STR + 1);
                    156: 
                    157:        bzero(szTime, MAX_STR + 1);
                    158:        time(&tim);
                    159:        strftime(szTime, MAX_STR, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
                    160:        if (!cfgDbg(f, "## Write Config :: %s\n#\n", szTime)) {
                    161:                LOGERR;
                    162:                return -1;
                    163:        }
                    164: 
                    165:        for (av = cfg->slh_first; av; av = av->sle_next) {
                    166:                if (av->psSection && strcmp((char*) av->psSection, (char*) szSection)) {
                    167:                        bzero(szSection, MAX_STR + 1);
                    168:                        strcpy((char*) szSection, (char*) av->psSection);
                    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>