43339effd0fb9525f276e43ef28d71e08f893ade
[public/netxms.git] / src / libnetxms / table.cpp
1 /* $Id: table.cpp,v 1.1 2006-11-03 08:58:58 victor Exp $ */
2 /*
3 ** NetXMS - Network Management System
4 ** Copyright (C) 2003, 2004, 2005, 2006 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 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 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: table.cpp
21 **
22 **/
23
24 #include "libnetxms.h"
25
26
27 //
28 // Table constructor
29 //
30
31 Table::Table()
32 {
33 m_nNumRows = 0;
34 m_nNumCols = 0;
35 m_ppData = NULL;
36 m_ppColNames = NULL;
37 }
38
39
40 //
41 // Table destructor
42 //
43
44 Table::~Table()
45 {
46 int i;
47
48 for(i = 0; i < m_nNumCols; i++)
49 safe_free(m_ppColNames[i]);
50 safe_free(m_ppColNames);
51
52 for(i = 0; i < m_nNumRows * m_nNumCols; i++)
53 safe_free(m_ppData[i]);
54 safe_free(m_ppData);
55 }
56
57
58 //
59 // Add new column
60 //
61
62 void Table::AddColumn(TCHAR *pszName)
63 {
64 m_ppColNames = (TCHAR **)realloc(m_ppColNames, sizeof(TCHAR *) * (m_nNumCols + 1));
65 m_ppColNames[m_nNumCols] = _tcsdup(pszName);
66
67 if (m_nNumRows > 0)
68 {
69 TCHAR **ppNewData;
70 int i, nPosOld, nPosNew;
71
72 ppNewData = (TCHAR **)malloc(sizeof(TCHAR *) * m_nNumRows * (m_nNumCols + 1));
73 for(i = 0, nPosOld = 0, nPosNew = 0; i < m_nNumRows; i++)
74 {
75 memcpy(&ppNewData[nPosNew], &m_ppData[nPosOld], sizeof(TCHAR *) * m_nNumCols);
76 ppNewData[nPosNew + m_nNumCols] = NULL;
77 nPosOld += m_nNumCols;
78 nPosNew += m_nNumCols + 1;
79 }
80 safe_free(m_ppData);
81 m_ppData = ppNewData;
82 }
83
84 m_nNumCols++;
85 }
86
87
88 //
89 // Add new row
90 //
91
92 void Table::AddRow(void)
93 {
94 if (m_nNumCols > 0)
95 {
96 m_ppData = (TCHAR **)realloc(m_ppData, sizeof(TCHAR *) * (m_nNumRows + 1) * m_nNumCols);
97 memset(&m_ppData[m_nNumRows * m_nNumCols], 0, sizeof(TCHAR *) * m_nNumCols);
98 }
99 m_nNumRows++;
100 }
101
102
103 //
104 // Set data at position
105 //
106
107 void Table::SetAt(int nRow, int nCol, TCHAR *pszData)
108 {
109 if ((nRow < 0) || (nRow >= m_nNumRows) ||
110 (nCol < 0) || (nCol >= m_nNumCols))
111 return;
112
113 safe_free(m_ppData[nRow * m_nNumCols + nCol]);
114 m_ppData[nRow * m_nNumCols + nCol] = _tcsdup(pszData);
115 }
116
117 void Table::SetAt(int nRow, int nCol, LONG nData)
118 {
119 TCHAR szBuffer[32];
120
121 _stprintf(szBuffer, _T("%d"), nData);
122 SetAt(nRow, nCol, szBuffer);
123 }
124
125 void Table::SetAt(int nRow, int nCol, DWORD dwData)
126 {
127 TCHAR szBuffer[32];
128
129 _stprintf(szBuffer, _T("%u"), dwData);
130 SetAt(nRow, nCol, szBuffer);
131 }
132
133 void Table::SetAt(int nRow, int nCol, INT64 nData)
134 {
135 TCHAR szBuffer[32];
136
137 _stprintf(szBuffer, INT64_FMT, nData);
138 SetAt(nRow, nCol, szBuffer);
139 }
140
141 void Table::SetAt(int nRow, int nCol, QWORD qwData)
142 {
143 TCHAR szBuffer[32];
144
145 _stprintf(szBuffer, UINT64_FMT, qwData);
146 SetAt(nRow, nCol, szBuffer);
147 }
148
149 void Table::SetAt(int nRow, int nCol, double dData)
150 {
151 TCHAR szBuffer[32];
152
153 _stprintf(szBuffer, _T("%f"), dData);
154 SetAt(nRow, nCol, szBuffer);
155 }
156
157
158 //
159 // Get data from position
160 //
161
162 TCHAR *Table::GetAsString(int nRow, int nCol)
163 {
164 if ((nRow < 0) || (nRow >= m_nNumRows) ||
165 (nCol < 0) || (nCol >= m_nNumCols))
166 return NULL;
167
168 return m_ppData[nRow * m_nNumCols + nCol];
169 }
170
171 LONG Table::GetAsInt(int nRow, int nCol)
172 {
173 TCHAR *pszVal;
174
175 pszVal = GetAsString(nRow, nCol);
176 return pszVal != NULL ? _tcstol(pszVal, NULL, 0) : 0;
177 }
178
179 DWORD Table::GetAsUInt(int nRow, int nCol)
180 {
181 TCHAR *pszVal;
182
183 pszVal = GetAsString(nRow, nCol);
184 return pszVal != NULL ? _tcstoul(pszVal, NULL, 0) : 0;
185 }
186
187 INT64 Table::GetAsInt64(int nRow, int nCol)
188 {
189 TCHAR *pszVal;
190
191 pszVal = GetAsString(nRow, nCol);
192 return pszVal != NULL ? _tcstoll(pszVal, NULL, 0) : 0;
193 }
194
195 QWORD Table::GetAsUInt64(int nRow, int nCol)
196 {
197 TCHAR *pszVal;
198
199 pszVal = GetAsString(nRow, nCol);
200 return pszVal != NULL ? _tcstoull(pszVal, NULL, 0) : 0;
201 }
202
203 double Table::GetAsDouble(int nRow, int nCol)
204 {
205 TCHAR *pszVal;
206
207 pszVal = GetAsString(nRow, nCol);
208 return pszVal != NULL ? _tcstod(pszVal, NULL) : 0;
209 }