home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / ForCLI / OpenSaveClip10.lha / OpenClip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-09  |  2.2 KB  |  95 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. void cleanup(char *,int);
  12. int OpenClip(void);
  13. void CloseClip(void);
  14.  
  15. struct DosLibrary *DOSBase=NULL;
  16. struct IOClipReq *ioc=NULL;
  17. struct MsgPort *port=NULL;
  18. char buf[102],clipb=FALSE,ver[]="\0$VER: OpenClip 1.0 " __AMIGADATE__;
  19. BPTR fh=NULL;
  20.  
  21. int __saveds __asm not_main(register __a0 char *args, register __d0 long len) {
  22.     ULONG length,formlength;
  23.     if(!(DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",0L))) {
  24.         return(20);
  25.     }
  26.     if(!OpenClip()) cleanup("Couldn't open clipboard.device\n",20);
  27.     args[len-1]=0;
  28.     if(!(fh=Open(args,MODE_OLDFILE))) {
  29.         DOSPRINT("Couldn't open ")
  30.         DOSPRINT(args)
  31.         DOSPRINT("\n")
  32.         cleanup(NULL,10);
  33.     }
  34.     Seek(fh,0,OFFSET_END);
  35.     length=Seek(fh,0,OFFSET_BEGINNING);
  36.     formlength=length+12;
  37.     ioc->io_Offset=0;
  38.     ioc->io_ClipID=0;
  39.     ioc->io_Command=CMD_WRITE;
  40.     ioc->io_Length=4;
  41.     ioc->io_Data=(APTR)"FORM";
  42.     DoIO((struct IORequest *)ioc);
  43.     ioc->io_Data=(APTR)&formlength;
  44.     DoIO((struct IORequest *)ioc);
  45.     ioc->io_Data=(APTR)"FTXT";
  46.     DoIO((struct IORequest *)ioc);
  47.     ioc->io_Data=(APTR)"CHRS";
  48.     DoIO((struct IORequest *)ioc);
  49.     ioc->io_Data=(APTR)&length;
  50.     DoIO((struct IORequest *)ioc);
  51.     while((length=Read(fh,buf,100))) {
  52.         if(length==-1) {
  53.             DOSPRINT("Error reading file\n");
  54.             break;
  55.         }
  56.         ioc->io_Length=length;
  57.         ioc->io_Data=(APTR)buf;
  58.         DoIO((struct IORequest *)ioc);
  59.     }
  60.     ioc->io_Command=CMD_UPDATE;
  61.     DoIO((struct IORequest *)ioc);
  62.     cleanup(NULL,0);
  63. }
  64.  
  65. void cleanup(char *msg,int code) {
  66.     if(fh) Close(fh);
  67.     if(clipb) CloseClip();
  68.     if(DOSBase) {
  69.         if(msg) DOSPRINT(msg);
  70.         CloseLibrary((struct Library *)DOSBase);
  71.     }
  72.     Exit(code);
  73. }
  74.  
  75. int OpenClip(void) {
  76.     if(!(port=CreatePort(NULL,0L))) return(FALSE);
  77.     if(!(ioc=(struct IOClipReq *)CreateExtIO(port,sizeof(struct IOClipReq)))) {
  78.         DeletePort(port);
  79.         return(FALSE);
  80.     }
  81.     if(OpenDevice("clipboard.device",0,(struct IORequest *)ioc,0)) {
  82.         DeleteExtIO((struct IORequest *)ioc);
  83.         DeletePort(port);
  84.         return(FALSE);
  85.     }
  86.     clipb=TRUE;
  87.     return(TRUE);
  88. }
  89.  
  90. void CloseClip(void) {
  91.     CloseDevice((struct IORequest *)ioc);
  92.     DeleteExtIO((struct IORequest *)ioc);
  93.     DeletePort(port);
  94. }
  95.