home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Cruncher / XPK416SR.LHA / xpk_Source / test / testMemUnpack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-06  |  3.1 KB  |  126 lines

  1. #define NAME        "testMemUnpack"
  2. #define DISTRIBUTION    "(Freeware) "
  3. #define REVISION    "2"
  4. #define ENDCODE_NOCTRLC
  5.  
  6. /* Programmheader
  7.  
  8.     Name:        testMemUnpack
  9.     Author:        SDI
  10.     Distribution:    Freeware
  11.     Description:    tests Xpk Pack function
  12.     Compileropts:    -
  13.     Linkeropts:    -l xpkmaster amiga
  14.  
  15.  1.1   06.12.96 : fixed for new includes, added new WriteXpkFib
  16.  1.2   15.04.97 : removed WriteXpkFib.c
  17. */
  18.  
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21. #include <proto/xpkmaster.h>
  22. #include <exec/memory.h>
  23. #include "SDI_defines.h"
  24.  
  25. #ifdef __MAXON__
  26.   #define __asm
  27.   #define __saveds
  28. #endif
  29.  
  30. struct Library        *XpkBase        = 0;
  31. ULONG            DosVersion        = 37;
  32. STRPTR            ibuf            = 0,
  33.             obuf            = 0;
  34. ULONG            ibuflen            = 0,
  35.             obuflen            = 0;
  36. BPTR            fh            = 0;
  37. struct RDArgs        *rda            = 0;
  38. struct FileInfoBlock    *fib            = 0;
  39. struct XpkFib        *xfib            = 0;
  40.  
  41. #define PARAM "FROM/A,TO"
  42.  
  43. LONG __asm __saveds chunkfunc(register __a1 struct XpkProgress *prog)
  44. {
  45.   switch(prog->xp_Type)
  46.   {
  47.   case XPKPROG_START: PutStr("Start: "); break;
  48.   case XPKPROG_MID: PutStr("\rMid  : "); break;
  49.   case XPKPROG_END: PutStr("\rEnd  : "); break;
  50.   }
  51.  
  52.   if(prog->xp_Type != XPKPROG_END)
  53.     Printf("%4s: %-8s (%3ld%% done, %2ld%% CF, %6ld cps) %s",
  54.       prog->xp_PackerName, prog->xp_Activity, prog->xp_Done,
  55.       prog->xp_CF, prog->xp_Speed, prog->xp_FileName);
  56.   else
  57.     Printf("%4s: %-8s (%3ldK, %2ld%% CF, %6ld cps) %s\033[K\n",
  58.       prog->xp_PackerName, prog->xp_Activity, prog->xp_ULen >> 10,
  59.       prog->xp_CF, prog->xp_Speed, prog->xp_FileName);
  60.  
  61.   Flush(Output());
  62.   return (LONG) SetSignal(0, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C;
  63. }
  64.  
  65. struct Hook chunkhook = { {0}, (ULONG (*)()) chunkfunc};
  66.  
  67. void main(void)
  68. {
  69.   UBYTE errbuf[XPKERRMSGSIZE+1];
  70.   ULONG olen;
  71.   struct {
  72.     STRPTR from;
  73.     STRPTR to;
  74.   } args = {0,0};
  75.  
  76.   if(!(rda = ReadArgs(PARAM, (LONG *) &args, 0)) ||
  77.   !(xfib = (struct XpkFib *) AllocMem(sizeof(struct XpkFib), MEMF_ANY|MEMF_CLEAR)) ||
  78.   !(XpkBase = OpenLibrary(XPKNAME, 0)) ||
  79.   !(fh = Open(args.from, MODE_OLDFILE)) ||
  80.   !(fib = (struct FileInfoBlock *) AllocDosObject(DOS_FIB, 0)) ||
  81.   !(ExamineFH(fh, fib)) ||
  82.   !(ibuf = (STRPTR) AllocMem(fib->fib_Size, MEMF_ANY)) ||
  83.   Read(fh, ibuf, fib->fib_Size) != fib->fib_Size)
  84.     End(RETURN_FAIL);
  85.  
  86.   ibuflen = fib->fib_Size;
  87.   Close(fh); fh = 0;
  88.  
  89.   if(XpkExamineTags(xfib, XPK_InBuf, ibuf, XPK_InLen, ibuflen,
  90.     XPK_GetError, errbuf, TAG_DONE))
  91.   {
  92.     STRPTR a = errbuf;
  93.     VPrintf("Can't XpkExamine: %s\n", &a);
  94.     End(RETURN_FAIL);
  95.   }
  96.  
  97.   if(XpkUnpackTags(XPK_InBuf, ibuf, XPK_InLen, ibuflen,
  98.     XPK_GetOutBuf, &obuf, XPK_GetOutBufLen, &obuflen,
  99.     XPK_GetOutLen, &olen, XPK_GetError, errbuf,
  100.     XPK_PassThru, 1, XPK_ChunkHook, &chunkhook, TAG_DONE))
  101.   {
  102.     STRPTR a = errbuf;
  103.     VPrintf("Can't XpkUnpack: %s\n", &a);
  104.     End(RETURN_FAIL);
  105.   }
  106.  
  107.   if(args.to)
  108.     if(!(fh = Open(args.to, MODE_NEWFILE)) ||
  109.     Write(fh, obuf, olen) != olen)
  110.       End(RETURN_FAIL);
  111.  
  112.   End(RETURN_OK);
  113. }
  114.  
  115. void end(void)
  116. {
  117.   if(fh)    Close(fh);
  118.   if(xfib)    FreeMem(xfib, sizeof(struct XpkFib));
  119.   if(XpkBase)    CloseLibrary(XpkBase);
  120.   if(fib)    FreeDosObject(DOS_FIB, fib);
  121.   if(rda)    FreeArgs(rda);
  122.   if(ibuf)    FreeMem(ibuf, ibuflen);
  123.   if(obuf)    FreeMem(obuf, obuflen);
  124. }
  125.  
  126.