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 >
C/C++ Source or Header  |  2012-07-03  |  9KB  |  280 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //    License (GPLv2+):
  3. //    This program is free software; you can redistribute it and/or modify
  4. //    it under the terms of the GNU General Public License as published by
  5. //    the Free Software Foundation; either version 2 of the License, or
  6. //    (at your option) any later version.
  7. //
  8. //    This program is distributed in the hope that it will be useful, but
  9. //    WITHOUT ANY WARRANTY; without even the implied warranty of
  10. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. //    General Public License for more details.
  12. //
  13. //    You should have received a copy of the GNU General Public License
  14. //    along with this program; if not, write to the Free Software
  15. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. /////////////////////////////////////////////////////////////////////////////
  17.  
  18. // fshedDoc.cpp : implementation of the CFshedDoc class
  19. //
  20.  
  21. #include "stdafx.h"
  22. #include <fcntl.h>
  23. #include <io.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include "fshed.h"
  27.  
  28. #include "fshedDoc.h"
  29.  
  30. #ifdef _DEBUG
  31. #define new DEBUG_NEW
  32. #undef THIS_FILE
  33. static char THIS_FILE[] = __FILE__;
  34. #endif
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CFshedDoc
  38.  
  39. IMPLEMENT_DYNCREATE(CFshedDoc, CDocument)
  40.  
  41. BEGIN_MESSAGE_MAP(CFshedDoc, CDocument)
  42.     //{{AFX_MSG_MAP(CFshedDoc)
  43.         // NOTE - the ClassWizard will add and remove mapping macros here.
  44.         //    DO NOT EDIT what you see in these blocks of generated code!
  45.     //}}AFX_MSG_MAP
  46. END_MESSAGE_MAP()
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CFshedDoc construction/destruction
  50.  
  51. CFshedDoc::CFshedDoc()
  52. {
  53.     bReadOnly = FALSE;
  54.     bPartialOpen=FALSE;
  55.     bFilestatusChanged = TRUE;
  56.     filename = new TCHAR[_MAX_PATH];
  57.     filename[0] = '\0';
  58.     bFileNeverSaved = TRUE;
  59.     bFileNeverSaved = TRUE;
  60.     DataArray.ClearAll ();
  61.     DataArray.SetGrowBy (100);
  62.     _stprintf (filename, _T("Untitled"));
  63.     iPartialOffset=0;
  64. }
  65.  
  66. CFshedDoc::~CFshedDoc()
  67. {
  68.     if (filename != NULL)
  69.         delete [] filename;
  70. }
  71.  
  72. BOOL CFshedDoc::OnNewDocument()
  73. {
  74.     if (!CDocument::OnNewDocument())
  75.         return FALSE;
  76.  
  77.     // TODO: add reinitialization code here
  78.     // (SDI documents will reuse this document)
  79.  
  80.     return TRUE;
  81. }
  82.  
  83.  
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CFshedDoc serialization
  87.  
  88. void CFshedDoc::Serialize(CArchive& ar)
  89. {
  90.     if (ar.IsStoring())
  91.     {
  92.         SetCursor (LoadCursor (NULL, IDC_WAIT));
  93.         ar.GetFile()->Write(DataArray, DataArray.GetLength());
  94.         SetCursor (LoadCursor (NULL, IDC_ARROW));
  95.         bFilestatusChanged = TRUE;
  96.         bPartialOpen=FALSE;
  97.     }
  98.     else
  99.     {
  100.         // TODO: add loading code here
  101.     }
  102. }
  103.  
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CFshedDoc diagnostics
  106.  
  107. #ifdef _DEBUG
  108. void CFshedDoc::AssertValid() const
  109. {
  110.     CDocument::AssertValid();
  111. }
  112.  
  113. void CFshedDoc::Dump(CDumpContext& dc) const
  114. {
  115.     CDocument::Dump(dc);
  116. }
  117. #endif //_DEBUG
  118.  
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CFshedDoc commands
  121.  
  122.  
  123. //--------------------------------------------------------------------------------------------
  124. int CFshedDoc::file_is_loadable (LPCTSTR fname)
  125. {
  126.     int filehandle;
  127.     if ((filehandle = _topen (fname,_O_RDONLY|_O_BINARY,_S_IREAD|_S_IWRITE)) != -1)
  128.     {
  129.         _close (filehandle);
  130.         return TRUE;
  131.     }
  132.     else
  133.         return FALSE;
  134. }
  135.  
  136. //--------------------------------------------------------------------------------------------
  137. int CFshedDoc::load_file (LPCTSTR fname)
  138. {
  139.     if (file_is_loadable (fname))
  140.     {        
  141.         int filehandle;
  142.         if ((filehandle = _topen (fname,_O_RDONLY|_O_BINARY,_S_IREAD|_S_IWRITE)) != -1)
  143.         {
  144.             int filelen = _filelength (filehandle);
  145.             DataArray.ClearAll ();
  146.             // Try to allocate memory for the file.
  147.             if (DataArray.SetSize (filelen) == TRUE)
  148.             {
  149.                 // If read-only mode on opening is enabled:
  150.                 if( App->m_bOpenReadOnly )
  151.                     bReadOnly = TRUE;
  152.                 else
  153.                     bReadOnly = FALSE;
  154.  
  155.                 if( filelen == 0)
  156.                 {
  157.                     // This is an empty file. Don't need to read anything.
  158.                     _close( filehandle );
  159.                     _tcscpy( filename, fname );
  160.                     bFileNeverSaved = FALSE;
  161.                     bPartialOpen=FALSE;
  162.                     // Update MRU list.
  163. //                    update_MRU ();
  164.                     bFilestatusChanged = TRUE;
  165. //                    update_for_new_datasize();
  166.                     return TRUE;
  167.                 }
  168.                 else
  169.                 {
  170.                     // Load the file.
  171.                     SetCursor (LoadCursor (NULL, IDC_WAIT));
  172.                     DataArray.SetUpperBound (filelen-1);
  173.                     if (_read (filehandle, DataArray, DataArray.GetLength ()) != -1)
  174.                     {
  175.                         _close (filehandle);
  176.                         _tcscpy (filename, fname);
  177.                         bFileNeverSaved = FALSE;
  178.                         bPartialOpen=FALSE;
  179.                         // Update MRU list.
  180. //                        update_MRU ();
  181.                         bFilestatusChanged = TRUE;
  182. //                        update_for_new_datasize();
  183.                         SetCursor (LoadCursor (NULL, IDC_ARROW));
  184.                         return TRUE;
  185.                     }
  186.                     else
  187.                     {
  188.                         _close (filehandle);
  189.                         SetCursor (LoadCursor (NULL, IDC_ARROW));
  190.                   ::MessageBox (NULL, _T("Error while reading from file."), _T("Load error"), MB_OK | MB_ICONERROR);
  191.                         return FALSE;
  192.                     }
  193.                 }
  194.             }
  195.             else
  196.             {
  197.             ::MessageBox (NULL, _T("Not enough memory to load file."), _T("Load error"), MB_OK | MB_ICONERROR);
  198.                 return FALSE;
  199.             }
  200.         }
  201.         else
  202.         {
  203.             TCHAR buf[500];
  204.             _stprintf (buf, _T("Error code 0x%x occured while opening file %s."), errno, fname);
  205.          ::MessageBox (NULL, buf, _T("Load error"), MB_OK | MB_ICONERROR);
  206.             return FALSE;
  207.         }
  208.     }
  209.     else
  210.         return FALSE;
  211. }
  212.  
  213. BOOL CFshedDoc::OnOpenDocument(LPCTSTR lpszPathName) 
  214. {
  215.     if (IsModified())
  216.         TRACE0("Warning: OnOpenDocument replaces an unsaved document.\n");
  217.  
  218.     CFileException fe;
  219.     CFile* pFile = GetFile(lpszPathName,
  220.         CFile::modeRead|CFile::shareDenyWrite, &fe);
  221.     if (pFile == NULL)
  222.     {
  223.         ReportSaveLoadException(lpszPathName, &fe,
  224.             FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
  225.         return FALSE;
  226.     }
  227.    else
  228.       delete pFile;
  229.  
  230.     DeleteContents();
  231.     SetModifiedFlag();  // dirty during de-serialize
  232.  
  233.    if (!load_file (lpszPathName))
  234.         return FALSE;
  235.  
  236.     SetModifiedFlag(FALSE);     // start off with unmodified
  237.     AfxGetApp()->AddToRecentFileList(lpszPathName);
  238.  
  239.    return TRUE;
  240. }
  241.  
  242. BOOL CFshedDoc::OnSaveDocument(LPCTSTR lpszPathName) 
  243. {
  244.     // File is partially loaded => must be saved partially or saved as.
  245.     if (bPartialOpen)
  246.     {
  247.         int filehandle;
  248.         if ((filehandle = _topen (filename,_O_RDWR|_O_BINARY,_S_IREAD|_S_IWRITE)) != -1)
  249.         {
  250.             CWaitCursor w1;
  251.             if( _lseek( filehandle, iPartialOffset, 0 ) == -1 )
  252.             {
  253.                 AfxMessageBox(_T("Could not seek in file."), MB_OK | MB_ICONERROR );
  254.                 _close( filehandle );
  255.                 return 0;
  256.             }
  257.             if( _write( filehandle, DataArray, DataArray.GetLength() ) == -1 )
  258.             {
  259.                 AfxMessageBox(_T("Could not write data to file."), MB_OK | MB_ICONERROR );
  260.             }
  261.             _close (filehandle);
  262. //            m_iFileChanged = FALSE;
  263.             bFilestatusChanged = TRUE;
  264.         }
  265.         else
  266.         {
  267.             AfxMessageBox(_T("Could not save partially opened file."), MB_OK | MB_ICONERROR );
  268.         }
  269. //        repaint ();
  270.         return 1;
  271.     }
  272.     // File was not saved before => name must be chosen.
  273.     //if( bFileNeverSaved )
  274.     //{
  275.     //    MessageBox (NULL, _T("Can't save because file is untitled.\nPlease choose \"Save As...\" from menu"), _T("Save"), MB_OK | MB_ICONINFORMATION);
  276.     //    return 0;
  277.     //}
  278.     return CDocument::OnSaveDocument(lpszPathName);
  279. }
  280.