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 / wiff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  3.5 KB  |  181 lines

  1. /*  :ts=8 bk=0
  2.  *
  3.  * wiff.c:    Test IFF write program.  Writes junk, really.
  4.  *
  5.  * Usage: wiff [-c] [file]
  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.  * shf:   Updated to Dec 88 design revision        8812.15
  14.  * ewhac: Updated to 1.4 Beta                8912.06
  15.  * ewhac: Latticeification                9005.31
  16.  */
  17. #include <exec/types.h>
  18. #include <libraries/dos.h>
  19. #include <libraries/iffparse.h>
  20. #include <clib/exec_protos.h>
  21. #include <clib/dos_protos.h>
  22. #include "iffparse_protos.h"
  23. #include "iffparse.p"
  24.  
  25. #define    ID_FTXT    MAKE_ID('F','T','X','T')
  26. #define    ID_CHRS    MAKE_ID('C','H','R','S')
  27. #define    ID_FNAM    MAKE_ID('F','N','A','M')
  28. #define    ID_ZOID    MAKE_ID('Z','O','I','D')
  29.  
  30.  
  31. /*
  32.  * Forward function declarations.  (I hate ANSI.)
  33.  */
  34. LONG TextChunkOut (struct IFFHandle *, char *);
  35. LONG TestChunkOut (struct IFFHandle *);
  36.  
  37. struct Library    *IFFParseBase;
  38.  
  39.  
  40. main (argc, argv)
  41. int    argc;
  42. char    **argv;
  43. {
  44.     struct IFFHandle    *iff = NULL;
  45.     long            error;
  46.     short            cbio;
  47.  
  48.     if (argc < 2) {
  49.         printf ("usage: %s <outfile | \"-c\">\n", argv[0]);
  50.         goto die;
  51.     }
  52.     cbio = (argv[1][0] == '-' && argv[1][1] == 'c');
  53.  
  54.     if (!(IFFParseBase = OpenLibrary ("iffparse.library", 0L))) {
  55.         puts ("Can't get IFF parse library\n");
  56.         goto die;
  57.     }
  58.  
  59.     if (!(iff = AllocIFF())) {
  60.         puts ("AllocIFF() failed.");
  61.         goto die;
  62.     }
  63.  
  64.     if (cbio) {
  65.         if (!(iff->iff_Stream =
  66.                 (ULONG) OpenClipboard (PRIMARY_CLIP)))
  67.         {
  68.             puts ("Clipboard open failed.");
  69.             goto die;
  70.         }
  71.         InitIFFasClip (iff);
  72.     } else {
  73.         if (!(iff->iff_Stream = Open (argv[1], MODE_NEWFILE))) {
  74.             puts ("File open failed.");
  75.             goto die;
  76.         }
  77.         InitIFFasDOS (iff);
  78.     }
  79.  
  80.     if (error = OpenIFF (iff, IFFF_WRITE)) {
  81.         puts ("OpenIFF failed.");
  82.         goto die;
  83.     }
  84.  
  85.     if (error = PushChunk (iff, ID_ZOID, ID_LIST, IFFSIZE_UNKNOWN)) {
  86.         printf ("Initial PushChunk() error %ld\n", error);
  87.         goto die;
  88.     }
  89.     if (error = PushChunk (iff, ID_FTXT, ID_FORM, IFFSIZE_UNKNOWN)) {
  90.         printf ("Initial PushChunk() error %ld\n", error);
  91.         goto die;
  92.     }
  93.     if (error = TestChunkOut (iff)) {
  94.         printf ("Test out error %ld.\n", error);
  95.         goto die;
  96.     }
  97.  
  98.     if (error = TextChunkOut (iff, "This is a test")) {
  99.         printf ("Text out error %ld.\n", error);
  100.         goto die;
  101.     }
  102.     if (error = TextChunkOut (iff, "And another")) {
  103.         printf ("Text out error %ld.\n", error);
  104.         goto die;
  105.     }
  106.     if (error = PopChunk (iff)) {
  107.         printf ("penFinal PopChunk() error %ld\n", error);
  108.         goto die;
  109.     }
  110.     if (error = PopChunk (iff)) {
  111.         printf ("Final PopChunk() error %ld\n", error);
  112.         goto die;
  113.     }
  114.  
  115. die:
  116.     if (iff) {
  117.         CloseIFF (iff);
  118.  
  119.         if (iff->iff_Stream)
  120.             if (cbio)
  121.                 CloseClipboard ((struct ClipboardHandle *)
  122.                         iff->iff_Stream);
  123.             else
  124.                 Close (iff->iff_Stream);
  125.  
  126.         FreeIFF (iff);
  127.     }
  128.  
  129.     if (IFFParseBase)    CloseLibrary (IFFParseBase);
  130. }
  131.  
  132.  
  133. LONG
  134. TextChunkOut (iff, str)
  135. struct IFFHandle *iff;
  136. char        *str;
  137. {
  138.     LONG    error, size;
  139.  
  140.     if (error = PushChunk (iff, 0L, ID_CHRS, IFFSIZE_UNKNOWN))
  141.         return (error);
  142.  
  143.     size = strlen (str);
  144.     if (WriteChunkRecords (iff, (APTR) str, 1L, size) != size)
  145.         return (IFFERR_WRITE);
  146.  
  147.     printf ("wrote chunk <%s>\n", str);
  148.  
  149.     return (PopChunk (iff));
  150. }
  151.  
  152.  
  153. LONG
  154. TestChunkOut (iff)
  155. struct IFFHandle *iff;
  156. {
  157.     LONG    error, size;
  158.  
  159.     size = 10;
  160.     if (error = PushChunk (iff, 0L, ID_FNAM, size))
  161.         return (error);
  162.  
  163.     if (WriteChunkBytes (iff, (APTR) "fname67890", size) != size)
  164.         return (-100);
  165.  
  166.     return (PopChunk (iff));
  167. }
  168.  
  169. /*
  170.  * Disable Lattice's default ^C trap.
  171.  */
  172. chkabort ()
  173. {
  174.     return (0);
  175. }
  176.  
  177. CXBRK ()
  178. {
  179.     return (0);
  180. }
  181.