📄 fsheddoc.cpp
字号:
// fshedDoc.cpp : implementation of the CFshedDoc class
//
#include "stdafx.h"
#include <fcntl.h>
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "fshed.h"
#include "fshedDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFshedDoc
IMPLEMENT_DYNCREATE(CFshedDoc, CDocument)
BEGIN_MESSAGE_MAP(CFshedDoc, CDocument)
//{{AFX_MSG_MAP(CFshedDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFshedDoc construction/destruction
CFshedDoc::CFshedDoc()
{
bReadOnly = FALSE;
bPartialOpen=FALSE;
bFilestatusChanged = TRUE;
filename = new char[_MAX_PATH];
filename[0] = '\0';
bFileNeverSaved = TRUE;
bFileNeverSaved = TRUE;
DataArray.ClearAll ();
DataArray.SetGrowBy (100);
sprintf (filename, "Untitled");
iPartialOffset=0;
}
CFshedDoc::~CFshedDoc()
{
if (filename != NULL)
delete [] filename;
}
BOOL CFshedDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CFshedDoc serialization
void CFshedDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
SetCursor (LoadCursor (NULL, IDC_WAIT));
ar.GetFile()->Write(DataArray, DataArray.GetLength());
SetCursor (LoadCursor (NULL, IDC_ARROW));
bFilestatusChanged = TRUE;
bPartialOpen=FALSE;
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CFshedDoc diagnostics
#ifdef _DEBUG
void CFshedDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CFshedDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CFshedDoc commands
//--------------------------------------------------------------------------------------------
int CFshedDoc::file_is_loadable (LPCTSTR fname)
{
int filehandle;
if ((filehandle = _open (fname,_O_RDONLY|_O_BINARY,_S_IREAD|_S_IWRITE)) != -1)
{
_close (filehandle);
return TRUE;
}
else
return FALSE;
}
//--------------------------------------------------------------------------------------------
int CFshedDoc::load_file (LPCTSTR fname)
{
if (file_is_loadable (fname))
{
int filehandle;
if ((filehandle = _open (fname,_O_RDONLY|_O_BINARY,_S_IREAD|_S_IWRITE)) != -1)
{
int filelen = _filelength (filehandle);
DataArray.ClearAll ();
// Try to allocate memory for the file.
if (DataArray.SetSize (filelen) == TRUE)
{
// If read-only mode on opening is enabled:
if( App->m_bOpenReadOnly )
bReadOnly = TRUE;
else
bReadOnly = FALSE;
if( filelen == 0)
{
// This is an empty file. Don't need to read anything.
_close( filehandle );
strcpy( filename, fname );
bFileNeverSaved = FALSE;
bPartialOpen=FALSE;
// Update MRU list.
// update_MRU ();
bFilestatusChanged = TRUE;
// update_for_new_datasize();
return TRUE;
}
else
{
// Load the file.
SetCursor (LoadCursor (NULL, IDC_WAIT));
DataArray.SetUpperBound (filelen-1);
if (_read (filehandle, DataArray, DataArray.GetLength ()) != -1)
{
_close (filehandle);
strcpy (filename, fname);
bFileNeverSaved = FALSE;
bPartialOpen=FALSE;
// Update MRU list.
// update_MRU ();
bFilestatusChanged = TRUE;
// update_for_new_datasize();
SetCursor (LoadCursor (NULL, IDC_ARROW));
return TRUE;
}
else
{
_close (filehandle);
SetCursor (LoadCursor (NULL, IDC_ARROW));
::MessageBox (NULL, "Error while reading from file.", "Load error", MB_OK | MB_ICONERROR);
return FALSE;
}
}
}
else
{
::MessageBox (NULL, "Not enough memory to load file.", "Load error", MB_OK | MB_ICONERROR);
return FALSE;
}
}
else
{
char buf[500];
sprintf (buf, "Error code 0x%x occured while opening file %s.", errno, fname);
::MessageBox (NULL, buf, "Load error", MB_OK | MB_ICONERROR);
return FALSE;
}
}
else
return FALSE;
}
BOOL CFshedDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (IsModified())
TRACE0("Warning: OnOpenDocument replaces an unsaved document.\n");
CFileException fe;
CFile* pFile = GetFile(lpszPathName,
CFile::modeRead|CFile::shareDenyWrite, &fe);
if (pFile == NULL)
{
ReportSaveLoadException(lpszPathName, &fe,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
return FALSE;
}
else
delete pFile;
DeleteContents();
SetModifiedFlag(); // dirty during de-serialize
if (!load_file (lpszPathName))
return FALSE;
SetModifiedFlag(FALSE); // start off with unmodified
return TRUE;
}
BOOL CFshedDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// File is partially loaded => must be saved partially or saved as.
if (bPartialOpen)
{
int filehandle;
if ((filehandle = _open (filename,_O_RDWR|_O_BINARY,_S_IREAD|_S_IWRITE)) != -1)
{
CWaitCursor w1;
if( _lseek( filehandle, iPartialOffset, 0 ) == -1 )
{
AfxMessageBox("Could not seek in file.", MB_OK | MB_ICONERROR );
_close( filehandle );
return 0;
}
if( _write( filehandle, DataArray, DataArray.GetLength() ) == -1 )
{
AfxMessageBox("Could not write data to file.", MB_OK | MB_ICONERROR );
}
_close (filehandle);
// m_iFileChanged = FALSE;
bFilestatusChanged = TRUE;
}
else
{
AfxMessageBox("Could not save partially opened file.", MB_OK | MB_ICONERROR );
}
// repaint ();
return 1;
}
// File was not saved before => name must be chosen.
if( bFileNeverSaved )
{
MessageBox (NULL, "Can't save because file is untitled.\nPlease choose \"Save As...\" from menu", "Save", MB_OK | MB_ICONINFORMATION);
return 0;
}
/* int filehandle;
if ((filehandle = _open (filename,_O_RDWR|_O_CREAT|_O_TRUNC|_O_BINARY,_S_IREAD|_S_IWRITE)) != -1)
{
SetCursor (LoadCursor (NULL, IDC_WAIT));
_write (filehandle, DataArray, DataArray.GetLength ());
_close (filehandle);
SetCursor (LoadCursor (NULL, IDC_ARROW));
// m_iFileChanged = FALSE;
bFilestatusChanged = TRUE;
bPartialOpen=FALSE;
}
else
{
MessageBox (NULL, "Could not save file.", "Save", MB_OK | MB_ICONERROR);
}
*/
return CDocument::OnSaveDocument(lpszPathName);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -