

// CompilerAufruf fuer pOS CrossCompiling
// mcpp:cppc -gs -o df4:prog p:pLib/StartCode.o df4:prog.c p:pLib/StdIO.o -l pOSStub -l pOS -l pOSxA p:/pOSxA/clib/iffparse_protos.o -l list


#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <libraries/iffparse.h>

#ifdef __cplusplus
extern "C" {
#endif

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/iffparse_protos.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifdef __cplusplus
}
#endif

/*
 * Text error messages for possible IFFERR_#? returns from various
 * IFF routines.  To get the index into this array, take your IFFERR code,
 * negate it, and subtract one.
 *  idx = -error - 1;
 */
char    *errormsgs[] = {
        "End of file (not an error).",
        "End of context (not an error).",
        "No lexical scope.",
        "Insufficient memory.",
        "Stream read error.",
        "Stream write error.",
        "Stream seek error.",
        "File is corrupt.",
        "IFF syntax error.",
        "Not an IFF file.",
        "Required call-back hook missing.",
        "Return to client.  You should never see this."
};

#define RBUFSZ 512

#define  ID_FTXT        MAKE_ID('F','T','X','T')
#define  ID_CHRS        MAKE_ID('C','H','R','S')

#ifdef __pOS__
struct Library *gb_IFFParseBase;
#else
struct Library *IFFParseBase;
#endif

UBYTE mytext[]="This FTXT written to file by iffparse example.\n";


#ifdef __cplusplus
extern "C"
#endif
void main(void)
{
  struct IFFHandle    *iff = NULL;
  struct ContextNode  *cn;
  long                error=0, unitnumber=0, rlen;
  int textlen;
  UBYTE readbuf[RBUFSZ];

#ifdef __pOS__
  if (!(gb_IFFParseBase = OpenLibrary ("piffparse.library", 0L)))
#else
  if (!(IFFParseBase = OpenLibrary ("iffparse.library", 0L)))
#endif
  {
    puts("Can't open iff parsing library.");
    goto bye;
  }

  if (!(iff = AllocIFF ()))
  {
    puts ("AllocIFF() failed.");
    goto bye;
  }

  if (!(iff->iff_Stream = (ULONG) Open("test.iff",MODE_NEWFILE)))
  {
    puts ("File open failed.");
    goto bye;
  }

  InitIFFasDOS(iff);

  if(error = OpenIFF(iff, IFFF_WRITE))
  {
    puts ("OpenIFF for write failed.");
    goto bye;
  }

  if(!(error=PushChunk(iff, ID_FTXT, ID_FORM, IFFSIZE_UNKNOWN)))
  {
    if(!(error=PushChunk(iff, 0, ID_CHRS, IFFSIZE_UNKNOWN)))
    {
      textlen = strlen(mytext);
      if(WriteChunkBytes(iff, mytext, textlen) != textlen)
      {
        puts("Error writing CHRS data.");
        error = IFFERR_WRITE;
      }
    }
    if(!error) error = PopChunk(iff);
  }
  if(!error) error = PopChunk(iff);

  if(error)
  {
    printf("IFF write failed, error %ld: %s\n",
             error, errormsgs[-error - 1]);
    goto bye;
  }
  else printf("Wrote text to File as FTXT\n");

  CloseIFF(iff);
  if(iff->iff_Stream) Close(iff->iff_Stream);


  if(!(iff->iff_Stream = (ULONG)Open("test.iff",MODE_OLDFILE)))
  {
    puts ("Reopen of File failed.");
    goto bye;
  }


  if (error = OpenIFF (iff, IFFF_READ))
  {
    puts ("OpenIFF for read failed.");
    goto bye;
  }

  if (error = StopChunk(iff, ID_FTXT, ID_CHRS))
  {
    puts ("StopChunk failed.");
    goto bye;
  }

  while(1)
  {
    error = ParseIFF(iff,IFFPARSE_SCAN);
    if(error == IFFERR_EOC) continue;
    else if(error) break;

    cn = CurrentChunk(iff);

    if((cn)&&(cn->cn_Type == ID_FTXT)&&(cn->cn_ID == ID_CHRS))
    {
      printf("CHRS chunk contains:\n");
      while((rlen = ReadChunkBytes(iff,readbuf,RBUFSZ)) > 0)
      {
        Write(Output(),readbuf,rlen);
      }
      if(rlen < 0)    error = rlen;
    }
  }

  if((error)&&(error != IFFERR_EOF))
  {
    printf("IFF read failed, error %ld: %s\n",
          error, errormsgs[-error - 1]);
  }

bye:
  if (iff)
  {
    CloseIFF (iff);

    if(iff->iff_Stream) Close(iff->iff_Stream);

    FreeIFF (iff);
  }

#ifdef __pOS__
  if(gb_IFFParseBase) CloseLibrary(gb_IFFParseBase);
#else
  if(IFFParseBase) CloseLibrary(IFFParseBase);
#endif

  //exit (RETURN_OK);
}
