home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / util / cli / OpenSaveClip10.lha / SaveClip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-09  |  2.6 KB  |  115 lines

  1. #include <exec/types.h>
  2. #include <devices/clipboard.h>
  3. #include <dos/dos.h>
  4. #include <libraries/iffparse.h>
  5. #include <proto/exec.h>
  6. #include <proto/dos.h>
  7. #include <string.h>
  8.  
  9. #define DOSPRINT(x) Write(Output(),x,strlen(x));
  10.  
  11. struct iffstart {
  12.     ULONG form;
  13.     ULONG length;
  14.     ULONG formtype;
  15. } is;
  16.  
  17. void cleanup(char *,int);
  18. int OpenClip(void);
  19. void CloseClip(void);
  20. int savechunk(void);
  21.  
  22. struct DosLibrary *DOSBase=NULL;
  23. struct IOClipReq *ioc=NULL;
  24. struct MsgPort *port=NULL;
  25. char buf[102],clipb=FALSE,ver[]="\0$VER: SaveClip 1.0 " __AMIGADATE__;
  26. BPTR fh=NULL;
  27.  
  28. int __saveds __asm not_main(register __a0 char *args, register __d0 long len) {
  29.     if(!(DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",0L))) {
  30.         return(20);
  31.     }
  32.     if(!OpenClip()) cleanup("Couldn't open clipboard.device\n",20);
  33.     args[len-1]=0;
  34.     if(!(fh=Open(args,MODE_NEWFILE))) {
  35.         DOSPRINT("Couldn't open ")
  36.         DOSPRINT(args)
  37.         DOSPRINT("\n")
  38.         cleanup(NULL,10);
  39.     }
  40.     ioc->io_Offset=0;
  41.     ioc->io_ClipID=0;
  42.     ioc->io_Command=CMD_READ;
  43.     ioc->io_Length=12;
  44.     ioc->io_Data=(APTR)&is;
  45.     DoIO((struct IORequest *)ioc);
  46.     if(ioc->io_Actual<8 || is.form!=(ULONG)(ID_FORM) || is.formtype!=(ULONG)MAKE_ID('F','T','X','T')) {
  47.         ioc->io_Length=~0L;
  48.         ioc->io_Data=NULL;
  49.         DoIO((struct IORequest *)ioc);
  50.         if(ioc->io_Actual) DoIO((struct IORequest *)ioc);
  51.         cleanup("Not an IFF-FTXT clip\n",5);
  52.     }
  53.     while(savechunk());
  54.     cleanup(NULL,0);
  55. }
  56.  
  57. void cleanup(char *msg,int code) {
  58.     if(fh) Close(fh);
  59.     if(clipb) CloseClip();
  60.     if(DOSBase) {
  61.         if(msg) DOSPRINT(msg);
  62.         CloseLibrary((struct Library *)DOSBase);
  63.     }
  64.     Exit(code);
  65. }
  66.  
  67. int OpenClip(void) {
  68.     if(!(port=CreatePort(NULL,0L))) return(FALSE);
  69.     if(!(ioc=(struct IOClipReq *)CreateExtIO(port,sizeof(struct IOClipReq)))) {
  70.         DeletePort(port);
  71.         return(FALSE);
  72.     }
  73.     if(OpenDevice("clipboard.device",0,(struct IORequest *)ioc,0)) {
  74.         DeleteExtIO((struct IORequest *)ioc);
  75.         DeletePort(port);
  76.         return(FALSE);
  77.     }
  78.     clipb=TRUE;
  79.     return(TRUE);
  80. }
  81.  
  82. void CloseClip(void) {
  83.     CloseDevice((struct IORequest *)ioc);
  84.     DeleteExtIO((struct IORequest *)ioc);
  85.     DeletePort(port);
  86. }
  87.  
  88. int savechunk(void) {
  89.     int x,read;
  90.     ioc->io_Length=8;
  91.     ioc->io_Data=(APTR)&is;
  92.     DoIO((struct IORequest *)ioc);
  93.     if(!ioc->io_Actual) return(FALSE);
  94.     if(is.form!=(ULONG)MAKE_ID('C','H','R','S')) {
  95.         ioc->io_Length=is.length;
  96.         ioc->io_Data=NULL;
  97.         DoIO((struct IORequest *)ioc);
  98.         return(TRUE);
  99.     }
  100.     for(x=0;x<is.length;x+=100) {
  101.         if((is.length-x) > 100) read=100;
  102.         else read=is.length-x;
  103.         ioc->io_Length=read;
  104.         ioc->io_Data=buf;
  105.         DoIO((struct IORequest *)ioc);
  106.         if(Write(fh,buf,read)==-1) {
  107.             ioc->io_Length=~0L;
  108.             ioc->io_Data=NULL;
  109.             DoIO((struct IORequest *)ioc);
  110.             cleanup("Error writing file\n",10);
  111.         }
  112.     }
  113.     return(TRUE);
  114. }
  115.