📄 table_io.cpp
字号:
///////////////////////////////////////////////////////////
// //
// SAGA //
// //
// System for Automated Geoscientific Analyses //
// //
// Application Programming Interface //
// //
// Library: SAGA_API //
// //
//-------------------------------------------------------//
// //
// table_io.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"
#include "table_dbase.h"
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Table::_Load(const SG_Char *File_Name, int Format, SG_Char Separator)
{
bool bResult;
CSG_String fName;
_Destroy();
SG_UI_Msg_Add(CSG_String::Format(SG_T("%s: %s..."), LNG("[MSG] Load table"), File_Name), true);
//-----------------------------------------------------
if( Format <= TABLE_FILETYPE_Undefined || Format > TABLE_FILETYPE_DBase )
{
if( SG_File_Cmp_Extension(File_Name, SG_T("dbf")) )
{
Format = TABLE_FILETYPE_DBase;
}
else if( SG_File_Cmp_Extension(File_Name, SG_T("csv")) )
{
Format = TABLE_FILETYPE_Text;
Separator = ';';
}
else //if( SG_File_Cmp_Extension(File_Name, SG_T("txt")) )
{
Format = TABLE_FILETYPE_Text;
}
}
//-----------------------------------------------------
switch( Format )
{
case TABLE_FILETYPE_Text:
bResult = _Load_Text (File_Name, true , Separator);
break;
case TABLE_FILETYPE_Text_NoHeadLine:
bResult = _Load_Text (File_Name, false, Separator);
break;
case TABLE_FILETYPE_DBase:
bResult = _Load_DBase(File_Name);
break;
default:
bResult = false;
}
//-----------------------------------------------------
if( bResult )
{
Set_Modified(false);
Set_File_Name(File_Name);
if( !Get_History().Load(File_Name, HISTORY_EXT_TABLE) )
{
Get_History().Add_Entry(LNG("[HST] Loaded from file"), File_Name);
}
SG_UI_Msg_Add(LNG("[MSG] okay"), false);
return( true );
}
SG_UI_Msg_Add(LNG("[MSG] failed"), false);
return( false );
}
//---------------------------------------------------------
bool CSG_Table::Save(const SG_Char *File_Name, int Format)
{
return( Save(File_Name, Format, '\t') );
}
//---------------------------------------------------------
bool CSG_Table::Save(const SG_Char *File_Name, int Format, SG_Char Separator)
{
bool bResult;
SG_UI_Msg_Add(CSG_String::Format(SG_T("%s: %s..."), LNG("[MSG] Save table"), File_Name), true);
//-----------------------------------------------------
if( Format <= TABLE_FILETYPE_Undefined || Format > TABLE_FILETYPE_DBase )
{
if( SG_File_Cmp_Extension(File_Name, SG_T("dbf")) )
{
Format = TABLE_FILETYPE_DBase;
}
else if( SG_File_Cmp_Extension(File_Name, SG_T("csv")) )
{
Format = TABLE_FILETYPE_Text;
Separator = ';';
}
else //if( SG_File_Cmp_Extension(File_Name, SG_T("txt")) )
{
Format = TABLE_FILETYPE_Text;
}
}
switch( Format )
{
case TABLE_FILETYPE_Text:
bResult = _Save_Text (File_Name, true , Separator);
break;
case TABLE_FILETYPE_Text_NoHeadLine:
bResult = _Save_Text (File_Name, false, Separator);
break;
case TABLE_FILETYPE_DBase:
bResult = _Save_DBase(File_Name);
break;
}
//-----------------------------------------------------
if( bResult )
{
Set_Modified(false);
Set_File_Type(Format);
Set_File_Name(File_Name);
Get_History().Save(File_Name, HISTORY_EXT_TABLE);
SG_UI_Msg_Add(LNG("[MSG] okay"), false);
return( true );
}
SG_UI_Msg_Add(LNG("[MSG] failed"), false);
return( false );
}
///////////////////////////////////////////////////////////
// //
// Text //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Table::_Load_Text(const SG_Char *File_Name, bool bHeadline, SG_Char Separator)
{
bool bContinue, bNumeric, bFloat;
int i, iField, iRecord, fLength;
double Value;
CSG_String sLine, sField;
CSG_File Stream;
CSG_Table_Record *pRecord;
CSG_Table newTable;
//-----------------------------------------------------
if( Stream.Open(File_Name, SG_FILE_R, false) )
{
fLength = Stream.Length();
if( Stream.Read_Line(sLine) )
{
while( (i = sLine.Find(Separator)) >= 0 )
{
sField.Printf(bHeadline ? sLine.Left(i) : SG_T("FIELD_%02d"), newTable.Get_Field_Count() + 1);
newTable.Add_Field(sField, TABLE_FIELDTYPE_String);
sLine.Remove(0, i + 1);
}
sField.Printf(bHeadline ? sLine : SG_T("FIELD_%02d"), newTable.Get_Field_Count() + 1);
newTable.Add_Field(sField, TABLE_FIELDTYPE_String);
}
//-------------------------------------------------
if( newTable.Get_Field_Count() > 0 )
{
if( !bHeadline )
{
Stream.Seek_Start();
}
bContinue = true;
while( bContinue && Stream.Read_Line(sLine) && SG_UI_Process_Set_Progress(Stream.Tell(), fLength) )
{
sLine.Append(Separator);
pRecord = newTable._Add_Record();
for(iField=0; iField<newTable.Get_Field_Count() && bContinue; iField++)
{
if( (i = sLine.Find(Separator)) >= 0 )
{
sField.Printf(sLine.Left(i));
pRecord->Set_Value(iField, sField);
sLine.Remove(0, i + 1);
}
else
{
bContinue = false;
}
}
if( !bContinue )
{
newTable._Del_Record(newTable.Get_Record_Count() - 1);
}
}
//---------------------------------------------
if( newTable.Get_Record_Count() > 0 )
{
for(iField=0; iField<newTable.Get_Field_Count(); iField++)
{
for(iRecord=0, bNumeric=true, bFloat=false; iRecord<newTable.Get_Record_Count() && bNumeric; iRecord++)
{
if( SG_SSCANF(newTable.Get_Record(iRecord)->asString(iField), SG_T("%lf"), &Value) != 1 )
{
bNumeric = false;
}
else if( !bFloat && Value - (int)Value != 0.0 )
{
bFloat = true;
}
}
if( !bNumeric )
{
Add_Field(newTable.Get_Field_Name(iField), TABLE_FIELDTYPE_String);
}
else if( !bFloat )
{
Add_Field(newTable.Get_Field_Name(iField), TABLE_FIELDTYPE_Int);
}
else
{
Add_Field(newTable.Get_Field_Name(iField), TABLE_FIELDTYPE_Double);
}
}
for(iRecord=0; iRecord<newTable.Get_Record_Count() && SG_UI_Process_Set_Progress(iRecord, newTable.Get_Record_Count()); iRecord++)
{
pRecord = _Add_Record();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -