home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cmprss.zip / sample.zip / Smash.c next >
C/C++ Source or Header  |  1994-06-15  |  6KB  |  180 lines

  1. /*****************************************************************************
  2.  * Smash.c -- A sample program to demonstrate the usage of the
  3.  *            CompressObject API.
  4.  *
  5.  *            Usage: SMASH sourcefile destfile
  6.  *
  7.  *            Reads the contents of sourcefile into memory, compresses it,
  8.  *            and writes the compressed data to destfile.
  9.  *****************************************************************************/
  10.  
  11. #define INCL_DOSFILEMGR
  12. #define INCL_DOSMEMMGR
  13. #include <os2.h>
  14. #include <stdio.h>
  15. #include <Compress.h>
  16.  
  17. int main( int argc,char *argv[] )
  18. {
  19.     APIRET              rc          = 0;
  20.     HFILE               hsource     = NULLHANDLE,
  21.                         hdest       = NULLHANDLE;
  22.     ULONG               filesize    = 0;
  23.     PBYTE               pData       = NULL;
  24.     PCOMPRESSED_DATA    pCompressed = NULL;
  25.  
  26.     if ( argc != 3 )
  27.         {
  28.         printf( "Usage: SMASH sourcefile destfile\n" );
  29.         return 1;
  30.         }
  31.  
  32.     /* Open the source file */
  33.     {
  34.         ULONG ulAction = 0;
  35.  
  36.         rc = DosOpen( argv[1],
  37.                       &hsource,
  38.                       &ulAction,
  39.                       0,
  40.                       FILE_NORMAL,
  41.                       OPEN_ACTION_FAIL_IF_NEW|OPEN_ACTION_OPEN_IF_EXISTS,
  42.                       OPEN_FLAGS_SEQUENTIAL|OPEN_SHARE_DENYWRITE|OPEN_ACCESS_READONLY,
  43.                       0 );
  44.         if ( rc )
  45.             {
  46.             printf( "Error %u opening file %s.\n",rc,argv[1] );
  47.             return 1;
  48.             }
  49.     }
  50.  
  51.     /* Determine size of source file */
  52.     {
  53.         FILESTATUS3 fs;
  54.  
  55.         rc = DosQueryFileInfo( hsource,FIL_STANDARD,&fs,sizeof(fs) );
  56.         if ( rc )
  57.             {
  58.             printf( "Error %u returned by DosQueryFileInfo.\n",rc );
  59.             return 1;
  60.             }
  61.         filesize = fs.cbFile;
  62.     }
  63.  
  64.     /* Allocate storage for contents of source file. */
  65.     rc = DosAllocMem( (PPVOID)&pData,filesize,PAG_READ|PAG_WRITE|PAG_COMMIT );
  66.     if ( rc )
  67.         {
  68.         printf( "Error %u allocating memory for source file contents.\n",rc );
  69.         return 1;
  70.         }
  71.  
  72.     /* Read the source file into memory */
  73.     {
  74.         ULONG ulBytesRead = 0;
  75.  
  76.         rc = DosRead( hsource,pData,filesize,&ulBytesRead );
  77.         if ( rc )
  78.             {
  79.             printf( "Error %u reading source file into memory.\n",rc );
  80.             return 1;
  81.             }
  82.         if ( ulBytesRead != filesize )
  83.             {
  84.             printf( "Unable to read entire source file into memory.\n" );
  85.             return 1;
  86.             }
  87.     }
  88.  
  89.     /* Now compress the source file */
  90.     rc = CompressObject( pData,filesize,&pCompressed,METHOD_DEFLATE,5 );
  91.     switch ( rc ) {
  92.         case 0:
  93.             /* Print compression statistics */
  94.             printf( "Compress.DLL version    = %u.%.2u\n",
  95.                     (ULONG)HIBYTE(pCompressed->version),
  96.                     (ULONG)LOBYTE(pCompressed->version) );
  97.             printf( "Compression method      = " );
  98.             switch ( pCompressed->method ) {
  99.                 case METHOD_STORE:      printf( "STORED" ); break;
  100.                 case METHOD_DEFLATE:    printf( "DEFLATED" ); break;
  101.                 default:                printf( "unknown" ); break;
  102.                 }
  103.             printf( "\n" );
  104.             printf( "Size of input data      = %u bytes\n",pCompressed->ulOriginalSize );
  105.             printf( "Size of compressed data = %u bytes\n",pCompressed->ulCompressedSize );
  106.             if ( filesize == 0 ) filesize = 1; /* prevent divide by zero error */
  107.             printf( "Compression ratio       = %.2f%%\n",
  108.                     100.0 - (100.0*(double)pCompressed->ulCompressedSize/(double)filesize) );
  109.             break;
  110.  
  111.         case COMPRESS_INVALID_METHOD:
  112.             printf( "Invalid compression method specified.\n" );
  113.             return 1;
  114.  
  115.         case COMPRESS_INVALID_LEVEL:
  116.             printf( "Invalid compression level specified.\n" );
  117.             return 1;
  118.  
  119.         case COMPRESS_INVALID_PARAMETER:
  120.             printf( "An invalid parameter was passed to CompressObject.\n" );
  121.             return 1;
  122.  
  123.         case COMPRESS_INTERNAL_ERROR:
  124.             printf( "An internal error occurred in CompressObject.\n" );
  125.             return 1;
  126.  
  127.         default:
  128.             printf( "Error %u occurred in CompressObject.\n",rc );
  129.             return 1;
  130.         }
  131.  
  132.     /* Create the destination file */
  133.     {
  134.         ULONG ulAction = 0;
  135.  
  136.         rc = DosOpen( argv[2],
  137.                       &hdest,
  138.                       &ulAction,
  139.                       0,
  140.                       FILE_NORMAL,
  141.                       OPEN_ACTION_CREATE_IF_NEW|OPEN_ACTION_REPLACE_IF_EXISTS,
  142.                       OPEN_FLAGS_SEQUENTIAL|OPEN_SHARE_DENYREADWRITE|OPEN_ACCESS_READWRITE,
  143.                       0 );
  144.         if ( rc )
  145.             {
  146.             printf( "Error %u opening file %s.\n",rc,argv[1] );
  147.             return 1;
  148.             }
  149.     }
  150.  
  151.     /* Write the compressed data to the destination file */
  152.     {
  153.         ULONG ulBytesWritten = 0;
  154.  
  155.         rc = DosWrite( hdest,
  156.                        pCompressed,
  157.                        COMPRESSED_HEADER_SIZE + pCompressed->ulCompressedSize,
  158.                        &ulBytesWritten );
  159.         if ( rc )
  160.             {
  161.             printf( "Error %u writing to destination file.\n",rc );
  162.             return 1;
  163.             }
  164.         if ( ulBytesWritten != COMPRESSED_HEADER_SIZE+pCompressed->ulCompressedSize )
  165.             {
  166.             printf( "Unable to write entire compressed data block to destination file.\n" );
  167.             return 1;
  168.             }
  169.     }
  170.  
  171.     /* Clean up used resources and exit */
  172.     DosClose( hsource );
  173.     DosClose( hdest );
  174.     DosFreeMem( pData );
  175.     DosFreeMem( pCompressed );
  176.  
  177.     return 0;
  178. }
  179.  
  180.