www.pudn.com > 球和立方体.rar > MultiViewsTestDoc.cpp


// MultiViewsTestDoc.cpp : implementation of the CMultiViewsTestDoc class 
// 
 
#include "stdafx.h" 
#include "MultiViewsTest.h" 
 
#include "MultiViewsTestDoc.h" 
#include "MyLink.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
 
 
extern GraphNode* glHead; 
extern "C" GraphNode* GraphAddHead(GraphNode* head , GraphNode* pNewNode); 
///////////////////////////////////////////////////////////////////////////// 
// CMultiViewsTestDoc 
 
IMPLEMENT_DYNCREATE(CMultiViewsTestDoc, CDocument) 
 
BEGIN_MESSAGE_MAP(CMultiViewsTestDoc, CDocument) 
	//{{AFX_MSG_MAP(CMultiViewsTestDoc) 
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen) 
	ON_COMMAND(ID_FILE_SAVE, OnFileSave) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMultiViewsTestDoc construction/destruction 
 
CMultiViewsTestDoc::CMultiViewsTestDoc() 
{ 
	// TODO: add one-time construction code here 
 
} 
 
CMultiViewsTestDoc::~CMultiViewsTestDoc() 
{ 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CMultiViewsTestDoc serialization 
 
void CMultiViewsTestDoc::Serialize(CArchive& ar) 
{ 
	if (ar.IsStoring()) 
	{ 
		// TODO: add storing code here 
	} 
	else 
	{ 
		// TODO: add loading code here 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMultiViewsTestDoc diagnostics 
 
#ifdef _DEBUG 
void CMultiViewsTestDoc::AssertValid() const 
{ 
	CDocument::AssertValid(); 
} 
 
void CMultiViewsTestDoc::Dump(CDumpContext& dc) const 
{ 
	CDocument::Dump(dc); 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMultiViewsTestDoc commands 
 
void CMultiViewsTestDoc::OnFileOpen()  
{ 
	// TODO: Add your command handler code here 
		CFileDialog mFileDlg(true, NULL, NULL, 
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,  
		"All Files (*.*)|*.*| |", AfxGetMainWnd()); 
 
	if ( (mFileDlg.DoModal() ) == IDOK) 
	{	 
		 SPHEREDATA* strSphereData; 
		 GRAPHDATA pObject; 
 
		 CUBEDATA*  strCubeData; 
		 CString csFilePath; 
		 long int  currentsize; 
		 long int filesize;  
		 GLFILEHEAD strGLFileHead; 
	     FILE *fp; 
		  
		 csFilePath = mFileDlg.GetPathName(); 
         fp=fopen(csFilePath, "rb"); 
 
		 fread(&strGLFileHead, sizeof(GLFILEHEAD), 1, fp); 
		 currentsize = sizeof(GLFILEHEAD); 
		 filesize = strGLFileHead.wLength; 
		 GraphNode* strNewNode; 
 
		while(currentsize < filesize) 
		{ 
            fseek(fp ,currentsize, SEEK_SET);  
			fread( &pObject, sizeof(GRAPHDATA), 1, fp); 
			switch(pObject.iType) 
			{ 
			case 1: 
				fseek(fp ,currentsize, SEEK_SET); 
				strCubeData = new CUBEDATA; 
				fread(strCubeData,sizeof(CUBEDATA), 1, fp); 
                 
				strNewNode = new GraphNode; 
				strNewNode->pGraphData = (char *) strCubeData; 
				strNewNode->next = NULL; 
				glHead = GraphAddHead(glHead, strNewNode); 
 
				currentsize += sizeof(CUBEDATA); 
				break; 
	 
			case 2: 
			 
		    	fseek(fp ,currentsize, SEEK_SET); 
				strSphereData = new SPHEREDATA; 
				fread(strSphereData,sizeof(SPHEREDATA), 1, fp); 
 
                strNewNode = new GraphNode; 
				strNewNode->pGraphData = (char *) strSphereData; 
				strNewNode->next = NULL; 
				glHead = GraphAddHead(glHead, strNewNode); 
 
				currentsize += sizeof(SPHEREDATA); 
			     
 
				break; 
 
			default: 
				break; 
			} 
		} 
		fclose(fp); 
	} 
} 
 
void CMultiViewsTestDoc::OnFileSave()  
{ 
	// TODO: Add your command handler code here 
	CFileDialog mFileDlg(false, NULL, NULL, 
	OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,  
	"All Files (*.*)|*.*| |", AfxGetMainWnd()); 
	if ( (mFileDlg.DoModal()) == IDOK ) 
	{ 
		 
		 GRAPHDATA* pObject; 
		 CString csFilePath; 
		 GraphNode* current; 
		 long int filesize; 
	   	 FILE *fp;  
 
		csFilePath = mFileDlg.GetPathName(); 
		fp=fopen(csFilePath, "wb");  
		GLFILEHEAD* pGLFileHead = new GLFILEHEAD; 
       
//		pGLFileHead->pfUsername = "ll"; 
        
		fseek (fp, sizeof(GLFILEHEAD), SEEK_SET); 
		filesize = sizeof(GLFILEHEAD); 
		current = glHead; 
		while(current) 
		{ 
 
			pObject =  (GRAPHDATA*)current->pGraphData; 
			switch(pObject->iType) 
			{ 
			case 1: 
                fseek (fp, filesize, SEEK_SET); 
				fwrite(current->pGraphData, 
					sizeof(CUBEDATA), 1, fp); 
				filesize += sizeof(CUBEDATA); 
				break; 
	 
			case 2: 
				fseek (fp, filesize, SEEK_SET); 
				fwrite(current->pGraphData, 
					sizeof(SPHEREDATA), 1, fp); 
				filesize += sizeof(SPHEREDATA); 
				break; 
 
			default: 
				break; 
			} 
	   	 current = current->next; 
		} 
		pGLFileHead->wLength = filesize; 
      	fseek (fp, 0, SEEK_SET); 
		fwrite(pGLFileHead,sizeof(GLFILEHEAD), 1, fp); 
	    fclose(fp);	 
	} 
} 
 
BOOL CMultiViewsTestDoc::OnOpenDocument(LPCTSTR lpszPathName)  
{ 
	if (!CDocument::OnOpenDocument(lpszPathName)) 
		return FALSE; 
	 
	// TODO: Add your specialized creation code here 
	//OnFileOpen(); 
		 SPHEREDATA* strSphereData; 
		 GRAPHDATA pObject; 
 
		 CUBEDATA*  strCubeData; 
		 CString csFilePath; 
		 long int  currentsize; 
		 long int filesize;  
		 GLFILEHEAD strGLFileHead; 
	     FILE *fp; 
		  
		 csFilePath =lpszPathName ; 
         fp=fopen(csFilePath, "rb"); 
 
		 fread(&strGLFileHead, sizeof(GLFILEHEAD), 1, fp); 
		 currentsize = sizeof(GLFILEHEAD); 
		 filesize = strGLFileHead.wLength; 
		 GraphNode* strNewNode; 
 
		while(currentsize < filesize) 
		{ 
            fseek(fp ,currentsize, SEEK_SET);  
			fread( &pObject, sizeof(GRAPHDATA), 1, fp); 
			switch(pObject.iType) 
			{ 
			case 1: 
				fseek(fp ,currentsize, SEEK_SET); 
				strCubeData = new CUBEDATA; 
				fread(strCubeData,sizeof(CUBEDATA), 1, fp); 
                 
				strNewNode = new GraphNode; 
				strNewNode->pGraphData = (char *) strCubeData; 
				strNewNode->next = NULL; 
				glHead = GraphAddHead(glHead, strNewNode); 
 
				currentsize += sizeof(CUBEDATA); 
				break; 
	 
			case 2: 
			 
		    	fseek(fp ,currentsize, SEEK_SET); 
				strSphereData = new SPHEREDATA; 
				fread(strSphereData,sizeof(SPHEREDATA), 1, fp); 
 
                strNewNode = new GraphNode; 
				strNewNode->pGraphData = (char *) strSphereData; 
				strNewNode->next = NULL; 
				glHead = GraphAddHead(glHead, strNewNode); 
 
				currentsize += sizeof(SPHEREDATA); 
			     
 
				break; 
 
			default: 
				break; 
			} 
		} 
		fclose(fp); 
		 
	return TRUE; 
} 
 
BOOL CMultiViewsTestDoc::OnNewDocument()  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	 
	return CDocument::OnNewDocument(); 
}