SAGA API Version 9.13
Loading...
Searching...
No Matches
table_record.cpp
Go to the documentation of this file.
1
3// //
4// SAGA //
5// //
6// System for Automated Geoscientific Analyses //
7// //
8// Application Programming Interface //
9// //
10// Library: SAGA_API //
11// //
12//-------------------------------------------------------//
13// //
14// table_record.cpp //
15// //
16// Copyright (C) 2005 by Olaf Conrad //
17// //
18//-------------------------------------------------------//
19// //
20// This file is part of 'SAGA - System for Automated //
21// Geoscientific Analyses'. //
22// //
23// This library is free software; you can redistribute //
24// it and/or modify it under the terms of the GNU Lesser //
25// General Public License as published by the Free //
26// Software Foundation, either version 2.1 of the //
27// License, or (at your option) any later version. //
28// //
29// This library is distributed in the hope that it will //
30// be useful, but WITHOUT ANY WARRANTY; without even the //
31// implied warranty of MERCHANTABILITY or FITNESS FOR A //
32// PARTICULAR PURPOSE. See the GNU Lesser General Public //
33// License for more details. //
34// //
35// You should have received a copy of the GNU Lesser //
36// General Public License along with this program; if //
37// not, see <http://www.gnu.org/licenses/>. //
38// //
39//-------------------------------------------------------//
40// //
41// contact: Olaf Conrad //
42// Institute of Geography //
43// University of Goettingen //
44// Goldschmidtstr. 5 //
45// 37077 Goettingen //
46// Germany //
47// //
48// e-mail: oconrad@saga-gis.org //
49// //
51
52//---------------------------------------------------------
53#include "table.h"
54#include "table_value.h"
55
56
58// //
59// //
60// //
62
63//---------------------------------------------------------
65{
66 m_pTable = pTable;
67 m_Index = Index;
68 m_Flags = 0;
69
70 if( m_pTable && m_pTable->Get_Field_Count() > 0 )
71 {
72 m_Values = (CSG_Table_Value **)SG_Malloc(m_pTable->Get_Field_Count() * sizeof(CSG_Table_Value *));
73
74 for(int iField=0; iField<m_pTable->Get_Field_Count(); iField++)
75 {
76 m_Values[iField] = _Create_Value(m_pTable->Get_Field_Type(iField));
77 }
78 }
79 else
80 {
81 m_Values = NULL;
82 }
83}
84
85//---------------------------------------------------------
87{
88 if( is_Selected() )
89 {
90 m_pTable->Select(m_Index, true);
91 }
92
93 if( m_pTable->Get_Field_Count() > 0 )
94 {
95 for(int iField=0; iField<m_pTable->Get_Field_Count(); iField++)
96 {
97 delete(m_Values[iField]);
98 }
99
101 }
102}
103
104
106// //
108
109//---------------------------------------------------------
111{
112 switch( Type )
113 {
114 default :
115 case SG_DATATYPE_String: return( new CSG_Table_Value_String() );
116 case SG_DATATYPE_Date : return( new CSG_Table_Value_Date () );
117 case SG_DATATYPE_Binary: return( new CSG_Table_Value_Binary() );
118 case SG_DATATYPE_Byte :
119 case SG_DATATYPE_Char :
120 case SG_DATATYPE_Word :
121 case SG_DATATYPE_Short :
122 case SG_DATATYPE_DWord :
123 case SG_DATATYPE_Int :
124 case SG_DATATYPE_ULong :
125 case SG_DATATYPE_Long :
126 case SG_DATATYPE_Float :
127 case SG_DATATYPE_Double: return( new CSG_Table_Value_Number(Type) );
129 }
130}
131
132
134// //
136
137//---------------------------------------------------------
139{
140 if( add_Field < 0 )
141 {
142 add_Field = 0;
143 }
144 else if( add_Field >= m_pTable->Get_Field_Count() )
145 {
146 add_Field = m_pTable->Get_Field_Count() - 1;
147 }
148
149 m_Values = (CSG_Table_Value **)SG_Realloc(m_Values, m_pTable->Get_Field_Count() * sizeof(CSG_Table_Value *));
150
151 for(int iField=m_pTable->Get_Field_Count()-1; iField>add_Field; iField--)
152 {
153 m_Values[iField] = m_Values[iField - 1];
154 }
155
156 m_Values[add_Field] = _Create_Value(m_pTable->Get_Field_Type(add_Field));
157
158 return( true );
159}
160
161//---------------------------------------------------------
163{
164 delete(m_Values[del_Field]);
165
166 for(int iField=del_Field; iField<m_pTable->Get_Field_Count(); iField++)
167 {
168 m_Values[iField] = m_Values[iField + 1];
169 }
170
171 m_Values = (CSG_Table_Value **)SG_Realloc(m_Values, m_pTable->Get_Field_Count() * sizeof(CSG_Table_Value *));
172
173 return( true );
174}
175
176//---------------------------------------------------------
178{
179 if( Field.Length() )
180 {
181 for(int iField=0; iField<m_pTable->Get_Field_Count(); iField++)
182 {
183 if( !Field.Cmp(m_pTable->Get_Field_Name(iField)) )
184 {
185 return( iField );
186 }
187 }
188 }
189
190 return( -1 );
191}
192
193
195// //
197
198//---------------------------------------------------------
200{
201 if( bOn != is_Selected() )
202 {
203 if( bOn )
204 {
206 }
207 else
208 {
210 }
211 }
212}
213
214//---------------------------------------------------------
216{
217 if( bOn != is_Modified() )
218 {
219 if( bOn )
220 {
222 }
223 else
224 {
226 }
227 }
228
229 if( bOn )
230 {
231 m_pTable->Set_Modified();
232 }
233}
234
235
237// //
239
240//---------------------------------------------------------
241bool CSG_Table_Record::Set_Value(int Field, const CSG_String &Value)
242{
243 if( Field < 0 || Field >= m_pTable->Get_Field_Count() || !m_Values[Field]->Set_Value(Value) ) { return( false ); }
244
245 Set_Modified(true); m_pTable->Set_Update_Flag(); m_pTable->_Stats_Invalidate(Field);
246
247 return( true );
248}
249
250//---------------------------------------------------------
251bool CSG_Table_Record::Set_Value(int Field, const CSG_Bytes &Value)
252{
253 if( Field < 0 || Field >= m_pTable->Get_Field_Count() || !m_Values[Field]->Set_Value(Value) ) { return( false ); }
254
255 Set_Modified(true); m_pTable->Set_Update_Flag(); m_pTable->_Stats_Invalidate(Field);
256
257 return( true );
258}
259
260//---------------------------------------------------------
261bool CSG_Table_Record::Set_Value(int Field, const sLong &Value)
262{
263 if( Field < 0 || Field >= m_pTable->Get_Field_Count() || !m_Values[Field]->Set_Value(Value) ) { return( false ); }
264
265 Set_Modified(true); m_pTable->Set_Update_Flag(); m_pTable->_Stats_Invalidate(Field);
266
267 return( true );
268}
269
270//---------------------------------------------------------
271bool CSG_Table_Record::Set_Value(int Field, const uLong &Value)
272{
273 if( Field < 0 || Field >= m_pTable->Get_Field_Count() || !m_Values[Field]->Set_Value(Value) ) { return( false ); }
274
275 Set_Modified(true); m_pTable->Set_Update_Flag(); m_pTable->_Stats_Invalidate(Field);
276
277 return( true );
278}
279
280//---------------------------------------------------------
281bool CSG_Table_Record::Set_Value(int Field, const double &Value)
282{
283 if( Field < 0 || Field >= m_pTable->Get_Field_Count() || !m_Values[Field]->Set_Value(Value) ) { return( false ); }
284
285 Set_Modified(true); m_pTable->Set_Update_Flag(); m_pTable->_Stats_Invalidate(Field);
286
287 return( true );
288}
289
290//---------------------------------------------------------
291bool CSG_Table_Record::Add_Value(int Field, const double &Value)
292{
293 if( Field >= 0 && Field < m_pTable->Get_Field_Count() )
294 {
295 return( Set_Value(Field, asDouble(Field) + Value) );
296 }
297
298 return( false );
299}
300
301//---------------------------------------------------------
302bool CSG_Table_Record::Mul_Value(int Field, const double &Value)
303{
304 if( Field >= 0 && Field < m_pTable->Get_Field_Count() )
305 {
306 return( Set_Value(Field, asDouble(Field) * Value) );
307 }
308
309 return( false );
310}
311
312
314// //
316
317//---------------------------------------------------------
319{
320 if( Field >= 0 && Field < m_pTable->Get_Field_Count() )
321 {
322 switch( m_pTable->Get_Field_Type(Field) )
323 {
324 default:
326 if( !m_Values[Field]->Set_Value(SG_T("")) )
327 return( false );
328 break;
329
330 case SG_DATATYPE_Date :
331 case SG_DATATYPE_Color :
332 case SG_DATATYPE_Byte :
333 case SG_DATATYPE_Char :
334 case SG_DATATYPE_Word :
335 case SG_DATATYPE_Short :
336 case SG_DATATYPE_DWord :
337 case SG_DATATYPE_Int :
338 case SG_DATATYPE_ULong :
339 case SG_DATATYPE_Long :
340 case SG_DATATYPE_Float :
342 if( !m_Values[Field]->Set_Value(m_pTable->Get_NoData_Value()) )
343 return( false );
344 break;
345
347 m_Values[Field]->asBinary().Destroy();
348 break;
349 }
350
351 Set_Modified(true);
352
353 m_pTable->Set_Update_Flag();
354 m_pTable->_Stats_Invalidate(Field);
355
356 return( true );
357 }
358
359 return( false );
360}
361
362//---------------------------------------------------------
363bool CSG_Table_Record::is_NoData(int Field) const
364{
365 if( Field >= 0 && Field < m_pTable->Get_Field_Count() )
366 {
367 switch( m_pTable->Get_Field_Type(Field) )
368 {
369 default:
370 case SG_DATATYPE_String: return( !m_Values[Field]->asString() || !*m_Values[Field]->asString() );
371
372 case SG_DATATYPE_Color :
373 case SG_DATATYPE_Byte :
374 case SG_DATATYPE_Word :
375 case SG_DATATYPE_DWord :
376 case SG_DATATYPE_ULong : return( m_pTable->is_NoData_Value(m_Values[Field]->asDouble()) );
377
378 case SG_DATATYPE_Char :
379 case SG_DATATYPE_Short :
380 case SG_DATATYPE_Int :
381 case SG_DATATYPE_Long : return( m_pTable->is_NoData_Value(m_Values[Field]->asDouble()) );
382
383 case SG_DATATYPE_Date :
384 case SG_DATATYPE_Float :
385 case SG_DATATYPE_Double: return( m_pTable->is_NoData_Value(m_Values[Field]->asDouble()) );
386
387 case SG_DATATYPE_Binary: return( m_Values[Field]->asBinary().Get_Count() == 0 );
388 }
389 }
390
391 return( true );
392}
393
394
396// //
398
399//---------------------------------------------------------
400const SG_Char * CSG_Table_Record::asString(int Field, int Decimals) const
401{
402 return( Field >= 0 && Field < m_pTable->Get_Field_Count() ? m_Values[Field]->asString(Decimals) : NULL );
403}
404
405//---------------------------------------------------------
407{
408 return( Field >= 0 && Field < m_pTable->Get_Field_Count() ? m_Values[Field]->asULong () : 0 );
409}
410
411//---------------------------------------------------------
413{
414 return( Field >= 0 && Field < m_pTable->Get_Field_Count() ? m_Values[Field]->asLong () : 0 );
415}
416
417//---------------------------------------------------------
418double CSG_Table_Record::asDouble(int Field) const
419{
420 return( Field >= 0 && Field < m_pTable->Get_Field_Count() ? m_Values[Field]->asDouble() : 0. );
421}
422
423
425// //
427
428//---------------------------------------------------------
430{
431 if( pRecord )
432 {
433 int nFields
434 = m_pTable->Get_Field_Count() < pRecord->m_pTable->Get_Field_Count()
435 ? m_pTable->Get_Field_Count() : pRecord->m_pTable->Get_Field_Count();
436
437 for(int iField=0; iField<nFields; iField++)
438 {
439 *(m_Values[iField]) = *(pRecord->m_Values[iField]);
440 }
441
442 Set_Modified();
443
444 return( true );
445 }
446
447 return( false );
448}
449
450
452// //
453// //
454// //
456
457//---------------------------------------------------------
unsigned long long uLong
Definition api_core.h:159
SAGA_API_DLL_EXPORT void * SG_Malloc(size_t size)
signed long long sLong
Definition api_core.h:158
#define SG_T(s)
Definition api_core.h:537
SAGA_API_DLL_EXPORT void SG_Free(void *memblock)
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
TSG_Data_Type
Definition api_core.h:1043
@ SG_DATATYPE_Long
Definition api_core.h:1052
@ SG_DATATYPE_Byte
Definition api_core.h:1045
@ SG_DATATYPE_Short
Definition api_core.h:1048
@ SG_DATATYPE_Word
Definition api_core.h:1047
@ SG_DATATYPE_ULong
Definition api_core.h:1051
@ SG_DATATYPE_Float
Definition api_core.h:1053
@ SG_DATATYPE_Binary
Definition api_core.h:1058
@ SG_DATATYPE_Double
Definition api_core.h:1054
@ SG_DATATYPE_Color
Definition api_core.h:1057
@ SG_DATATYPE_Int
Definition api_core.h:1050
@ SG_DATATYPE_Char
Definition api_core.h:1046
@ SG_DATATYPE_String
Definition api_core.h:1055
@ SG_DATATYPE_DWord
Definition api_core.h:1049
@ SG_DATATYPE_Date
Definition api_core.h:1056
#define SG_Char
Definition api_core.h:536
size_t Length(void) const
int Cmp(const CSG_String &String) const
void Set_Modified(bool bOn=true)
sLong m_Index
Definition table.h:266
bool Set_Value(int Field, const CSG_String &Value)
class CSG_Table_Value ** m_Values
Definition table.h:268
CSG_Table_Record(class CSG_Table *pTable, sLong Index)
virtual ~CSG_Table_Record(void)
bool _Del_Field(int del_Field)
bool is_Modified(void) const
Definition table.h:255
void Set_Selected(bool bOn=true)
class CSG_Table * m_pTable
Definition table.h:270
bool is_NoData(int Field) const
sLong asLong(int Field) const
bool _Add_Field(int add_Field)
uLong asULong(int Field) const
bool Set_NoData(int Field)
double asDouble(int Field) const
int _Get_Field(const CSG_String &Field) const
bool is_Selected(void) const
Definition table.h:254
friend class CSG_Table
Definition table.h:131
virtual bool Assign(CSG_Table_Record *pRecord)
bool Add_Value(int Field, const double &Value)
bool Mul_Value(int Field, const double &Value)
const SG_Char * asString(int Field, int Decimals=-99) const
static CSG_Table_Value * _Create_Value(TSG_Data_Type Type)
int Get_Field_Count(void) const
Definition table.h:371
#define SG_TABLE_REC_FLAG_Selected
Definition table.h:119
#define SG_TABLE_REC_FLAG_Modified
Definition table.h:118