home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / packery / xpk_source / xpkmaster / test / testmemunpack.c < prev   
C/C++ Source or Header  |  1996-10-19  |  3KB  |  129 lines

  1. #include <proto/exec.h>
  2. #include <exec/memory.h>
  3. #include <proto/dos.h>
  4. #include <proto/xpk.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. struct Library *XpkBase = NULL;
  10.  
  11. char errbuf[XPKERRMSGSIZE + 1], *ibuf = NULL, *obuf = NULL;
  12. long ibuflen, obuflen;
  13. BPTR fh = 0;
  14.  
  15. void 
  16. print (char *s)
  17. {
  18.   Write (Output (), s, strlen (s));
  19. }
  20.  
  21. void 
  22. end (char *text)
  23. {
  24.   if (fh)
  25.     Close (fh);
  26.   if (ibuf)
  27.     FreeMem (ibuf, ibuflen);
  28.   if (obuf)
  29.     FreeMem (obuf, obuflen);
  30.   if (text) {
  31.     print (text); print (errbuf); print ("\n");
  32.   }
  33.   if (XpkBase)
  34.     CloseLibrary (XpkBase);
  35.   exit (text ? 10 : 0);
  36. }
  37.  
  38. static char * xpkprogname[4] = { "unknown", "Start:", "Mid  :", "End  :"} ;
  39.  
  40. long __asm
  41. chunkfunc (register __a1 struct XpkProgress *prog)
  42. {
  43.   char buf[120];
  44.  
  45.   print (xpkprogname[prog->Type]);
  46.  
  47.   switch (prog->Type) {
  48.   case XPKPROG_START:
  49.     print ("Start:");
  50.     break;
  51.   case XPKPROG_MID:
  52.     print ("Mid  :");
  53.     break;
  54.   case XPKPROG_END:
  55.     print ("End  :");
  56.     break;
  57.   }
  58.   if (prog->Type != XPKPROG_END)
  59.     sprintf (buf, "\r%4s: %-8s (%3ld%% done, %2ld%% CF, %6ld cps) %s\n",
  60.          prog->PackerName, prog->Activity, prog->Done,
  61.          prog->CF, prog->Speed, prog->FileName);
  62.   else
  63.     sprintf (buf,
  64.          "\r%4s: %-8s (%3ldK, %2ld%% CF, %6ld cps) %s\033[K\n",
  65.          prog->PackerName, prog->Activity, prog->ULen / 1024,
  66.          prog->CF, prog->Speed, prog->FileName);
  67.  
  68.   print (buf);
  69.  
  70.   return (long) SetSignal (0, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C;
  71. }
  72. struct Hook chunkhook = { {0}, chunkfunc };
  73.  
  74. void 
  75. main (int argc, char *argv[])
  76. {
  77.   struct XpkFib xfib;
  78.   struct FileInfoBlock fib;
  79.   ULONG ilen, olen;
  80.  
  81.   errbuf[0] = 0;
  82.  
  83.   if (!(XpkBase = OpenLibrary (XPKNAME, 0)))
  84.     end ("Cannot open " XPKNAME "\n");
  85.  
  86.   if (argc != 2)
  87.     end ("Usage: testMemUnpack filename\n");
  88.  
  89.   if (!(fh = Open (argv[1], MODE_OLDFILE)))
  90.     end ("Cannot open input file");
  91.  
  92.   if (!(ExamineFH (fh, &fib)))
  93.     end ("Failed to ExamineFH input file");
  94.  
  95.   ibuflen = fib.fib_Size;
  96.  
  97.   if (!(ibuf = AllocMem (ibuflen, MEMF_ANY)))
  98.     end ("Out of memory");
  99.  
  100.   ilen = Read (fh, ibuf, ibuflen);
  101.  
  102.   if (ilen != ibuflen)
  103.     end ("Could not read whole file with one Read()");
  104.  
  105.   Close (fh);
  106.   fh = 0;
  107.  
  108.   if (XpkExamineTags( &xfib,
  109.               XPK_InBuf, ibuf,
  110.               XPK_InLen, ibuflen,
  111.                       XPK_GetError, errbuf,
  112.               TAG_DONE
  113.             ))
  114.     end ("Can't XpkExamine");
  115.     
  116.   if (XpkUnpackTags (XPK_InBuf, ibuf,
  117.                      XPK_InLen, ilen,
  118.                      XPK_GetOutBuf, &obuf,
  119.                      XPK_GetOutBufLen, &obuflen,
  120.                      XPK_GetOutLen, &olen,
  121.                      XPK_GetError, errbuf,
  122.                      XPK_PassThru, 1,
  123.                      XPK_ChunkHook, &chunkhook,
  124.                      TAG_DONE)) 
  125.     end ("Can't XpkUnpackTags");
  126.  
  127.   end (NULL);
  128. }
  129.