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

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

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