?? stu3901070115doc.cpp
字號:
// STU3901070115Doc.cpp : CSTU3901070115Doc 類的實現
//
#include "stdafx.h"
#include "STU3901070115.h"
#include "STU3901070115Doc.h"
#include "MainFrm.h"
#include "ConflictDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CSTU3901070115Doc
IMPLEMENT_DYNCREATE(CSTU3901070115Doc, CDocument)
BEGIN_MESSAGE_MAP(CSTU3901070115Doc, CDocument)
ON_COMMAND(ID_DATA_IMPORT, &CSTU3901070115Doc::OnDataImport)
END_MESSAGE_MAP()
// CSTU3901070115Doc 構造/析構
CSTU3901070115Doc::CSTU3901070115Doc()
{
m_strCurrentFund=_T("");
/*m_DocList.AddTail( CStockData( _T("ARSC"),
COleDateTime( 1999, 4, 1, 0, 0, 0 ),
22.33 ));
m_DocList.AddTail( CStockData( _T("ARSC"),
COleDateTime( 1999, 4, 2, 0, 0, 0 ),
23.44 ));
m_DocList.AddTail( CStockData( _T("ARSC"),
COleDateTime( 1999, 4, 3, 0, 0, 0 ),
24.55 ));
m_DocList.AddTail( CStockData( _T("ARSC"),
COleDateTime( 1999, 4, 4, 0, 0, 0 ),
25.66 ));
m_DocList.AddTail( CStockData( _T("ARSC"),
COleDateTime( 1999, 4, 5, 0, 0, 0 ),
26.77 ));*/
// TODO: 在此添加一次性構造代碼
}
CSTU3901070115Doc::~CSTU3901070115Doc()
{
}
BOOL CSTU3901070115Doc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: 在此添加重新初始化代碼
// (SDI 文檔將重用該文檔)
return TRUE;
}
bool CSTU3901070115Doc::LoadData(CStdioFile& infile)
{
// Check for NULL
ASSERT( infile.m_hFile != NULL );
// Hold data in temporary list of CStockData objects
// which we only assign to CSTUploadDoc::m_DocList
// when we are sure load has been completed succesfully
CStockDataList TempList;
// Additions are cumulative so we need to copy in existing data
TempList.AddHead( &m_DocList );
// line buffer
CString strTemp;
// Today's date
COleDateTime Today = COleDateTime::GetCurrentTime();
COleDateTime FileDate;
CString strFileHeader;
int addedCtr = 0; // count added items
int discardedCtr = 0; // count discarded items
BOOL bFirstLine = TRUE;
while( infile.ReadString( strTemp ) )
{
BOOL bValidDate = FALSE;
CString strFund;
CString strDate;
// Exclude blank lines
if( strTemp.GetLength() == 0 ) continue;
if( bFirstLine )
{
// Get Header information
strFileHeader = strTemp.Left(18);
strFileHeader.TrimRight();
strDate = strTemp.Mid( 18, 10 );
}
else
{
strFund = strTemp.Left(8);
strFund.TrimRight();
strDate = strTemp.Mid( 8, 10 );
}
int nYear = _ttoi( strDate.Right( 4 ));
int nMonth = _ttoi( strDate.Left( 2 ));
int nDay = _ttoi( strDate.Mid( 3, 2 ));
COleDateTime aDate( nYear, nMonth, nDay, 0, 0, 0 );
if( aDate.GetStatus() != COleDateTime::valid )
{
if( bFirstLine )
{
// Cannot read file date - assume invalid
AfxMessageBox( _T("Invalid File Format") );
return FALSE;
}
else
{
// Cannot read record date - discard line
discardedCtr++;
continue;
}
}
if( bFirstLine )
{
// Get file date - loop back to top
FileDate = aDate;
bFirstLine = FALSE;
continue;
}
double dPrice = _tstof( strTemp.Mid( 19 ));
// Make a CStockData object and add it
// to our temporary array
CStockData aStData( strFund, aDate, dPrice );
CStockDataList::errorstatus err;
POSITION CurPos = TempList.AddSorted( aStData, err );
switch( err )
{
// Discard identical entry
case CStockDataList::duplicate_entry :
discardedCtr ++ ;
continue;
// Same record, different price value
case CStockDataList::conflicting_entry :
{
// Query if user wishes to discard duplicate, replace or abort.
CConflictDialog aDialog;
// Construct text to appear in Rich Edit control
CString strText = _T("Existing entry:\n\n");
CStockData SDTemp = TempList.GetAt( CurPos );
strText += SDTemp.GetAsString();
strText += _T("\n\nReplacement entry:\n\n");
strText += aStData.GetAsString();
// Assign text to control variable
aDialog.m_REditText = strText;
switch( aDialog.DoModal() )
{
case IDABORT : // Abandon
return FALSE;
case IDCANCEL : // Discard new record
discardedCtr++ ;
continue;
case IDOK : // Replace existing record
TempList.SetAt( CurPos, aStData );
}
}
default: // ok
addedCtr++ ;
}
}
// If we got this far then everything is OK -
CString strPrompt;
strPrompt.Format( _T(
"Import of file %s complete:\nRecords loaded: %d \
\nRecords discarded: %d \
\n\nHit OK to load data into document."),
strFileHeader, addedCtr, discardedCtr );
if( AfxMessageBox( strPrompt, MB_OKCANCEL ) == IDOK )
{
// Update document data
m_DocList.RemoveAll();
m_DocList.AddHead( &TempList );
// Update fund view
CMainFrame * pWnd =
dynamic_cast< CMainFrame * > (AfxGetMainWnd());
if( pWnd )
{
pWnd->UpdateFundList( m_DocList );
// Show fund window after loading new funds
pWnd->SetFundsVisible( TRUE );
}
return TRUE;
}
else
return FALSE;
}
// CSTU3901070115Doc 序列化
void CSTU3901070115Doc::Serialize(CArchive& ar)
{
m_DocList.Serialize( ar );
if (ar.IsStoring())
{
ar << m_strCurrentFund;
}
else
{
ar >> m_strCurrentFund;
// Update Fund Selection window
CMainFrame* pWnd =
dynamic_cast< CMainFrame * > (AfxGetMainWnd());
if( pWnd )
// Will fail if running from icon or from
// command line with file name argument
{
// Update and show fund window
pWnd->UpdateFundList( m_DocList, m_strCurrentFund );
pWnd->SetFundsVisible( TRUE );
}
}
}
// CSTU3901070115Doc 診斷
#ifdef _DEBUG
void CSTU3901070115Doc::AssertValid() const
{
CDocument::AssertValid();
}
void CSTU3901070115Doc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
// CSTU3901070115Doc 命令
void CSTU3901070115Doc::OnDataImport()
{
CString strFilter = _T("Data Files (*.dat)|*.dat|All Files (*.*)|*.*||");
CFileDialog aFileDialog( TRUE, NULL, NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
strFilter);
INT_PTR nID = aFileDialog.DoModal();
if(nID == IDOK)
{
CStdioFile aFile;
CFileException fx;
if( !aFile.Open( aFileDialog.GetPathName(), CFile::modeRead | CFile::typeText, &fx ) )
{
TCHAR buf[ 255 ];
fx.GetErrorMessage( buf, 255 );
CString strPrompt( buf );
AfxMessageBox( strPrompt );
return;
}
if(LoadData(aFile))
{
SetModifiedFlag();
UpdateAllViews(NULL);
}
}
// TODO: 在此添加命令處理程序代碼
}
void CSTU3901070115Doc::DeleteContents()
{
// TODO: 在此添加專用代碼和/或調用基類
m_DocList.RemoveAll();
CMainFrame * pWnd =
dynamic_cast< CMainFrame * > (AfxGetMainWnd());
if( pWnd )
{
pWnd->UpdateFundList( m_DocList );
// No funds on file, so hide fund window
pWnd->SetFundsVisible( FALSE );
// And reset current fund value
SetCurrentFund(_T(""));
}
CDocument::DeleteContents();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -