/* ===================== endReplaySystem ===================== */ BOOL endReplaySystem() { repHeader_t repHeader = {0}; HANDLE hFile; HANDLE hTmpFile; BYTE buff[BLOCK_SIZE]; TCHAR szRepTmpPath[MAX_PATH]; TCHAR szRepPath[MAX_PATH]; TCHAR szTmpPath[MAX_PATH]; TCHAR szArchPath[MAX_PATH]; DWORD crc; DWORD tmp, tmp2; int i; //this will make replay hold till //the end. // addReplayFrame( NOMOVE, LEFTGAME ); N_FClose( ghRepFile ); //create paths // N_Sprintf( szTmpPath, MAX_PATH, TEXT( "%s\\demos\\~demo.rep" ), kcfTable[VAR_BASE].v.s ); N_Sprintf( szRepPath, MAX_PATH, TEXT( "%s\\demos\\demo_%i.rep" ), kcfTable[VAR_BASE].v.s, gSuffix ); N_Sprintf( szRepTmpPath, MAX_PATH, TEXT( "%s.tmp" ), szRepPath ); N_Sprintf( szArchPath, MAX_PATH, TEXT( "%s.z" ), szRepTmpPath); //create temporary replay file // hFile = N_FOpenC( szRepTmpPath ); if( hFile == INVALID_HANDLE_VALUE ) { return FALSE; } //write figure data // N_FWrite( hFile, &k_replayFigSize, sizeof(k_replayFigSize) ); for( i=0; i<k_replayFigSize; i++ ) { N_FWrite( hFile, &(k_replayFig[i].type), sizeof(k_replayFig[i].type) ); N_FWrite( hFile, &(k_replayFig[i].state), sizeof(k_replayFig[i].state) ); } //copy replay data from temporary file // hTmpFile = N_FOpenR( szTmpPath ); while( ReadFile( hTmpFile, buff, BLOCK_SIZE, &tmp, NULL ) ) { if( tmp == 0 ) { break; } WriteFile( hFile, buff, tmp, &tmp2, NULL ); } N_FClose( hTmpFile ); N_FClose( hFile ); //create replay file // hFile = N_FOpenC( szRepPath ); //write header // repHeader.header = REPLAYHEADER; repHeader.gametype = gGameType; if( gMapID >= 0 ) { repHeader.mapCRC = k_system.pMaps[gMapID].fe.dwCRC; } else { repHeader.mapCRC = 0; } N_FWrite( hFile, &repHeader, sizeof(repHeader) ); crcInit( &crc ); //compress // if( beginCompress( szRepTmpPath ) ) { hTmpFile = N_FOpenR( szArchPath ); while( ReadFile( hTmpFile, buff, BLOCK_SIZE, &tmp, NULL ) ) { if( tmp == 0 ) { break; } WriteFile( hFile, buff, tmp, &tmp2, NULL ); crcUpdate( &crc, buff, tmp ); } N_FClose( hTmpFile ); endCompress( szRepTmpPath ); } crcFinish( &crc ); //write CRC // repHeader.crc = crc; N_FSeek( hFile, 0, FILE_BEGIN ); N_FWrite( hFile, &repHeader, sizeof(repHeader) ); //clean-up // N_FClose( hFile ); //delete ~demo.rep and demo_%i.rep.tmp // DeleteFile( szTmpPath ); DeleteFile( szRepTmpPath ); N_Free( k_replayFig ); k_replayFig = NULL; k_replayFigSize = 0; k_replayFigID = 0; gFrameTime = 0; gGameType = GNO; gMapID = -1; k_system.flags &= ~SF_RECORDING; return TRUE; }
int main(int argc, char **argv) { PHASH freq = NewHash(); int freqfid, codefid; int nrSource, delta; double clk; int count = 0; INDATA srcFile, compFile; /* Must be passed the name of a file to compress */ if(argc < 2) { printf("Must give a file to compress\n"); exit(1); } TIMESTART(clk); initLFIOFile(&srcFile, argv[1]); /* Read the file, 2 bytes at a time and create the frequency table */ nrSource = 0; while(canFetch(&srcFile, 2)) { U16 wrd = fetchWordLE(&srcFile); nrSource += 2; bumpFrequency(freq, wrd); } finishIOFile(&srcFile); printf("Read %d bytes\n", nrSource); /* Open the code and frequency files */ freqfid = CREAT("huffman.freq"); codefid = CREAT("huffman.code"); compressfid = CREAT("huffman.compressed"); if(freqfid < 0 || codefid < 0 || compressfid < 0) { ERROR0(1, "Cannot create frequency and code files\n"); } createCompressTables(freq, freqfid, codefid); close(freqfid); close(codefid); FreeHash(freq); /* Now read again and compress */ initCompressor("huffman.code"); initLFIOFile(&srcFile, argv[1]); outPoint = 0; written = 0; startCompress(&writeByte); /* Read the file, 2 bytes at a time and compress */ while(canFetch(&srcFile, 2)) { U16 wrd = fetchWordLE(&srcFile); writeCompressedSymbol(wrd); } endCompress(); flushOut(); close(compressfid); finishIOFile(&srcFile); /* Now decompress and compare */ for(count=0;count<30;count++) { initLFIOFile(&compFile, "huffman.compressed"); initLFIOFile(&srcFile, argv[1]); startDecompress(); delta = nrSource; while(delta > 0) { int comp = fetchCompressedSymbol(&compFile); int src = fetchWordLE(&srcFile); if(src != comp) { ERROR3(-1, "Src(%04x) != Comp(%04x) (at offset %d)\n", src, comp, nrSource - delta); } delta -= 2; } endDecompress(&compFile); finishIOFile(&srcFile); finishIOFile(&compFile); } finalizeCompressor(); TIMESTOP(clk); // sm: according to my man pages, the 'l' flag doesn't apply // to the 'f' format, which is always a double argument printf("Source %d bytes. Compressed %d bytes. Ratio: %5.2f\n", nrSource, written, (double)nrSource / (double)written); printf("Run hufftest in %8.3fms\n", clk / 1000.0); exit (0); return 0; }