home *** CD-ROM | disk | FTP | other *** search
- /*
- ** $Id: termClip.c,v 1.5 92/08/15 20:13:44 olsen Sta Locker: olsen $
- ** $Revision: 1.5 $
- ** $Date: 92/08/15 20:13:44 $
- **
- ** Clipboard support routines
- **
- ** Copyright ⌐ 1990-1992 by Olaf `Olsen' Barthel & MXM
- ** All Rights Reserved
- */
-
- #include "termGlobal.h"
-
- STATIC struct IFFHandle *ClipHandle;
- STATIC STRPTR ClipBuffer,
- ClipIndex;
- STATIC LONG ClipSize,
- ClipLength;
-
- /* ClipServer():
- *
- * Quiet background process which saves data to the clipboard
- * or fills a buffer with fresh clipboard data.
- */
-
- VOID __saveds
- ClipServer()
- {
- if(ClipPort = CreateMsgPort())
- {
- ULONG Mask;
- struct Message *ClipMsg;
- UBYTE *Buffer;
- BYTE Terminated = FALSE;
-
- Signal(ThisProcess,SIGBREAKF_CTRL_C);
-
- while(!Terminated)
- {
- Mask = Wait(SIGBREAKF_CTRL_C | (1 << ClipPort -> mp_SigBit));
-
- if(Mask & SIGBREAKF_CTRL_C)
- Terminated = TRUE;
-
- if(Mask & (1 << ClipPort -> mp_SigBit))
- {
- while(ClipMsg = GetMsg(ClipPort))
- {
- Buffer = ClipMsg -> mn_Node . ln_Name;
-
- if(Buffer[0])
- SaveClip(Buffer,strlen(Buffer));
- else
- {
- WORD Len = LoadClip(Buffer,256);
-
- Buffer[Len] = 0;
- }
-
- ReplyMsg(ClipMsg);
- }
- }
- }
-
- DeleteMsgPort(ClipPort);
- }
-
- Forbid();
-
- ClipProcess = NULL;
-
- Signal(ThisProcess,SIGBREAKF_CTRL_C);
- }
-
- /* CloseClip():
- *
- * Close clipboard handle, stop reading.
- */
-
- VOID
- CloseClip()
- {
- if(ClipHandle)
- {
- CloseIFF(ClipHandle);
-
- if(ClipHandle -> iff_Stream)
- CloseClipboard((struct ClipboardHandle *)ClipHandle -> iff_Stream);
-
- FreeIFF(ClipHandle);
-
- ClipHandle = NULL;
- }
-
- if(ClipBuffer)
- {
- FreeVec(ClipBuffer);
-
- ClipBuffer = NULL;
- }
- }
-
- /* GetClip(STRPTR Buffer,WORD Len,BYTE Filter):
- *
- * Read text data from clipboard and put it into the supplied buffer.
- */
-
- WORD
- GetClip(STRPTR Buffer,WORD Len,BYTE Filter)
- {
- WORD BytesPut = 0;
-
- /* Is the read buffer already exhausted? */
-
- if(!ClipLength)
- {
- /* Is there still any data to read? */
-
- if(ClipSize)
- {
- WORD Size = ClipSize < 1024 ? ClipSize : 1024;
-
- /* Try to read the data and return failure if necessary. */
-
- if(ReadChunkBytes(ClipHandle,ClipBuffer,Size) != Size)
- return(-1);
- else
- {
- ClipSize -= Size;
- ClipLength = Size;
- ClipIndex = ClipBuffer;
- }
- }
- else
- {
- /* We just parsed a single chunk, now go on and
- * look for another one.
- */
-
- if(!ParseIFF(ClipHandle,IFFPARSE_SCAN))
- {
- struct ContextNode *ContextNode;
-
- if(ContextNode = CurrentChunk(ClipHandle))
- {
- if(ContextNode -> cn_Type == 'FTXT')
- {
- WORD Size;
-
- ClipSize = ContextNode -> cn_Size;
- Size = ClipSize < 1024 ? ClipSize : 1024;
-
- if(ReadChunkBytes(ClipHandle,ClipBuffer,Size) != Size)
- return(-1);
- else
- {
- ClipSize -= Size;
- ClipLength = Size;
- ClipIndex = ClipBuffer;
- }
- }
- else
- return(-1);
- }
- else
- return(-1);
- }
- else
- return(-1);
- }
- }
-
- /* The following loop processes the contents of
- * the clipboard buffer read. Special characters
- * such as LF and CR will be converted according
- * to the current settings if enabled. No bytes
- * will be lost, though.
- */
-
- while(ClipLength && BytesPut < Len)
- {
- if(*ClipIndex == '\n')
- {
- if(Filter)
- {
- *Buffer++ = *ClipIndex++;
-
- BytesPut++;
-
- ClipLength--;
- }
- else
- {
- switch(Config . SendLF)
- {
- case LF_IGNORE: ClipIndex++;
- ClipLength--;
-
- break;
-
- case LF_ASLFCR: if(BytesPut + 2 <= Len)
- {
- *Buffer++ = '\n';
- *Buffer++ = '\r';
-
- BytesPut += 2;
-
- ClipLength--;
- ClipIndex++;
- }
- else
- return(BytesPut);
-
- break;
-
- case LF_ASLF: *Buffer++ = *ClipIndex++;
-
- BytesPut++;
-
- ClipLength--;
-
- break;
- }
- }
- }
- else
- {
- if(*ClipIndex == '\r')
- {
- if(Filter)
- {
- ClipIndex++;
- ClipLength--;
- }
- else
- {
- switch(Config . SendCR)
- {
- case CR_IGNORE: ClipIndex++;
- ClipLength--;
-
- break;
-
- case CR_ASCRLF: if(BytesPut + 2 <= Len)
- {
- *Buffer++ = '\r';
- *Buffer++ = '\n';
-
- BytesPut += 2;
-
- ClipLength--;
- ClipIndex++;
- }
- else
- return(BytesPut);
-
- break;
-
- case CR_ASCR: *Buffer++ = *ClipIndex++;
-
- BytesPut++;
-
- ClipLength--;
-
- break;
- }
- }
- }
- else
- {
- if(Filter)
- {
- if(IsPrintable(*ClipIndex))
- {
- *Buffer++ = *ClipIndex++;
-
- BytesPut++;
-
- ClipLength--;
- }
- else
- {
- ClipIndex++;
- ClipLength--;
- }
- }
- else
- {
- *Buffer++ = *ClipIndex++;
-
- BytesPut++;
-
- ClipLength--;
- }
- }
- }
- }
-
- return(BytesPut);
- }
-
- /* OpenClip():
- *
- * Open the clipboard for sequential reading.
- */
-
- BYTE
- OpenClip()
- {
- BYTE Error;
-
- CloseClip();
-
- if(ClipBuffer = (STRPTR)AllocVec(1024,MEMF_ANY))
- {
- if(ClipHandle = AllocIFF())
- {
- if(ClipHandle -> iff_Stream = (ULONG)OpenClipboard(Config . ClipboardUnit))
- {
- InitIFFasClip(ClipHandle);
-
- if(!OpenIFF(ClipHandle,IFFF_READ))
- {
- if(!StopChunk(ClipHandle,'FTXT','CHRS'))
- {
- if(!ParseIFF(ClipHandle,IFFPARSE_SCAN))
- {
- struct ContextNode *ContextNode;
-
- if(ContextNode = CurrentChunk(ClipHandle))
- {
- if(ContextNode -> cn_Type == 'FTXT')
- {
- ClipSize = ContextNode -> cn_Size;
- ClipLength = 0;
-
- return(CLIPERR_NONE);
- }
- else
- Error = CLIPERR_NOTEXT;
- }
- else
- Error = CLIPERR_NOTEXT;
- }
- else
- Error = CLIPERR_NOTEXT;
- }
- else
- Error = CLIPERR_IFF;
- }
- else
- Error = CLIPERR_OPEN;
- }
- else
- Error = CLIPERR_OPEN;
- }
- else
- Error = CLIPERR_MEM;
- }
- else
- Error = CLIPERR_MEM;
-
- CloseClip();
-
- return(Error);
- }
-
- /* SaveClip(UBYTE *Buffer,LONG Size):
- *
- * Send a given text buffer to the clipboard.
- */
-
- BYTE
- SaveClip(UBYTE *Buffer,LONG Size)
- {
- BYTE Success = FALSE;
-
- if(Size > 0)
- {
- struct IFFHandle *Handle;
-
- if(Handle = AllocIFF())
- {
- if(Handle -> iff_Stream = (ULONG)OpenClipboard(Config . ClipboardUnit))
- {
- InitIFFasClip(Handle);
-
- if(!OpenIFF(Handle,IFFF_WRITE))
- {
- if(!PushChunk(Handle,'FTXT','FORM',IFFSIZE_UNKNOWN))
- {
- if(!PushChunk(Handle,0,'CHRS',IFFSIZE_UNKNOWN))
- {
- if(WriteChunkBytes(Handle,Buffer,Size) == Size)
- {
- if(!PopChunk(Handle))
- Success = TRUE;
- }
- }
- }
-
- if(Success)
- {
- if(PopChunk(Handle))
- Success = FALSE;
- }
-
- CloseIFF(Handle);
- }
-
- CloseClipboard((struct ClipboardHandle *)Handle -> iff_Stream);
- }
-
- FreeIFF(Handle);
- }
- }
-
- return(Success);
- }
-
- /* LoadClip(UBYTE *Buffer,LONG Size):
- *
- * Put the contents of the clipboard into a given
- * buffer. Note that only the first FTXT chunk will
- * be read. Since this code will only be called by
- * the clipboard server process which serves the
- * string gadget editing hook, this will hopefully
- * not be fatal. If you want more data to be read,
- * including multiple FTXT chunks, use the OpenClip(),
- * GetClip(), CloseClip() combo above.
- */
-
- LONG
- LoadClip(UBYTE *Buffer,LONG Size)
- {
- struct IFFHandle *Handle;
- LONG Bytes = 0;
-
- if(Handle = AllocIFF())
- {
- if(Handle -> iff_Stream = (ULONG)OpenClipboard(Config . ClipboardUnit))
- {
- InitIFFasClip(Handle);
-
- if(!OpenIFF(Handle,IFFF_READ))
- {
- if(!StopChunk(Handle,'FTXT','CHRS'))
- {
- if(!ParseIFF(Handle,IFFPARSE_SCAN))
- {
- struct ContextNode *ContextNode;
-
- if(ContextNode = CurrentChunk(Handle))
- {
- if(ContextNode -> cn_Type == 'FTXT')
- {
- if(Size > ContextNode -> cn_Size)
- Size = ContextNode -> cn_Size;
-
- if(ReadChunkRecords(Handle,Buffer,Size,1))
- Bytes = Size;
- }
- }
- }
- }
-
- CloseIFF(Handle);
- }
-
- CloseClipboard((struct ClipboardHandle *)Handle -> iff_Stream);
- }
-
- FreeIFF(Handle);
- }
-
- return(Bytes);
- }
-