home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / commodities / newedit / clipboard.c < prev    next >
C/C++ Source or Header  |  1992-08-16  |  6KB  |  221 lines

  1. /*----------------------------------------------------------------------------
  2.    File   :    ClipBoard.c
  3.    Projekt:    NewEdit
  4.    Inhalt :    init_iffparse()
  5.                close_iffparse()
  6.                read_clip()
  7.                write_clip()
  8.  
  9.    Version:    1.5
  10.    Datum  :    16. August 1992
  11.  
  12.    Autor  :    Uwe Röhm
  13.    Adresse:    Wörthstr. 18,  W-8390 Passau
  14.    Bemerkung:
  15.    Funktionen zum Öffnen, Lesen, Schreiben und Schliessen des Clipboards
  16.    Unit 0 mittels der iffparse.library.
  17. ----------------------------------------------------------------------------*/
  18. #include <libraries/iffparse.h>
  19.  
  20. /* ------------------------  Prototypes & Pragmas  ------------------------ */
  21. #include <clib/dos_protos.h>
  22. #include <clib/exec_protos.h>
  23. #include <clib/iffparse_protos.h>
  24. #ifdef LATTICE
  25.    #include <pragmas/dos_pragmas.h>
  26.    #include <pragmas/exec_pragmas.h>
  27.    #include <pragmas/iffparse_pragmas.h>
  28. #endif
  29.  
  30. /* ---------------------------  eigene Defines  --------------------------- */
  31. #define NO_IFFHANDLE   "Couldn't allocate IFFHandle\n."
  32. #define NO_CLIPHANDLE  "Couldn't open Clipboard.\n"
  33. #define ERROR_LIBRARY  "Can't open library '%s' V %ld.\n"
  34. #define IFFNAME        "iffparse.library"
  35. #define ID_FTXT  MAKE_ID('F','T','X','T')
  36. #define ID_CHRS  MAKE_ID('C','H','R','S')
  37.  
  38. /* ----------------------------  globale Vars  ---------------------------- */
  39. extern struct Library *DOSBase;
  40. extern struct Library *SysBase;
  41. struct Library   *IFFParseBase = NULL;
  42. struct IFFHandle *Iff          = NULL;
  43.  
  44.  
  45. /****** init_iffparse() *****************************************************
  46. *
  47. *   NAME
  48. *        init_iffparse -- initialises iffparse.library for clipboard unit 0
  49. *   SYNOPSIS
  50. *        success = init_iffparse ()
  51. *
  52. *        BOOL      init_iffparse ( void );
  53. *   FUNCTION
  54. *        Allocates and initialises IFFHandle structure for Read/Write
  55. *        to/from clipboard.device unit 0. Must be matched with call to
  56. *        close_iffparse().
  57. *   INPUTS
  58. *
  59. *   RESULT
  60. *        success - TRUE if all done correct, else FALSE
  61. *   DATE
  62. *        16 Aug 1992
  63. *   SEE ALSO
  64. *        close_iffparse()
  65. *****************************************************************************
  66. */
  67. BOOL init_iffparse ( void )
  68. {
  69.    if ( (IFFParseBase = OpenLibrary(IFFNAME, 37L)) != NULL )
  70.    {
  71.       if ( (Iff = AllocIFF()) != NULL )
  72.       {
  73.          if ( (Iff->iff_Stream = (ULONG) OpenClipboard(0)) != NULL )
  74.          {
  75.             InitIFFasClip (Iff);
  76.             return TRUE;
  77.  
  78.          } /* if OpenClipboard() */
  79.          else
  80.             PutStr ( NO_CLIPHANDLE );
  81.  
  82.       } /* if AllocIFF () */
  83.       else
  84.          PutStr ( NO_IFFHANDLE );
  85.  
  86.    } /* if OpenLibrary() */
  87.    else
  88.       Printf ( ERROR_LIBRARY, IFFNAME, 37 );
  89.  
  90.    return FALSE;
  91. }
  92.  
  93. /****** close_iffparse() ****************************************************
  94. *
  95. *   NAME
  96. *        close_iffparse -- frees space allocated by init_iffparse
  97. *   SYNOPSIS
  98. *              close_iffparse ()
  99. *
  100. *        void  close_iffparse ( void );
  101. *   FUNCTION
  102. *        Opposite to init_iffparse().
  103. *   INPUTS
  104. *
  105. *   RESULT
  106. *
  107. *   DATE
  108. *        18. April 1992
  109. *   SEE ALSO
  110. *        init_iffparse()
  111. *****************************************************************************
  112. */
  113. void close_iffparse ( void )
  114. {
  115.    if (Iff != NULL  &&  Iff->iff_Stream != NULL)
  116.       CloseClipboard ((void *) Iff->iff_Stream);
  117.  
  118.    if (Iff != NULL)
  119.       FreeIFF (Iff);
  120.  
  121.    if (IFFParseBase != NULL)
  122.       CloseLibrary (IFFParseBase);
  123. }
  124.  
  125. /****** read_clip() *********************************************************
  126. *
  127. *   NAME
  128. *        read_clip -- read text from clipboard unit 0 into specified buffer
  129. *   SYNOPSIS
  130. *        Length = read_clip ( buffer, buflen )
  131. *
  132. *        ULONG    read_clip ( UBYTE *, ULONG )
  133. *   FUNCTION
  134. *        Fills the specified buffer with the text from clipboard unit 0
  135. *        upto buflen characters. The number of read chars are returned
  136. *        (may be 0, if clipboard is empty or contains no text).
  137. *        IMPORTANT:
  138. *        The iffparse.library must be open and the global IFFHandle Iff must
  139. *        be initialised. Use init_iffparse() before!!!
  140. *   INPUTS
  141. *        buffer - buffer to be filled
  142. *        buflen - length of the given buffer
  143. *   RESULT
  144. *        Length - actual read characters,
  145. *                 0 if clipboard was empty or contained no-text
  146. *   DATE
  147. *        12. August 1992
  148. *   SEE ALSO
  149. *        write_clip()  init_iffparse()
  150. *****************************************************************************
  151. */
  152. ULONG read_clip ( UBYTE *buffer, ULONG buflen )
  153. {
  154.    struct ContextNode  *cn;
  155.    ULONG length = 0;
  156.  
  157.    OpenIFF (Iff, IFFF_READ);
  158.    if ( StopChunk(Iff, ID_FTXT, ID_CHRS) == 0 )
  159.    {
  160.       while (ParseIFF (Iff, IFFPARSE_SCAN) == 0 )
  161.       {
  162.          cn = CurrentChunk(Iff);
  163.          if ( (cn) && (cn->cn_Type == ID_FTXT) && (cn->cn_ID == ID_CHRS) )
  164.          {
  165.             length = ReadChunkBytes(Iff, buffer, buflen);
  166.             break;
  167.  
  168.          } /* if cn ... */
  169.  
  170.       } /* while ParseIff() */
  171.  
  172.    } /* if stopchunk() */
  173.  
  174.    CloseIFF(Iff);
  175.    return length;
  176. }
  177.  
  178. /****** write_clip() ********************************************************
  179. *
  180. *   NAME
  181. *        write_clip -- write text into clipboard unit 0
  182. *   SYNOPSIS
  183. *              write_clip ( buffer, length )
  184. *
  185. *        void  write_clip ( UBYTE *, ULONG )
  186. *   FUNCTION
  187. *        Writes 'length' characters from the specified buffer into clipboard
  188. *        unit 0.
  189. *        IMPORTANT:
  190. *        The iffparse.library must be open and the global IFFHandle Iff must
  191. *        be initialised. Use init_iffparse() before!!!
  192. *   INPUTS
  193. *        buffer - buffer to be written
  194. *        length - number of chars to be written
  195. *   RESULT
  196. *
  197. *   DATE
  198. *        12. August 1992
  199. *   SEE ALSO
  200. *        read_clip()  init_iffparse()
  201. *****************************************************************************
  202. */
  203. void write_clip ( UBYTE *data, ULONG length )
  204. {
  205.    OpenIFF (Iff, IFFF_WRITE);
  206.    if( PushChunk(Iff, ID_FTXT, ID_FORM, IFFSIZE_UNKNOWN) == 0 )
  207.    {
  208.       if( PushChunk(Iff, 0, ID_CHRS, IFFSIZE_UNKNOWN) == 0 )
  209.       {
  210.          WriteChunkBytes(Iff, data, length);
  211.          PopChunk (Iff);
  212.  
  213.       } /* if PushChunk() */
  214.  
  215.       PopChunk (Iff);
  216.  
  217.    } /* if PushChunk() */
  218.  
  219.    CloseIFF(Iff);
  220. }
  221.