Commit | Line | Data |
---|---|---|
d9794cb6 | 1 | /* |
8a5ba964 | 2 | ** NetXMS - Network Management System |
68f384ea | 3 | ** Copyright (C) 2003-2010 Victor Kirhenshtein |
8a5ba964 AK |
4 | ** |
5 | ** This program is free software; you can redistribute it and/or modify | |
68f384ea VK |
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 | |
8a5ba964 AK |
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 | ** | |
68f384ea | 15 | ** You should have received a copy of the GNU Lesser General Public License |
8a5ba964 AK |
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 | ||
e6c91aac VK |
26 | #include <nms_common.h> |
27 | #include <nms_util.h> | |
c9df9ad9 | 28 | #include <uuid.h> |
e6c91aac | 29 | |
e5f88e5a VK |
30 | class Config; |
31 | ||
8b7ae7eb VK |
32 | /** |
33 | * Config entry | |
34 | */ | |
e6c91aac VK |
35 | class LIBNETXMS_EXPORTABLE ConfigEntry |
36 | { | |
37 | private: | |
38 | TCHAR *m_name; | |
39 | ConfigEntry *m_parent; | |
40 | ConfigEntry *m_next; | |
9387bc59 VK |
41 | ConfigEntry *m_first; |
42 | ConfigEntry *m_last; | |
e6c91aac VK |
43 | int m_valueCount; |
44 | TCHAR **m_values; | |
45 | TCHAR *m_file; | |
46 | int m_line; | |
a65c1819 | 47 | int m_id; |
8b7ae7eb | 48 | StringMap m_attributes; |
e5f88e5a | 49 | const Config *m_owner; |
e6c91aac | 50 | |
9387bc59 | 51 | void addEntry(ConfigEntry *entry); |
203e9d8a | 52 | void linkEntry(ConfigEntry *entry) { entry->m_next = m_next; m_next = entry; } |
e6c91aac VK |
53 | |
54 | public: | |
e5f88e5a | 55 | ConfigEntry(const TCHAR *name, ConfigEntry *parent, const Config *owner, const TCHAR *file, int line, int id); |
e6c91aac VK |
56 | ~ConfigEntry(); |
57 | ||
fa01c3b6 VK |
58 | ConfigEntry *getNext() const { return m_next; } |
59 | ConfigEntry *getParent() const { return m_parent; } | |
e6c91aac | 60 | |
fa01c3b6 VK |
61 | const TCHAR *getName() const { return m_name; } |
62 | int getId() const { return m_id; } | |
63 | int getValueCount() const { return m_valueCount; } | |
e6c91aac | 64 | int getConcatenatedValuesLength(); |
d9794cb6 | 65 | |
a65c1819 | 66 | const TCHAR *getValue(int index = 0); |
31b0f68b VK |
67 | INT32 getValueAsInt(int index = 0, INT32 defaultValue = 0); |
68 | UINT32 getValueAsUInt(int index = 0, UINT32 defaultValue = 0); | |
69 | INT64 getValueAsInt64(int index = 0, INT64 defaultValue = 0); | |
70 | UINT64 getValueAsUInt64(int index = 0, UINT64 defaultValue = 0); | |
71 | bool getValueAsBoolean(int index = 0, bool defaultValue = false); | |
999945fa | 72 | uuid getValueAsUUID(int index); |
31b0f68b VK |
73 | |
74 | void addValue(const TCHAR *value); | |
99877e7b | 75 | void addValuePreallocated(TCHAR *value); |
31b0f68b | 76 | void setValue(const TCHAR*value); |
a65c1819 | 77 | |
87ffef60 VK |
78 | const TCHAR *getSubEntryValue(const TCHAR *name, int index = 0, const TCHAR *defaultValue = NULL) const; |
79 | INT32 getSubEntryValueAsInt(const TCHAR *name, int index = 0, INT32 defaultValue = 0) const; | |
80 | UINT32 getSubEntryValueAsUInt(const TCHAR *name, int index = 0, UINT32 defaultValue = 0) const; | |
81 | INT64 getSubEntryValueAsInt64(const TCHAR *name, int index = 0, INT64 defaultValue = 0) const; | |
82 | UINT64 getSubEntryValueAsUInt64(const TCHAR *name, int index = 0, UINT64 defaultValue = 0) const; | |
83 | bool getSubEntryValueAsBoolean(const TCHAR *name, int index = 0, bool defaultValue = false) const; | |
84 | uuid getSubEntryValueAsUUID(const TCHAR *name, int index = 0) const; | |
85 | ||
86 | const TCHAR *getAttribute(const TCHAR *name) const { return m_attributes.get(name); } | |
87 | INT32 getAttributeAsInt(const TCHAR *name, INT32 defaultValue = 0) const; | |
88 | UINT32 getAttributeAsUInt(const TCHAR *name, UINT32 defaultValue = 0) const; | |
89 | INT64 getAttributeAsInt64(const TCHAR *name, INT64 defaultValue = 0) const; | |
90 | UINT64 getAttributeAsUInt64(const TCHAR *name, UINT64 defaultValue = 0) const; | |
91 | bool getAttributeAsBoolean(const TCHAR *name, bool defaultValue = false) const; | |
31b0f68b | 92 | |
8b7ae7eb VK |
93 | void setAttribute(const TCHAR *name, const TCHAR *value) { m_attributes.set(name, value); } |
94 | void setAttributePreallocated(TCHAR *name, TCHAR *value) { m_attributes.setPreallocated(name, value); } | |
31b0f68b VK |
95 | void setAttribute(const TCHAR *name, INT32 value); |
96 | void setAttribute(const TCHAR *name, UINT32 value); | |
97 | void setAttribute(const TCHAR *name, INT64 value); | |
98 | void setAttribute(const TCHAR *name, UINT64 value); | |
99 | void setAttribute(const TCHAR *name, bool value); | |
8b7ae7eb | 100 | |
87ffef60 VK |
101 | const TCHAR *getFile() const { return m_file; } |
102 | int getLine() const { return m_line; } | |
e6c91aac | 103 | |
8a0dffac VK |
104 | void setName(const TCHAR *name); |
105 | ||
203e9d8a | 106 | ConfigEntry *createEntry(const TCHAR *name); |
87ffef60 VK |
107 | ConfigEntry *findEntry(const TCHAR *name) const; |
108 | ObjectArray<ConfigEntry> *getSubEntries(const TCHAR *mask) const; | |
109 | ObjectArray<ConfigEntry> *getOrderedSubEntries(const TCHAR *mask) const; | |
203e9d8a | 110 | void unlinkEntry(ConfigEntry *entry); |
abf35d58 | 111 | |
e77b1f4e | 112 | void print(FILE *file, int level, TCHAR *prefix); |
8a0dffac | 113 | void createXml(String &xml, int level = 0); |
e6c91aac VK |
114 | }; |
115 | ||
9387bc59 | 116 | /** |
9387bc59 VK |
117 | * Hierarchical config |
118 | */ | |
e6c91aac | 119 | class LIBNETXMS_EXPORTABLE Config |
8a5ba964 AK |
120 | { |
121 | private: | |
e6c91aac VK |
122 | ConfigEntry *m_root; |
123 | int m_errorCount; | |
203e9d8a | 124 | MUTEX m_mutex; |
e5f88e5a | 125 | StringMap m_aliases; |
0745c414 | 126 | bool m_allowMacroExpansion; |
e6c91aac VK |
127 | |
128 | protected: | |
129 | virtual void onError(const TCHAR *errorMessage); | |
d9794cb6 | 130 | |
e6c91aac | 131 | void error(const TCHAR *format, ...); |
8a0dffac | 132 | ConfigEntry *createEntry(const TCHAR *path); |
8a5ba964 AK |
133 | |
134 | public: | |
0745c414 | 135 | Config(bool allowMacroExpansion = true); |
98c0358d | 136 | virtual ~Config(); |
8a5ba964 | 137 | |
c17f6cbc | 138 | void lock() { MutexLock(m_mutex); } |
203e9d8a VK |
139 | void unlock() { MutexUnlock(m_mutex); } |
140 | ||
8a0dffac VK |
141 | void setTopLevelTag(const TCHAR *topLevelTag) { m_root->setName(topLevelTag); } |
142 | ||
a65c1819 | 143 | bool loadXmlConfig(const TCHAR *file, const char *topLevelTag = NULL); |
d9794cb6 | 144 | bool loadXmlConfigFromMemory(const char *xml, int xmlSize, const TCHAR *name = NULL, const char *topLevelTag = NULL, bool merge = true); |
31e21b08 AK |
145 | bool loadIniConfig(const TCHAR *file, const TCHAR *defaultIniSection, bool ignoreErrors = true); |
146 | bool loadConfig(const TCHAR *file, const TCHAR *defaultIniSection, bool ignoreErrors = true); | |
8a5ba964 | 147 | |
31e21b08 | 148 | bool loadConfigDirectory(const TCHAR *path, const TCHAR *defaultIniSection, bool ignoreErrors = true); |
e6c91aac | 149 | |
203e9d8a VK |
150 | void deleteEntry(const TCHAR *path); |
151 | ||
e6c91aac | 152 | ConfigEntry *getEntry(const TCHAR *path); |
ec19c32d | 153 | const TCHAR *getValue(const TCHAR *path, const TCHAR *defaultValue = NULL); |
31b0f68b VK |
154 | INT32 getValueAsInt(const TCHAR *path, INT32 defaultValue); |
155 | UINT32 getValueAsUInt(const TCHAR *path, UINT32 defaultValue); | |
156 | INT64 getValueAsInt64(const TCHAR *path, INT64 defaultValue); | |
157 | UINT64 getValueAsUInt64(const TCHAR *path, UINT64 defaultValue); | |
158 | bool getValueAsBoolean(const TCHAR *path, bool defaultValue); | |
999945fa | 159 | uuid getValueAsUUID(const TCHAR *path); |
8b7ae7eb VK |
160 | ObjectArray<ConfigEntry> *getSubEntries(const TCHAR *path, const TCHAR *mask); |
161 | ObjectArray<ConfigEntry> *getOrderedSubEntries(const TCHAR *path, const TCHAR *mask); | |
e6c91aac | 162 | |
8a0dffac | 163 | bool setValue(const TCHAR *path, const TCHAR *value); |
967893bb VK |
164 | bool setValue(const TCHAR *path, INT32 value); |
165 | bool setValue(const TCHAR *path, UINT32 value); | |
8a0dffac | 166 | bool setValue(const TCHAR *path, INT64 value); |
967893bb | 167 | bool setValue(const TCHAR *path, UINT64 value); |
8a0dffac | 168 | bool setValue(const TCHAR *path, double value); |
999945fa | 169 | bool setValue(const TCHAR *path, const uuid& value); |
8a0dffac | 170 | |
97a92859 | 171 | bool parseTemplate(const TCHAR *section, NX_CFG_TEMPLATE *cfgTemplate); |
e6c91aac | 172 | |
e5f88e5a | 173 | int getErrorCount() const { return m_errorCount; } |
abf35d58 VK |
174 | |
175 | void print(FILE *file); | |
8a0dffac | 176 | String createXml(); |
e5f88e5a VK |
177 | |
178 | void setAlias(const TCHAR *alias, const TCHAR *value) { if (alias != NULL) m_aliases.set(alias, value); else m_aliases.remove(alias); } | |
179 | const TCHAR *getAlias(const TCHAR *alias) const { return m_aliases.get(alias); } | |
8a5ba964 AK |
180 | }; |
181 | ||
182 | ||
183 | #endif |