FFM.cpp
资源名称:DXGuide.zip [点击查看]
上传用户:wymy58
上传日期:2007-01-07
资源大小:2086k
文件大小:4k
源码类别:
DirextX编程
开发平台:
Visual C++
- // FFM.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "FFM.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // The one and only application object
- CWinApp theApp;
- using namespace std;
- void Usage();
- FILE_ENTRY *pFe;
- CFile fPf;
- PACK_FILE_HEAD pfh;
- int Compare(const void* p1, const void* p2)
- {
- return (_stricmp(((FILE_ENTRY*)(p1))->szFileName, ((FILE_ENTRY*)(p2))->szFileName));
- }
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- int nRetCode = 0;
- // initialize MFC and print and error on failure
- if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
- {
- // TODO: change error code to suit your needs
- cerr << _T("Fatal Error: MFC initialization failed") << endl;
- nRetCode = 1;
- }
- else
- {
- // TODO: code your application's behavior here.
- char buf[1024];
- CString strHello;
- strHello.LoadString(IDS_HELLO);
- cout << (LPCTSTR)strHello << endl;
- if (argc < 3)
- Usage();
- else
- {
- cout << "Counting matched files......";
- pfh.dwEntryCount = 0;
- CFileFind ff;
- BOOL bFound = ff.FindFile(argv[1]);
- while (bFound)
- {
- bFound = ff.FindNextFile();
- if (!ff.IsDirectory())
- pfh.dwEntryCount++;
- }
- ff.Close();
- cout << "OK!" << endl;
- if (pfh.dwEntryCount == 0)
- {
- cout << "matching file " << argv[1] << " not found, program terminated!" << endl;
- return 0;
- }
- cout << "loading file infomation......";
- pFe = NULL;
- pFe = new FILE_ENTRY[pfh.dwEntryCount+1];
- if (pFe == NULL)
- {
- cout << endl << "no enough memory to load all file infomation, terminated!" << endl;
- return -1;
- }
- DWORD dwCnt = 0;
- bFound = ff.FindFile(argv[1]);
- while (bFound)
- {
- bFound = ff.FindNextFile();
- if (!ff.IsDirectory())
- {
- GetShortPathName(ff.GetFileName(), pFe[dwCnt].szFileName, 13);
- pFe[dwCnt].lOffset = ff.GetLength();
- ff.GetCreationTime(&pFe[dwCnt].ftCreationTime);
- ff.GetLastWriteTime(&pFe[dwCnt].ftWriteTime);
- dwCnt++;
- }
- }
- ASSERT(dwCnt = pfh.dwEntryCount);
- ff.Close();
- memcpy(&pfh.dwFlag, "DGFF", 4);
- pfh.dwHeadSize = sizeof(PACK_FILE_HEAD);
- pfh.wMajorVersion = 0x1;
- pfh.wMinorVersion = 0x0;
- strcpy(pfh.szDescription, "This file is created by DirectX Guide");
- strcpy(pFe[pfh.dwEntryCount].szFileName, "[The End]");
- cout << "OK!" << endl;
- cout << "Sorting file entries......";
- qsort(pFe, pfh.dwEntryCount, sizeof(FILE_ENTRY), Compare);
- cout << "OK!" << endl;
- cout << "Creating PackFile......";
- if (!fPf.Open(argv[2], CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
- {
- cout << "can't create file " << argv[2] << ", terminated!" << endl;
- delete [] pFe;
- return -1;
- }
- CFile fin;
- LONG lOffset = sizeof(PACK_FILE_HEAD) + (pfh.dwEntryCount+1)*sizeof(FILE_ENTRY);
- for (dwCnt = 0; dwCnt < pfh.dwEntryCount; dwCnt++)
- {
- if (!fin.Open(pFe[dwCnt].szFileName, CFile::modeRead|CFile::typeBinary))
- {
- cout << "can't open file " << pFe[dwCnt].szFileName << " for reading, terminated!" << endl;
- fPf.Close();
- delete [] pFe;
- return -1;
- }
- pFe[dwCnt].lOffset = lOffset;
- fPf.Seek(lOffset, CFile::begin);
- int nRead;
- do
- {
- nRead = fin.Read(buf, 1024);
- fPf.Write(buf, nRead);
- }while (nRead == 1024);
- lOffset += fin.GetLength();
- fin.Close();
- }
- pFe[pfh.dwEntryCount].lOffset = lOffset;
- pfh.dwFileSize = lOffset;
- fPf.Seek(0, CFile::begin);
- fPf.Write(&pfh, sizeof(pfh));
- fPf.Write(pFe, (pfh.dwEntryCount+1)*sizeof(FILE_ENTRY));
- fPf.Close();
- cout << "OK!" << endl;
- }
- }
- return nRetCode;
- }
- void Usage()
- {
- CString strHelp;
- strHelp.LoadString(IDS_HELP);
- cout << (LPCTSTR)strHelp << endl;
- }