/* :ts=3
** Stores RAW contents of clipboards to T:clipboard.%03u
**
** written 20-Jun-98 & © Ralph Reuchlein 1998
** 
** Contact:   raresoft@rz.fh-ausgburg.de
**
** I am sorry for mixing up AmigaOS and C standard library functions, but I
** like the great buffer mechanism of the ANSI-C IO functions.
*/


#include <exec/types.h>
#include <proto/exec.h>
#include <proto/iffparse.h>
#include <stdlib.h>
#include <stdio.h>

STRPTR   versionstr="\0$VER: StoreClips V1.0 (20-Jun-98)";

STRPTR   filename="T:clipboard.000";

UBYTE   *buffer[256];


ULONG StoreClip(struct IOClipReq *cbio,FILE *fh)
{
   ULONG read=0;

   cbio->io_Command  = CMD_READ;
   cbio->io_Data     = (APTR)buffer;
   cbio->io_Length   = 256;
   cbio->io_Actual   = 256;
   cbio->io_Offset   = 0;
   cbio->io_ClipID   = 0;     /* do not manipulate after first DoIO()! */

   while (cbio->io_Length == cbio->io_Actual)
   {
      DoIO((struct IORequest *)cbio);
      if (cbio->io_Error)  break;                 /* be safe */
      if (fwrite(buffer,1,cbio->io_Actual,fh)!=cbio->io_Actual)   break;
      read+=cbio->io_Actual;
   }

   /* signalize end of read (read again BEYOND data) */
   cbio->io_Offset   = -1;             /* hugest value possible :-) */
   DoIO((struct IORequest *)cbio);

   return(read);
}




void Usage(STRPTR progname)
{
   printf("Usage: %s [start [end]]\n",progname);
   exit(0);
}




void main(UWORD argc,STRPTR argv[])
{
   UWORD                   unit,start,end;
   struct ClipboardHandle *cbh;
   struct IOClipReq       *cbio;
   FILE                   *fh;
   BOOL                    ok;
   ULONG                   read;


   if (argc==1)         /* we gave no parameter */
   {
      start=0; end=255;       /* scan all clipboards */
   }
   else if (argc==2)    /* we gave one parameter */
   {
      if (*argv[1]=='?')
      {
         Usage(argv[0]);
      }
      start=atoi(argv[1]);    /* scan only one clipboard */
      end=start;
   }
   else if (argc==3)    /* we gave two parameter */
   {
      start=atoi(argv[1]);
      end=atoi(argv[2]);      /* scan given range */
      if (start>end)
      {
         printf("You are not one of the brightest?\n"
                "Start value must be less or same as end value!\n\n");
         Usage(argv[0]);
      }
   }
   else
   {
      Usage(argv[0]);
   }

   if (start==end)
   {
      printf("Storing clipboard unit %u\n",start);
   }
   else
   {
      printf("Storing clipboard units %u - %u\n",start,end);
   }

   /* main loop increases clipboard number */
   for (unit=start; unit<=end; unit++)
   {
      ok=FALSE;
      printf("Storing unit %3u ...\n›A",unit);
      cbh=OpenClipboard(unit);         /* iffparse call */
      if (cbh)
      {
         /* determine IOClipReq */
         cbio=&(cbh->cbh_Req);

         /* Open output file */
         sprintf(filename,"T:clipboard.%03u",unit);
         fh=fopen(filename,"wb");
         if (fh)
         {
            ok=TRUE;
            /* store clipboard unit */
            read=StoreClip(cbio,fh);

            /* close opened file */
            fclose(fh);

            if (read==0)
            {
               /* remove file if nothing has been written in */
               remove(filename);
            }
            else
            {
               printf("›KClipboard unit %3u: %u bytes stored\n",unit,read);
            }
         }
         else
         {
            printf("Error opening file '%s'!",filename);
         }

         CloseClipboard(cbh);       /* iffparse call */
      }
      else
      {
         printf("Error opening Clipboard #%u\n",unit);
      }
   }
   if (ok)  printf("›K");
}



