libnetxms changed to unicode
[public/netxms.git] / src / libnetxms / config.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Utility Library
4 ** Copyright (C) 2003, 2004 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** $module: config.cpp
21 **
22 **/
23
24 #include "libnetxms.h"
25
26
27 //
28 // Universal configuration loader
29 //
30
31 DWORD LIBNETXMS_EXPORTABLE NxLoadConfig(TCHAR *pszFileName, TCHAR *pszSection,
32 NX_CFG_TEMPLATE *pTemplateList, BOOL bPrint)
33 {
34 FILE *cfg;
35 TCHAR *ptr, *eptr, szBuffer[4096];
36 int i, iSourceLine = 0, iErrors = 0;
37 BOOL bActiveSection = (pszSection[0] == 0);
38
39 cfg = _tfopen(pszFileName, _T("r"));
40 if (cfg == NULL)
41 {
42 if (bPrint)
43 #ifndef UNDER_CE
44 _tprintf(_T("Unable to open configuration file: %s\n"), _tcserror(errno));
45 #endif // UNDER_CE
46 return NXCFG_ERR_NOFILE;
47 }
48
49 while(!feof(cfg))
50 {
51 // Read line from file
52 szBuffer[0] = 0;
53 _fgetts(szBuffer, 4095, cfg);
54 iSourceLine++;
55 ptr = _tcschr(szBuffer, _T('\n'));
56 if (ptr != NULL)
57 *ptr = 0;
58 ptr = _tcschr(szBuffer, _T('#'));
59 if (ptr != NULL)
60 *ptr = 0;
61
62 StrStrip(szBuffer);
63 if (szBuffer[0] == 0)
64 continue;
65
66 // Check if it's a section name
67 if (szBuffer[0] == _T('*'))
68 {
69 bActiveSection = !_tcsicmp(&szBuffer[1], pszSection);
70 }
71 else
72 {
73 if (!bActiveSection)
74 continue;
75
76 // Divide on two parts at = sign
77 ptr = _tcschr(szBuffer, '=');
78 if (ptr == NULL)
79 {
80 iErrors++;
81 if (bPrint)
82 _tprintf(_T("Syntax error in configuration file at line %d\n"), iSourceLine);
83 continue;
84 }
85 *ptr = 0;
86 ptr++;
87 StrStrip(szBuffer);
88 StrStrip(ptr);
89
90 // Find corresponding token in template list
91 for(i = 0; pTemplateList[i].iType != CT_END_OF_LIST; i++)
92 if (!_tcsicmp(pTemplateList[i].szToken, szBuffer))
93 {
94 switch(pTemplateList[i].iType)
95 {
96 case CT_LONG:
97 *((long *)pTemplateList[i].pBuffer) = _tcstol(ptr, &eptr, 0);
98 if (*eptr != 0)
99 {
100 iErrors++;
101 if (bPrint)
102 _tprintf(_T("Invalid number '%s' in configuration file at line %d\n"), ptr, iSourceLine);
103 }
104 break;
105 case CT_WORD:
106 *((WORD *)pTemplateList[i].pBuffer) = (WORD)_tcstoul(ptr, &eptr, 0);
107 if (*eptr != 0)
108 {
109 iErrors++;
110 if (bPrint)
111 _tprintf(_T("Invalid number '%s' in configuration file at line %d\n"),
112 ptr, iSourceLine);
113 }
114 break;
115 case CT_BOOLEAN:
116 if (!_tcsicmp(ptr, _T("yes")) || !_tcsicmp(ptr, _T("true")) ||
117 !_tcsicmp(ptr, _T("on")) || !_tcsicmp(ptr, _T("1")))
118 {
119 *((DWORD *)pTemplateList[i].pBuffer) |= pTemplateList[i].dwBufferSize;
120 }
121 else
122 {
123 *((DWORD *)pTemplateList[i].pBuffer) &= ~(pTemplateList[i].dwBufferSize);
124 }
125 break;
126 case CT_STRING:
127 _tcsncpy((TCHAR *)pTemplateList[i].pBuffer, ptr, pTemplateList[i].dwBufferSize);
128 break;
129 case CT_STRING_LIST:
130 if (pTemplateList[i].dwBufferPos < pTemplateList[i].dwBufferSize)
131 {
132 _tcsncpy((TCHAR *)pTemplateList[i].pBuffer + pTemplateList[i].dwBufferPos,
133 ptr, pTemplateList[i].dwBufferSize - pTemplateList[i].dwBufferPos - 1);
134 pTemplateList[i].dwBufferPos += _tcslen(ptr);
135 if (pTemplateList[i].dwBufferPos < pTemplateList[i].dwBufferSize)
136 ((TCHAR *)pTemplateList[i].pBuffer)[pTemplateList[i].dwBufferPos++] = pTemplateList[i].cSeparator;
137 }
138 break;
139 default:
140 break;
141 }
142 break;
143 }
144
145 // Invalid keyword
146 if (pTemplateList[i].iType == CT_END_OF_LIST)
147 {
148 iErrors++;
149 if (bPrint)
150 _tprintf(_T("Invalid keyword %s in configuration file at line %d\n"), szBuffer, iSourceLine);
151 }
152 }
153 }
154 fclose(cfg);
155
156 if ((!iErrors) && (bPrint))
157 _tprintf(_T("Configuration file OK\n"));
158
159 return (iErrors > 0) ? NXCFG_ERR_SYNTAX : NXCFG_ERR_OK;
160 }