home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Atlanta_1990 / Atlanta-Devcon.2 / Libraries / IFFParse / Examples / wiff2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  4.9 KB  |  241 lines

  1. /*  :ts=8 bk=0
  2.  *
  3.  * wiff2.c:    Test IFF write program.  Writes junk, really.
  4.  *
  5.  * Usage: wiff <file | "-c">
  6.  *
  7.  * Writes a demo IFF file to the specified stream -- either a DOS file or
  8.  * the clipboard's primary clip.
  9.  *
  10.  * Stuart Ferguson (created)                8809.01
  11.  * ewhac: Fixed bugs, added testing/illustration of
  12.  *      client-supplied sizes.            8811.02
  13.  * ewhac: Updated to 1.4 Beta.                8912.06
  14.  * ewhac: Latticeification                9005.31
  15.  */
  16. #include <exec/types.h>
  17. #include <libraries/dos.h>
  18. #include <libraries/iffparse.h>
  19. #include <clib/exec_protos.h>
  20. #include <clib/dos_protos.h>
  21. #include "iffparse_protos.h"
  22. #include "iffparse.p"
  23.  
  24. #define    ID_ILBM    MAKE_ID('I','L','B','M')
  25. #define    ID_BMHD    MAKE_ID('B','M','H','D')
  26. #define    ID_BODY    MAKE_ID('B','O','D','Y')
  27. #define    ID_CRNG    MAKE_ID('C','R','N','G')
  28. #define    ID_TEST    MAKE_ID('T','E','S','T')
  29.  
  30.  
  31. /*
  32.  * Forward function declarations.  (I hate ANSI.)
  33.  */
  34. LONG PropOut (struct IFFHandle *);
  35. LONG FormOut1 (struct IFFHandle *);
  36. LONG FormOut2 (struct IFFHandle *);
  37.  
  38.  
  39. struct Library    *IFFParseBase;
  40.  
  41.  
  42. main (argc, argv)
  43. int    argc;
  44. char    **argv;
  45. {
  46.     struct IFFHandle    *iff = NULL;
  47.     long            error;
  48.     short            cbio;
  49.  
  50.     if (argc < 2) {
  51.         printf ("usage: %s <outfile | \"-c\">\n", argv[0]);
  52.         goto die;
  53.     }
  54.  
  55.     cbio = (argv[1][0] == '-' && argv[1][1] == 'c');
  56.  
  57.     if (!(IFFParseBase = OpenLibrary ("iffparse.library", 0L))) {
  58.         puts ("Cannot open library.");
  59.         goto die;
  60.     }
  61.  
  62.     if (!(iff = AllocIFF())) {
  63.         puts ("AllocIFF() failed.");
  64.         goto die;
  65.     }
  66.  
  67.     if (cbio) {
  68.         if (!(iff->iff_Stream =
  69.                 (ULONG) OpenClipboard (PRIMARY_CLIP)))
  70.         {
  71.             puts ("Clipboard open failed.");
  72.             goto die;
  73.         }
  74.         InitIFFasClip (iff);
  75.     } else {
  76.         if (!(iff->iff_Stream = Open (argv[1], MODE_NEWFILE))) {
  77.             puts ("File open failed.");
  78.             goto die;
  79.         }
  80.         InitIFFasDOS (iff);
  81.     }
  82.  
  83.     if (error = OpenIFF (iff, IFFF_WRITE)) {
  84.         puts ("OpenIFF failed.");
  85.         goto die;
  86.     }
  87.  
  88.     if (error = PushChunk (iff, ID_TEST, ID_LIST, IFFSIZE_UNKNOWN)) {
  89.         puts ("Initial PushChunk() failed.");
  90.         goto die;
  91.     }
  92.     if (error = PropOut (iff)) {
  93.         puts ("PROP out failed.");
  94.         goto die;
  95.     }
  96.     if (error = FormOut1 (iff)) {
  97.         puts ("First FORM out died.");
  98.         goto die;
  99.     }
  100.     if (error = FormOut2 (iff)) {
  101.         puts ("Second FORM out died.");
  102.         goto die;
  103.     }
  104.     if (error = PopChunk (iff))
  105.         puts ("Final PopChunk() failed.");
  106.  
  107. die:
  108.     if (error)
  109.         printf ("error code: %ld\n", error);
  110.  
  111.     if (iff) {
  112.         CloseIFF (iff);
  113.  
  114.         if (iff->iff_Stream)
  115.             if (cbio)
  116.                 CloseClipboard ((struct ClipboardHandle *)
  117.                         iff->iff_Stream);
  118.             else
  119.                 Close (iff->iff_Stream);
  120.  
  121.         FreeIFF (iff);
  122.     }
  123.  
  124.     if (IFFParseBase)    CloseLibrary (IFFParseBase);
  125. }
  126.  
  127.  
  128. LONG
  129. PropOut (iff)
  130. struct IFFHandle *iff;
  131. {
  132.     LONG    error;
  133.  
  134.     if (error = PushChunk (iff, ID_ILBM, ID_PROP, IFFSIZE_UNKNOWN))
  135.         return (error);
  136.  
  137.     if (error = PushChunk (iff, 0L, ID_BMHD, IFFSIZE_UNKNOWN))
  138.         return (error);
  139.     if (WriteChunkBytes (iff, (APTR) "This is the BMHD.......", 20L) != 20)
  140.         return (10);
  141.     if (error = PopChunk (iff))
  142.         return (error);
  143.  
  144.     if (error = PushChunk (iff, 0L, ID_CRNG, IFFSIZE_UNKNOWN))
  145.         return (error);
  146.     if (WriteChunkBytes (iff, (APTR) "a", 1L) != 1)
  147.         return (11);
  148.     if (error = PopChunk (iff))
  149.         return (error);
  150.  
  151.     if (error = PushChunk (iff, 0L, ID_CRNG, IFFSIZE_UNKNOWN))
  152.         return (error);
  153.     if (WriteChunkBytes (iff, (APTR) "ab", 2L) != 2)
  154.         return (12);
  155.     if (error = PopChunk (iff))
  156.         return (error);
  157.  
  158.     return (PopChunk (iff));
  159. }
  160.  
  161.  
  162. LONG
  163. FormOut1 (iff)
  164. struct IFFHandle *iff;
  165. {
  166.     LONG    error;
  167.  
  168.     if (error = PushChunk (iff, ID_ILBM, ID_FORM, IFFSIZE_UNKNOWN))
  169.         return (error);
  170.  
  171.     if (error = PushChunk (iff, 0L, ID_BODY, IFFSIZE_UNKNOWN))
  172.         return (error);
  173.     if (WriteChunkBytes (iff, (APTR) "I'm the Body.  Hi.", 18L) != 18)
  174.         return (20);
  175.     if (error = PopChunk (iff))
  176.         return (error);
  177.  
  178.     if (error = PushChunk (iff, 0L, ID_CRNG, IFFSIZE_UNKNOWN))
  179.         return (error);
  180.     if (WriteChunkBytes (iff, (APTR) "abc", 3L) != 3)
  181.         return (21);
  182.     if (error = PopChunk (iff))
  183.         return (error);
  184.  
  185.     if (error = PushChunk (iff, 0L, ID_CRNG, IFFSIZE_UNKNOWN))
  186.         return (error);
  187.     if (WriteChunkBytes (iff, (APTR) "abcd", 4L) != 4)
  188.         return (22);
  189.     if (error = PopChunk (iff))
  190.         return (error);
  191.  
  192.     return (PopChunk (iff));
  193. }
  194.  
  195.  
  196. LONG
  197. FormOut2 (iff)
  198. struct IFFHandle *iff;
  199. {
  200.     LONG    error;
  201.  
  202.     if (error = PushChunk (iff, ID_ILBM, ID_FORM, IFFSIZE_UNKNOWN))
  203.         return (error);
  204.  
  205.     if (error = PushChunk (iff, 0L, ID_CRNG, IFFSIZE_UNKNOWN))
  206.         return (error);
  207.     if (WriteChunkBytes (iff, (APTR) "abcde", 5L) != 5)
  208.         return (30);
  209.     if (error = PopChunk (iff))
  210.         return (error);
  211.  
  212.     if (error = PushChunk (iff, 0L, ID_CRNG, IFFSIZE_UNKNOWN))
  213.         return (error);
  214.     if (WriteChunkBytes (iff, (APTR) "abcdef", 6L) != 6)
  215.         return (31);
  216.     if (error = PopChunk (iff))
  217.         return (error);
  218.  
  219.     if (error = PushChunk (iff, 0L, ID_BODY, IFFSIZE_UNKNOWN))
  220.         return (error);
  221.     if (WriteChunkBytes (iff, (APTR) "I'm the Body.  Hi.", 18L) != 18)
  222.         return (32);
  223.     if (error = PopChunk (iff))
  224.         return (error);
  225.  
  226.     return (PopChunk (iff));
  227. }
  228.  
  229. /*
  230.  * Disable Lattice's default ^C trap.
  231.  */
  232. chkabort ()
  233. {
  234.     return (0);
  235. }
  236.  
  237. CXBRK ()
  238. {
  239.     return (0);
  240. }
  241.