Annotation of libaitcfg/src/aitcfg.c, revision 1.1.1.1.2.1
1.1 misho 1: #include "global.h"
2: #include "aitcfg.h"
3:
4:
5: #pragma GCC visibility push(hidden)
6:
7: int cfgErrno;
8: char cfgError[MAX_STR + 1];
9:
10: #pragma GCC visibility pop
11:
12:
13: /*
14: * InitConfig() Head initializing function for config
15: * @cfg = New head element for init
16: * return: 0 ok; -1 error:: new head element is null
17: */
18: inline int InitConfig(sl_config * __restrict cfg)
19: {
20: if (!cfg)
21: return -1;
22:
1.1.1.1.2.1! misho 23: cfg->slh_first = NULL;
! 24: return 0;
1.1 misho 25: }
26:
27: /*
28: * LoadConfig() Load config from file
29: * @csConfigName = Filename of config
30: * @cfg = Head list element
31: * return: 0 ok; -1 error:: can`t load config
32: */
33: int LoadConfig(const char *csConfigName, sl_config * __restrict cfg)
34: {
35: FILE *f;
36: int ret;
37:
38: if (!csConfigName || !cfg)
39: return -1;
40:
41: InitConfig(cfg);
42: if (access(csConfigName, R_OK)) {
43: LOGERR;
44: return -1;
45: }
46:
47: f = fopen(csConfigName, "rt");
48: if (!f) {
49: LOGERR;
50: return -1;
51: }
52:
53: ret ^= ret;
54: ret = ReadConfig(f, cfg);
55:
56: fclose(f);
57: return ret;
58: }
59:
60: /*
61: * UnloadConfig() Unload config from memory and free resources
62: * @cfg = Head list element
63: */
64: void UnloadConfig(sl_config * __restrict cfg)
65: {
66: struct tagPair *av;
67:
68: if (!cfg->slh_first)
69: return;
70:
71: for (av = cfg->slh_first; av; av = av->sle_next) {
72: cfg->slh_first = cfg->slh_first->sle_next;
73:
74: if (av->psValue)
75: free(av->psValue);
76: if (av->psAttribute)
77: free(av->psAttribute);
78: if (av->psSection)
79: free(av->psSection);
80: free(av);
81: }
82: }
83:
84: /*
85: * CreateConfig() Create config file from memory
86: * @csConfigName = New config filename
87: * @cfg = Head list element
88: * return: 0 ok; -1 error:: can`t save new config
89: */
90: int CreateConfig(const char *csConfigName, sl_config * __restrict cfg)
91: {
92: FILE *f;
93: int ret;
94:
95: if (!csConfigName || !cfg)
96: return -1;
97:
98: f = fopen(csConfigName, "wt");
99: if (!f) {
100: LOGERR;
101: return -1;
102: }
103:
104: ret ^= ret;
105: ret = WriteConfig(f, cfg);
106:
107: fclose(f);
108: return ret;
109: }
110:
111: // -----------------------------------------------------------
112:
113: //
114: // Error maintenance functions ...
115: //
116:
117: // cfg_GetErrno() Get error code of last operation
118: inline int cfg_GetErrno()
119: {
120: return cfgErrno;
121: }
122:
123: // cfg_GetError() Get error text of last operation
124: inline const char *cfg_GetError()
125: {
126: return cfgError;
127: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>