added persistent storage in agent; added agent policy registration in persistent...
[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 addEntry(ConfigEntry *entry) { entry->m_parent = this; entry->m_next = m_childs; m_childs = entry; }
50 void linkEntry(ConfigEntry *entry) { entry->m_next = m_next; m_next = 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 ConfigEntry *getParent() { return m_parent; }
58
59 const TCHAR *getName() { return m_name; }
60 int getId() { return m_id; }
61 int getValueCount() { return m_valueCount; }
62 int getConcatenatedValuesLength();
63
64 const TCHAR *getValue(int index = 0);
65 LONG getValueInt(int index = 0, LONG defaultValue = 0);
66 DWORD getValueUInt(int index = 0, DWORD defaultValue = 0);
67 INT64 getValueInt64(int index = 0, INT64 defaultValue = 0);
68 QWORD getValueUInt64(int index = 0, QWORD defaultValue = 0);
69 bool getValueBoolean(int index = 0, bool defaultValue = false);
70 bool getValueUUID(int index, uuid_t uuid);
71
72 const TCHAR *getSubEntryValue(const TCHAR *name, int index = 0, const TCHAR *defaultValue = NULL);
73 LONG getSubEntryValueInt(const TCHAR *name, int index = 0, LONG defaultValue = 0);
74 DWORD getSubEntryValueUInt(const TCHAR *name, int index = 0, DWORD defaultValue = 0);
75 INT64 getSubEntryValueInt64(const TCHAR *name, int index = 0, INT64 defaultValue = 0);
76 QWORD getSubEntryValueUInt64(const TCHAR *name, int index = 0, QWORD defaultValue = 0);
77 bool getSubEntryValueBoolean(const TCHAR *name, int index = 0, bool defaultValue = false);
78 bool getSubEntryValueUUID(const TCHAR *name, uuid_t uuid, int index = 0);
79
80 const TCHAR *getFile() { return m_file; }
81 int getLine() { return m_line; }
82
83 void setName(const TCHAR *name);
84
85 void addValue(const TCHAR *value);
86 void setValue(const TCHAR*value);
87
88 ConfigEntry *createEntry(const TCHAR *name);
89 ConfigEntry *findEntry(const TCHAR *name);
90 ConfigEntryList *getSubEntries(const TCHAR *mask);
91 ConfigEntryList *getOrderedSubEntries(const TCHAR *mask);
92 void unlinkEntry(ConfigEntry *entry);
93
94 void print(FILE *file, int level);
95 void createXml(String &xml, int level = 0);
96 };
97
98
99 //
100 // List of config entries
101 //
102
103 class ConfigEntryList
104 {
105 private:
106 ConfigEntry **m_list;
107 int m_size;
108
109 public:
110 ConfigEntryList(ConfigEntry **list, int size) { m_list = list; m_size = size; }
111 ~ConfigEntryList() { safe_free(m_list); }
112
113 int getSize() { return m_size; }
114 ConfigEntry *getEntry(int index) { return ((index >= 0) && (index < m_size)) ? m_list[index] : NULL; }
115
116 void sortById();
117 };
118
119
120 //
121 // Config class
122 //
123
124 class LIBNETXMS_EXPORTABLE Config
125 {
126 private:
127 ConfigEntry *m_root;
128 int m_errorCount;
129 MUTEX m_mutex;
130
131 protected:
132 virtual void onError(const TCHAR *errorMessage);
133
134 void error(const TCHAR *format, ...);
135 ConfigEntry *createEntry(const TCHAR *path);
136
137 public:
138 Config();
139 ~Config();
140
141 void lock() { MutexLock(m_mutex, INFINITE); }
142 void unlock() { MutexUnlock(m_mutex); }
143
144 void setTopLevelTag(const TCHAR *topLevelTag) { m_root->setName(topLevelTag); }
145
146 bool loadXmlConfig(const TCHAR *file, const char *topLevelTag = NULL);
147 bool loadXmlConfigFromMemory(const char *xml, int xmlSize, const TCHAR *name = NULL, const char *topLevelTag = NULL);
148 bool loadIniConfig(const TCHAR *file, const TCHAR *defaultIniSection);
149 bool loadConfig(const TCHAR *file, const TCHAR *defaultIniSection);
150
151 bool loadConfigDirectory(const TCHAR *path, const TCHAR *defaultIniSection);
152
153 void deleteEntry(const TCHAR *path);
154
155 ConfigEntry *getEntry(const TCHAR *path);
156 const TCHAR *getValue(const TCHAR *path, const TCHAR *defaultValue = NULL);
157 LONG getValueInt(const TCHAR *path, LONG defaultValue);
158 DWORD getValueUInt(const TCHAR *path, DWORD defaultValue);
159 INT64 getValueInt64(const TCHAR *path, INT64 defaultValue);
160 QWORD getValueUInt64(const TCHAR *path, QWORD defaultValue);
161 bool getValueBoolean(const TCHAR *path, bool defaultValue);
162 bool getValueUUID(const TCHAR *path, uuid_t uuid);
163 ConfigEntryList *getSubEntries(const TCHAR *path, const TCHAR *mask);
164 ConfigEntryList *getOrderedSubEntries(const TCHAR *path, const TCHAR *mask);
165
166 bool setValue(const TCHAR *path, const TCHAR *value);
167 bool setValue(const TCHAR *path, LONG value);
168 bool setValue(const TCHAR *path, DWORD value);
169 bool setValue(const TCHAR *path, INT64 value);
170 bool setValue(const TCHAR *path, QWORD value);
171 bool setValue(const TCHAR *path, double value);
172 bool setValue(const TCHAR *path, uuid_t value);
173
174 bool parseTemplate(const TCHAR *section, NX_CFG_TEMPLATE *cfgTemplate);
175
176 int getErrorCount() { return m_errorCount; }
177
178 void print(FILE *file);
179 String createXml();
180 };
181
182
183 #endif