0aedd5863c28c159dabcdc8dea2913673c90ac7c
[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(char *pszFileName, char *pszSection,
32 NX_CFG_TEMPLATE *pTemplateList, BOOL bPrint)
33 {
34 FILE *cfg;
35 char *ptr, *eptr, szBuffer[4096];
36 int i, iSourceLine = 0, iErrors = 0;
37 BOOL bActiveSection = (pszSection[0] == 0);
38
39 cfg = fopen(pszFileName, "r");
40 if (cfg == NULL)
41 {
42 if (bPrint)
43 printf("Unable to open configuration file: %s\n", strerror(errno));
44 return NXCFG_ERR_NOFILE;
45 }
46
47 while(!feof(cfg))
48 {
49 // Read line from file
50 szBuffer[0] = 0;
51 fgets(szBuffer, 4095, cfg);
52 iSourceLine++;
53 ptr = strchr(szBuffer, '\n');
54 if (ptr != NULL)
55 *ptr = 0;
56 ptr = strchr(szBuffer, '#');
57 if (ptr != NULL)
58 *ptr = 0;
59
60 StrStrip(szBuffer);
61 if (szBuffer[0] == 0)
62 continue;
63
64 // Check if it's a section name
65 if (szBuffer[0] == '*')
66 {
67 bActiveSection = !stricmp(&szBuffer[1], pszSection);
68 }
69 else
70 {
71 if (!bActiveSection)
72 continue;
73
74 // Divide on two parts at = sign
75 ptr = strchr(szBuffer, '=');
76 if (ptr == NULL)
77 {
78 iErrors++;
79 if (bPrint)
80 printf("Syntax error in configuration file at line %d\n", iSourceLine);
81 continue;
82 }
83 *ptr = 0;
84 ptr++;
85 StrStrip(szBuffer);
86 StrStrip(ptr);
87
88 // Find corresponding token in template list
89 for(i = 0; pTemplateList[i].iType != CT_END_OF_LIST; i++)
90 if (!stricmp(pTemplateList[i].szToken, szBuffer))
91 {
92 switch(pTemplateList[i].iType)
93 {
94 case CT_LONG:
95 *((long *)pTemplateList[i].pBuffer) = strtol(ptr, &eptr, 0);
96 if (*eptr != 0)
97 {
98 iErrors++;
99 if (bPrint)
100 printf("Invalid number '%s' in configuration file at line %d\n", ptr, iSourceLine);
101 }
102 break;
103 case CT_WORD:
104 *((WORD *)pTemplateList[i].pBuffer) = (WORD)strtoul(ptr, &eptr, 0);
105 if (*eptr != 0)
106 {
107 iErrors++;
108 if (bPrint)
109 printf("Invalid number '%s' in configuration file at line %d\n", ptr, iSourceLine);
110 }
111 break;
112 case CT_BOOLEAN:
113 if (!stricmp(ptr, "yes") || !stricmp(ptr, "true") ||
114 !stricmp(ptr, "on") || !stricmp(ptr, "1"))
115 {
116 *((DWORD *)pTemplateList[i].pBuffer) |= pTemplateList[i].dwBufferSize;
117 }
118 else
119 {
120 *((DWORD *)pTemplateList[i].pBuffer) &= ~(pTemplateList[i].dwBufferSize);
121 }
122 break;
123 case CT_STRING:
124 strncpy((char *)pTemplateList[i].pBuffer, ptr, pTemplateList[i].dwBufferSize);
125 break;
126 case CT_STRING_LIST:
127 if (pTemplateList[i].dwBufferPos < pTemplateList[i].dwBufferSize)
128 {
129 strncpy((char *)pTemplateList[i].pBuffer + pTemplateList[i].dwBufferPos,
130 ptr, pTemplateList[i].dwBufferSize - pTemplateList[i].dwBufferPos - 1);
131 pTemplateList[i].dwBufferPos += strlen(ptr);
132 if (pTemplateList[i].dwBufferPos < pTemplateList[i].dwBufferSize)
133 ((char *)pTemplateList[i].pBuffer)[pTemplateList[i].dwBufferPos++] = pTemplateList[i].cSeparator;
134 }
135 break;
136 default:
137 break;
138 }
139 break;
140 }
141
142 // Invalid keyword
143 if (pTemplateList[i].iType == CT_END_OF_LIST)
144 {
145 iErrors++;
146 if (bPrint)
147 printf("Invalid keyword %s in configuration file at line %d\n", szBuffer, iSourceLine);
148 }
149 }
150 }
151 fclose(cfg);
152
153 if ((!iErrors) && (bPrint))
154 printf("Configuration file OK\n");
155
156 return (iErrors > 0) ? NXCFG_ERR_SYNTAX : NXCFG_ERR_OK;
157 }