c2d9a641fea91947910e4dfc5de080655facdfe9
[public/netxms.git] / include / nxconfig.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2010 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU Lesser General Public License as published
7 ** by the Free Software Foundation; either version 3 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU Lesser General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: nxconfig.h
20 **
21 **/
22
23 #ifndef _nxconfig_h_
24 #define _nxconfig_h_
25
26 #include <nms_common.h>
27 #include <nms_util.h>
28
29
30 //
31 // Config entry
32 //
33
34 class ConfigEntryList;
35
36 class LIBNETXMS_EXPORTABLE ConfigEntry
37 {
38 private:
39 TCHAR *m_name;
40 ConfigEntry *m_parent;
41 ConfigEntry *m_next;
42 ConfigEntry *m_childs;
43 int m_valueCount;
44 TCHAR **m_values;
45 TCHAR *m_file;
46 int m_line;
47 int m_id;
48
49 void linkEntry(ConfigEntry *entry) { entry->m_next = m_next; m_next = entry; }
50 void addEntry(ConfigEntry *entry) { entry->m_parent = this; entry->m_next = m_childs; m_childs = entry; }
51
52 public:
53 ConfigEntry(const TCHAR *name, ConfigEntry *parent, const TCHAR *file, int line, int id);
54 ~ConfigEntry();
55
56 ConfigEntry *getNext() { return m_next; }
57
58 const TCHAR *getName() { return m_name; }
59 int getId() { return m_id; }
60 int getValueCount() { return m_valueCount; }
61 int getConcatenatedValuesLength();
62
63 const TCHAR *getValue(int index = 0);
64 LONG getValueInt(int index = 0, LONG defaultValue = 0);
65 DWORD getValueUInt(int index = 0, DWORD defaultValue = 0);
66 INT64 getValueInt64(int index = 0, INT64 defaultValue = 0);
67 QWORD getValueUInt64(int index = 0, QWORD defaultValue = 0);
68 bool getValueBoolean(int index = 0, bool defaultValue = false);
69
70 const TCHAR *getSubEntryValue(const TCHAR *name, int index = 0, const TCHAR *defaultValue = NULL);
71 LONG getSubEntryValueInt(const TCHAR *name, int index = 0, LONG defaultValue = 0);
72 DWORD getSubEntryValueUInt(const TCHAR *name, int index = 0, DWORD defaultValue = 0);
73 INT64 getSubEntryValueInt64(const TCHAR *name, int index = 0, INT64 defaultValue = 0);
74 QWORD getSubEntryValueUInt64(const TCHAR *name, int index = 0, QWORD defaultValue = 0);
75 bool getSubEntryValueBoolean(const TCHAR *name, int index = 0, bool defaultValue = false);
76
77 const TCHAR *getFile() { return m_file; }
78 int getLine() { return m_line; }
79
80 void setName(const TCHAR *name);
81
82 void addValue(const TCHAR *value);
83 void setValue(const TCHAR*value);
84
85 ConfigEntry *findEntry(const TCHAR *name);
86 ConfigEntryList *getSubEntries(const TCHAR *mask);
87 ConfigEntryList *getOrderedSubEntries(const TCHAR *mask);
88
89 void print(FILE *file, int level);
90 void createXml(String &xml, int level = 0);
91 };
92
93
94 //
95 // List of config entries
96 //
97
98 class ConfigEntryList
99 {
100 private:
101 ConfigEntry **m_list;
102 int m_size;
103
104 public:
105 ConfigEntryList(ConfigEntry **list, int size) { m_list = list; m_size = size; }
106 ~ConfigEntryList() { safe_free(m_list); }
107
108 int getSize() { return m_size; }
109 ConfigEntry *getEntry(int index) { return ((index >= 0) && (index < m_size)) ? m_list[index] : NULL; }
110
111 void sortById();
112 };
113
114
115 //
116 // Config class
117 //
118
119 class LIBNETXMS_EXPORTABLE Config
120 {
121 private:
122 ConfigEntry *m_root;
123 int m_errorCount;
124
125 protected:
126 virtual void onError(const TCHAR *errorMessage);
127
128 void error(const TCHAR *format, ...);
129 ConfigEntry *createEntry(const TCHAR *path);
130
131 public:
132 Config();
133 ~Config();
134
135 void setTopLevelTag(const TCHAR *topLevelTag) { m_root->setName(topLevelTag); }
136
137 bool loadXmlConfig(const TCHAR *file, const char *topLevelTag = NULL);
138 bool loadXmlConfigFromMemory(const char *xml, int xmlSize, const TCHAR *name = NULL, const char *topLevelTag = NULL);
139 bool loadIniConfig(const TCHAR *file, const TCHAR *defaultIniSection);
140 bool loadConfig(const TCHAR *file, const TCHAR *defaultIniSection);
141
142 bool loadConfigDirectory(const TCHAR *path, const TCHAR *defaultIniSection);
143
144 ConfigEntry *getEntry(const TCHAR *path);
145 const TCHAR *getValue(const TCHAR *path, const TCHAR *defaultValue = NULL);
146 LONG getValueInt(const TCHAR *path, LONG defaultValue);
147 DWORD getValueUInt(const TCHAR *path, DWORD defaultValue);
148 INT64 getValueInt64(const TCHAR *path, INT64 defaultValue);
149 QWORD getValueUInt64(const TCHAR *path, QWORD defaultValue);
150 bool getValueBoolean(const TCHAR *path, bool defaultValue);
151 ConfigEntryList *getSubEntries(const TCHAR *path, const TCHAR *mask);
152 ConfigEntryList *getOrderedSubEntries(const TCHAR *path, const TCHAR *mask);
153
154 bool setValue(const TCHAR *path, const TCHAR *value);
155 bool setValue(const TCHAR *path, LONG value);
156 bool setValue(const TCHAR *path, DWORD value);
157 bool setValue(const TCHAR *path, INT64 value);
158 bool setValue(const TCHAR *path, QWORD value);
159 bool setValue(const TCHAR *path, double value);
160
161 bool parseTemplate(const TCHAR *section, NX_CFG_TEMPLATE *cfgTemplate);
162
163 int getErrorCount() { return m_errorCount; }
164
165 void print(FILE *file);
166 String createXml();
167 };
168
169
170 #endif