Changelog update
[public/netxms.git] / src / libnetxms / strmap.cpp
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
3** NetXMS Foundation Library
ca0a165b 4** Copyright (C) 2003-2016 Victor Kirhenshtein
5039dede
AK
5**
6** This program is free software; you can redistribute it and/or modify
68f384ea
VK
7** it under the terms of the GNU Lesser General Public License as published
8** by the Free Software Foundation; either version 3 of the License, or
5039dede
AK
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
68f384ea 16** You should have received a copy of the GNU Lesser General Public License
5039dede
AK
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19**
20** File: strmap.cpp
21**
22**/
23
24#include "libnetxms.h"
400e55c4 25#include "strmap-internal.h"
c7c23e6d 26#include <nxcpapi.h>
5039dede 27
3fe40613
VK
28/**
29 * Copy constructor
30 */
31StringMap::StringMap(const StringMap &src) : StringMapBase(true)
5039dede 32{
3fe40613 33 m_objectOwner = src.m_objectOwner;
400e55c4
VK
34 m_ignoreCase = src.m_ignoreCase;
35 m_objectDestructor = src.m_objectDestructor;
36
37 StringMapEntry *entry, *tmp;
38 HASH_ITER(hh, src.m_data, entry, tmp)
39 {
82966fa6 40 setObject(_tcsdup(m_ignoreCase ? entry->originalKey : entry->key), _tcsdup((TCHAR *)entry->value), true);
400e55c4 41 }
5039dede
AK
42}
43
3fe40613
VK
44/**
45 * Destructor
46 */
5039dede
AK
47
48StringMap::~StringMap()
49{
5039dede
AK
50}
51
3fe40613
VK
52/**
53 * Assignment
54 */
fb986055 55StringMap& StringMap::operator =(const StringMap &src)
5039dede 56{
fb986055 57 clear();
400e55c4
VK
58 m_objectOwner = src.m_objectOwner;
59 m_ignoreCase = src.m_ignoreCase;
60 m_objectDestructor = src.m_objectDestructor;
61
62 StringMapEntry *entry, *tmp;
63 HASH_ITER(hh, src.m_data, entry, tmp)
64 {
82966fa6 65 setObject(_tcsdup(m_ignoreCase ? entry->originalKey : entry->key), _tcsdup((TCHAR *)entry->value), true);
400e55c4 66 }
5039dede
AK
67 return *this;
68}
69
3fe40613 70/**
a6312bd6
VK
71 * Add all values from another string map
72 */
dedeffb3 73void StringMap::addAll(const StringMap *src)
a6312bd6 74{
400e55c4
VK
75 StringMapEntry *entry, *tmp;
76 HASH_ITER(hh, src->m_data, entry, tmp)
77 {
82966fa6 78 setObject(_tcsdup(src->m_ignoreCase ? entry->originalKey : entry->key), _tcsdup((TCHAR *)entry->value), true);
400e55c4 79 }
a6312bd6
VK
80}
81
82/**
967893bb 83 * Set value from UINT32
3fe40613 84 */
967893bb 85void StringMap::set(const TCHAR *key, UINT32 value)
5ad2167d
VK
86{
87 TCHAR buffer[32];
88
89 _sntprintf(buffer, 32, _T("%u"), (unsigned int)value);
90 set(key, buffer);
91}
92
3fe40613 93/**
7ec22f81 94 * Get value by key as INT32
3fe40613 95 */
7ec22f81 96INT32 StringMap::getInt32(const TCHAR *key, INT32 defaultValue) const
5ad2167d
VK
97{
98 const TCHAR *value = get(key);
99 if (value == NULL)
100 return defaultValue;
7ec22f81
VK
101 return _tcstol(value, NULL, 0);
102}
103
104/**
105 * Get value by key as UINT32
106 */
107UINT32 StringMap::getUInt32(const TCHAR *key, UINT32 defaultValue) const
108{
109 const TCHAR *value = get(key);
110 if (value == NULL)
111 return defaultValue;
112 return _tcstoul(value, NULL, 0);
113}
114
115/**
116 * Get value by key as INT64
117 */
118INT64 StringMap::getInt64(const TCHAR *key, INT64 defaultValue) const
119{
120 const TCHAR *value = get(key);
121 if (value == NULL)
122 return defaultValue;
123 return _tcstoll(value, NULL, 0);
124}
125
126/**
127 * Get value by key as UINT64
128 */
129UINT64 StringMap::getUInt64(const TCHAR *key, UINT64 defaultValue) const
130{
131 const TCHAR *value = get(key);
132 if (value == NULL)
133 return defaultValue;
134 return _tcstoull(value, NULL, 0);
135}
136
137/**
138 * Get value by key as double
139 */
140double StringMap::getDouble(const TCHAR *key, double defaultValue) const
141{
142 const TCHAR *value = get(key);
143 if (value == NULL)
144 return defaultValue;
145 return _tcstod(value, NULL);
5ad2167d
VK
146}
147
3fe40613
VK
148/**
149 * Get value by key as boolean
150 */
6e14de7c 151bool StringMap::getBoolean(const TCHAR *key, bool defaultValue) const
6acb8479
VK
152{
153 const TCHAR *value = get(key);
154 if (value == NULL)
155 return defaultValue;
156 if (!_tcsicmp(value, _T("false")))
157 return false;
158 if (!_tcsicmp(value, _T("true")))
159 return true;
160 return (_tcstoul(value, NULL, 0) != 0) ? true : false;
161}
400e55c4
VK
162
163/**
164 * Fill NXCP message with map data
165 */
6e14de7c 166void StringMap::fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId) const
400e55c4 167{
b368969c 168 msg->setField(sizeFieldId, (UINT32)size());
400e55c4
VK
169 UINT32 id = baseFieldId;
170 StringMapEntry *entry, *tmp;
171 HASH_ITER(hh, m_data, entry, tmp)
172 {
b368969c
VK
173 msg->setField(id++, m_ignoreCase ? entry->originalKey : entry->key);
174 msg->setField(id++, (TCHAR *)entry->value);
400e55c4
VK
175 }
176}
ca0a165b
VK
177
178/**
179 * Load data from NXCP message
180 */
181void StringMap::loadMessage(const NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId)
182{
183 int count = msg->getFieldAsInt32(sizeFieldId);
184 UINT32 id = baseFieldId;
185 for(int i = 0; i < count; i++)
186 {
187 TCHAR *key = msg->getFieldAsString(id++);
188 TCHAR *value = msg->getFieldAsString(id++);
189 setPreallocated(key, value);
190 }
191}
6336bba3
VK
192
193/**
194 * Serialize as JSON
195 */
88e14cac 196json_t *StringMap::toJson() const
6336bba3
VK
197{
198 json_t *root = json_array();
199 StringMapEntry *entry, *tmp;
200 HASH_ITER(hh, m_data, entry, tmp)
201 {
202 json_t *e = json_array();
203 json_array_append_new(e, json_string_t(m_ignoreCase ? entry->originalKey : entry->key));
204 json_array_append_new(e, json_string_t((TCHAR *)entry->value));
205 json_array_append_new(root, e);
206 }
207 return root;
208}