home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / xdme_1.84_src.lha / XDME / Src / clipboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-14  |  10.4 KB  |  539 lines

  1. /******************************************************************************
  2.  
  3.     MODUL
  4.     $Id: clipboard.c 1.1 1994/08/14 12:30:15 digulla Exp digulla $
  5.  
  6.     DESCRIPTION
  7.     Provide standard clipboard.device interface functions like Open,
  8.     Close, Read, Write, etc.
  9.  
  10.     This is typed from "Amiga ROM Kernel Reference Manual: Devices"
  11.     Page 50 and following.
  12.  
  13.     NOTES
  14.     These functions are usefull for writing and reading simple FTXT.
  15.     Writing and reading complex FTXT, ILBM, etc. requires more work.
  16.     Under 2.0 it is highly recommended that you use iffparse.library.
  17.  
  18.     HISTORY
  19.     29. May 1992    ada created
  20.     $Log: clipboard.c $
  21.  * Revision 1.1  1994/08/14  12:30:15  digulla
  22.  * Initial revision
  23.  *
  24.  
  25. ******************************************************************************/
  26.  
  27. /* Includes */
  28. #include <exec/types.h>
  29. #include <exec/ports.h>
  30. #include <exec/io.h>
  31. #include <exec/memory.h>
  32. #include <devices/clipboard.h>
  33. #include "clipboard.h"
  34. #include <clib/exec_protos.h>
  35. #include <clib/alib_protos.h>
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <pragmas/exec_pragmas.h>
  40.  
  41. /* Globale Variable */
  42.  
  43. /* Interne Defines & Strukturen */
  44. /*#define DEBUG*/
  45.  
  46. /* Interne Variable */
  47.  
  48. /* Interne Prototypes */
  49. static int          ReadLong     (struct IOClipReq *, ULONG *);
  50. static struct cbbuf * FillCBData (struct IOClipReq *, ULONG);
  51.  
  52.  
  53. /*****************************************************************************
  54.  
  55.     NAME
  56.     CBOpen - Open the clipboard.device
  57.  
  58.     PARAMETER
  59.     ULONG unit;
  60.  
  61.     RESULT
  62.  
  63.     RETURN
  64.     struct IOClipReq * ior;     Initialized IOClipReq-structure or NULL
  65.  
  66.     DESCRIPTION
  67.     Opens the clipboard.device. A clipboard unit number must be passed in
  68.     as an argument. By default, the unit number should be 0 (currently
  69.     valid unit numbers are 0-255).
  70.  
  71.     NOTES
  72.  
  73.     BUGS
  74.  
  75.     EXAMPLES
  76.  
  77.     SEE ALSO
  78.  
  79.     INTERNALS
  80.  
  81.     HISTORY
  82.     29. May 1992    ada created
  83.  
  84. ******************************************************************************/
  85.  
  86. struct IOClipReq * CBOpen (ULONG unit)
  87. {
  88.     struct MsgPort * mp;
  89.     struct IOStdReq * ior;
  90.  
  91.     if (mp = CreatePort (NULL, NULL)) {
  92.     if (ior = (struct IOStdReq *)CreateExtIO (mp,
  93.         sizeof (struct IOClipReq))) {
  94.         if (!(OpenDevice ((STRPTR)"clipboard.device", unit,
  95.             (struct IORequest *)ior, NULL))) {
  96. #ifdef DEBUG
  97.     printf ("Open of clipboard.device unit %d successful\n", unit);
  98. #endif
  99.         return ((struct IOClipReq *)ior);
  100.         }
  101.     }
  102.     }
  103.  
  104. #ifdef DEBUG
  105.     printf ("No clipboard.device unit %d\n", unit);
  106. #endif
  107.  
  108.     return (NULL);
  109. } /* CBOpen */
  110.  
  111.  
  112. /*****************************************************************************
  113.  
  114.     NAME
  115.     CBClose - Close the clipboard.device
  116.  
  117.     PARAMETER
  118.     struct IOClipReq * ior;
  119.  
  120.     RESULT
  121.  
  122.     RETURN
  123.     void
  124.  
  125.     DESCRIPTION
  126.     Close the clipboard.device unit which was opened via CBOpen().
  127.  
  128.     NOTES
  129.  
  130.     BUGS
  131.  
  132.     EXAMPLES
  133.  
  134.     SEE ALSO
  135.  
  136.     INTERNALS
  137.  
  138.     HISTORY
  139.     29. May 1992    ada created
  140.  
  141. ******************************************************************************/
  142.  
  143. void CBClose (struct IOClipReq * ior)
  144. {
  145.     struct MsgPort * mp;
  146.  
  147.     mp = ior->io_Message.mn_ReplyPort;
  148.  
  149.     CloseDevice ((struct IORequest *)ior);
  150.     DeleteExtIO ((struct IORequest *)ior);
  151.     DeletePort (mp);
  152. } /* CBClose */
  153.  
  154.  
  155. /*****************************************************************************
  156.  
  157.     NAME
  158.     CBWriteFTXT - Write a string of text to the clipboard
  159.  
  160.     PARAMETER
  161.     struct IOClipReq * ior;
  162.     char         * string;
  163.  
  164.     RESULT
  165.  
  166.     RETURN
  167.     int success;        TRUE/FALSE
  168.  
  169.     DESCRIPTION
  170.     Write a NULL terminated string of text to the clipboard. The string
  171.     will be written in simple FTXT format.
  172.  
  173.     Note that this function pads odd length strings automatically to
  174.     conform to the IFF standard.
  175.  
  176.     NOTES
  177.  
  178.     BUGS
  179.  
  180.     EXAMPLES
  181.  
  182.     SEE ALSO
  183.  
  184.     INTERNALS
  185.  
  186.     HISTORY
  187.     29. May 1992    ada created
  188.  
  189. ******************************************************************************/
  190.  
  191. int CBWriteFTXT (struct IOClipReq * ior, char * string)
  192. {
  193.     ULONG length;    /* EVEN string-length */
  194.     ULONG slen;     /* length of string */
  195.     BOOL  odd;        /* ODD(slen) */
  196.     static ULONG IFF_header[] = {
  197.     ID_FORM,
  198.     0,
  199.     ID_FTXT,
  200.     ID_CHRS,
  201.     0
  202.     };
  203.  
  204.     slen = strlen (string);
  205.     odd  = slen & 1;
  206.  
  207.     length = odd ? slen+1 : slen;
  208.  
  209.     /* initial setup for Offset, Error and ClipID */
  210.     ior->io_Offset  = 0;
  211.     ior->io_Error   = 0;
  212.     ior->io_ClipID  = 0;
  213.  
  214.     /* Create the IFF-header information and write it */
  215.     IFF_header[1] = length + 12;
  216.     IFF_header[4] = slen;
  217.  
  218.     ior->io_Data    = (STRPTR)IFF_header;
  219.     ior->io_Length  = sizeof (IFF_header);
  220.     ior->io_Command = CMD_WRITE;
  221.     DoIO ((struct IORequest *)ior);
  222.  
  223.     /* write string itself */
  224.     ior->io_Data    = (STRPTR)string;
  225.     ior->io_Length  = length;           /* We can write slen+1 Bytes since
  226.                       a C-String always has a '\0'-Byte
  227.                       at his end. */
  228.     ior->io_Command = CMD_WRITE;
  229.     DoIO ((struct IORequest *)ior);
  230.  
  231.     /* Tell the clipboard we are done writing */
  232.     ior->io_Command = CMD_UPDATE;
  233.     DoIO ((struct IORequest *)ior);
  234.  
  235.     return (ior->io_Error ? FALSE : TRUE);
  236. } /* CBWriteFTXT */
  237.  
  238.  
  239. /*****************************************************************************
  240.  
  241.     NAME
  242.     CBQueryFTXT
  243.  
  244.     PARAMETER
  245.     struct IOClipReq * ior;
  246.  
  247.     RESULT
  248.  
  249.     RETURN
  250.     int result;        TRUE/FALSE
  251.  
  252.     DESCRIPTION
  253.     Check if the clipboard contains a FTXT.
  254.  
  255.     NOTES
  256.     If this function returns TRUE, you MUST either call CBReadCHRS()
  257.     until it returns FALSE or CBReadDone() to tell the clipboard.device
  258.     that we are done reading.
  259.  
  260.     BUGS
  261.  
  262.     EXAMPLES
  263.  
  264.     SEE ALSO
  265.  
  266.     INTERNALS
  267.  
  268.     HISTORY
  269.     29. May 1992    ada created
  270.  
  271. ******************************************************************************/
  272.  
  273. int CBQueryFTXT (struct IOClipReq * ior)
  274. {
  275.     ULONG cbuff[3];
  276.  
  277.     /* initial setup for Offset, Error and ClipID */
  278.     ior->io_Offset  = 0;
  279.     ior->io_Error   = 0;
  280.     ior->io_ClipID  = 0;
  281.  
  282.     /* Look for "FORM[size]FTXT" */
  283.     ior->io_Data    = (STRPTR)cbuff;
  284.     ior->io_Length  = 12;        /* 3*4 Bytes */
  285.     ior->io_Command = CMD_READ;
  286.     DoIO ((struct IORequest *)ior);
  287.  
  288.     /* Have we at least 12 bytes and a valid IFF-Header ? */
  289.     if (ior->io_Actual == 12L && cbuff[0] == ID_FORM && cbuff[2] == ID_FTXT) {
  290. #ifdef DEBUG
  291.     printf ("FTXT ok\n");
  292. #endif
  293.     return (TRUE);
  294.     }
  295.  
  296. #ifdef DEBUG
  297.     printf ("No FTXT\n");
  298. #endif
  299.  
  300.     CBReadDone (ior);
  301.     return (FALSE);
  302. } /* CBQueryFTXT */
  303.  
  304.  
  305. /*****************************************************************************
  306.  
  307.     NAME
  308.     CBReadCHRS
  309.  
  310.     PARAMETER
  311.     struct IOClipReq * ior;
  312.  
  313.     RESULT
  314.  
  315.     RETURN
  316.     struct cbbuf * cbbuf;        NULL on failure (out-of-memory or
  317.                     no more CHRS-chunks).
  318.  
  319.     DESCRIPTION
  320.     Reads and returns the text in the next CHRS chunk (if any) from the
  321.     clipboard.
  322.  
  323.     Allocates memory to hold data in next CHRS chunk.
  324.  
  325.     NOTES
  326.     The caller must free the returned buffer when done via CBFreeBuf().
  327.  
  328.     BUGS
  329.  
  330.     EXAMPLES
  331.  
  332.     SEE ALSO
  333.  
  334.     INTERNALS
  335.  
  336.     HISTORY
  337.     29. May 1992    ada created
  338.  
  339. ******************************************************************************/
  340.  
  341. struct cbbuf * CBReadCHRS (struct IOClipReq * ior)
  342. {
  343.     ULONG chunk;
  344.     ULONG size;
  345.     struct cbbuf * buf;
  346.  
  347.     /* Find next CHRS chunk */
  348.     buf = NULL;
  349.  
  350.     for (;;) {
  351.     if (!(ReadLong (ior, &chunk)) )
  352.         break;
  353.  
  354.     if (!(ReadLong (ior, &size)) )
  355.         break;
  356.  
  357.     /* if it is a CHRS-hunk */
  358.     if (chunk == ID_CHRS) {
  359. #ifdef DEBUG
  360.     printf ("Found CHRS\n");
  361. #endif
  362. #ifdef DEBUG
  363.     printf ("CHRS length = %d\n", size);
  364. #endif
  365.         if (size) { /* Read data */
  366.         buf = FillCBData (ior, size);
  367.         break;
  368.         }
  369.     } else { /* else skip this hunk */
  370.         if (size & 1)
  371.         size ++;
  372.  
  373.         ior->io_Offset += size;
  374.     }
  375.     }
  376.  
  377.     /* tell clipboard we are done */
  378.     if (!buf)
  379.     CBReadDone (ior);
  380.  
  381. #ifdef DEBUG
  382.     printf ("Found '%s'\n", buf->mem);
  383. #endif
  384.  
  385.     return (buf);
  386. } /* CBReadCHRS */
  387.  
  388.  
  389. static int ReadLong (struct IOClipReq * ior, ULONG * ldata)
  390. {
  391.     ior->io_Data    = (STRPTR)ldata;
  392.     ior->io_Length  = 4;
  393.     ior->io_Command = CMD_READ;
  394.  
  395.     DoIO ((struct IORequest *)ior);
  396.  
  397.     if (ior->io_Actual == 4 && !ior->io_Error)
  398.     return (TRUE);
  399.  
  400.     return (FALSE);
  401. }
  402.  
  403.  
  404. static struct cbbuf * FillCBData (struct IOClipReq * ior, ULONG size)
  405. {
  406.     register UBYTE * to;
  407.     register UBYTE * from;
  408.     register ULONG   t;
  409.     register ULONG   count;
  410.     struct cbbuf   * buf;
  411.  
  412.     if (!size)
  413.     return (NULL);
  414.  
  415.     if (size & 1)
  416.     size ++;
  417.  
  418.     if (buf = AllocMem (sizeof (struct cbbuf) + size, MEMF_PUBLIC)) {
  419.     buf->size = sizeof (struct cbbuf) + size;
  420.  
  421.     ior->io_Data    = (STRPTR)buf->mem;
  422.     ior->io_Length    = size;
  423.     ior->io_Command = CMD_READ;
  424.  
  425.     to = (UBYTE *)buf->mem;
  426.     count = NULL;
  427.  
  428.     if (!(DoIO ((struct IORequest *)ior)) && ior->io_Actual == size) {
  429.         for (t=0, from=to; t<size; t++) {
  430.         if (*from) {
  431.             *to ++ = * from;
  432.             count ++;
  433.         }
  434.  
  435.         from ++;
  436.         } /* for all bytes in the buffer */
  437.  
  438.         *to = 0;    /* terminate buffer with 0 */
  439.         buf->count = count;
  440.     } else { /* if DoIO && number of bytes */
  441.         FreeMem (buf, buf->size);
  442.         return (NULL);
  443.     }
  444.     }
  445.  
  446.     return (buf);
  447. } /* FillCBData */
  448.  
  449.  
  450. /*****************************************************************************
  451.  
  452.     NAME
  453.     CBReadDone - Tell clipboard we are done reading
  454.  
  455.     PARAMETER
  456.     struct IOClipReq * ior;
  457.  
  458.     RESULT
  459.  
  460.     RETURN
  461.     void
  462.  
  463.     DESCRIPTION
  464.     Reads past end of clipboard file until io_Actual is equal to 0. This
  465.     tells the clipboard that we are done reading.
  466.  
  467.     NOTES
  468.  
  469.     BUGS
  470.  
  471.     EXAMPLES
  472.  
  473.     SEE ALSO
  474.  
  475.     INTERNALS
  476.  
  477.     HISTORY
  478.     29. May 1992    ada created
  479.  
  480. ******************************************************************************/
  481.  
  482. void CBReadDone (struct IOClipReq * ior)
  483. {
  484.     char tmp_buffer[64];
  485.  
  486.     ior->io_Data    = (STRPTR)tmp_buffer;
  487.     ior->io_Length  = sizeof(tmp_buffer);
  488.     ior->io_Command = CMD_READ;
  489.  
  490.     /* falls through immediately if io_Actual == 0 */
  491.     while (ior->io_Actual) {
  492.     if (DoIO ((struct IORequest *)ior))
  493.         break;
  494.     }
  495.  
  496. } /* CBReadDone */
  497.  
  498.  
  499. /*****************************************************************************
  500.  
  501.     NAME
  502.     CBFreeBuf - Free buffer allocated by CBReadCHRS()
  503.  
  504.     PARAMETER
  505.     struct cbbuf * buf;
  506.  
  507.     RESULT
  508.  
  509.     RETURN
  510.     void
  511.  
  512.     DESCRIPTION
  513.     Frees a buffer allocated by CBReadCHRS().
  514.  
  515.     NOTES
  516.  
  517.     BUGS
  518.  
  519.     EXAMPLES
  520.  
  521.     SEE ALSO
  522.  
  523.     INTERNALS
  524.  
  525.     HISTORY
  526.     29. May 1992    ada created
  527.  
  528. ******************************************************************************/
  529.  
  530. void CBFreeBuf (struct cbbuf * buf)
  531. {
  532.     FreeMem (buf, buf->size);
  533. } /* CBFreeBuf */
  534.  
  535.  
  536. /******************************************************************************
  537. *****  ENDE clipboard.c
  538. ******************************************************************************/
  539.