home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / comms / comprgs / term232.lha / Source_Code / termSource / termClip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-18  |  7.9 KB  |  478 lines

  1. /*
  2. **    $Id: termClip.c,v 1.5 92/08/15 20:13:44 olsen Sta Locker: olsen $
  3. **    $Revision: 1.5 $
  4. **    $Date: 92/08/15 20:13:44 $
  5. **
  6. **    Clipboard support routines
  7. **
  8. **    Copyright ⌐ 1990-1992 by Olaf `Olsen' Barthel & MXM
  9. **        All Rights Reserved
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14. STATIC struct IFFHandle    *ClipHandle;
  15. STATIC STRPTR         ClipBuffer,
  16.              ClipIndex;
  17. STATIC LONG         ClipSize,
  18.              ClipLength;
  19.  
  20.     /* ClipServer():
  21.      *
  22.      *    Quiet background process which saves data to the clipboard
  23.      *    or fills a buffer with fresh clipboard data.
  24.      */
  25.  
  26. VOID __saveds
  27. ClipServer()
  28. {
  29.     if(ClipPort = CreateMsgPort())
  30.     {
  31.         ULONG         Mask;
  32.         struct Message    *ClipMsg;
  33.         UBYTE        *Buffer;
  34.         BYTE         Terminated = FALSE;
  35.  
  36.         Signal(ThisProcess,SIGBREAKF_CTRL_C);
  37.  
  38.         while(!Terminated)
  39.         {
  40.             Mask = Wait(SIGBREAKF_CTRL_C | (1 << ClipPort -> mp_SigBit));
  41.  
  42.             if(Mask & SIGBREAKF_CTRL_C)
  43.                 Terminated = TRUE;
  44.  
  45.             if(Mask & (1 << ClipPort -> mp_SigBit))
  46.             {
  47.                 while(ClipMsg = GetMsg(ClipPort))
  48.                 {
  49.                     Buffer = ClipMsg -> mn_Node . ln_Name;
  50.  
  51.                     if(Buffer[0])
  52.                         SaveClip(Buffer,strlen(Buffer));
  53.                     else
  54.                     {
  55.                         WORD Len = LoadClip(Buffer,256);
  56.  
  57.                         Buffer[Len] = 0;
  58.                     }
  59.  
  60.                     ReplyMsg(ClipMsg);
  61.                 }
  62.             }
  63.         }
  64.  
  65.         DeleteMsgPort(ClipPort);
  66.     }
  67.  
  68.     Forbid();
  69.  
  70.     ClipProcess = NULL;
  71.  
  72.     Signal(ThisProcess,SIGBREAKF_CTRL_C);
  73. }
  74.  
  75.     /* CloseClip():
  76.      *
  77.      *    Close clipboard handle, stop reading.
  78.      */
  79.  
  80. VOID
  81. CloseClip()
  82. {
  83.     if(ClipHandle)
  84.     {
  85.         CloseIFF(ClipHandle);
  86.  
  87.         if(ClipHandle -> iff_Stream)
  88.             CloseClipboard((struct ClipboardHandle *)ClipHandle -> iff_Stream);
  89.  
  90.         FreeIFF(ClipHandle);
  91.  
  92.         ClipHandle = NULL;
  93.     }
  94.  
  95.     if(ClipBuffer)
  96.     {
  97.         FreeVec(ClipBuffer);
  98.  
  99.         ClipBuffer = NULL;
  100.     }
  101. }
  102.  
  103.     /* GetClip(STRPTR Buffer,WORD Len,BYTE Filter):
  104.      *
  105.      *    Read text data from clipboard and put it into the supplied buffer.
  106.      */
  107.  
  108. WORD
  109. GetClip(STRPTR Buffer,WORD Len,BYTE Filter)
  110. {
  111.     WORD BytesPut = 0;
  112.  
  113.         /* Is the read buffer already exhausted? */
  114.  
  115.     if(!ClipLength)
  116.     {
  117.             /* Is there still any data to read? */
  118.  
  119.         if(ClipSize)
  120.         {
  121.             WORD Size = ClipSize < 1024 ? ClipSize : 1024;
  122.  
  123.                 /* Try to read the data and return failure if necessary. */
  124.  
  125.             if(ReadChunkBytes(ClipHandle,ClipBuffer,Size) != Size)
  126.                 return(-1);
  127.             else
  128.             {
  129.                 ClipSize    -= Size;
  130.                 ClipLength     = Size;
  131.                 ClipIndex     = ClipBuffer;
  132.             }
  133.         }
  134.         else
  135.         {
  136.                 /* We just parsed a single chunk, now go on and
  137.                  * look for another one.
  138.                  */
  139.  
  140.             if(!ParseIFF(ClipHandle,IFFPARSE_SCAN))
  141.             {
  142.                 struct ContextNode *ContextNode;
  143.  
  144.                 if(ContextNode = CurrentChunk(ClipHandle))
  145.                 {
  146.                     if(ContextNode -> cn_Type == 'FTXT')
  147.                     {
  148.                         WORD Size;
  149.  
  150.                         ClipSize    = ContextNode -> cn_Size;
  151.                         Size        = ClipSize < 1024 ? ClipSize : 1024;
  152.  
  153.                         if(ReadChunkBytes(ClipHandle,ClipBuffer,Size) != Size)
  154.                             return(-1);
  155.                         else
  156.                         {
  157.                             ClipSize    -= Size;
  158.                             ClipLength     = Size;
  159.                             ClipIndex     = ClipBuffer;
  160.                         }
  161.                     }
  162.                     else
  163.                         return(-1);
  164.                 }
  165.                 else
  166.                     return(-1);
  167.             }
  168.             else
  169.                 return(-1);
  170.         }
  171.     }
  172.  
  173.         /* The following loop processes the contents of
  174.          * the clipboard buffer read. Special characters
  175.          * such as LF and CR will be converted according
  176.          * to the current settings if enabled. No bytes
  177.          * will be lost, though.
  178.          */
  179.  
  180.     while(ClipLength && BytesPut < Len)
  181.     {
  182.         if(*ClipIndex == '\n')
  183.         {
  184.             if(Filter)
  185.             {
  186.                 *Buffer++ = *ClipIndex++;
  187.  
  188.                 BytesPut++;
  189.  
  190.                 ClipLength--;
  191.             }
  192.             else
  193.             {
  194.                 switch(Config . SendLF)
  195.                 {
  196.                     case LF_IGNORE:    ClipIndex++;
  197.                             ClipLength--;
  198.  
  199.                             break;
  200.  
  201.                     case LF_ASLFCR:    if(BytesPut + 2 <= Len)
  202.                             {
  203.                                 *Buffer++ = '\n';
  204.                                 *Buffer++ = '\r';
  205.  
  206.                                 BytesPut += 2;
  207.  
  208.                                 ClipLength--;
  209.                                 ClipIndex++;
  210.                             }
  211.                             else
  212.                                 return(BytesPut);
  213.  
  214.                             break;
  215.  
  216.                     case LF_ASLF:    *Buffer++ = *ClipIndex++;
  217.  
  218.                             BytesPut++;
  219.  
  220.                             ClipLength--;
  221.  
  222.                             break;
  223.                 }
  224.             }
  225.         }
  226.         else
  227.         {
  228.             if(*ClipIndex == '\r')
  229.             {
  230.                 if(Filter)
  231.                 {
  232.                     ClipIndex++;
  233.                     ClipLength--;
  234.                 }
  235.                 else
  236.                 {
  237.                     switch(Config . SendCR)
  238.                     {
  239.                         case CR_IGNORE:    ClipIndex++;
  240.                                 ClipLength--;
  241.  
  242.                                 break;
  243.  
  244.                         case CR_ASCRLF:    if(BytesPut + 2 <= Len)
  245.                                 {
  246.                                     *Buffer++ = '\r';
  247.                                     *Buffer++ = '\n';
  248.  
  249.                                     BytesPut += 2;
  250.  
  251.                                     ClipLength--;
  252.                                     ClipIndex++;
  253.                                 }
  254.                                 else
  255.                                     return(BytesPut);
  256.  
  257.                                 break;
  258.  
  259.                         case CR_ASCR:    *Buffer++ = *ClipIndex++;
  260.  
  261.                                 BytesPut++;
  262.  
  263.                                 ClipLength--;
  264.  
  265.                                 break;
  266.                     }
  267.                 }
  268.             }
  269.             else
  270.             {
  271.                 if(Filter)
  272.                 {
  273.                     if(IsPrintable(*ClipIndex))
  274.                     {
  275.                         *Buffer++ = *ClipIndex++;
  276.  
  277.                         BytesPut++;
  278.  
  279.                         ClipLength--;
  280.                     }
  281.                     else
  282.                     {
  283.                         ClipIndex++;
  284.                         ClipLength--;
  285.                     }
  286.                 }
  287.                 else
  288.                 {
  289.                     *Buffer++ = *ClipIndex++;
  290.  
  291.                     BytesPut++;
  292.  
  293.                     ClipLength--;
  294.                 }
  295.             }
  296.         }
  297.     }
  298.  
  299.     return(BytesPut);
  300. }
  301.  
  302.     /* OpenClip():
  303.      *
  304.      *    Open the clipboard for sequential reading.
  305.      */
  306.  
  307. BYTE
  308. OpenClip()
  309. {
  310.     BYTE Error;
  311.  
  312.     CloseClip();
  313.  
  314.     if(ClipBuffer = (STRPTR)AllocVec(1024,MEMF_ANY))
  315.     {
  316.         if(ClipHandle = AllocIFF())
  317.         {
  318.             if(ClipHandle -> iff_Stream = (ULONG)OpenClipboard(Config . ClipboardUnit))
  319.             {
  320.                 InitIFFasClip(ClipHandle);
  321.  
  322.                 if(!OpenIFF(ClipHandle,IFFF_READ))
  323.                 {
  324.                     if(!StopChunk(ClipHandle,'FTXT','CHRS'))
  325.                     {
  326.                         if(!ParseIFF(ClipHandle,IFFPARSE_SCAN))
  327.                         {
  328.                             struct ContextNode *ContextNode;
  329.  
  330.                             if(ContextNode = CurrentChunk(ClipHandle))
  331.                             {
  332.                                 if(ContextNode -> cn_Type == 'FTXT')
  333.                                 {
  334.                                     ClipSize    = ContextNode -> cn_Size;
  335.                                     ClipLength    = 0;
  336.  
  337.                                     return(CLIPERR_NONE);
  338.                                 }
  339.                                 else
  340.                                     Error = CLIPERR_NOTEXT;
  341.                             }
  342.                             else
  343.                                 Error = CLIPERR_NOTEXT;
  344.                         }
  345.                         else
  346.                             Error = CLIPERR_NOTEXT;
  347.                     }
  348.                     else
  349.                         Error = CLIPERR_IFF;
  350.                 }
  351.                 else
  352.                     Error = CLIPERR_OPEN;
  353.             }
  354.             else
  355.                 Error = CLIPERR_OPEN;
  356.         }
  357.         else
  358.             Error = CLIPERR_MEM;
  359.     }
  360.     else
  361.         Error = CLIPERR_MEM;
  362.  
  363.     CloseClip();
  364.  
  365.     return(Error);
  366. }
  367.  
  368.     /* SaveClip(UBYTE *Buffer,LONG Size):
  369.      *
  370.      *    Send a given text buffer to the clipboard.
  371.      */
  372.  
  373. BYTE
  374. SaveClip(UBYTE *Buffer,LONG Size)
  375. {
  376.     BYTE Success = FALSE;
  377.  
  378.     if(Size > 0)
  379.     {
  380.         struct IFFHandle *Handle;
  381.  
  382.         if(Handle = AllocIFF())
  383.         {
  384.             if(Handle -> iff_Stream = (ULONG)OpenClipboard(Config . ClipboardUnit))
  385.             {
  386.                 InitIFFasClip(Handle);
  387.     
  388.                 if(!OpenIFF(Handle,IFFF_WRITE))
  389.                 {
  390.                     if(!PushChunk(Handle,'FTXT','FORM',IFFSIZE_UNKNOWN))
  391.                     {
  392.                         if(!PushChunk(Handle,0,'CHRS',IFFSIZE_UNKNOWN))
  393.                         {
  394.                             if(WriteChunkBytes(Handle,Buffer,Size) == Size)
  395.                             {
  396.                                 if(!PopChunk(Handle))
  397.                                     Success = TRUE;
  398.                             }
  399.                         }
  400.                     }
  401.  
  402.                     if(Success)
  403.                     {
  404.                         if(PopChunk(Handle))
  405.                             Success = FALSE;
  406.                     }
  407.  
  408.                     CloseIFF(Handle);
  409.                 }
  410.  
  411.                 CloseClipboard((struct ClipboardHandle *)Handle -> iff_Stream);
  412.             }
  413.  
  414.             FreeIFF(Handle);
  415.         }
  416.     }
  417.  
  418.     return(Success);
  419. }
  420.  
  421.     /* LoadClip(UBYTE *Buffer,LONG Size):
  422.      *
  423.      *    Put the contents of the clipboard into a given
  424.      *    buffer. Note that only the first FTXT chunk will
  425.      *    be read. Since this code will only be called by
  426.      *    the clipboard server process which serves the
  427.      *    string gadget editing hook, this will hopefully
  428.      *    not be fatal. If you want more data to be read,
  429.      *    including multiple FTXT chunks, use the OpenClip(),
  430.      *    GetClip(), CloseClip() combo above.
  431.      */
  432.  
  433. LONG
  434. LoadClip(UBYTE *Buffer,LONG Size)
  435. {
  436.     struct IFFHandle    *Handle;
  437.     LONG             Bytes = 0;
  438.  
  439.     if(Handle = AllocIFF())
  440.     {
  441.         if(Handle -> iff_Stream = (ULONG)OpenClipboard(Config . ClipboardUnit))
  442.         {
  443.             InitIFFasClip(Handle);
  444.  
  445.             if(!OpenIFF(Handle,IFFF_READ))
  446.             {
  447.                 if(!StopChunk(Handle,'FTXT','CHRS'))
  448.                 {
  449.                     if(!ParseIFF(Handle,IFFPARSE_SCAN))
  450.                     {
  451.                         struct ContextNode *ContextNode;
  452.  
  453.                         if(ContextNode = CurrentChunk(Handle))
  454.                         {
  455.                             if(ContextNode -> cn_Type == 'FTXT')
  456.                             {
  457.                                 if(Size > ContextNode -> cn_Size)
  458.                                     Size = ContextNode -> cn_Size;
  459.  
  460.                                 if(ReadChunkRecords(Handle,Buffer,Size,1))
  461.                                     Bytes = Size;
  462.                             }
  463.                         }
  464.                     }
  465.                 }
  466.  
  467.                 CloseIFF(Handle);
  468.             }
  469.  
  470.             CloseClipboard((struct ClipboardHandle *)Handle -> iff_Stream);
  471.         }
  472.  
  473.         FreeIFF(Handle);
  474.     }
  475.  
  476.     return(Bytes);
  477. }
  478.