#ifndef __INC_IFFREAD_C
#define __INC_IFFREAD_C
/*******************************************************************
 $CRT 18 Dec 1996 : hp

 $AUT Holger Burkarth
 $DAT >>iffread.c<<   29 Jan 1997    13:52:46 - (C) ProDAD
*******************************************************************/

//##ex mcpp:cppc -gs -o pos:pos/ex/iffread p:pLib/StartCode.o p:/pOS_RKRM/pIFFParse/iffread.c p:pLib/StdIO.o -l pOSStub -l pOS -l CPPList

/***********************************************************
  pOS programing example - Copyright (C) 1995-97 proDAD

  This code was written as an easy to understand example,
  how to program pOS features. It is provided 'as-is',
  without any express or implied warranty.

  Permission is hereby granted to use, copy and modify
  this source code for any purpose, without fee, subject
  to the following conditions:

    (1) This notice may not be removed or altered from any
        source distribution.

    (2) Altered source versions must be plainly marked as
        such, and must not be misrepresented as being
        the original source code.

    (3) If only executable code is distributed, then the
        accompanying documentation have to state that
        "this software is based in part on examples of
        the pOS developer packet".

    (4) Permission for use of this code is granted only
        if the user accepts full responsibility for any
        undesirable consequences. proDAD accept NO LIABILITY
        for damages of any kind.

  ©proDAD
***********************************************************/

#include <p:pExec/Types.h>
#include <p:pExec/Library.h>
#include <p:pDOS/DosArgs.h>
#include <p:pDOS/ArgTags.h>
#include <p:pIFFParse/IFFParse.h>
#include <p:pIFFParse/IFFParseTags.h>
#include <p:pProto/pExec2.h>
#include <p:pProto/pDOS2.h>
#include <p:pProto/pIFFParse2.h>


#ifdef _____ME_____
  #include "grund/inc_string.h"
  #include "grund/inc_stdio.h"
#else
 #ifdef __cplusplus
 extern "C"  {
 #endif
  #include <string.h>
  #include <stdio.h>
 #ifdef __cplusplus
 }
 #endif
#endif



#define ID_ADRS MAKE_ID('A','D','R','S')
#define ID_NAME MAKE_ID('N','A','M','E')
#define ID_CITY MAKE_ID('C','I','T','Y')


struct pOS_Library *gb_IFFParseBase;


const CHAR *HelpText=
""
;

const CHAR *PrgHeader=
"Demo for easy reading an IFF file";

const CHAR *PrgVerText=
"$VER: 1.0 ("__DATE2__") (Copyright 1996-97 by proDAD) (Created by Holger Papajewski)";


#ifdef __cplusplus
extern "C"
#endif

void main()
{
  SLONG err=0;
  ULONG Ops[1] = {(ULONG)"test.iff"};
  pOS_DosArgs *Args;

  Args = pOS_ReadDosArgs( "NAME",Ops,sizeof(Ops)/sizeof(ULONG),
    ARGTAG_PrgHeaderText, (ULONG)PrgHeader,    /* kurze Programm-Beschreibung */
    ARGTAG_HelpText,      (ULONG)HelpText,     /* Help-Texte */
    ARGTAG_PrgVerText,    (ULONG)PrgVerText,   /* VER-String */
    TAG_DONE);

  if(Args) {
    if(gb_IFFParseBase=pOS_OpenLibrary("piffparse.library",0L)) {
      pOS_IFFHandle *iff;
      const pOS_IFFStoredProp *sp;

      iff=pOS_OpenIFF(IFFTAG_DosName,Ops[0],
        IFFTAG_AccessMode,IFFACCMD_Read,TAG_DONE);

      if( iff ) {
        printf("pOS_FindProp()\n");

        err = pOS_PropChunk(iff,IFFTAG_ParseType,ID_ADRS,
          IFFTAG_ParseID,ID_NAME,IFFTAG_ParseID,ID_CITY,TAG_DONE);
        if(!err)
          err=pOS_StopChunk(iff,IFFTAG_StopOnExit,TRUE,
            IFFTAG_StopType,ID_ADRS,IFFTAG_StopID,ID_FORM,TAG_DONE);

        while(!err && pOS_ParseIFF(iff,IFFPARSE_Scan)==IFFERR_EOC)
        {
          sp=pOS_FindProp(iff,ID_ADRS,ID_NAME);
          if(sp)
            printf("NAME |%s|\n",sp->sp_Data);

          sp=pOS_FindProp(iff,ID_ADRS,ID_CITY);
          if(sp)
            printf("CITY |%s|\n",sp->sp_Data);
        }

        pOS_CloseIFF(iff);
      }
      else printf("Open IFF File |%s| Failed.\n",Ops[0]);


      iff=pOS_OpenIFF(IFFTAG_DosName,Ops[0],
            IFFTAG_AccessMode,IFFACCMD_Read,TAG_DONE);

      if( iff ) {
        printf("\npOS_ReadChunkBytes()\n");
        UBYTE Buf[64];

        err=pOS_StopChunk(iff,IFFTAG_StopType,ID_ADRS,
          IFFTAG_StopID,ID_NAME,IFFTAG_StopID,ID_CITY,TAG_DONE);

        while(!err && !pOS_ParseIFF(iff,IFFPARSE_Scan))
        {
          const pOS_IFFLocContext *lc=pOS_CurrentChunk(iff);
          if(pOS_ReadChunkBytes(iff,Buf,lc->ifflc_Size) == lc->ifflc_Size) {
            switch(lc->ifflc_ID) {
              case ID_NAME:
                printf("NAME |%s|\n",Buf);
                break;

              case ID_CITY:
                printf("CITY |%s|\n",Buf);
                break;

              default:
                break;
            }
          }
          else err=TRUE;
        }

        pOS_CloseIFF(iff);
      }
      else printf("Open IFF File |%s| Failed.\n",Ops[0]);

      pOS_CloseLibrary(gb_IFFParseBase);
    }
    else printf("Cannot Open IFFParse.library\n");

    pOS_DeleteDosArgs(Args);
  }
}

#endif
