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