home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / packery / xpk_source / xpkmaster / test / testmempack.c < prev    next >
C/C++ Source or Header  |  1996-10-19  |  3KB  |  139 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.   int errcode;
  81.  
  82.   errbuf[0] = 0;
  83.  
  84.   if (!(XpkBase = OpenLibrary (XPKNAME, 0)))
  85.     end ("Cannot open " XPKNAME "\n");
  86.  
  87.   if (argc != 4)
  88.     end ("Usage: testMemPack infilename method outfilename\n");
  89.  
  90.   if (!(fh = Open (argv[1], MODE_OLDFILE)))
  91.     end ("Cannot open input file");
  92.  
  93.   if (!(ExamineFH (fh, &fib)))
  94.     end ("Failed to ExamineFH input file");
  95.  
  96.   ibuflen = fib.fib_Size;
  97.  
  98.   if (!(ibuf = AllocMem (ibuflen, MEMF_ANY)))
  99.     end ("Out of memory");
  100.  
  101.   ilen = Read (fh, ibuf, ibuflen);
  102.  
  103.   if (ilen != ibuflen)
  104.     end ("Could not read whole file with one Read()");
  105.  
  106.   Close (fh); 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 (errcode = XpkPackTags (XPK_InBuf, ibuf,
  117.                    XPK_InLen, ilen,
  118.                    XPK_PackMethod, argv[2],
  119.                    XPK_GetOutBuf, &obuf,
  120.                    XPK_GetOutBufLen, &obuflen,
  121.                    XPK_GetOutLen, &olen,
  122.                    XPK_GetError, errbuf,
  123.                    XPK_ChunkHook, &chunkhook,
  124.                    TAG_DONE)) {
  125.     fprintf(stderr, "errcode: %d\n", errcode);
  126.     end ("Can't XpkPackTags");
  127.   }
  128.  
  129.   if (!(fh = Open (argv[3], MODE_NEWFILE)))
  130.     end ("Can not create output file");
  131.  
  132.   if (Write (fh, obuf, olen) != olen)
  133.     end ("Can not Write output");
  134.  
  135.   Close (fh); fh =0;
  136.  
  137.   end (NULL);
  138. }
  139.