📄 st3901070422doc.cpp
字号:
// ST3901070422Doc.cpp : CST3901070422Doc 类的实现
//
#include "stdafx.h"
#include "ST3901070422.h"
#include "ST3901070422Doc.h"
#include "MainFrm.h"
#include "ConflictDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CST3901070422Doc
IMPLEMENT_DYNCREATE(CST3901070422Doc, CDocument)
BEGIN_MESSAGE_MAP(CST3901070422Doc, CDocument)
ON_COMMAND(ID_DATA_IMPORT, &CST3901070422Doc::OnDataImport)
END_MESSAGE_MAP()
// CST3901070422Doc 构造/析构
CST3901070422Doc::CST3901070422Doc()
{
// TODO: 在此添加一次性构造代码
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 ));*/
}
CST3901070422Doc::~CST3901070422Doc()
{
}
bool CST3901070422Doc::LoadData(CStdioFile &infile)
{
/*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 ),
25.44 ));
m_DocList.AddTail( CStockData( _T("ARSC"),
COleDateTime( 1999, 4, 3, 0, 0, 0 ),
23.55 ));
m_DocList.AddTail( CStockData( _T("ARSC"),
COleDateTime( 1999, 4, 4, 0, 0, 0 ),
27.66 ));
m_DocList.AddTail( CStockData( _T("ARSC"),
COleDateTime( 1999, 4, 5, 0, 0, 0 ),
28.77 ));
m_DocList.AddTail( CStockData( _T("BBIC"),
COleDateTime( 1999, 4, 1, 0, 0, 0 ),
15.33 ));
m_DocList.AddTail( CStockData( _T("BBIC"),
COleDateTime( 1999, 4, 2, 0, 0, 0 ),
16.44 ));
m_DocList.AddTail( CStockData( _T("BBIC"),
COleDateTime( 1999, 4, 3, 0, 0, 0 ),
13.55 ));
m_DocList.AddTail( CStockData( _T("BBIC"),
COleDateTime( 1999, 4, 4, 0, 0, 0 ),
14.66 ));
m_DocList.AddTail( CStockData( _T("BBIC"),
COleDateTime( 1999, 4, 5, 0, 0, 0 ),
12.77 ));
m_DocList.AddTail( CStockData( _T("COMP"),
COleDateTime( 1999, 4, 1, 0, 0, 0 ),
15.33 ));
m_DocList.AddTail( CStockData( _T("COMP"),
COleDateTime( 1999, 4, 2, 0, 0, 0 ),
16.44 ));
m_DocList.AddTail( CStockData( _T("COMP"),
COleDateTime( 1999, 4, 3, 0, 0, 0 ),
18.55 ));
m_DocList.AddTail( CStockData( _T("COMP"),
COleDateTime( 1999, 4, 4, 0, 0, 0 ),
21.66 ));
m_DocList.AddTail( CStockData( _T("COMP"),
COleDateTime( 1999, 4, 5, 0, 0, 0 ),
19.77 ));*/
// Check for NULL
ASSERT( infile.m_hFile != NULL );
// Hold data in temporary list of CStockData objects
// which we only assign to CST3901070422Doc::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;
// Update main window
UpdateAllViews( NULL );
// Update Fund Selection window
CMainFrame * pWnd =
dynamic_cast< CMainFrame * > (AfxGetMainWnd());
if( pWnd )
{
pWnd->UpdateFundList( m_DocList );
// Show fund window after loading new funds
pWnd->SetFundsVisible( TRUE );
}
return true;
}
BOOL CST3901070422Doc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: 在此添加重新初始化代码
// (SDI 文档将重用该文档)
return TRUE;
}
// CST3901070422Doc 序列化
void CST3901070422Doc::Serialize(CArchive& ar)
{
m_DocList.Serialize(ar);
if (ar.IsStoring())
{
// TODO: 在此添加存储代码
ar << m_strCurrentFund;
}
else
{
// TODO: 在此添加加载代码
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 );
}
}
}
// CST3901070422Doc 诊断
#ifdef _DEBUG
void CST3901070422Doc::AssertValid() const
{
CDocument::AssertValid();
}
void CST3901070422Doc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
// CST3901070422Doc 命令
void CST3901070422Doc::OnDataImport()
{
// TODO: 在此添加命令处理程序代码
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;
}
/*LoadData(aFile);*/
if(LoadData(aFile))
{
SetModifiedFlag();
UpdateAllViews(NULL);
}
}
}
void CST3901070422Doc::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();
CDocument::DeleteContents();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -