home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.funduc.com
/
2014.08.ftp.funduc.com.tar
/
ftp.funduc.com
/
fshedcode-072212.zip
/
fshedDoc.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2012-07-03
|
9KB
|
280 lines
/////////////////////////////////////////////////////////////////////////////
// License (GPLv2+):
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
/////////////////////////////////////////////////////////////////////////////
// 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 TCHAR[_MAX_PATH];
filename[0] = '\0';
bFileNeverSaved = TRUE;
bFileNeverSaved = TRUE;
DataArray.ClearAll ();
DataArray.SetGrowBy (100);
_stprintf (filename, _T("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 = _topen (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 = _topen (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 );
_tcscpy( 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);
_tcscpy (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, _T("Error while reading from file."), _T("Load error"), MB_OK | MB_ICONERROR);
return FALSE;
}
}
}
else
{
::MessageBox (NULL, _T("Not enough memory to load file."), _T("Load error"), MB_OK | MB_ICONERROR);
return FALSE;
}
}
else
{
TCHAR buf[500];
_stprintf (buf, _T("Error code 0x%x occured while opening file %s."), errno, fname);
::MessageBox (NULL, buf, _T("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
AfxGetApp()->AddToRecentFileList(lpszPathName);
return TRUE;
}
BOOL CFshedDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// File is partially loaded => must be saved partially or saved as.
if (bPartialOpen)
{
int filehandle;
if ((filehandle = _topen (filename,_O_RDWR|_O_BINARY,_S_IREAD|_S_IWRITE)) != -1)
{
CWaitCursor w1;
if( _lseek( filehandle, iPartialOffset, 0 ) == -1 )
{
AfxMessageBox(_T("Could not seek in file."), MB_OK | MB_ICONERROR );
_close( filehandle );
return 0;
}
if( _write( filehandle, DataArray, DataArray.GetLength() ) == -1 )
{
AfxMessageBox(_T("Could not write data to file."), MB_OK | MB_ICONERROR );
}
_close (filehandle);
// m_iFileChanged = FALSE;
bFilestatusChanged = TRUE;
}
else
{
AfxMessageBox(_T("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, _T("Can't save because file is untitled.\nPlease choose \"Save As...\" from menu"), _T("Save"), MB_OK | MB_ICONINFORMATION);
// return 0;
//}
return CDocument::OnSaveDocument(lpszPathName);
}