home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / packery / crmv1.9t / developer / c_example / crmscrunch.c < prev   
C/C++ Source or Header  |  1980-01-10  |  3KB  |  86 lines

  1. #include <libraries/crm.h>
  2. #include <libraries/dos.h>
  3. #include <dos/dos.h>
  4. #include    <stdio.h>
  5. #include <stdlib.h>
  6. #include <clib/crm_protos.h>
  7.  
  8. #define ERR_template 20
  9. #define ERR_nolib    19
  10. #define ERR_struct   18
  11. #define ERR_lock        17
  12. #define ERR_NofibMem    16
  13. #define ERR_Nofmem    15
  14. #define ERR_read        14
  15. #define ERR_crunchfail 13
  16. #define ERR_write        12
  17. #define ERR_file        11
  18.  
  19. struct cmCrunchStruct *crunchstruct;
  20. struct Library *CrMBase;
  21. BPTR    handle;
  22. BPTR    lock;
  23. struct FileInfoBlock *fib;
  24. ULONG    flen;
  25. APTR    fmem;
  26. ULONG len, newlen;
  27. struct DataHeader dataheader;
  28.  
  29.  
  30. void end(int err);
  31.  
  32. main(int argc, char** argv)
  33. {
  34.     if (argc!=3) end(ERR_template);
  35.     if (!(CrMBase = OpenLibrary("CrM.library",0))) end(ERR_nolib);
  36.     if (!(crunchstruct = cmProcessCrunchStruct(NULL, cm_AllocStruct,
  37.                     CMCS_Algo,cm_LZH | cmF_Sample | cmF_Overlay,TAG_DONE)))
  38.            end(ERR_struct);
  39.     if (!(lock = Lock(argv[1],ACCESS_READ))) end(ERR_lock);
  40.     if (!(fib = malloc(sizeof(struct FileInfoBlock)))) end(ERR_NofibMem);
  41.     Examine(lock,fib);
  42.     flen = fib->fib_Size;
  43.     if (!(fmem = malloc(flen))) end(ERR_Nofmem);
  44.     UnLock(lock);
  45.     lock = NULL;
  46.     handle = Open(argv[1],MODE_OLDFILE);
  47.     len = Read(handle, fmem, flen);
  48.     if (len != flen) end(ERR_read);
  49.     Close(handle);
  50.     handle = NULL;
  51.     crunchstruct->cmcr_Src    = crunchstruct->cmcr_Dest    = fmem;
  52.     crunchstruct->cmcr_SrcLen = crunchstruct->cmcr_DestLen = flen;
  53.     crunchstruct->cmcr_DataHdr = &dataheader;
  54.     if (!(newlen = cmCrunchData(crunchstruct))) end(ERR_crunchfail);
  55.     if (!(handle = Open(argv[2], MODE_NEWFILE))) end(ERR_file);
  56.     len = Write(handle, &dataheader, sizeof(struct DataHeader));
  57.     if (len != sizeof(struct DataHeader)) end(ERR_write);
  58.     len = Write(handle, fmem, newlen);
  59.     if (len != newlen) end(ERR_write);
  60.     printf("oldfile: %s  length: %ld\n",argv[1],flen);
  61.     printf("newfile: %s  length: %ld\n",argv[2],newlen);
  62.     end(0);
  63. }
  64.  
  65. void end(int err)
  66. {
  67.     if (err == ERR_write)        printf("Error while writing!\n");
  68.     if (err == ERR_file)            printf("Dest File open failed!\n");
  69.     if (err == ERR_crunchfail)    printf("crunching failed!\n");
  70.     if (err == ERR_read)            printf("Error while reading!\n");
  71.     if (err == ERR_Nofmem)        printf("No Mem!\n");
  72.     if (err == ERR_template)    printf("Usage: crmscrunch <infile> <outfile>\n");
  73.     if (err == ERR_nolib)        printf("couldn't open Library!\n");
  74.     if (err == ERR_struct)        printf("couldn't allocate struct!\n");
  75.     if (err == ERR_lock)            printf("file not found!\n");
  76.     if (err == ERR_NofibMem)    printf("No Mem!\n");
  77.  
  78.     if (handle) Close(handle);
  79.     if (fmem) free(fmem);
  80.     if (fib) free(fib);
  81.     if (lock) UnLock(lock);
  82.     if (crunchstruct) cmProcessCrunchStruct(crunchstruct,cm_FreeStruct,TAG_DONE);
  83.     if (CrMBase) CloseLibrary(CrMBase);
  84.     exit(err);
  85. }
  86.