license headers in libnetxms changed to LGPL
[public/netxms.git] / src / libnetxms / stringlist.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: stringlist.cpp
21 **
22 **/
23
24 #include "libnetxms.h"
25
26 #define ALLOCATION_STEP 16
27
28
29 //
30 // Constructor
31 //
32
33 StringList::StringList()
34 {
35 m_count = 0;
36 m_allocated = ALLOCATION_STEP;
37 m_values = (TCHAR **)malloc(sizeof(TCHAR *) * m_allocated);
38 memset(m_values, 0, sizeof(TCHAR *) * m_allocated);
39 }
40
41
42 //
43 // Destructor
44 //
45
46 StringList::~StringList()
47 {
48 for(int i = 0; i < m_count; i++)
49 safe_free(m_values[i]);
50 safe_free(m_values);
51 }
52
53
54 //
55 // Clear list
56 //
57
58 void StringList::clear()
59 {
60 for(int i = 0; i < m_count; i++)
61 safe_free(m_values[i]);
62 m_count = 0;
63 memset(m_values, 0, sizeof(TCHAR *) * m_allocated);
64 }
65
66
67 //
68 // Add preallocated string to list
69 //
70
71 void StringList::addPreallocated(TCHAR *value)
72 {
73 if (m_allocated == m_count)
74 {
75 m_allocated += ALLOCATION_STEP;
76 m_values = (TCHAR **)realloc(m_values, sizeof(TCHAR *) * m_allocated);
77 }
78 m_values[m_count++] = value;
79 }
80
81
82 //
83 // Add string to list
84 //
85
86 void StringList::add(const TCHAR *value)
87 {
88 addPreallocated(_tcsdup(value));
89 }
90
91
92 //
93 // Add signed 32-bit integer as string
94 //
95
96 void StringList::add(LONG value)
97 {
98 TCHAR buffer[32];
99
100 _sntprintf(buffer, 32, _T("%d"), value);
101 add(buffer);
102 }
103
104
105 //
106 // Add unsigned 32-bit integer as string
107 //
108
109 void StringList::add(DWORD value)
110 {
111 TCHAR buffer[32];
112
113 _sntprintf(buffer, 32, _T("%u"), value);
114 add(buffer);
115 }
116
117
118 //
119 // Add signed 64-bit integer as string
120 //
121
122 void StringList::add(INT64 value)
123 {
124 TCHAR buffer[32];
125
126 _sntprintf(buffer, 32, INT64_FMT, value);
127 add(buffer);
128 }
129
130
131 //
132 // Add unsigned 64-bit integer as string
133 //
134
135 void StringList::add(QWORD value)
136 {
137 TCHAR buffer[32];
138
139 _sntprintf(buffer, 32, UINT64_FMT, value);
140 add(buffer);
141 }
142
143
144 //
145 // Add floating point number as string
146 //
147
148 void StringList::add(double value)
149 {
150 TCHAR buffer[64];
151
152 _sntprintf(buffer, 64, _T("%f"), value);
153 add(buffer);
154 }