Annotation of libaitcfg/src/parse.c, revision 1.1.1.1.2.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] != ']') {
1.1.1.1.2.1! misho 58: #ifdef __DEBUG
1.1 misho 59: cfgDbg(stdout, "WARNING:: Ignore section %s ... not closed breket\n", szLine);
1.1.1.1.2.1! misho 60: #endif
1.1 misho 61: } else {
62: szLine[pos] = 0;
63: strncpy((char*) szSection, (char*) szLine + 1, MAX_STR);
64: #ifdef __DEBUG
65: cfgDbg(stdout, "DEBUG:: Section %s\n", szSection);
66: #endif
67: }
68: continue;
69: }
70:
71: // Devide pairs
72: pos = strchr((char*) szLine, '=') ? strchr((char*) szLine, '=') - (char*) szLine : 0;
73: if (!pos) {
1.1.1.1.2.1! misho 74: #ifdef __DEBUG
1.1 misho 75: cfgDbg(stdout, "WARNING:: Ignore a/v %s ... format error!\n", szLine);
1.1.1.1.2.1! misho 76: #endif
1.1 misho 77: continue;
78: } else {
79: av = malloc(sizeof(struct tagPair));
80: if (!av) {
81: LOGERR;
82: return -1;
83: } else {
84: // added new element
85: av->sle_next = cfg->slh_first;
86: cfg->slh_first = av;
87: }
88: // added section name to element
89: if (*szSection) {
90: av->psSection = malloc(strlen((char*) szSection) + 1);
91: if (!av->psSection) {
92: LOGERR;
93: free(av);
94: return -1;
95: } else
96: strcpy((char*) av->psSection, (char*) szSection);
97: } else
98: av->psSection = NULL;
99:
100: psAttr = szLine;
101: psVal = (szLine + pos + 1);
102: szLine[pos] = 0;
103: rtrim(psAttr);
104: ltrim(psVal);
105: #ifdef __DEBUG
106: cfgDbg(stdout, "DEBUG:: Attr(%p) ->%s size=%d Value(%p) ->%s size=%d\n",
107: psAttr, psAttr, strlen((char*) psAttr), psVal, psVal, strlen((char*) psVal));
108: #endif
109: // added attribute to element
110: av->psAttribute = malloc(strlen((char*) psAttr) + 1);
111: if (!av->psAttribute) {
112: LOGERR;
113: free(av->psSection);
114: free(av);
115: return -1;
116: } else
117: strcpy((char*) av->psAttribute, (char*) psAttr);
118: // added value to element
119: av->psValue = malloc(strlen((char*) psVal) + 1);
120: if (!av->psValue) {
121: LOGERR;
122: free(av->psAttribute);
123: free(av->psSection);
124: free(av);
125: return -1;
126: } else
127: strcpy((char*) av->psValue, (char*) psVal);
128: }
129: }
130:
131: return 0;
132: }
133:
134: /*
135: * WriteConfig() Write to file from items in config list
136: * @f = file resource
137: * @cfg = Head list element
138: * return: 0 ok; -1 error:: can`t write to file
139: */
140: int WriteConfig(FILE *f, sl_config * __restrict cfg)
141: {
142: struct tagPair *av;
143: time_t tim;
144: char szTime[MAX_STR + 1];
145: u_char szSection[MAX_STR + 1];
146:
147: bzero(szSection, MAX_STR + 1);
148:
149: bzero(szTime, MAX_STR + 1);
150: time(&tim);
151: strftime(szTime, MAX_STR, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
152: if (!cfgDbg(f, "## Write Config :: %s\n#\n", szTime)) {
153: LOGERR;
154: return -1;
155: }
156:
157: for (av = cfg->slh_first; av; av = av->sle_next) {
158: if (av->psSection && strcmp((char*) av->psSection, (char*) szSection)) {
159: bzero(szSection, MAX_STR + 1);
160: strcpy((char*) szSection, (char*) av->psSection);
161: if (!cfgDbg(f, "\n[%s]\n", av->psSection)) {
162: LOGERR;
163: return -1;
164: }
165: }
166: if (!av->psSection && *szSection) {
167: bzero(szSection, MAX_STR + 1);
168: if (!cfgDbg(f, "\n[]\n")) {
169: LOGERR;
170: return -1;
171: }
172: }
173:
174: if (!cfgDbg(f, "%s = %s\n", av->psAttribute, av->psValue)) {
175: LOGERR;
176: return -1;
177: }
178: }
179:
180: bzero(szTime, MAX_STR + 1);
181: time(&tim);
182: strftime(szTime, MAX_STR, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
183: if (!cfgDbg(f, "\n#\n## Done. :: %s\n", szTime)) {
184: LOGERR;
185: return -1;
186: }
187:
188: return 0;
189: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>