home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of A1200
/
World_Of_A1200.iso
/
programs
/
compress
/
packers
/
crunchmania1.9t
/
developer
/
c_example
/
crmscrunch.c
< prev
Wrap
C/C++ Source or Header
|
1995-02-27
|
3KB
|
86 lines
#include <libraries/crm.h>
#include <libraries/dos.h>
#include <dos/dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <clib/crm_protos.h>
#define ERR_template 20
#define ERR_nolib 19
#define ERR_struct 18
#define ERR_lock 17
#define ERR_NofibMem 16
#define ERR_Nofmem 15
#define ERR_read 14
#define ERR_crunchfail 13
#define ERR_write 12
#define ERR_file 11
struct cmCrunchStruct *crunchstruct;
struct Library *CrMBase;
BPTR handle;
BPTR lock;
struct FileInfoBlock *fib;
ULONG flen;
APTR fmem;
ULONG len, newlen;
struct DataHeader dataheader;
void end(int err);
main(int argc, char** argv)
{
if (argc!=3) end(ERR_template);
if (!(CrMBase = OpenLibrary("CrM.library",0))) end(ERR_nolib);
if (!(crunchstruct = cmProcessCrunchStruct(NULL, cm_AllocStruct,
CMCS_Algo,cm_LZH | cmF_Sample | cmF_Overlay,TAG_DONE)))
end(ERR_struct);
if (!(lock = Lock(argv[1],ACCESS_READ))) end(ERR_lock);
if (!(fib = malloc(sizeof(struct FileInfoBlock)))) end(ERR_NofibMem);
Examine(lock,fib);
flen = fib->fib_Size;
if (!(fmem = malloc(flen))) end(ERR_Nofmem);
UnLock(lock);
lock = NULL;
handle = Open(argv[1],MODE_OLDFILE);
len = Read(handle, fmem, flen);
if (len != flen) end(ERR_read);
Close(handle);
handle = NULL;
crunchstruct->cmcr_Src = crunchstruct->cmcr_Dest = fmem;
crunchstruct->cmcr_SrcLen = crunchstruct->cmcr_DestLen = flen;
crunchstruct->cmcr_DataHdr = &dataheader;
if (!(newlen = cmCrunchData(crunchstruct))) end(ERR_crunchfail);
if (!(handle = Open(argv[2], MODE_NEWFILE))) end(ERR_file);
len = Write(handle, &dataheader, sizeof(struct DataHeader));
if (len != sizeof(struct DataHeader)) end(ERR_write);
len = Write(handle, fmem, newlen);
if (len != newlen) end(ERR_write);
printf("oldfile: %s length: %ld\n",argv[1],flen);
printf("newfile: %s length: %ld\n",argv[2],newlen);
end(0);
}
void end(int err)
{
if (err == ERR_write) printf("Error while writing!\n");
if (err == ERR_file) printf("Dest File open failed!\n");
if (err == ERR_crunchfail) printf("crunching failed!\n");
if (err == ERR_read) printf("Error while reading!\n");
if (err == ERR_Nofmem) printf("No Mem!\n");
if (err == ERR_template) printf("Usage: crmscrunch <infile> <outfile>\n");
if (err == ERR_nolib) printf("couldn't open Library!\n");
if (err == ERR_struct) printf("couldn't allocate struct!\n");
if (err == ERR_lock) printf("file not found!\n");
if (err == ERR_NofibMem) printf("No Mem!\n");
if (handle) Close(handle);
if (fmem) free(fmem);
if (fib) free(fib);
if (lock) UnLock(lock);
if (crunchstruct) cmProcessCrunchStruct(crunchstruct,cm_FreeStruct,TAG_DONE);
if (CrMBase) CloseLibrary(CrMBase);
exit(err);
}