home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / amigalib / d993 / muiffr.lha / MUIFFR / src / clipftxt.c next >
Encoding:
C/C++ Source or Header  |  1994-04-05  |  6.4 KB  |  302 lines

  1. /*
  2.  *  clipftxt.c             © by Martin Steppler
  3.  *
  4.  */
  5.  
  6. #include "muiffr.h"
  7. #include "muiffr_locale.h"
  8.  
  9. #define RBUFSZ BUF_SIZE
  10.  
  11. #define  ID_FTXT        MAKE_ID('F','T','X','T')
  12. #define  ID_CHRS        MAKE_ID('C','H','R','S')
  13.  
  14. struct Library *IFFParseBase;
  15.  
  16. static int LoadBuf(UBYTE *buf, int rlen);
  17.  
  18. int CopyClip(void)
  19. {
  20.     int return_ok = FALSE;
  21.     struct IFFHandle *iff = NULL;
  22.     long error = 0, unitnumber = 0;
  23.     int textlen;
  24.  
  25.     // empty request list
  26.     if (!app->app_RequestList)
  27.         goto abort;
  28.  
  29.     if (!(IFFParseBase = OpenLibrary("iffparse.library", 0L)))
  30.     {
  31.         DispError(MSG_ERROR_CANT_OPEN, "iffparse.library");
  32.         goto abort;
  33.     }
  34.  
  35.     /*
  36.      * Allocate IFF_File structure.
  37.      */
  38.     if (!(iff = AllocIFF()))
  39.         goto abort;
  40.  
  41.     /*
  42.      * Set up IFF_File for Clipboard I/O.
  43.      */
  44.     if (!(iff->iff_Stream = (ULONG) OpenClipboard (unitnumber)))
  45.     {
  46.         DispError(MSG_ERROR_CANT_OPEN, "clipboard");
  47.         goto abort;
  48.     }
  49.  
  50.     InitIFFasClip (iff);
  51.  
  52.     /*
  53.      * Start the IFF transaction.
  54.      */
  55.     if (error = OpenIFF(iff, IFFF_WRITE))
  56.         goto abort;
  57.  
  58.     /*
  59.      * Write our text to the clipboard as CHRS chunk in FORM FTXT
  60.      *
  61.      * First, write the FORM ID (FTXT)
  62.      */
  63.     if (!(error = PushChunk(iff, ID_FTXT, ID_FORM, IFFSIZE_UNKNOWN)))
  64.     {
  65.         UBYTE *buf, *linefeed = "\n";
  66.         LONG pos = -1;
  67.  
  68.         /* Now the CHRS chunk ID followed by the chunk data
  69.          */
  70.         if (error = PushChunk(iff, 0, ID_CHRS, IFFSIZE_UNKNOWN))
  71.             goto abort;
  72.  
  73.         do
  74.         {
  75.             DoMethod(app->app_lv_Main_RequestList, MUIM_List_NextSelected, &pos);
  76.  
  77.             if (pos >= 0)
  78.             {
  79.                 DoMethod(app->app_lv_Main_RequestList, MUIM_List_GetEntry, pos, &buf);
  80.  
  81.                 /* Now the actual data (the text) */
  82.                 textlen = strlen(buf);
  83.                 if (WriteChunkBytes(iff, buf, textlen) != textlen)
  84.                     error = IFFERR_WRITE;
  85.                 if (!error)
  86.                     // append linefeed
  87.                     if (WriteChunkBytes(iff, linefeed, 1) != 1)
  88.                         error = IFFERR_WRITE;
  89.             }
  90.         }
  91.         while (!error && pos >= 0);
  92.  
  93.         if (!error)
  94.             error = PopChunk(iff);
  95.     }
  96.     if(!error)
  97.         error = PopChunk(iff);
  98.  
  99.     return_ok = TRUE;
  100. abort:
  101.     if (iff)
  102.     {
  103.         /*
  104.          * Terminate the IFF transaction with the stream.  Free
  105.          * all associated structures.
  106.          */
  107.         CloseIFF(iff);
  108.  
  109.         /*
  110.          * Close the clipboard stream
  111.          */
  112.         if (iff->iff_Stream)
  113.             CloseClipboard((struct ClipboardHandle *) iff->iff_Stream);
  114.  
  115.         /*
  116.          * Free the IFF_File structure itself.
  117.          */
  118.         FreeIFF(iff);
  119.     }
  120.     if (IFFParseBase)
  121.     {
  122.         CloseLibrary (IFFParseBase);
  123.         IFFParseBase = NULL;
  124.     }
  125.     return (return_ok);
  126. }
  127.  
  128. int CutClip(void)
  129. {
  130.     int return_ok = FALSE;
  131.  
  132.     if (!CopyClip())
  133.         goto abort;
  134.  
  135.     DeleteListEntry();
  136.  
  137.     return_ok = TRUE;
  138. abort:
  139.     return (return_ok);
  140. }
  141.  
  142. int PasteClip(void)
  143. {
  144.     int return_ok = FALSE;
  145.     struct IFFHandle *iff = NULL;
  146.     long error = 0, unitnumber = 0;
  147.     struct ContextNode  *cn;
  148.     int rlen;
  149.     UBYTE buf[RBUFSZ];
  150.  
  151.  
  152.     if (!(IFFParseBase = OpenLibrary("iffparse.library", 0L)))
  153.     {
  154.         DispError(MSG_ERROR_CANT_OPEN, "iffparse.library");
  155.         goto abort;
  156.     }
  157.  
  158.     /*
  159.      * Allocate IFF_File structure.
  160.      */
  161.     if (!(iff = AllocIFF ()))
  162.         goto abort;
  163.  
  164.     /*
  165.      * Set up IFF_File for Clipboard I/O.
  166.      */
  167.     if (!(iff->iff_Stream = (ULONG) OpenClipboard (unitnumber)))
  168.     {
  169.         DispError(MSG_ERROR_CANT_OPEN, "clipboard");
  170.         goto abort;
  171.     }
  172.  
  173.     InitIFFasClip (iff);
  174.  
  175.     /*
  176.      * Start the IFF transaction.
  177.      */
  178.     if (error = OpenIFF(iff, IFFF_READ))
  179.         goto abort;
  180.  
  181.     /* Tell iffparse we want to stop on FTXT CHRS chunks */
  182.     if (error = StopChunk(iff, ID_FTXT, ID_CHRS))
  183.         goto abort;
  184.  
  185.     /* Find all of the FTXT CHRS chunks */
  186.     while (1)
  187.     {
  188.         error = ParseIFF(iff, IFFPARSE_SCAN);
  189.         /* enter next context */
  190.         if (error == IFFERR_EOC)
  191.             continue;
  192.         else if (error)
  193.             break;
  194.  
  195.         /* We only asked to stop at FTXT CHRS chunks
  196.          * If no error we've hit a stop chunk
  197.          * Read the CHRS chunk data
  198.          */
  199.         cn = CurrentChunk(iff);
  200.  
  201.         if (cn && cn->cn_Type == ID_FTXT && cn->cn_ID == ID_CHRS)
  202.         {
  203.             while ((rlen = ReadChunkBytes(iff, buf, RBUFSZ)) > 0)
  204.             {
  205.                 if (!LoadBuf(buf, rlen))
  206.                     goto abort;
  207.             }
  208.  
  209.             if (rlen < 0)
  210.                 error = rlen;
  211.         }
  212.     }
  213.     return_ok = TRUE;
  214. abort:
  215.     if (iff)
  216.     {
  217.         /*
  218.          * Terminate the IFF transaction with the stream.  Free
  219.          * all associated structures.
  220.          */
  221.         CloseIFF(iff);
  222.  
  223.         /*
  224.          * Close the clipboard stream
  225.          */
  226.         if (iff->iff_Stream)
  227.             CloseClipboard((struct ClipboardHandle *) iff->iff_Stream);
  228.  
  229.         /*
  230.          * Free the IFF_File structure itself.
  231.          */
  232.         FreeIFF(iff);
  233.     }
  234.     if (IFFParseBase)
  235.     {
  236.         CloseLibrary (IFFParseBase);
  237.         IFFParseBase = NULL;
  238.     }
  239.     return (return_ok);
  240. }
  241.  
  242. static int LoadBuf(UBYTE *buf, int rlen)
  243. {
  244.     int return_ok = FALSE;
  245.     int pos, rpos;
  246.     UBYTE rbuf[RBUFSZ + 1];
  247.  
  248.     pos = 0;
  249.  
  250.     while (pos < rlen)
  251.     {
  252.         *rbuf = EOS;
  253.         rpos = 0;
  254.         do
  255.         {
  256.             if ((buf[pos] >= 32 && buf[pos] <= 127) || buf[pos] >= 160)
  257.                 rbuf[rpos++] = buf[pos];
  258.             // tab
  259.             else if (buf[pos] == 9)
  260.             {
  261.                 do
  262.                 {
  263.                     rbuf[rpos++] = ' ';
  264.                 }
  265.                 while ((rpos & 7) && rpos < RBUFSZ);
  266.             }
  267.             // linefeed
  268.             else if (buf[pos] == 10)
  269.             {
  270.                 pos++;
  271.                 break;
  272.             }
  273.         } while (rpos < RBUFSZ && ++pos < rlen);
  274.  
  275.         if (rpos)
  276.             rbuf[rpos--] = EOS;
  277.  
  278.         // remove spaces
  279.         while (1)
  280.         {
  281.             if (rbuf[rpos] == ' ')
  282.             {
  283.                 rbuf[rpos--] = EOS;
  284.                 if (rpos < 0)
  285.                     rpos = 0;
  286.             }
  287.             else
  288.                 break;
  289.         }
  290.  
  291.         if (*rbuf)
  292.             rpos++;
  293.  
  294.         if (!AddListEntry(rbuf, FALSE))
  295.             goto abort;
  296.     }
  297.  
  298.     return_ok = TRUE;
  299. abort:
  300.     return (return_ok);
  301. }
  302.