?? main.c
字號:
/* Name : main.c * * Notes: This is the main driver code for the xpkCRSH command-line utility. * It supports all features of xpkCRSH, even those that are not * accessable using xpkmaster.library. */#include <stdio.h>#include <stdlib.h>#include <assert.h>#include "stdtypes.h"#include "lz77.h"#include "getopt.h"#ifdef UNIX#include <sys/types.h>#include <sys/stat.h>#endifstatic void ShowUsage( STRPTR );ULONG FileLength( FILE * file );#define SEEK_END 2int action = 1;struct option opts[] ={ { "compress", 0, &action, 1 }, { "decompress", 0, &action, 0 }, { "mode", 1, NULL, 'm' }, { NULL, }};/******************************************************************************//* Name : main() * * Notes: * */int main( int argc, char ** argv ){ FILE * input; FILE * output; struct LZ77Info info; int opt; int ch; info.Mode = 0; info.Number = 0; info.buffer = NULL; memset( &info, 0, sizeof( info ) ); /* Parse the command line and determine what the user wants us to do and * how they want us to do it. */ optind = 0; while ( (ch = getopt_long_only( argc, argv, opts, &opt )) != EOF ) { switch( ch ) { case 'm' : { info.Mode = atoi( optarg ); printf( "info.Mode = %ld\n", info.Mode ); break; } case '?' : { ShowUsage( argv[0] ); exit( 5 ); } default: break; } } /* Once we've parsed the initial arguments, we need to get the names * of the files that the user wants us to work on. */ if ( (argc - optind) < 2 ) { ShowUsage( argv[0] ); exit( 5 ); } input = fopen( argv[ optind ], "rb" ); if ( input == NULL ) { perror( argv[ optind ] ); exit( 5 ); } output = fopen( argv[ optind + 1 ], "wb" ); if ( output == NULL ) { fclose( input ); perror( argv[ optind + 1 ] ); exit( 5 ); } info.length = FileLength( input ); info.input = malloc( info.length ); printf( "info.length = %ld\n", info.length ); /* Now we need to decide what the user wants us to do. There's only * two choices, so it's not too rough. To compress, or to decompress, * that is the question. */ if ( action == 0 ) { ULONG temp; fread( &temp, 4L, 1L, input ); printf( __FILE__ ",%ld: temp = %ld\n", (long)__LINE__, temp ); fflush( stdout ); info.outBufSize = temp + 32; info.output = malloc( temp + 32 ); info.length -= 4; fread( info.input, 1L, info.length, input ); fclose( input ); info.length = DecompressLZ77( &info ); if ( temp != info.length ) { printf( __FILE__ ",%ld: THPT! %ld != %ld\n", (long)__LINE__, info.length, temp ); } } else { fread( info.input, 1L, info.length, input ); fclose( input ); info.outBufSize = ((info.length * 17) / 16) + 1; info.output = malloc( info.outBufSize ); fwrite( &info.length, 4L, 1L, output ); info.length = CompressLZ77( &info ); printf( __FILE__ ",%ld: Done compressing the data.\n", (long)__LINE__ ); } fwrite( info.output, 1L, info.length, output ); fclose( output ); PackFreeLZ77( &info ); free( info.input ); free( info.output ); return( 0 );}/******************************************************************************//* Name : FileLength() * * Notes: This function is used to determine the length of the file * associated file handle in bytes. This is a very generic ANSI * implementation of this function. It is advised that versions * of this routine for other operating systems (i.e., Unix, AmigaDOS) * use more specified OS functions (i.e., fstat(), Examine()). */ULONG FileLength( FILE * file ){#ifndef UNIX ULONG length; length = 0L; if ( fseek( file, 0L, SEEK_END ) == 0 ) { length = ftell( file ); rewind( file ); } return( length );#else struct stat buf; fstat( file->_file, &buf ); return( buf.st_size );#endif}/******************************************************************************//* Name : ShowUsage() * * Notes: This tiny routine simply shows the user how to run our neat little * compression program. */static void ShowUsage( STRPTR name ){ printf( "Usage: %s [options] infile outfile\n", name ); printf( "Options:\n" " --compress Compress the input file\n" " --decompress Decompress the input file\n" " --mode n Use compression mode ``n''\n" );}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -