Annotation of mstools/mfc/samples/chart/chfile.cpp, revision 1.1

1.1     ! root        1: // chfile.cpp : Defines the file input and output logic.
        !             2: //
        !             3: // This is a part of the Microsoft Foundation Classes C++ library.
        !             4: // Copyright (C) 1992 Microsoft Corporation
        !             5: // All rights reserved.
        !             6: //
        !             7: // This source code is only intended as a supplement to the
        !             8: // Microsoft Foundation Classes Reference and Microsoft
        !             9: // QuickHelp documentation provided with the library.
        !            10: // See these sources for detailed information regarding the
        !            11: // Microsoft Foundation Classes product.
        !            12: //
        !            13: 
        !            14: #include "chart.h"
        !            15: 
        !            16: #include <commdlg.h>
        !            17: 
        !            18: /////////////////////////////////////////////////////////////////////////////
        !            19: 
        !            20: // LoadFile:
        !            21: // Load the named file.  Uses Foundation archive/serialization format.
        !            22: //
        !            23: int CChartWnd::LoadFile(const char* pName)
        !            24: {
        !            25:        CFile file;
        !            26:        
        !            27:        // The file has a title, so reset the UNTITLED flag.
        !            28:        //
        !            29:        m_bUntitled = FALSE;
        !            30:        
        !            31:        if (!file.Open(pName, CFile::modeRead))
        !            32:        {
        !            33:                MessageBox("Not able to open file.","Chart",
        !            34:                        MB_ICONEXCLAMATION | MB_OK);
        !            35:                return FALSE;
        !            36:        }
        !            37: 
        !            38:        // Read the file into the buffer.
        !            39:        // If it fails, inform the user.
        !            40:        //
        !            41: 
        !            42:        if (m_pChartObject != NULL)
        !            43:        {
        !            44:                delete m_pChartObject;
        !            45:                m_pChartObject = NULL;
        !            46:        }
        !            47: 
        !            48:        // Set up a CArchive to serialize in the data.
        !            49:        CArchive ar(&file, CArchive::load);
        !            50: 
        !            51:        TRY
        !            52:        {
        !            53:                m_bChartSerializedOK = TRUE;
        !            54:                ar >> m_pChartObject;
        !            55:                ASSERT(m_pChartObject->m_bDirty == FALSE);
        !            56:        }
        !            57:        CATCH(CArchiveException, e)
        !            58:        {
        !            59:                MessageBox("Not able to read from file.", "Chart",
        !            60:                        MB_ICONEXCLAMATION | MB_OK);
        !            61: 
        !            62:                m_bChartSerializedOK = FALSE;
        !            63:        }
        !            64:        AND_CATCH(CFileException, e)
        !            65:        {
        !            66:                MessageBox("Not able to read from file.", "Chart",
        !            67:                        MB_ICONEXCLAMATION | MB_OK);
        !            68: 
        !            69:                m_bChartSerializedOK = FALSE;
        !            70:        }
        !            71:        END_CATCH
        !            72: 
        !            73:        ar.Close();
        !            74:        file.Close();
        !            75: 
        !            76:        return m_bChartSerializedOK;
        !            77: }
        !            78: 
        !            79: 
        !            80: // FileDlg:
        !            81: // Put up an open or save file dialog, collect response (passed back
        !            82: // via szFile parameter).  
        !            83: //
        !            84: BOOL CChartWnd::FileDlg( BOOL bOpen, int nMaxFile, LPSTR szFile)
        !            85: {
        !            86:        char szFilter[] = "Chart Files (*.cht)|*.cht||";
        !            87:        CFileDialog fileDialog(bOpen, "cht", szFile, OFN_HIDEREADONLY, szFilter);
        !            88: 
        !            89:        if (fileDialog.DoModal() == IDCANCEL)
        !            90:                return FALSE;
        !            91:        _fstrcpy(szFile, fileDialog.GetPathName());
        !            92:        return TRUE;
        !            93: }
        !            94: 
        !            95: 
        !            96: 
        !            97: 
        !            98: // ReadFile:
        !            99: // Prompt user for a filename and try to read in the contained chart.
        !           100: //
        !           101: #define FILENAMELEN 128
        !           102: 
        !           103: void CChartWnd::ReadFile()
        !           104: {
        !           105:    
        !           106:        // Set the length of the CString, m_szFileName, to the maximum 
        !           107:        // file length.  This prevents overwriting.
        !           108: 
        !           109:        // If there's no filename, the dialog was canceled.
        !           110:        //
        !           111:        if (!FileDlg(TRUE, FILENAMELEN, m_szFileName.GetBuffer(FILENAMELEN)))
        !           112:        {
        !           113:                return;
        !           114:        }
        !           115: 
        !           116:        // Reset the length of the CString, m_szFileName, to the actual
        !           117:        // length of the file name.
        !           118: 
        !           119:        m_szFileName.ReleaseBuffer();
        !           120: 
        !           121:        // Load file into this window.
        !           122:        //
        !           123:        LoadFile(m_szFileName);
        !           124: }
        !           125: 
        !           126: // SaveFile:
        !           127: // Prompt user for a save file name and write the chart to the file
        !           128: //
        !           129: void CChartWnd::SaveFile(BOOL bNewFileName)
        !           130: {
        !           131:        CFile file;
        !           132: 
        !           133:        // Set the length of the buffer to the maximum file length.  By
        !           134:        // doing this, possible memory overwriting is prevented.
        !           135: 
        !           136:        if (bNewFileName)
        !           137:        {   
        !           138:                if (m_bUntitled)
        !           139:                {
        !           140:                        // set a default string if the filename has not
        !           141:                        // been set.
        !           142:                        m_szFileName = "chart.cht";
        !           143:                }
        !           144: 
        !           145:                if (!FileDlg(FALSE, FILENAMELEN, 
        !           146:                        m_szFileName.GetBuffer(FILENAMELEN)))
        !           147:                {
        !           148:                        return;
        !           149:                }
        !           150:        }
        !           151: 
        !           152:        // Have the m_szFileName "shrink" to the actual length of the
        !           153:        // file name.
        !           154: 
        !           155:        m_szFileName.ReleaseBuffer();
        !           156: 
        !           157:        ASSERT(m_szFileName != "");
        !           158:        m_bUntitled = FALSE;
        !           159: 
        !           160:        if (!file.Open(m_szFileName, CFile::modeCreate | CFile::modeWrite))
        !           161:        {
        !           162:                MessageBox("Not able to create file.", "Chart",
        !           163:                        MB_ICONEXCLAMATION | MB_OK);
        !           164:                return;
        !           165:        }
        !           166:        
        !           167:        // Write out the contents of the buffer to the file.
        !           168:        // If this fails, inform the user.
        !           169:        //
        !           170:        CArchive ar(&file, CArchive::store);
        !           171: 
        !           172:        TRY
        !           173:        {
        !           174:                ar << m_pChartObject;
        !           175:        }
        !           176:        CATCH(CFileException, e)
        !           177:        {
        !           178:                MessageBox("Not able to write to file.", "Chart",
        !           179:                        MB_ICONEXCLAMATION | MB_OK);
        !           180:        }
        !           181:        AND_CATCH(CArchiveException, e)
        !           182:        {
        !           183:                MessageBox("Not able to write to file.", "Chart",
        !           184:                        MB_ICONEXCLAMATION | MB_OK);
        !           185:        }
        !           186:        END_CATCH
        !           187: 
        !           188:        ar.Close();
        !           189:        file.Close();
        !           190:        m_pChartObject->m_bDirty = FALSE;
        !           191: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.