SAGA API  v9.6
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  {
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 
100  SG_Free(m_Values);
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 //---------------------------------------------------------
143 bool CSG_Table_Record::_Add_Field(int add_Field)
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 
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 //---------------------------------------------------------
167 bool CSG_Table_Record::_Del_Field(int del_Field)
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 
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  {
237  }
238 }
239 
240 
242 // //
244 
245 //---------------------------------------------------------
246 bool 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 
255  m_pTable->_Stats_Invalidate(Field);
256 
257  return( true );
258  }
259  }
260 
261  return( false );
262 }
263 
264 bool 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 //---------------------------------------------------------
270 bool 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 
279  m_pTable->_Stats_Invalidate(Field);
280 
281  return( true );
282  }
283  }
284 
285  return( false );
286 }
287 
288 bool 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 //---------------------------------------------------------
294 bool 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 //---------------------------------------------------------
305 bool 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 
314  m_pTable->_Stats_Invalidate(Field);
315 
316  return( true );
317  }
318  }
319 
320  return( false );
321 }
322 
323 bool CSG_Table_Record::Set_Value(const CSG_String &Field, double Value)
324 {
325  return( Set_Value(_Get_Field(Field), Value) );
326 }
327 
328 //---------------------------------------------------------
329 bool 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 
339 bool CSG_Table_Record::Add_Value(const CSG_String &Field, double Value)
340 {
341  return( Add_Value(_Get_Field(Field), Value) );
342 }
343 
344 //---------------------------------------------------------
345 bool 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 
355 bool 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:
373  case SG_DATATYPE_String:
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 :
389  case SG_DATATYPE_Double:
390  if( !m_Values[Field]->Set_Value(m_pTable->Get_NoData_Value()) )
391  return( false );
392  break;
393 
394  case SG_DATATYPE_Binary:
395  m_Values[Field]->asBinary().Destroy();
396  break;
397  }
398 
399  Set_Modified(true);
400 
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 //---------------------------------------------------------
416 bool 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:
423  case SG_DATATYPE_String:
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 :
439  case SG_DATATYPE_Double:
440  return( m_pTable->is_NoData_Value(m_Values[Field]->asDouble()) );
441 
442  case SG_DATATYPE_Binary:
443  return( m_Values[Field]->asBinary().Get_Count() == 0 );
444  }
445  }
446 
447  return( true );
448 }
449 
450 bool CSG_Table_Record::is_NoData(const CSG_String &Field) const
451 {
452  return( is_NoData(_Get_Field(Field)) );
453 }
454 
455 
457 // //
459 
460 //---------------------------------------------------------
461 const 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 
466 const 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 //---------------------------------------------------------
483 short CSG_Table_Record::asShort(int Field) const
484 {
485  return( (short)asInt(Field) );
486 }
487 
488 short CSG_Table_Record::asShort(const CSG_String &Field) const
489 {
490  return( asShort(_Get_Field(Field)) );
491 }
492 
493 //---------------------------------------------------------
494 int 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 
499 int 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 //---------------------------------------------------------
516 float CSG_Table_Record::asFloat(int Field) const
517 {
518  return( (float)asDouble(Field) );
519 }
520 
521 float CSG_Table_Record::asFloat(const CSG_String &Field) const
522 {
523  return( asFloat(_Get_Field(Field)) );
524 }
525 
526 //---------------------------------------------------------
527 double 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 
532 double 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()
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 //---------------------------------------------------------
SG_DATATYPE_Color
@ SG_DATATYPE_Color
Definition: api_core.h:1007
SG_DATATYPE_Int
@ SG_DATATYPE_Int
Definition: api_core.h:1000
CSG_Table_Record::asDouble
double asDouble(int Field) const
Definition: table_record.cpp:527
SG_T
#define SG_T(s)
Definition: api_core.h:537
table_value.h
SG_DATATYPE_String
@ SG_DATATYPE_String
Definition: api_core.h:1005
SG_DATATYPE_DWord
@ SG_DATATYPE_DWord
Definition: api_core.h:999
SG_DATATYPE_Binary
@ SG_DATATYPE_Binary
Definition: api_core.h:1008
CSG_String::Length
size_t Length(void) const
Definition: api_string.cpp:172
CSG_Table_Record::asShort
short asShort(int Field) const
Definition: table_record.cpp:483
CSG_Bytes::Destroy
bool Destroy(void)
Definition: api_memory.cpp:829
CSG_Table_Record::~CSG_Table_Record
virtual ~CSG_Table_Record(void)
Definition: table_record.cpp:86
CSG_Table_Record
Definition: table.h:130
CSG_Table_Record::Set_Selected
void Set_Selected(bool bOn=true)
Definition: table_record.cpp:204
CSG_Data_Object::Get_NoData_Value
double Get_NoData_Value(bool bUpper=false) const
Definition: dataobject.h:253
CSG_Table_Record::m_pTable
class CSG_Table * m_pTable
Definition: table.h:258
SG_Malloc
SAGA_API_DLL_EXPORT void * SG_Malloc(size_t size)
Definition: api_memory.cpp:65
SG_TABLE_REC_FLAG_Selected
#define SG_TABLE_REC_FLAG_Selected
Definition: table.h:119
CSG_Table_Record::asLong
sLong asLong(int Field) const
Definition: table_record.cpp:505
CSG_Data_Object::is_NoData_Value
bool is_NoData_Value(double Value) const
Definition: dataobject.h:255
CSG_Table_Record::Assign
virtual bool Assign(CSG_Table_Record *pRecord)
Definition: table_record.cpp:543
SG_DATATYPE_Byte
@ SG_DATATYPE_Byte
Definition: api_core.h:995
CSG_Table_Value::asBinary
virtual CSG_Bytes asBinary(void) const
Definition: table_value.h:126
CSG_Table_Record::is_Selected
bool is_Selected(void) const
Definition: table.h:242
CSG_Table::Get_Field_Count
int Get_Field_Count(void) const
Definition: table.h:356
CSG_Table_Record::asChar
SG_Char asChar(int Field) const
Definition: table_record.cpp:472
SG_Free
SAGA_API_DLL_EXPORT void SG_Free(void *memblock)
Definition: api_memory.cpp:83
CSG_Table_Record::m_Values
class CSG_Table_Value ** m_Values
Definition: table.h:256
CSG_Table_Value
Definition: table_value.h:111
CSG_Table_Value_String
Definition: table_value.h:230
CSG_Table_Record::m_Index
sLong m_Index
Definition: table.h:254
CSG_Data_Object::Set_Update_Flag
void Set_Update_Flag(bool bOn=true)
Definition: dataobject.h:285
CSG_Table_Record::is_NoData
bool is_NoData(int Field) const
Definition: table_record.cpp:416
CSG_Table::Set_Modified
virtual void Set_Modified(bool bModified=true)
Definition: table.cpp:1113
CSG_String::Cmp
int Cmp(const CSG_String &String) const
Definition: api_string.cpp:515
CSG_Table::_Stats_Invalidate
bool _Stats_Invalidate(void) const
Definition: table.cpp:1200
CSG_Table_Record::m_Flags
char m_Flags
Definition: table.h:252
CSG_Table_Value_Int
Definition: table_value.h:384
CSG_Table_Value_Binary
Definition: table_value.h:160
SG_DATATYPE_Long
@ SG_DATATYPE_Long
Definition: api_core.h:1002
CSG_Table_Record::asString
const SG_Char * asString(int Field, int Decimals=-99) const
Definition: table_record.cpp:461
CSG_Table_Record::CSG_Table_Record
CSG_Table_Record(class CSG_Table *pTable, sLong Index)
Definition: table_record.cpp:64
CSG_Table::Get_Field_Name
const SG_Char * Get_Field_Name(int iField) const
Definition: table.h:357
CSG_Table_Value_Double
Definition: table_value.h:544
SG_TABLE_REC_FLAG_Modified
#define SG_TABLE_REC_FLAG_Modified
Definition: table.h:118
CSG_Table_Value_Date
Definition: table_value.h:299
sLong
signed long long sLong
Definition: api_core.h:158
CSG_Table_Record::_Create_Value
static CSG_Table_Value * _Create_Value(TSG_Data_Type Type)
Definition: table_record.cpp:110
SG_DATATYPE_Float
@ SG_DATATYPE_Float
Definition: api_core.h:1003
CSG_Table_Record::asFloat
float asFloat(int iField) const
Definition: table_record.cpp:516
SG_DATATYPE_Date
@ SG_DATATYPE_Date
Definition: api_core.h:1006
SG_DATATYPE_Word
@ SG_DATATYPE_Word
Definition: api_core.h:997
CSG_Table_Record::_Add_Field
bool _Add_Field(int add_Field)
Definition: table_record.cpp:143
CSG_Table_Record::Add_Value
bool Add_Value(int Field, double Value)
Definition: table_record.cpp:329
CSG_Bytes
Definition: api_core.h:812
CSG_Table_Record::Set_Modified
void Set_Modified(bool bOn=true)
Definition: table_record.cpp:220
CSG_Table
Definition: table.h:283
CSG_Table_Value_Long
Definition: table_value.h:464
CSG_Table_Record::asInt
int asInt(int Field) const
Definition: table_record.cpp:494
SG_Char
#define SG_Char
Definition: api_core.h:536
CSG_String
Definition: api_core.h:563
CSG_Table_Value::asDouble
virtual double asDouble(void) const =0
CSG_Table_Record::Set_Value
bool Set_Value(int Field, const CSG_String &Value)
Definition: table_record.cpp:270
SG_DATATYPE_Short
@ SG_DATATYPE_Short
Definition: api_core.h:998
CSG_Table_Record::Mul_Value
bool Mul_Value(int Field, double Value)
Definition: table_record.cpp:345
TSG_Data_Type
TSG_Data_Type
Definition: api_core.h:993
CSG_Table::Select
virtual bool Select(sLong Index, bool bInvert=false)
Definition: table_selection.cpp:136
SG_Realloc
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
Definition: api_memory.cpp:77
CSG_Table::Get_Field_Type
TSG_Data_Type Get_Field_Type(int iField) const
Definition: table.h:358
SG_DATATYPE_Char
@ SG_DATATYPE_Char
Definition: api_core.h:996
CSG_Table_Value::asInt
virtual int asInt(void) const =0
CSG_Table_Record::is_Modified
bool is_Modified(void) const
Definition: table.h:243
table.h
CSG_Table_Record::Set_NoData
bool Set_NoData(int Field)
Definition: table_record.cpp:366
SG_DATATYPE_ULong
@ SG_DATATYPE_ULong
Definition: api_core.h:1001
CSG_Table_Record::_Get_Field
int _Get_Field(const CSG_String &Field) const
Definition: table_record.cpp:182
CSG_Table_Record::_Del_Field
bool _Del_Field(int del_Field)
Definition: table_record.cpp:167
SG_DATATYPE_Double
@ SG_DATATYPE_Double
Definition: api_core.h:1004