license headers in libnetxms changed to LGPL
[public/netxms.git] / src / libnetxms / strmap.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NetXMS Foundation Library
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
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
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 **
16 ** You should have received a copy of the GNU Lesser General Public License
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"
25
26
27 //
28 // Constructors
29 //
30
31 StringMap::StringMap()
32 {
33 m_size = 0;
34 m_keys = NULL;
35 m_values = NULL;
36 }
37
38
39 //
40 // Copy constructor
41 //
42
43 StringMap::StringMap(const StringMap &src)
44 {
45 DWORD i;
46
47 m_size = src.m_size;
48 m_keys = (TCHAR **)malloc(sizeof(TCHAR *) * m_size);
49 m_values = (TCHAR **)malloc(sizeof(TCHAR *) * m_size);
50 for(i = 0; i < m_size; i++)
51 {
52 m_keys[i] = _tcsdup(src.m_keys[i]);
53 m_values[i] = _tcsdup(src.m_values[i]);
54 }
55 }
56
57
58 //
59 // Destructor
60 //
61
62 StringMap::~StringMap()
63 {
64 clear();
65 }
66
67
68 //
69 // Assignment
70 //
71
72 StringMap& StringMap::operator =(const StringMap &src)
73 {
74 DWORD i;
75
76 clear();
77 m_size = src.m_size;
78 m_keys = (TCHAR **)malloc(sizeof(TCHAR *) * m_size);
79 m_values = (TCHAR **)malloc(sizeof(TCHAR *) * m_size);
80 for(i = 0; i < m_size; i++)
81 {
82 m_keys[i] = _tcsdup(src.m_keys[i]);
83 m_values[i] = _tcsdup(src.m_values[i]);
84 }
85 return *this;
86 }
87
88
89 //
90 // Clear map
91 //
92
93 void StringMap::clear()
94 {
95 DWORD i;
96
97 for(i = 0; i < m_size; i++)
98 {
99 safe_free(m_keys[i]);
100 safe_free(m_values[i]);
101 }
102 m_size = 0;
103 safe_free_and_null(m_keys);
104 safe_free_and_null(m_values);
105 }
106
107
108 //
109 // Find value by key
110 //
111
112 DWORD StringMap::find(const TCHAR *key)
113 {
114 DWORD i;
115
116 for(i = 0; i < m_size; i++)
117 {
118 if (!_tcsicmp(key, m_keys[i]))
119 return i;
120 }
121 return INVALID_INDEX;
122 }
123
124
125 //
126 // Set value - arguments are preallocated dynamic strings
127 //
128
129 void StringMap::setPreallocated(TCHAR *key, TCHAR *value)
130 {
131 DWORD index;
132
133 index = find(key);
134 if (index != INVALID_INDEX)
135 {
136 free(key); // Not needed
137 safe_free(m_values[index]);
138 m_values[index] = value;
139 }
140 else
141 {
142 m_keys = (TCHAR **)realloc(m_keys, (m_size + 1) * sizeof(TCHAR *));
143 m_values = (TCHAR **)realloc(m_values, (m_size + 1) * sizeof(TCHAR *));
144 m_keys[m_size] = key;
145 m_values[m_size] = value;
146 m_size++;
147 }
148 }
149
150
151 //
152 // Set value
153 //
154
155 void StringMap::set(const TCHAR *key, const TCHAR *value)
156 {
157 DWORD index;
158
159 index = find(key);
160 if (index != INVALID_INDEX)
161 {
162 safe_free(m_values[index]);
163 m_values[index] = _tcsdup(value);
164 }
165 else
166 {
167 m_keys = (TCHAR **)realloc(m_keys, (m_size + 1) * sizeof(TCHAR *));
168 m_values = (TCHAR **)realloc(m_values, (m_size + 1) * sizeof(TCHAR *));
169 m_keys[m_size] = _tcsdup(key);
170 m_values[m_size] = _tcsdup(value);
171 m_size++;
172 }
173 }
174
175
176 //
177 // Get value by key
178 //
179
180 const TCHAR *StringMap::get(const TCHAR *key)
181 {
182 DWORD index;
183
184 index = find(key);
185 return (index != INVALID_INDEX) ? m_values[index] : NULL;
186 }
187
188
189 //
190 // Delete value
191 //
192
193 void StringMap::remove(const TCHAR *key)
194 {
195 DWORD index;
196
197 index = find(key);
198 if (index != INVALID_INDEX)
199 {
200 safe_free(m_keys[index]);
201 safe_free(m_values[index]);
202 m_size--;
203 memmove(&m_keys[index], &m_keys[index + 1], sizeof(TCHAR *) * (m_size - index));
204 memmove(&m_values[index], &m_values[index + 1], sizeof(TCHAR *) * (m_size - index));
205 }
206 }