home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / packery / xpk_source / shell / xbench.c < prev    next >
C/C++ Source or Header  |  1996-10-19  |  4KB  |  198 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <exec/exec.h>
  4. #include <libraries/dos.h>
  5. #include <devices/timer.h>
  6. #include <proto/timer.h>
  7. #include <proto/exec.h>
  8. #include <proto/dos.h>
  9. #include <proto/xpk.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. #define THREE_BUFS    0
  15. #define NOCRC        0
  16.  
  17. struct timerequest timerequest;
  18. struct Library *TimerBase = NULL;
  19. struct Library *XpkBase = NULL;
  20. BPTR fh = 0;
  21.  
  22. char errbuf[XPKERRMSGSIZE + 1];    /* +1 to make room for '\n'        */
  23. char *Password = NULL;
  24. char *buf1 = 0, *buf2 = 0, *buf3 = 0;
  25. int bufsize;
  26.  
  27. void
  28. end (char *str)
  29. {
  30.   if (fh)
  31.     Close (fh);
  32.   if (buf3)
  33.     FreeMem (buf3, bufsize);
  34.   if (buf2)
  35.     FreeMem (buf2, bufsize);
  36.   if (buf1)
  37.     FreeMem (buf1, bufsize);
  38.   if (TimerBase)
  39.     CloseDevice ((struct IORequest *) & timerequest);
  40.   if (XpkBase)
  41.     CloseLibrary (XpkBase);
  42.   if (str)
  43.     printf ("%s\n", str);
  44.  
  45.   exit (0);
  46. }
  47.  
  48. void
  49. init (void)
  50. {
  51.   if (OpenDevice ("timer.device", 0, (struct IORequest *) & timerequest, 0))
  52.     end ("Can't open timer.device");
  53.  
  54.   TimerBase = &timerequest.tr_node.io_Device->dd_Library;
  55.  
  56.   if (!(XpkBase = OpenLibrary (XPKNAME, 0)))
  57.     end ("Can't open " XPKNAME);
  58. }
  59.  
  60. void
  61. testfile (char *packer, char *file)
  62. {
  63.   struct EClockVal eval1, eval2;
  64.   struct FileInfoBlock fib;
  65.   ULONG freq, ulen, ulen2, clen, ctime, utime;
  66.   LONG cf;
  67.  
  68.   if (!(fh = Open (file, MODE_OLDFILE)))
  69.     end ("Cannot open input file");
  70.  
  71.   if (!(ExamineFH (fh, &fib)))
  72.     end ("Failed to ExamineFH input file");
  73.  
  74.   bufsize = fib.fib_Size + fib.fib_Size / 32 + 2 * XPK_MARGIN;
  75.  
  76.   if (!(buf1 = AllocMem (bufsize, MEMF_FAST)))
  77.     end ("Out of memory");
  78.  
  79.   if (!(buf2 = AllocMem (bufsize, MEMF_FAST)))
  80.     end ("Out of memory");
  81.  
  82. #if THREE_BUFS
  83.   if (!(buf3 = AllocMem (bufsize, MEMF_FAST)))
  84.     end ("Out of memory");
  85. #endif
  86.  
  87.   ulen = Read (fh, buf1, fib.fib_Size);
  88.  
  89.   if (ulen != fib.fib_Size)
  90.     end ("Could not read whole file with one Read()");
  91.  
  92.   Close (fh);
  93.   fh = 0;
  94.  
  95.   /*--------------------------------------------------------*/
  96.  
  97.   Forbid ();
  98.   freq = ReadEClock (&eval1);
  99.  
  100.   if (XpkPackTags (XPK_InBuf, buf1,
  101.            XPK_InLen, ulen,
  102.            XPK_OutBuf, buf2,
  103.            XPK_OutBufLen, bufsize,
  104.            XPK_GetOutLen, &clen,
  105.            XPK_GetError, errbuf,
  106.            XPK_Password, Password,
  107.            XPK_FindMethod, packer,
  108.            TAG_DONE
  109.       )){
  110.     Permit ();
  111.     end (errbuf);
  112.   }
  113.  
  114.   ReadEClock (&eval2);
  115.   Permit ();
  116.   ctime = eval2.ev_lo - eval1.ev_lo;
  117.  
  118.   /*--------------------------------------------------------*/
  119.  
  120.   Forbid ();
  121.   ReadEClock (&eval1);
  122.  
  123.   if (XpkUnpackTags (
  124.               XPK_InBuf, buf2,
  125.               XPK_InLen, clen,
  126. #if THREE_BUFS
  127.               XPK_OutBuf, buf3,
  128. #else
  129.               XPK_OutBuf, buf1,
  130. #endif
  131.               XPK_OutBufLen, bufsize,
  132.               XPK_GetOutLen, &ulen2,
  133.               XPK_GetError, errbuf,
  134. #if NOCRC
  135.               XPK_NoCRC, TRUE,
  136. #endif
  137.               XPK_Password, Password,
  138.               TAG_DONE
  139.       )){
  140.     Permit ();
  141.     end (errbuf);
  142.   }
  143.  
  144.   ReadEClock (&eval2);
  145.   Permit ();
  146.   utime = eval2.ev_lo - eval1.ev_lo;
  147.  
  148.   /*--------------------------------------------------------*/
  149.  
  150.   cf = 1000 - 1000 * clen / ulen;
  151.  
  152.   if (cf < 0)
  153.     cf = 0;
  154.  
  155.   printf ("%-8s   %6d %6d %2d.%d%    %3d.%02d %7d     %2d.%02d %7d\n",
  156.       packer,
  157.       ulen, clen, cf / 10, cf % 10,
  158.       ctime / freq, (100 * (ctime % freq)) / freq, (int) ((double) freq * (double) ulen / (double) ctime),
  159.       utime / freq, (100 * (utime % freq)) / freq, (int) ((double) freq * (double) ulen / (double) utime)
  160.     );
  161.  
  162.   if (ulen != ulen2) {
  163.     printf ("decompressed length is %ld !!!\n", ulen2);
  164.   }
  165.  
  166. #if THREE_BUFS
  167.   if (buf3) {
  168.     FreeMem (buf3, bufsize);
  169.     buf3 = 0;
  170.   }
  171. #endif
  172.   if (buf2) {
  173.     FreeMem (buf2, bufsize);
  174.     buf2 = 0;
  175.   }
  176.   if (buf1) {
  177.     FreeMem (buf1, bufsize);
  178.     buf1 = 0;
  179.   }
  180. }
  181.  
  182. main (int argc, char *argv[])
  183. {
  184.   int i = 1;
  185.  
  186.   if (!strcmp (argv[1], "-p"))
  187.     Password = "testpw", i++;
  188.  
  189.   init ();
  190.  
  191.   printf ("Warning: Have to Forbid() task rescheduling for accurate measurements.\n");
  192.   printf ("Packer      UComp   Comp    CF     CTime    CSpd     UTime    USpd\n");
  193.   for (; i < argc - 1; i++)
  194.     testfile (argv[i], argv[argc - 1]);
  195.  
  196.   end (NULL);
  197. }
  198.