UUID functions renamed to avoid conflict with Mac OS X system functions
[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 * Config entry
32 */
33 class LIBNETXMS_EXPORTABLE ConfigEntry
34 {
35 private:
36 TCHAR *m_name;
37 ConfigEntry *m_parent;
38 ConfigEntry *m_next;
39 ConfigEntry *m_first;
40 ConfigEntry *m_last;
41 int m_valueCount;
42 TCHAR **m_values;
43 TCHAR *m_file;
44 int m_line;
45 int m_id;
46 StringMap m_attributes;
47
48 void addEntry(ConfigEntry *entry);
49 void linkEntry(ConfigEntry *entry) { entry->m_next = m_next; m_next = entry; }
50
51 public:
52 ConfigEntry(const TCHAR *name, ConfigEntry *parent, const TCHAR *file, int line, int id);
53 ~ConfigEntry();
54
55 ConfigEntry *getNext() { return m_next; }
56 ConfigEntry *getParent() { return m_parent; }
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 INT32 getValueAsInt(int index = 0, INT32 defaultValue = 0);
65 UINT32 getValueAsUInt(int index = 0, UINT32 defaultValue = 0);
66 INT64 getValueAsInt64(int index = 0, INT64 defaultValue = 0);
67 UINT64 getValueAsUInt64(int index = 0, UINT64 defaultValue = 0);
68 bool getValueAsBoolean(int index = 0, bool defaultValue = false);
69 uuid getValueAsUUID(int index);
70
71 void addValue(const TCHAR *value);
72 void addValuePreallocated(TCHAR *value);
73 void setValue(const TCHAR*value);
74
75 const TCHAR *getSubEntryValue(const TCHAR *name, int index = 0, const TCHAR *defaultValue = NULL);
76 INT32 getSubEntryValueAsInt(const TCHAR *name, int index = 0, INT32 defaultValue = 0);
77 UINT32 getSubEntryValueAsUInt(const TCHAR *name, int index = 0, UINT32 defaultValue = 0);
78 INT64 getSubEntryValueAsInt64(const TCHAR *name, int index = 0, INT64 defaultValue = 0);
79 UINT64 getSubEntryValueAsUInt64(const TCHAR *name, int index = 0, UINT64 defaultValue = 0);
80 bool getSubEntryValueAsBoolean(const TCHAR *name, int index = 0, bool defaultValue = false);
81 uuid getSubEntryValueAsUUID(const TCHAR *name, int index = 0);
82
83 const TCHAR *getAttribute(const TCHAR *name) { return m_attributes.get(name); }
84 INT32 getAttributeAsInt(const TCHAR *name, INT32 defaultValue = 0);
85 UINT32 getAttributeAsUInt(const TCHAR *name, UINT32 defaultValue = 0);
86 INT64 getAttributeAsInt64(const TCHAR *name, INT64 defaultValue = 0);
87 UINT64 getAttributeAsUInt64(const TCHAR *name, UINT64 defaultValue = 0);
88 bool getAttributeAsBoolean(const TCHAR *name, bool defaultValue = false);
89
90 void setAttribute(const TCHAR *name, const TCHAR *value) { m_attributes.set(name, value); }
91 void setAttributePreallocated(TCHAR *name, TCHAR *value) { m_attributes.setPreallocated(name, value); }
92 void setAttribute(const TCHAR *name, INT32 value);
93 void setAttribute(const TCHAR *name, UINT32 value);
94 void setAttribute(const TCHAR *name, INT64 value);
95 void setAttribute(const TCHAR *name, UINT64 value);
96 void setAttribute(const TCHAR *name, bool value);
97
98 const TCHAR *getFile() { return m_file; }
99 int getLine() { return m_line; }
100
101 void setName(const TCHAR *name);
102
103 ConfigEntry *createEntry(const TCHAR *name);
104 ConfigEntry *findEntry(const TCHAR *name);
105 ObjectArray<ConfigEntry> *getSubEntries(const TCHAR *mask);
106 ObjectArray<ConfigEntry> *getOrderedSubEntries(const TCHAR *mask);
107 void unlinkEntry(ConfigEntry *entry);
108
109 void print(FILE *file, int level, TCHAR *prefix);
110 void createXml(String &xml, int level = 0);
111 };
112
113 /**
114 * Hierarchical config
115 */
116 class LIBNETXMS_EXPORTABLE Config
117 {
118 private:
119 ConfigEntry *m_root;
120 int m_errorCount;
121 MUTEX m_mutex;
122
123 protected:
124 virtual void onError(const TCHAR *errorMessage);
125
126 void error(const TCHAR *format, ...);
127 ConfigEntry *createEntry(const TCHAR *path);
128
129 public:
130 Config();
131 virtual ~Config();
132
133 void lock() { MutexLock(m_mutex); }
134 void unlock() { MutexUnlock(m_mutex); }
135
136 void setTopLevelTag(const TCHAR *topLevelTag) { m_root->setName(topLevelTag); }
137
138 bool loadXmlConfig(const TCHAR *file, const char *topLevelTag = NULL);
139 bool loadXmlConfigFromMemory(const char *xml, int xmlSize, const TCHAR *name = NULL, const char *topLevelTag = NULL, bool merge = true);
140 bool loadIniConfig(const TCHAR *file, const TCHAR *defaultIniSection, bool ignoreErrors = true);
141 bool loadConfig(const TCHAR *file, const TCHAR *defaultIniSection, bool ignoreErrors = true);
142
143 bool loadConfigDirectory(const TCHAR *path, const TCHAR *defaultIniSection, bool ignoreErrors = true);
144
145 void deleteEntry(const TCHAR *path);
146
147 ConfigEntry *getEntry(const TCHAR *path);
148 const TCHAR *getValue(const TCHAR *path, const TCHAR *defaultValue = NULL);
149 INT32 getValueAsInt(const TCHAR *path, INT32 defaultValue);
150 UINT32 getValueAsUInt(const TCHAR *path, UINT32 defaultValue);
151 INT64 getValueAsInt64(const TCHAR *path, INT64 defaultValue);
152 UINT64 getValueAsUInt64(const TCHAR *path, UINT64 defaultValue);
153 bool getValueAsBoolean(const TCHAR *path, bool defaultValue);
154 uuid getValueAsUUID(const TCHAR *path);
155 ObjectArray<ConfigEntry> *getSubEntries(const TCHAR *path, const TCHAR *mask);
156 ObjectArray<ConfigEntry> *getOrderedSubEntries(const TCHAR *path, const TCHAR *mask);
157
158 bool setValue(const TCHAR *path, const TCHAR *value);
159 bool setValue(const TCHAR *path, INT32 value);
160 bool setValue(const TCHAR *path, UINT32 value);
161 bool setValue(const TCHAR *path, INT64 value);
162 bool setValue(const TCHAR *path, UINT64 value);
163 bool setValue(const TCHAR *path, double value);
164 bool setValue(const TCHAR *path, const uuid& value);
165
166 bool parseTemplate(const TCHAR *section, NX_CFG_TEMPLATE *cfgTemplate);
167
168 int getErrorCount() { return m_errorCount; }
169
170 void print(FILE *file);
171 String createXml();
172 };
173
174
175 #endif