home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / DCLIB1.ZIP / EXAMPLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-24  |  4.2 KB  |  118 lines

  1. /*************************************************************************/
  2. /*      Example File for testing the Data Compression Library            */
  3. /*  Compiled with WATCOM C                                               */
  4. /*                                                                       */
  5. /*  READ THE 'README.TXT' FILE BEFORE USE !                              */
  6. /*                                                                       */
  7. /*  The author makes no warranty of any kind, expressed or implied,      */
  8. /*  with regard to this program and the Data Compression Library.        */
  9. /*  The author shall not be liable for any loss or damages resulting     */
  10. /*  from the use of any of the programs supplied.                        */
  11. /*                                                                       */
  12. /*************************************************************************/
  13.  
  14. /*************************************************************************/
  15. /*  This program Loads in a file to RAM.                                 */
  16. /*  Compresses it, decompresses it, and saves result                     */
  17. /*************************************************************************/
  18.  
  19. #include <stdio.h>                                              
  20. #include <stdlib.h>
  21. #include <fcntl.h>
  22. #include <io.h>
  23.  
  24. #include "dcl_386.h"                     // The Data Compression Library Header
  25.  
  26. #define BYTE unsigned char              // Makes Life a little easier
  27.  
  28.  
  29. void main(int argc,char *argv[])
  30. {
  31.   BYTE *source_data;                    // pointer to raw data
  32.   BYTE *compressed_data;                // pointer to compressed data
  33.   
  34.   unsigned int flength;                 
  35.   int hndl;
  36.  
  37.   
  38.   printf("\n\nData Compression Library Test\n");
  39.  
  40.   if(argc!=3) {
  41.     printf("COPYRIGHT Dani Arrusi 1995\n");
  42.     printf("SYNTAX : example <input file> <output file>\n\n");
  43.     exit(0);
  44.   }
  45.  
  46.   /////////////////////////////////// 
  47.   /* The first thing we need to do is allocate some space for the compression
  48.      buffers. The first parameter is the uncompressed(source) buffer size
  49.      in bytes, the second parameter is the compressed (destination) buffer
  50.      size in bytes. See Documentation For details.                         */
  51.  
  52.   if(DCL_init_compress(20000,20000)==NULL) {
  53.      printf("Cannot Allocate Memory for Compression Buffers\n");
  54.      exit(1);
  55.   }
  56.   ///////////////////////////////////
  57.  
  58.   ///////////////////////////////////
  59.   // Load in a file to memory 
  60.   if((hndl=open(argv[1],O_RDONLY | O_BINARY))==-1) {
  61.     printf("File '%s' not found\n",argv[1]);
  62.     exit(0);
  63.   }
  64.   if(filelength(hndl)>20000) {
  65.     printf("\nFile too big to fit into buffers\n");
  66.     printf("Edit 'example.c' and modify 'DCL_init_compress'\n");
  67.     printf("See documentation in 'readme.txt' for further details\n\n");
  68.     exit(1);
  69.   }
  70.   source_data=(BYTE *)malloc(filelength(hndl));
  71.   if(source_data==NULL) {
  72.     printf("Cannot allocate ram for input file\n");
  73.     exit(1);
  74.   }  
  75.   read(hndl,source_data,filelength(hndl));
  76.   flength=filelength(hndl);
  77.   close(hndl);
  78.   printf("Original File = %d Bytes\n\n",flength);
  79.   ///////////////////////////////////
  80.  
  81.   ///////////////////////////////////
  82.   // Compress and Decompress
  83.  
  84.   printf("Compressing file \n");
  85.   
  86.   // compress the ram data
  87.   compressed_data=DCL_shrink(source_data,&flength);     
  88.   
  89.   // we can now loose the original uncompressed file data
  90.   free(source_data);
  91.   
  92.   // Show how big the compressed data is
  93.   printf("Compressed file = %d Bytes\n\n",flength);
  94.  
  95.   // Uncompress the data
  96.   printf("Expanding Compressed File\n");
  97.   
  98.   source_data=DCL_expand(compressed_data,&flength);
  99.     
  100.   printf("Expanded File = %d Bytes\n",flength);
  101.   /////////////////////////////////////
  102.  
  103.   /////////////////////////////////////
  104.   // Save out new uncompressed data
  105.   
  106.   printf("Saving new data as '%s'\n",argv[2]);
  107.   hndl=open(argv[2], O_WRONLY | O_CREAT | O_BINARY | O_TRUNC , S_IREAD | S_IWRITE);
  108.   write(hndl,source_data,flength);
  109.   close(hndl);
  110.   /////////////////////////////////////
  111.  
  112.   /////////////////////////////////////
  113.   // De initialise Compression buffers
  114.   DCL_close_compress();       // deallocates 'static' input and output buffers
  115.   //////////////////////////////////////
  116.  
  117. }
  118.