home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / d / gstream / Example < prev    next >
Encoding:
Text File  |  1993-05-08  |  2.2 KB  |  93 lines

  1. Example code
  2. ------------
  3.  
  4. Here is a little bit of code to show how gstream can be used.  It should
  5. be regsistered with as a handler for win_ICONBARLOAD
  6.  
  7. What it does is when a file is dragged to the app, the function will
  8. use gsopenxferrecv to open a read stream (thus responding to the
  9. dataload or datasave message), then open an internal write stream, and
  10. copy the incoming data into the internal stream.
  11.  
  12. After closing the internal write stream and the xferrecv stream, it
  13. opens a xfersend stream (using savebox()) and opens the internal buffer,
  14. copying the data from the internal buffer to the write stream.  I bet
  15. that's a *lot* simpler than it is using RISC_OSLib's xfersend, xferrecv
  16. and saveas modules. :-)
  17.  
  18. static void icon_load_handler(wimp_eventstr *e, void *handle)
  19. {
  20.     gstream    stream,
  21.         internal_buffer;
  22.     int    safe,
  23.         type,
  24.         load_succeded=TRUE;
  25.     char    name[256]="File";
  26.  
  27.     switch(e->data.msg.hdr.action)
  28.     {
  29.         case wimp_MDATALOAD:
  30.             strcpy(name,e->data.msg.data.dataload.name);
  31.             type=e->data.msg.data.dataload.type;
  32.             break;
  33.         case wimp_MDATASAVE:
  34.             strcpy(name,e->data.msg.data.datasave.leaf);
  35.             type=e->data.msg.data.datasave.type;
  36.             break;
  37.     }
  38.  
  39.     visdelay_begin();
  40.     if (gsopenxferrecv(e,&stream))
  41.     {
  42.         if (gsopeninternalw(&internal_buffer))
  43.         {
  44.             char dummy[256];
  45.             int c;
  46.             while((c=gsread((void *) dummy, 1, 256, &stream))!=0)
  47.             {
  48.                 gswrite(dummy,1,c,&internal_buffer);
  49.                 if (gserror(&stream) || gserror(&internal_buffer))
  50.                 {
  51.                     werr(FALSE,"Error in load");
  52.                     break;
  53.                 }
  54.             }
  55.             gsclose(&internal_buffer);
  56.             if (gserror(&stream) || gserror(&internal_buffer))
  57.             {
  58.                 load_succeded=FALSE;
  59.                 gsdiscardinternal(&internal_buffer);
  60.             }
  61.         }
  62.         else
  63.             load_succeded=FALSE;
  64.         gsclose(&stream);
  65.     }
  66.     else
  67.         load_succeded=FALSE;
  68.     visdelay_end();
  69.  
  70.     if (load_succeded)
  71.     {
  72.         if (gsopeninternalr(&internal_buffer))
  73.         {
  74.             if (savebox(name,1024,type,&safe,&stream))
  75.             {
  76.                 char dummy[256];
  77.                 int c;
  78.                 visdelay_begin();
  79.                 while((c=gsread((void *) dummy, 1, 256, &internal_buffer))!=0)
  80.                 {
  81.                     gswrite(dummy,1,c,&stream);
  82.                     if (gserror(&stream))
  83.                         break;
  84.                 }
  85.                 visdelay_end();
  86.                 gsclose(&stream);
  87.             }
  88.             gsclose(&internal_buffer);
  89.         }
  90.         gsdiscardinternal(&internal_buffer);
  91.     }
  92. }
  93.