/*
**	ShowWABL: shows a WABL file. Useless, just to code a demo and check load routine...
*/

#include <wabl/wabl.h>
#include <exec/exec.h>
#include <exec/libraries.h>
#include <dos/dos.h>
#include <utility/tagitem.h>
#include <utility/hooks.h>
#include <inline/dos.h>

extern struct ExecBase *SysBase;
extern struct Library *DOSBase;

#define	ARG_WABLINPUT	0

UBYTE	indent=0L;
ULONG 	*outfh;
char	Separator[20]={27,'[','3','1','m',27,'[','4','0','m',9,9,0};
char    Blue[11]={27,'[','3','3','m',27,'[','4','0','m',0};
char    BlueD[11]={27,'[','3','3','m',27,'[','4','1','m',0};
char    White[11]={27,'[','3','2','m',27,'[','4','0','m',0};

void ShowTree(struct WABLObject *obj)
{
 struct WABLObject *nob;
 indent++;
 while (nob=obj->WABL_O_Node.mln_Succ)
  {
   int i;
   for (i=0;i<indent;i++)
    {
     FPutC(outfh,32);
    } 
   FPuts(outfh,&White);
   FPuts(outfh,obj->WABL_O_Type);
   FPuts(outfh,&Separator);
   FPuts(outfh,obj->WABL_O_Name);
   FPutC(outfh,10);
   {
    struct WABLAttr *att,*nat;
    att=obj->WABL_O_Attrs.mlh_Head;
    while (nat=att->WABL_A_Node.mln_Succ)
     {
      for (i=0;i<indent;i++)
       {
        FPutC(outfh,32);
       } 
      FPutC(outfh,'+');
      FPuts(outfh,&Blue);
      FPuts(outfh,att->WABL_A_Comm);
      FPuts(outfh,&Separator);
      FPuts(outfh,att->WABL_A_Value);
      FPutC(outfh,10);
      att=nat;
     }
   }
   {
    struct WABLFriend *fri,*nfr;
    fri=obj->WABL_O_Friends.mlh_Head;
    while (nfr=fri->WABL_F_Node.mln_Succ)
     {
      for (i=0;i<indent;i++)
       {
        FPutC(outfh,32);
       } 
      FPutC(outfh,'+');
      FPuts(outfh,&BlueD);
      FPuts(outfh,fri->WABL_F_Comm);
      FPuts(outfh,&Separator);
      FPuts(outfh,fri->WABL_F_Name);
      FPutC(outfh,10);
      fri=nfr;
     }
   }
   if (!(obj->WABL_O_Childs.mlh_Head==&obj->WABL_O_Childs.mlh_Tail)) ShowTree(obj->WABL_O_Childs.mlh_Head); 
   obj=nob;
  }
 indent--;
}

ULONG ReadHook( register struct Hook* hook __asm("a0"), register ULONG object __asm("a2"), register ULONG message __asm("a1"))
{
 return(FGetC(object));
}

int main()
{
 ULONG *rda,arg[1];
 if (rda=ReadArgs("WABL/A",&arg,0))
  {
   ULONG *fh;
   if (fh=Open(arg[ARG_WABLINPUT],MODE_OLDFILE))
    {
     struct WABL *wabl;
     struct Hook gethook;
     struct TagItem loadtags[3]={{WABL_FileHandle,fh},{WABL_GetCharHook,&gethook},{0,0}};
     gethook.h_Entry=&ReadHook;
     if (wabl=LoadWABL((struct TagItem*)loadtags))
      {
       outfh=Output();
       ShowTree(wabl->WABL_Childs.mlh_Head);
       FreeWABL(wabl);
      }
     Close(fh); 
    }
  }
}

