📄 table.cpp
字号:
///////////////////////////////////////////////////////////
// //
// SAGA //
// //
// System for Automated Geoscientific Analyses //
// //
// Application Programming Interface //
// //
// Library: SAGA_API //
// //
//-------------------------------------------------------//
// //
// table.cpp //
// //
// Copyright (C) 2005 by Olaf Conrad //
// //
//-------------------------------------------------------//
// //
// This file is part of 'SAGA - System for Automated //
// Geoscientific Analyses'. //
// //
// This library is free software; you can redistribute //
// it and/or modify it under the terms of the GNU Lesser //
// General Public License as published by the Free //
// Software Foundation, version 2.1 of the License. //
// //
// This library is distributed in the hope that it will //
// be useful, but WITHOUT ANY WARRANTY; without even the //
// implied warranty of MERCHANTABILITY or FITNESS FOR A //
// PARTICULAR PURPOSE. See the GNU Lesser General Public //
// License for more details. //
// //
// You should have received a copy of the GNU Lesser //
// General Public License along with this program; if //
// not, write to the Free Software Foundation, Inc., //
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, //
// USA. //
// //
//-------------------------------------------------------//
// //
// contact: Olaf Conrad //
// Institute of Geography //
// University of Goettingen //
// Goldschmidtstr. 5 //
// 37077 Goettingen //
// Germany //
// //
// e-mail: oconrad@saga-gis.org //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
#include <string.h>
#include "table.h"
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
CSG_Table * SG_Create_Table(void)
{
return( new CSG_Table );
}
//---------------------------------------------------------
CSG_Table * SG_Create_Table(const CSG_Table &Table)
{
return( new CSG_Table(Table) );
}
//---------------------------------------------------------
CSG_Table * SG_Create_Table(const SG_Char *FileName)
{
return( new CSG_Table(FileName) );
}
//---------------------------------------------------------
CSG_Table * SG_Create_Table(CSG_Table *pStructure)
{
return( new CSG_Table(pStructure) );
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
CSG_Table::CSG_Table(void)
: CSG_Data_Object()
{
_On_Construction();
}
//---------------------------------------------------------
CSG_Table::CSG_Table(const CSG_Table &Table)
: CSG_Data_Object()
{
_On_Construction();
Create(Table);
}
//---------------------------------------------------------
CSG_Table::CSG_Table(const SG_Char *File_Name, SG_Char Separator)
: CSG_Data_Object()
{
_On_Construction();
Create(File_Name, Separator);
}
//---------------------------------------------------------
CSG_Table::CSG_Table(CSG_Table *pStructure)
: CSG_Data_Object()
{
_On_Construction();
Create(pStructure);
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
void CSG_Table::_On_Construction(void)
{
m_nFields = 0;
m_Field_Name = NULL;
m_Field_Type = NULL;
m_Field_Val_Min = NULL;
m_Field_Val_Max = NULL;
m_nRecords = 0;
m_Records = NULL;
m_nSelected = 0;
m_Selected = NULL;
m_Index = NULL;
m_Index_Field = -1;
m_Index_Order = TABLE_INDEX_None;
m_pOwner = NULL;
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Table::Create(const CSG_Table &Table)
{
return( is_Private() ? false : _Create(Table) );
}
bool CSG_Table::_Create(const CSG_Table &Table)
{
return( Assign((CSG_Data_Object *)&Table) );
}
//---------------------------------------------------------
bool CSG_Table::Create(const SG_Char *File_Name, SG_Char Separator)
{
return( is_Private() ? false : _Create(File_Name, Separator) );
}
bool CSG_Table::_Create(const SG_Char *File_Name, SG_Char Separator)
{
return( _Load(File_Name, TABLE_FILETYPE_Undefined, Separator) );
}
//---------------------------------------------------------
bool CSG_Table::Create(CSG_Table *pStructure)
{
return( is_Private() ? false : _Create(pStructure) );
}
bool CSG_Table::_Create(CSG_Table *pStructure)
{
_Destroy();
if( pStructure && pStructure->Get_Field_Count() > 0 )
{
for(int i=0; i<pStructure->Get_Field_Count(); i++)
{
Add_Field(pStructure->Get_Field_Name(i), pStructure->Get_Field_Type(i));
}
return( true );
}
return( false );
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
CSG_Table::~CSG_Table(void)
{
_Destroy();
}
//---------------------------------------------------------
bool CSG_Table::Destroy(void)
{
return( is_Private() ? false : _Destroy() );
}
bool CSG_Table::_Destroy(void)
{
_Destroy_Selection();
_Del_Records();
if( m_nFields > 0 )
{
for(int i=0; i<m_nFields; i++)
{
delete(m_Field_Name[i]);
}
m_nFields = 0;
SG_Free(m_Field_Name);
SG_Free(m_Field_Type);
SG_Free(m_Field_Val_Min);
SG_Free(m_Field_Val_Max);
m_Field_Name = NULL;
m_Field_Type = NULL;
m_Field_Val_Min = NULL;
m_Field_Val_Max = NULL;
}
CSG_Data_Object::Destroy();
return( true );
}
///////////////////////////////////////////////////////////
// //
// Assign //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Table::Assign(CSG_Data_Object *pObject)
{
return( is_Private() ? false : _Assign(pObject) );
}
bool CSG_Table::_Assign(CSG_Data_Object *pObject)
{
int i;
CSG_Table *pTable;
if( pObject && pObject->is_Valid() && pObject->Get_ObjectType() == Get_ObjectType() )
{
_Destroy();
pTable = (CSG_Table *)pObject;
for(i=0; i<pTable->m_nFields; i++)
{
Add_Field(pTable->m_Field_Name[i]->c_str(), pTable->m_Field_Type[i]);
}
for(i=0; i<pTable->m_nRecords; i++)
{
_Add_Record(pTable->m_Records[i]);
}
Get_History().Assign(pTable->Get_History());
return( true );
}
return( false );
}
//---------------------------------------------------------
bool CSG_Table::Assign_Values(CSG_Table *pTable)
{
int i;
if( is_Compatible(pTable) )
{
if( is_Private() )
{
if( Get_Record_Count() == pTable->Get_Record_Count() )
{
_Index_Destroy();
for(i=0; i<pTable->Get_Record_Count(); i++)
{
Get_Record(i)->Assign(pTable->Get_Record(i));
}
return( true );
}
}
else
{
Del_Records();
for(i=0; i<pTable->Get_Record_Count(); i++)
{
Add_Record(pTable->Get_Record(i));
}
return( true );
}
}
return( false );
}
///////////////////////////////////////////////////////////
// //
// Checks //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Table::is_Compatible(CSG_Table *pTable, bool bExactMatch) const
{
if( Get_Field_Count() == pTable->Get_Field_Count() )
{
for(int i=0; i<Get_Field_Count(); i++)
{
if( bExactMatch )
{
if( Get_Field_Type(i) != pTable->Get_Field_Type(i) )
{
return( false );
}
}
else switch( Get_Field_Type(i) )
{
case TABLE_FIELDTYPE_String:
// if( pTable->Get_Field_Type(i) != TABLE_FIELDTYPE_String )
// {
// return( false );
// }
break;
default:
if( pTable->Get_Field_Type(i) == TABLE_FIELDTYPE_String )
{
return( false );
}
break;
}
}
return( true );
}
return( false );
}
///////////////////////////////////////////////////////////
// //
// Fields //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
void CSG_Table::Add_Field(const SG_Char *Name, TSG_Table_Field_Type Type, int add_Field)
{
int iField, iRecord;
//-----------------------------------------------------
if( add_Field < 0 || add_Field > m_nFields )
{
add_Field = m_nFields;
}
//-----------------------------------------------------
m_nFields++;
m_Field_Name = (CSG_String **)SG_Realloc(m_Field_Name , m_nFields * sizeof(CSG_String *));
m_Field_Type = (TSG_Table_Field_Type *)SG_Realloc(m_Field_Type , m_nFields * sizeof(TSG_Table_Field_Type));
m_Field_Val_Min = (double *)SG_Realloc(m_Field_Val_Min , m_nFields * sizeof(double));
m_Field_Val_Max = (double *)SG_Realloc(m_Field_Val_Max , m_nFields * sizeof(double));
//-----------------------------------------------------
for(iField=m_nFields-1; iField>add_Field; iField--)
{
m_Field_Name [iField] = m_Field_Name [iField - 1];
m_Field_Type [iField] = m_Field_Type [iField - 1];
m_Field_Val_Min[iField] = m_Field_Val_Min[iField - 1];
m_Field_Val_Max[iField] = m_Field_Val_Max[iField - 1];
}
//-----------------------------------------------------
m_Field_Name [add_Field] = new CSG_String(Name);
m_Field_Type [add_Field] = Type;
m_Field_Val_Min[add_Field] = 0.0;
m_Field_Val_Max[add_Field] = 0.0;
//-----------------------------------------------------
for(iRecord=0; iRecord<m_nRecords; iRecord++)
{
m_Records[iRecord]->_Add_Field(add_Field);
}
Set_Modified();
}
//---------------------------------------------------------
#ifdef _SAGA_UNICODE
void CSG_Table::Add_Field(const char *Name, TSG_Table_Field_Type Type, int iField)
{ Add_Field(CSG_String(Name), Type, iField); }
#endif
//---------------------------------------------------------
bool CSG_Table::Del_Field(int del_Field)
{
int iRecord, iField;
if( del_Field >= 0 && del_Field < m_nFields )
{
m_nFields--;
//-------------------------------------------------
delete(m_Field_Name[del_Field]);
//-------------------------------------------------
for(iField=del_Field; iField<m_nFields; iField++)
{
m_Field_Name [iField] = m_Field_Name [iField + 1];
m_Field_Type [iField] = m_Field_Type [iField + 1];
m_Field_Val_Min[iField] = m_Field_Val_Min[iField + 1];
m_Field_Val_Max[iField] = m_Field_Val_Max[iField + 1];
}
//-------------------------------------------------
m_Field_Name = (CSG_String **)SG_Realloc(m_Field_Name , m_nFields * sizeof(CSG_String *));
m_Field_Type = (TSG_Table_Field_Type *)SG_Realloc(m_Field_Type , m_nFields * sizeof(TSG_Table_Field_Type));
m_Field_Val_Min = (double *)SG_Realloc(m_Field_Val_Min , m_nFields * sizeof(double));
m_Field_Val_Max = (double *)SG_Realloc(m_Field_Val_Max , m_nFields * sizeof(double));
//-------------------------------------------------
for(iRecord=0; iRecord<m_nRecords; iRecord++)
{
m_Records[iRecord]->_Del_Field(del_Field);
}
Set_Modified();
return( true );
}
return( false );
}
///////////////////////////////////////////////////////////
// //
// Records //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
#define GET_GROW_SIZE(n) (n < 256 ? 1 : (n < 8192 ? 64 : 1024))
//---------------------------------------------------------
CSG_Table_Record * CSG_Table::Add_Record(CSG_Table_Record *pValues)
{
return( is_Private() ? NULL : _Add_Record(pValues) );
}
CSG_Table_Record * CSG_Table::_Add_Record(CSG_Table_Record *pValues)
{
if( (m_nRecords % GET_GROW_SIZE(m_nRecords)) == 0 )
{
CSG_Table_Record **pRecords = (CSG_Table_Record **)SG_Realloc(m_Records, (m_nRecords + GET_GROW_SIZE(m_nRecords)) * sizeof(CSG_Table_Record *));
if( pRecords == NULL )
{
return( NULL );
}
m_Records = pRecords;
}
CSG_Table_Record *pRecord;
//-----------------------------------------------------
if( is_Indexed() )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -