SAGA API v9.10
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
117 case SG_DATATYPE_Date : return( new CSG_Table_Value_Date () );
118
119 case SG_DATATYPE_Color :
120 case SG_DATATYPE_Byte :
121 case SG_DATATYPE_Char :
122 case SG_DATATYPE_Word :
123 case SG_DATATYPE_Short :
124 case SG_DATATYPE_DWord :
125 case SG_DATATYPE_Int : return( new CSG_Table_Value_Int () );
126
127 case SG_DATATYPE_ULong :
128 case SG_DATATYPE_Long : return( new CSG_Table_Value_Long () );
129
130 case SG_DATATYPE_Float :
131 case SG_DATATYPE_Double: return( new CSG_Table_Value_Double() );
132
133 case SG_DATATYPE_Binary: return( new CSG_Table_Value_Binary() );
134 }
135}
136
137
139// //
141
142//---------------------------------------------------------
144{
145 if( add_Field < 0 )
146 {
147 add_Field = 0;
148 }
149 else if( add_Field >= m_pTable->Get_Field_Count() )
150 {
151 add_Field = m_pTable->Get_Field_Count() - 1;
152 }
153
154 m_Values = (CSG_Table_Value **)SG_Realloc(m_Values, m_pTable->Get_Field_Count() * sizeof(CSG_Table_Value *));
155
156 for(int iField=m_pTable->Get_Field_Count()-1; iField>add_Field; iField--)
157 {
158 m_Values[iField] = m_Values[iField - 1];
159 }
160
161 m_Values[add_Field] = _Create_Value(m_pTable->Get_Field_Type(add_Field));
162
163 return( true );
164}
165
166//---------------------------------------------------------
168{
169 delete(m_Values[del_Field]);
170
171 for(int iField=del_Field; iField<m_pTable->Get_Field_Count(); iField++)
172 {
173 m_Values[iField] = m_Values[iField + 1];
174 }
175
176 m_Values = (CSG_Table_Value **)SG_Realloc(m_Values, m_pTable->Get_Field_Count() * sizeof(CSG_Table_Value *));
177
178 return( true );
179}
180
181//---------------------------------------------------------
183{
184 if( Field.Length() )
185 {
186 for(int iField=0; iField<m_pTable->Get_Field_Count(); iField++)
187 {
188 if( !Field.Cmp(m_pTable->Get_Field_Name(iField)) )
189 {
190 return( iField );
191 }
192 }
193 }
194
195 return( -1 );
196}
197
198
200// //
202
203//---------------------------------------------------------
205{
206 if( bOn != is_Selected() )
207 {
208 if( bOn )
209 {
211 }
212 else
213 {
215 }
216 }
217}
218
219//---------------------------------------------------------
221{
222 if( bOn != is_Modified() )
223 {
224 if( bOn )
225 {
227 }
228 else
229 {
231 }
232 }
233
234 if( bOn )
235 {
236 m_pTable->Set_Modified();
237 }
238}
239
240
242// //
244
245//---------------------------------------------------------
246bool CSG_Table_Record::Set_Value(int Field, const CSG_Bytes &Value)
247{
248 if( Field >= 0 && Field < m_pTable->Get_Field_Count() )
249 {
250 if( m_Values[Field]->Set_Value(Value) )
251 {
252 Set_Modified(true);
253
254 m_pTable->Set_Update_Flag();
255 m_pTable->_Stats_Invalidate(Field);
256
257 return( true );
258 }
259 }
260
261 return( false );
262}
263
264bool CSG_Table_Record::Set_Value(const CSG_String &Field, const CSG_Bytes &Value)
265{
266 return( Set_Value(_Get_Field(Field), Value) );
267}
268
269//---------------------------------------------------------
270bool CSG_Table_Record::Set_Value(int Field, const CSG_String &Value)
271{
272 if( Field >= 0 && Field < m_pTable->Get_Field_Count() )
273 {
274 if( m_Values[Field]->Set_Value(Value) )
275 {
276 Set_Modified(true);
277
278 m_pTable->Set_Update_Flag();
279 m_pTable->_Stats_Invalidate(Field);
280
281 return( true );
282 }
283 }
284
285 return( false );
286}
287
288bool CSG_Table_Record::Set_Value(const CSG_String &Field, const CSG_String &Value)
289{
290 return( Set_Value(_Get_Field(Field), Value) );
291}
292
293//---------------------------------------------------------
294bool CSG_Table_Record::Set_Value(int Field, sLong Value)
295{
296 return( Set_Value(Field, (double)Value) );
297}
298
300{
301 return( Set_Value(Field, (double)Value) );
302}
303
304//---------------------------------------------------------
305bool CSG_Table_Record::Set_Value(int Field, double Value)
306{
307 if( Field >= 0 && Field < m_pTable->Get_Field_Count() )
308 {
309 if( m_Values[Field]->Set_Value(Value) )
310 {
311 Set_Modified(true);
312
313 m_pTable->Set_Update_Flag();
314 m_pTable->_Stats_Invalidate(Field);
315
316 return( true );
317 }
318 }
319
320 return( false );
321}
322
323bool CSG_Table_Record::Set_Value(const CSG_String &Field, double Value)
324{
325 return( Set_Value(_Get_Field(Field), Value) );
326}
327
328//---------------------------------------------------------
329bool CSG_Table_Record::Add_Value(int Field, double Value)
330{
331 if( Field >= 0 && Field < m_pTable->Get_Field_Count() )
332 {
333 return( Set_Value(Field, asDouble(Field) + Value) );
334 }
335
336 return( false );
337}
338
339bool CSG_Table_Record::Add_Value(const CSG_String &Field, double Value)
340{
341 return( Add_Value(_Get_Field(Field), Value) );
342}
343
344//---------------------------------------------------------
345bool CSG_Table_Record::Mul_Value(int Field, double Value)
346{
347 if( Field >= 0 && Field < m_pTable->Get_Field_Count() )
348 {
349 return( Set_Value(Field, asDouble(Field) * Value) );
350 }
351
352 return( false );
353}
354
355bool CSG_Table_Record::Mul_Value(const CSG_String &Field, double Value)
356{
357 return( Mul_Value(_Get_Field(Field), Value) );
358}
359
360
362// //
364
365//---------------------------------------------------------
367{
368 if( Field >= 0 && Field < m_pTable->Get_Field_Count() )
369 {
370 switch( m_pTable->Get_Field_Type(Field) )
371 {
372 default:
374 if( !m_Values[Field]->Set_Value(SG_T("")) )
375 return( false );
376 break;
377
378 case SG_DATATYPE_Date :
379 case SG_DATATYPE_Color :
380 case SG_DATATYPE_Byte :
381 case SG_DATATYPE_Char :
382 case SG_DATATYPE_Word :
383 case SG_DATATYPE_Short :
384 case SG_DATATYPE_DWord :
385 case SG_DATATYPE_Int :
386 case SG_DATATYPE_ULong :
387 case SG_DATATYPE_Long :
388 case SG_DATATYPE_Float :
390 if( !m_Values[Field]->Set_Value(m_pTable->Get_NoData_Value()) )
391 return( false );
392 break;
393
395 m_Values[Field]->asBinary().Destroy();
396 break;
397 }
398
399 Set_Modified(true);
400
401 m_pTable->Set_Update_Flag();
402 m_pTable->_Stats_Invalidate(Field);
403
404 return( true );
405 }
406
407 return( false );
408}
409
411{
412 return( Set_NoData(_Get_Field(Field)) );
413}
414
415//---------------------------------------------------------
416bool CSG_Table_Record::is_NoData(int Field) const
417{
418 if( Field >= 0 && Field < m_pTable->Get_Field_Count() )
419 {
420 switch( m_pTable->Get_Field_Type(Field) )
421 {
422 default:
424 return( !m_Values[Field]->asString() || !*m_Values[Field]->asString() );
425
426 case SG_DATATYPE_Date :
427 case SG_DATATYPE_Color :
428 case SG_DATATYPE_Byte :
429 case SG_DATATYPE_Char :
430 case SG_DATATYPE_Word :
431 case SG_DATATYPE_Short :
432 case SG_DATATYPE_DWord :
433 case SG_DATATYPE_Int :
434 case SG_DATATYPE_ULong :
435 case SG_DATATYPE_Long :
436 return( m_pTable->is_NoData_Value(m_Values[Field]->asInt()) );
437
438 case SG_DATATYPE_Float :
440 return( m_pTable->is_NoData_Value(m_Values[Field]->asDouble()) );
441
443 return( m_Values[Field]->asBinary().Get_Count() == 0 );
444 }
445 }
446
447 return( true );
448}
449
451{
452 return( is_NoData(_Get_Field(Field)) );
453}
454
455
457// //
459
460//---------------------------------------------------------
461const SG_Char * CSG_Table_Record::asString(int Field, int Decimals) const
462{
463 return( Field >= 0 && Field < m_pTable->Get_Field_Count() ? m_Values[Field]->asString(Decimals) : NULL );
464}
465
466const SG_Char * CSG_Table_Record::asString(const CSG_String &Field, int Decimals) const
467{
468 return( asString(_Get_Field(Field), Decimals) );
469}
470
471//---------------------------------------------------------
473{
474 return( (SG_Char)asInt(Field) );
475}
476
478{
479 return( asChar(_Get_Field(Field)) );
480}
481
482//---------------------------------------------------------
483short CSG_Table_Record::asShort(int Field) const
484{
485 return( (short)asInt(Field) );
486}
487
488short CSG_Table_Record::asShort(const CSG_String &Field) const
489{
490 return( asShort(_Get_Field(Field)) );
491}
492
493//---------------------------------------------------------
494int CSG_Table_Record::asInt(int Field) const
495{
496 return( Field >= 0 && Field < m_pTable->Get_Field_Count() ? m_Values[Field]->asInt() : 0 );
497}
498
499int CSG_Table_Record::asInt(const CSG_String &Field) const
500{
501 return( asInt(_Get_Field(Field)) );
502}
503
504//---------------------------------------------------------
506{
507 return( Field >= 0 && Field < m_pTable->Get_Field_Count() ? m_Values[Field]->asLong() : 0 );
508}
509
511{
512 return( asLong(_Get_Field(Field)) );
513}
514
515//---------------------------------------------------------
516float CSG_Table_Record::asFloat(int Field) const
517{
518 return( (float)asDouble(Field) );
519}
520
521float CSG_Table_Record::asFloat(const CSG_String &Field) const
522{
523 return( asFloat(_Get_Field(Field)) );
524}
525
526//---------------------------------------------------------
527double CSG_Table_Record::asDouble(int Field) const
528{
529 return( Field >= 0 && Field < m_pTable->Get_Field_Count() ? m_Values[Field]->asDouble() : 0. );
530}
531
532double CSG_Table_Record::asDouble(const CSG_String &Field) const
533{
534 return( asDouble(_Get_Field(Field)) );
535}
536
537
539// //
541
542//---------------------------------------------------------
544{
545 if( pRecord )
546 {
547 int nFields = m_pTable->Get_Field_Count() < pRecord->m_pTable->Get_Field_Count()
548 ? m_pTable->Get_Field_Count() : pRecord->m_pTable->Get_Field_Count();
549
550 for(int iField=0; iField<nFields; iField++)
551 {
552 *(m_Values[iField]) = *(pRecord->m_Values[iField]);
553 }
554
555 Set_Modified();
556
557 return( true );
558 }
559
560 return( false );
561}
562
563
565// //
566// //
567// //
569
570//---------------------------------------------------------
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:997
@ SG_DATATYPE_Long
Definition api_core.h:1006
@ SG_DATATYPE_Byte
Definition api_core.h:999
@ SG_DATATYPE_Short
Definition api_core.h:1002
@ SG_DATATYPE_Word
Definition api_core.h:1001
@ SG_DATATYPE_ULong
Definition api_core.h:1005
@ SG_DATATYPE_Float
Definition api_core.h:1007
@ SG_DATATYPE_Binary
Definition api_core.h:1012
@ SG_DATATYPE_Double
Definition api_core.h:1008
@ SG_DATATYPE_Color
Definition api_core.h:1011
@ SG_DATATYPE_Int
Definition api_core.h:1004
@ SG_DATATYPE_Char
Definition api_core.h:1000
@ SG_DATATYPE_String
Definition api_core.h:1009
@ SG_DATATYPE_DWord
Definition api_core.h:1003
@ SG_DATATYPE_Date
Definition api_core.h:1010
#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:256
bool Set_Value(int Field, const CSG_String &Value)
class CSG_Table_Value ** m_Values
Definition table.h:258
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:245
void Set_Selected(bool bOn=true)
class CSG_Table * m_pTable
Definition table.h:260
bool is_NoData(int Field) const
sLong asLong(int Field) const
bool _Add_Field(int add_Field)
bool Set_NoData(int Field)
double asDouble(int Field) const
int asInt(int Field) const
int _Get_Field(const CSG_String &Field) const
bool is_Selected(void) const
Definition table.h:244
friend class CSG_Table
Definition table.h:131
float asFloat(int Field) const
virtual bool Assign(CSG_Table_Record *pRecord)
SG_Char asChar(int Field) const
short asShort(int Field) const
bool Mul_Value(int Field, double Value)
const SG_Char * asString(int Field, int Decimals=-99) const
static CSG_Table_Value * _Create_Value(TSG_Data_Type Type)
bool Add_Value(int Field, double Value)
int Get_Field_Count(void) const
Definition table.h:361
#define SG_TABLE_REC_FLAG_Selected
Definition table.h:119
#define SG_TABLE_REC_FLAG_Modified
Definition table.h:118