home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_USR08B.LHA / gerlib / examples / add / normal / TextClipSupport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  4.4 KB  |  224 lines

  1. /*-- Rev Header - do NOT edit!
  2.  *
  3.  *  Filename : TextClipSupport.c
  4.  *  Purpose  : Zwei Hilfsroutinen für die Benutzung von Clips
  5.  *
  6.  *  Program  : -
  7.  *  Author   : Gerhard Müller
  8.  *  Copyright: (c) by Gerhard Müller
  9.  *  Creation : Fri Sep 10 00:41:01 1993
  10.  *
  11.  *  compile  : makefile
  12.  *
  13.  *  Compile version  : 0.1
  14.  *  Ext. Version     : 0.1
  15.  *
  16.  *  REVISION HISTORY
  17.  *
  18.  *  Date                     Comment
  19.  *  ------------------------ -------------------------------------------------
  20.  *  Fri Sep 17 00:59:43 1993 Taken from some other source-code, I think it was
  21.  *                           some of olsens, but I don't remeber exactly
  22.  *
  23.  *
  24.  *
  25.  *-- REV_END --
  26.  */
  27.  
  28. #include <exec/types.h>
  29. #include <libraries/iffparse.h>
  30. #include <dos/dos.h>
  31. #include <inline/stubs.h>
  32. #ifdef __OPTIMIZE__
  33. #include <inline/exec.h>
  34. #include <inline/dos.h>
  35. #include <inline/iffparse.h>
  36. #else
  37. #include <clib/exec_protos.h>
  38. #include <clib/dos_protos.h>
  39. #include <clib/iffparse_protos.h>
  40. #endif
  41.  
  42. #include "add.h"
  43.  
  44. /* stellt folgende Funktionen zu verfügung: */
  45.  
  46. BYTE SaveClip(UBYTE *Buffer,LONG Size);
  47. LONG LoadClip(UBYTE *Buffer,LONG Size);
  48.  
  49.  
  50.     /* SaveClip(UBYTE *Buffer,LONG Size):
  51.      *
  52.      *    Save text data to the clipboard.
  53.      */
  54.  
  55. BYTE
  56. SaveClip(UBYTE *Buffer,LONG Size)
  57. {
  58.     struct IFFHandle    *Handle;
  59.     BYTE             Success = FALSE;
  60.  
  61.         /* Allocate an IFFHandle... */
  62.  
  63.     if(Handle = AllocIFF())
  64.     {
  65.             /* Make it operate on the clipboard. */
  66.  
  67.         if(Handle -> iff_Stream = (ULONG)OpenClipboard(PRIMARY_CLIP))
  68.         {
  69.                 /* Tell iffparse.library that it is to deal
  70.                  * with the clipboard, not with an AmigaDOS file.
  71.                  */
  72.  
  73.             InitIFFasClip(Handle);
  74.  
  75.                 /* Open the handle for writing. */
  76.  
  77.             if(!OpenIFF(Handle,IFFF_WRITE))
  78.             {
  79.                     /* Say it's a formatted text form. */
  80.  
  81.                 if(!PushChunk(Handle,'FTXT','FORM',IFFSIZE_UNKNOWN))
  82.                 {
  83.                         /* Create the text chunk. */
  84.  
  85.                     if(!PushChunk(Handle,0,'CHRS',Size))
  86.                     {
  87.                             /* Write the actual data. */
  88.  
  89.                         if(WriteChunkBytes(Handle,Buffer,Size) == Size)
  90.                         {
  91.                                 /* Did the chunk get written correctly? */
  92.  
  93.                             if(!PopChunk(Handle))
  94.                                 Success = TRUE;
  95.                         }
  96.                     }
  97.                 }
  98.  
  99.                     /* Did our previous actions succeed? */
  100.  
  101.                 if(Success)
  102.                 {
  103.                         /* Did the chunk get written correctly? */
  104.  
  105.                     if(PopChunk(Handle))
  106.                         Success = FALSE;
  107.                 }
  108.  
  109.                     /* Close the iff stream. */
  110.  
  111.                 CloseIFF(Handle);
  112.             }
  113.  
  114.                 /* Flush the data out to the clipboard. */
  115.  
  116.             CloseClipboard((struct ClipboardHandle *)Handle -> iff_Stream);
  117.         }
  118.  
  119.             /* Free the IFFHandle data. */
  120.  
  121.         FreeIFF(Handle);
  122.     }
  123.  
  124.         /* Return whether our actions were successful or not. */
  125.  
  126.     return(Success);
  127. }
  128.  
  129.     /* LoadClip(UBYTE *Buffer,LONG Size):
  130.      *
  131.      *    Fill the buffer with up to `Size' bytes.
  132.      */
  133.  
  134. LONG
  135. LoadClip(UBYTE *Buffer,LONG Size)
  136. {
  137.     struct IFFHandle    *Handle;
  138.     LONG             Bytes = 0;
  139.  
  140.         /* Allocate an IFFHandle... */
  141.  
  142.     if(Handle = AllocIFF())
  143.     {
  144.             /* Make it operate on the clipboard. */
  145.  
  146.         if(Handle -> iff_Stream = (ULONG)OpenClipboard(PRIMARY_CLIP))
  147.         {
  148.                 /* Tell iffparse.library that it is to deal
  149.                  * with the clipboard, not with an AmigaDOS file.
  150.                  */
  151.  
  152.             InitIFFasClip(Handle);
  153.  
  154.                 /* Open the handle for reading. */
  155.  
  156.             if(!OpenIFF(Handle,IFFF_READ))
  157.             {
  158.                     /* Tell the parser to stop at the
  159.                      * beginning of character data
  160.                      * inside formatted text chunks.
  161.                      */
  162.  
  163.                 if(!StopChunk(Handle,'FTXT','CHRS'))
  164.                 {
  165.                         /* Start the parser and process
  166.                          * the data encountered.
  167.                          */
  168.  
  169.                     if(!ParseIFF(Handle,IFFPARSE_SCAN))
  170.                     {
  171.                         struct ContextNode *ContextNode;
  172.  
  173.                             /* Determine the current chunk information
  174.                              * (we will need it in order to query the
  175.                              * size of it).
  176.                              */
  177.  
  178.                         if(ContextNode = CurrentChunk(Handle))
  179.                         {
  180.                             LONG BytesRead;
  181.  
  182.                                 /* Don't read more data
  183.                                  * than we will be able
  184.                                  * to handle.
  185.                                  */
  186.  
  187.                             if(Size > ContextNode -> cn_Size)
  188.                                 Size = ContextNode -> cn_Size;
  189.  
  190.                                 /* Read as much data as required. */
  191.  
  192.                             BytesRead = ReadChunkBytes(Handle,Buffer,Size);
  193.  
  194.                                 /* If any data was available,
  195.                                  * remember how much we were
  196.                                  * able to read.
  197.                                  */
  198.  
  199.                             if(BytesRead > 0)
  200.                                 Bytes = BytesRead;
  201.                         }
  202.                     }
  203.                 }
  204.  
  205.                     /* Close the iff stream. */
  206.  
  207.                 CloseIFF(Handle);
  208.             }
  209.  
  210.                 /* Free clipboard resources. */
  211.  
  212.             CloseClipboard((struct ClipboardHandle *)Handle -> iff_Stream);
  213.         }
  214.  
  215.             /* Free the IFFHandle data. */
  216.  
  217.         FreeIFF(Handle);
  218.     }
  219.  
  220.         /* Return the number of bytes we were actually able to read. */
  221.  
  222.     return(Bytes);
  223. }
  224.