Further non-Unicode fixes; LoraWAN subagent refactoring
[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 #include <uuid.h>
29
30 class Config;
31
32 /**
33 * Config entry
34 */
35 class LIBNETXMS_EXPORTABLE ConfigEntry
36 {
37 private:
38 TCHAR *m_name;
39 ConfigEntry *m_parent;
40 ConfigEntry *m_next;
41 ConfigEntry *m_first;
42 ConfigEntry *m_last;
43 int m_valueCount;
44 TCHAR **m_values;
45 TCHAR *m_file;
46 int m_line;
47 int m_id;
48 StringMap m_attributes;
49 const Config *m_owner;
50
51 void addEntry(ConfigEntry *entry);
52 void linkEntry(ConfigEntry *entry) { entry->m_next = m_next; m_next = entry; }
53
54 public:
55 ConfigEntry(const TCHAR *name, ConfigEntry *parent, const Config *owner, const TCHAR *file, int line, int id);
56 ~ConfigEntry();
57
58 ConfigEntry *getNext() const { return m_next; }
59 ConfigEntry *getParent() const { return m_parent; }
60
61 const TCHAR *getName() const { return m_name; }
62 int getId() const { return m_id; }
63 int getValueCount() const { return m_valueCount; }
64 int getConcatenatedValuesLength();
65
66 const TCHAR *getValue(int index = 0);
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);
72 uuid getValueAsUUID(int index);
73
74 void addValue(const TCHAR *value);
75 void addValuePreallocated(TCHAR *value);
76 void setValue(const TCHAR*value);
77
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;
92
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); }
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);
100
101 const TCHAR *getFile() const { return m_file; }
102 int getLine() const { return m_line; }
103
104 void setName(const TCHAR *name);
105
106 ConfigEntry *createEntry(const TCHAR *name);
107 ConfigEntry *findEntry(const TCHAR *name) const;
108 ObjectArray<ConfigEntry> *getSubEntries(const TCHAR *mask) const;
109 ObjectArray<ConfigEntry> *getOrderedSubEntries(const TCHAR *mask) const;
110 void unlinkEntry(ConfigEntry *entry);
111
112 void print(FILE *file, int level, TCHAR *prefix);
113 void createXml(String &xml, int level = 0);
114 };
115
116 /**
117 * Hierarchical config
118 */
119 class LIBNETXMS_EXPORTABLE Config
120 {
121 private:
122 ConfigEntry *m_root;
123 int m_errorCount;
124 MUTEX m_mutex;
125 StringMap m_aliases;
126 bool m_allowMacroExpansion;
127
128 protected:
129 virtual void onError(const TCHAR *errorMessage);
130
131 void error(const TCHAR *format, ...);
132 ConfigEntry *createEntry(const TCHAR *path);
133
134 public:
135 Config(bool allowMacroExpansion = true);
136 virtual ~Config();
137
138 void lock() { MutexLock(m_mutex); }
139 void unlock() { MutexUnlock(m_mutex); }
140
141 void setTopLevelTag(const TCHAR *topLevelTag) { m_root->setName(topLevelTag); }
142
143 bool loadXmlConfig(const TCHAR *file, const char *topLevelTag = NULL);
144 bool loadXmlConfigFromMemory(const char *xml, int xmlSize, const TCHAR *name = NULL, const char *topLevelTag = NULL, bool merge = true);
145 bool loadIniConfig(const TCHAR *file, const TCHAR *defaultIniSection, bool ignoreErrors = true);
146 bool loadConfig(const TCHAR *file, const TCHAR *defaultIniSection, bool ignoreErrors = true);
147
148 bool loadConfigDirectory(const TCHAR *path, const TCHAR *defaultIniSection, bool ignoreErrors = true);
149
150 void deleteEntry(const TCHAR *path);
151
152 ConfigEntry *getEntry(const TCHAR *path);
153 const TCHAR *getValue(const TCHAR *path, const TCHAR *defaultValue = NULL);
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);
159 uuid getValueAsUUID(const TCHAR *path);
160 ObjectArray<ConfigEntry> *getSubEntries(const TCHAR *path, const TCHAR *mask);
161 ObjectArray<ConfigEntry> *getOrderedSubEntries(const TCHAR *path, const TCHAR *mask);
162
163 bool setValue(const TCHAR *path, const TCHAR *value);
164 bool setValue(const TCHAR *path, INT32 value);
165 bool setValue(const TCHAR *path, UINT32 value);
166 bool setValue(const TCHAR *path, INT64 value);
167 bool setValue(const TCHAR *path, UINT64 value);
168 bool setValue(const TCHAR *path, double value);
169 bool setValue(const TCHAR *path, const uuid& value);
170
171 bool parseTemplate(const TCHAR *section, NX_CFG_TEMPLATE *cfgTemplate);
172
173 int getErrorCount() const { return m_errorCount; }
174
175 void print(FILE *file);
176 String createXml();
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); }
180 };
181
182
183 #endif