license headers in libnetxms changed to LGPL
[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 linkEntry(ConfigEntry *entry) { entry->m_next = m_next; m_next = entry; }
50 void addEntry(ConfigEntry *entry) { entry->m_parent = this; entry->m_next = m_childs; m_childs = 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
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 LONG getValueInt(int index = 0, LONG defaultValue = 0);
65 DWORD getValueUInt(int index = 0, DWORD defaultValue = 0);
66 INT64 getValueInt64(int index = 0, INT64 defaultValue = 0);
67 QWORD getValueUInt64(int index = 0, QWORD defaultValue = 0);
68 bool getValueBoolean(int index = 0, bool defaultValue = false);
69
70 const TCHAR *getSubEntryValue(const TCHAR *name, int index = 0, const TCHAR *defaultValue = NULL);
71 LONG getSubEntryValueInt(const TCHAR *name, int index = 0, LONG defaultValue = 0);
72 DWORD getSubEntryValueUInt(const TCHAR *name, int index = 0, DWORD defaultValue = 0);
73 INT64 getSubEntryValueInt64(const TCHAR *name, int index = 0, INT64 defaultValue = 0);
74 QWORD getSubEntryValueUInt64(const TCHAR *name, int index = 0, QWORD defaultValue = 0);
75 bool getSubEntryValueBoolean(const TCHAR *name, int index = 0, bool defaultValue = false);
76
77 const TCHAR *getFile() { return m_file; }
78 int getLine() { return m_line; }
79
80 void addValue(const TCHAR *value);
81 void setValue(const TCHAR*value);
82
83 ConfigEntry *findEntry(const TCHAR *name);
84 ConfigEntryList *getSubEntries(const TCHAR *mask);
85 ConfigEntryList *getOrderedSubEntries(const TCHAR *mask);
86
87 void print(FILE *file, int level);
88 };
89
90
91 //
92 // List of config entries
93 //
94
95 class ConfigEntryList
96 {
97 private:
98 ConfigEntry **m_list;
99 int m_size;
100
101 public:
102 ConfigEntryList(ConfigEntry **list, int size) { m_list = list; m_size = size; }
103 ~ConfigEntryList() { safe_free(m_list); }
104
105 int getSize() { return m_size; }
106 ConfigEntry *getEntry(int index) { return ((index >= 0) && (index < m_size)) ? m_list[index] : NULL; }
107
108 void sortById();
109 };
110
111
112 //
113 // Config class
114 //
115
116 class LIBNETXMS_EXPORTABLE Config
117 {
118 private:
119 ConfigEntry *m_root;
120 int m_errorCount;
121
122 protected:
123 virtual void onError(const TCHAR *errorMessage);
124
125 void error(const TCHAR *format, ...);
126
127 public:
128 Config();
129 ~Config();
130
131 bool loadXmlConfig(const TCHAR *file, const char *topLevelTag = NULL);
132 bool loadXmlConfigFromMemory(const char *xml, int xmlSize, const TCHAR *name = NULL, const char *topLevelTag = NULL);
133 bool loadIniConfig(const TCHAR *file, const TCHAR *defaultIniSection);
134 bool loadConfig(const TCHAR *file, const TCHAR *defaultIniSection);
135
136 bool loadConfigDirectory(const TCHAR *path, const TCHAR *defaultIniSection);
137
138 ConfigEntry *getEntry(const TCHAR *path);
139 const TCHAR *getValue(const TCHAR *path, const TCHAR *defaultValue = NULL);
140 LONG getValueInt(const TCHAR *path, LONG defaultValue);
141 DWORD getValueUInt(const TCHAR *path, DWORD defaultValue);
142 INT64 getValueInt64(const TCHAR *path, INT64 defaultValue);
143 QWORD getValueUInt64(const TCHAR *path, QWORD defaultValue);
144 bool getValueBoolean(const TCHAR *path, bool defaultValue);
145 ConfigEntryList *getSubEntries(const TCHAR *path, const TCHAR *mask);
146 ConfigEntryList *getOrderedSubEntries(const TCHAR *path, const TCHAR *mask);
147
148 bool parseTemplate(const TCHAR *section, NX_CFG_TEMPLATE *cfgTemplate);
149
150 int getErrorCount() { return m_errorCount; }
151
152 void print(FILE *file);
153 };
154
155
156 #endif