📄 table_io.cpp
字号:
for(iField=0; iField<Get_Field_Count(); iField++)
{
CSG_String sValue(newTable.Get_Record(iRecord)->asString(iField));
switch( Get_Field_Type(iField) )
{
default:
pRecord->Set_Value(iField, sValue);
break;
case TABLE_FIELDTYPE_Int:
pRecord->Set_Value(iField, sValue.asInt());
break;
case TABLE_FIELDTYPE_Double:
pRecord->Set_Value(iField, sValue.asDouble());
break;
}
}
}
}
}
SG_UI_Process_Set_Ready();
}
return( Get_Field_Count() > 0 );
}
//---------------------------------------------------------
bool CSG_Table::_Save_Text(const SG_Char *File_Name, bool bHeadline, SG_Char Separator)
{
int iField, iRecord;
CSG_File Stream;
if( Get_Field_Count() > 0 )
{
if( Stream.Open(File_Name, SG_FILE_W, false) )
{
for(iField=0; iField<Get_Field_Count(); iField++)
{
Stream.Printf(SG_T("%s%c"), Get_Field_Name(iField), iField < Get_Field_Count() - 1 ? Separator : SG_T('\n'));
}
for(iRecord=0; iRecord<Get_Record_Count() && SG_UI_Process_Set_Progress(iRecord, Get_Record_Count()); iRecord++)
{
for(iField=0; iField<Get_Field_Count(); iField++)
{
Stream.Printf(SG_T("%s"), Get_Record(iRecord)->asString(iField));
Stream.Printf(SG_T("%c"), iField < Get_Field_Count() - 1 ? Separator : SG_T('\n'));
}
}
SG_UI_Process_Set_Ready();
return( true );
}
}
return( false );
}
///////////////////////////////////////////////////////////
// //
// DBase //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Table::_Load_DBase(const SG_Char *File_Name)
{
int iField;
CSG_Table_DBase dbf;
CSG_Table_Record *pRecord;
//-----------------------------------------------------
if( dbf.Open(File_Name) )
{
Destroy();
for(iField=0; iField<dbf.Get_FieldCount(); iField++)
{
TSG_Table_Field_Type Type;
switch( dbf.Get_FieldType(iField) )
{
case DBF_FT_LOGICAL:
Type = TABLE_FIELDTYPE_Char;
break;
case DBF_FT_CHARACTER: default:
Type = TABLE_FIELDTYPE_String;
break;
case DBF_FT_DATE:
Type = TABLE_FIELDTYPE_Date;
break;
case DBF_FT_NUMERIC:
Type = dbf.Get_FieldDecimals(iField) > 0
? TABLE_FIELDTYPE_Double
: TABLE_FIELDTYPE_Long;
break;
}
Add_Field(SG_STR_MBTOSG(dbf.Get_FieldName(iField)), Type);
}
//-------------------------------------------------
if( dbf.Move_First() && dbf.Get_Record_Count() > 0 )
{
m_nRecords = dbf.Get_Record_Count();
m_Records = (CSG_Table_Record **)SG_Malloc(m_nRecords * sizeof(CSG_Table_Record *));
for(int iRecord=0; iRecord<m_nRecords && SG_UI_Process_Set_Progress(iRecord, m_nRecords); iRecord++)
{
m_Records[iRecord] = pRecord = new CSG_Table_Record(this, iRecord);
for(iField=0; iField<Get_Field_Count(); iField++)
{
switch( Get_Field_Type(iField) )
{
case TABLE_FIELDTYPE_Char:
pRecord->Set_Value(iField, SG_STR_MBTOSG(dbf.asString(iField)) );
break;
case TABLE_FIELDTYPE_String: default:
pRecord->Set_Value(iField, SG_STR_MBTOSG(dbf.asString(iField)) );
break;
case TABLE_FIELDTYPE_Long:
pRecord->Set_Value(iField, dbf.asInt(iField) );
break;
case TABLE_FIELDTYPE_Double:
pRecord->Set_Value(iField, dbf.asDouble(iField) );
break;
}
}
dbf.Move_Next();
}
SG_UI_Process_Set_Ready();
Set_Modified(false);
_Range_Invalidate();
}
return( true );
}
return( false );
}
//---------------------------------------------------------
bool CSG_Table::_Save_DBase(const SG_Char *File_Name)
{
int iField, iRecord;
CSG_Table_Record *pRecord;
CSG_Table_DBase dbf;
CSG_Table_DBase::TFieldDesc *dbfFieldDesc;
//-----------------------------------------------------
dbfFieldDesc = (CSG_Table_DBase::TFieldDesc *)SG_Calloc(Get_Field_Count(), sizeof(CSG_Table_DBase::TFieldDesc));
for(iField=0; iField<Get_Field_Count(); iField++)
{
strncpy(dbfFieldDesc[iField].Name, SG_STR_SGTOMB(Get_Field_Name(iField)), 11);
switch( Get_Field_Type(iField) )
{
default:
case TABLE_FIELDTYPE_String:
dbfFieldDesc[iField].Type = DBF_FT_CHARACTER;
dbfFieldDesc[iField].Width = (char)64;
break;
case TABLE_FIELDTYPE_Date:
dbfFieldDesc[iField].Type = DBF_FT_DATE;
dbfFieldDesc[iField].Width = (char)12;
break;
case TABLE_FIELDTYPE_Char:
dbfFieldDesc[iField].Type = DBF_FT_CHARACTER;
dbfFieldDesc[iField].Width = (char)1;
break;
case TABLE_FIELDTYPE_Short:
case TABLE_FIELDTYPE_Int:
case TABLE_FIELDTYPE_Long:
case TABLE_FIELDTYPE_Color:
dbfFieldDesc[iField].Type = DBF_FT_NUMERIC;
dbfFieldDesc[iField].Width = (char)16;
dbfFieldDesc[iField].Decimals = (char)0;
break;
case TABLE_FIELDTYPE_Float:
case TABLE_FIELDTYPE_Double:
dbfFieldDesc[iField].Type = DBF_FT_NUMERIC;
dbfFieldDesc[iField].Width = (char)16;
dbfFieldDesc[iField].Decimals = (char)8;
break;
}
}
dbf.Open(File_Name, Get_Field_Count(), dbfFieldDesc);
SG_Free(dbfFieldDesc);
//-----------------------------------------------------
for(iRecord=0; iRecord<Get_Record_Count() && SG_UI_Process_Set_Progress(iRecord, Get_Record_Count()); iRecord++)
{
pRecord = Get_Record(iRecord);
dbf.Add_Record();
for(iField=0; iField<Get_Field_Count(); iField++)
{
switch( dbf.Get_FieldType(iField) )
{
case DBF_FT_CHARACTER:
dbf.Set_Value(iField, SG_STR_SGTOMB(pRecord->asString(iField)));
break;
case DBF_FT_NUMERIC:
dbf.Set_Value(iField, pRecord->asDouble(iField));
break;
}
}
dbf.Flush_Record();
}
SG_UI_Process_Set_Ready();
return( true );
}
///////////////////////////////////////////////////////////
// //
// Serialize //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Table::Serialize(CSG_File &Stream, bool bSave)
{
SG_Char Separator = SG_T('\t');
int iField, nFields, iRecord, nRecords, FieldType;
CSG_Table_Record *pRecord;
CSG_String s, sLine;
//-----------------------------------------------------
if( bSave )
{
Stream.Printf(SG_T("%d %d\n"), m_nFields, m_nRecords);
for(iField=0; iField<m_nFields; iField++)
{
Stream.Printf(SG_T("%d \"%s\"\n"), Get_Field_Type(iField), Get_Field_Name(iField));
}
for(iRecord=0; iRecord<m_nRecords; iRecord++)
{
for(iField=0; iField<m_nFields; iField++)
{
Stream.Printf(SG_T("%s%c"), Get_Record(iRecord)->asString(iField), iField < m_nFields - 1 ? Separator : '\n');
}
}
return( true );
}
//-----------------------------------------------------
else if( Stream.Read_Line(sLine) && SG_SSCANF(sLine, SG_T("%d %d"), &nFields, &nRecords) == 2 && nFields > 0 )
{
_Destroy();
for(iField=0; iField<nFields; iField++)
{
if( Stream.Read_Line(sLine) && SG_SSCANF(sLine, SG_T("%d"), &FieldType) == 1 )
{
Add_Field(sLine.AfterFirst(SG_T('\"')).BeforeFirst(SG_T('\"')), (TSG_Table_Field_Type)FieldType);
}
}
for(iRecord=0; iRecord<nRecords; iRecord++)
{
if( Stream.Read_Line(sLine) )
{
pRecord = _Add_Record();
for(iField=0; iField<m_nFields; iField++)
{
pRecord->Set_Value(iField, sLine.BeforeFirst(Separator));
sLine = sLine.AfterFirst(Separator);
}
}
}
return( true );
}
//-----------------------------------------------------
return( false );
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -