//************************************
//
// Name : Read.c
//
//************************************


//**** Header files

//** OS Include files
#include <dos/dos.h>
#include <dos/rdargs.h>

//** OS function prototypes
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>

//** OS function inline calls
#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>

//** ANSI C includes
#include <string.h>
//#include <stdio.h>

//** application include
#include "Read.h"
#include "Node.h"


//**** non-local vars

extern struct Library *DOSBase;


//**** Declarations

#define BUFSIZE		256

#define TEMPLATE  "MENUTEXT/A,FILENAME/A,DIR/A,LC/A/N,STACK/A/N,PRI/A/N"
#define OPT_TEXT	0
#define OPT_FILE	1
#define OPT_DIR		2
#define OPT_CODE	3
#define OPT_STACK	4
#define OPT_PRI		5
#define OPT_COUNT	6


//**** Read Tools file

int ReadFile(STRPTR FileName) {
  char buffer[BUFSIZE];
  BPTR fh;
  struct ProgNode *pn;
  BOOL reading;
  struct RDArgs rdargs;
  long opts[OPT_COUNT];

  if (0==FreeList(0)) {
    return 0;
  } // if FreeList

  if (NULL==(fh=Open(FileName,MODE_OLDFILE))) {
    //intf("ReadPrefs ERROR : Could not open file %s\n",FileName);
    return 0;
  } // if open

  memset(&rdargs,0,sizeof(rdargs));
  rdargs.RDA_Flags=RDAF_NOPROMPT;
  rdargs.RDA_Source.CS_Buffer=buffer;
  rdargs.RDA_Source.CS_Length=BUFSIZE;
  for (reading=TRUE; TRUE==reading ; ) {
    if (NULL==FGets(fh,buffer,BUFSIZE)) {
      reading=FALSE;
      break;
    } // if
    rdargs.RDA_Source.CS_CurChr=0;
    if (ReadArgs(TEMPLATE,opts,&rdargs)) {
      if (NULL==(pn=NewNode())) break;
      pnSetItem(pn,(char *)opts[OPT_TEXT]);
      pnSetFilename(pn,(char *)opts[OPT_FILE]);
      pnSetDirectory(pn,(char *)opts[OPT_DIR]);
      pn->pn_LaunchCode=(ULONG)*(ULONG *)opts[OPT_CODE];
      pn->pn_Stack=(ULONG)*(ULONG *)opts[OPT_STACK];
      pn->pn_Priority=(LONG)*(LONG *)opts[OPT_PRI];
      pn->pn_appmenu=NULL;
      AddTail( List, (struct Node *)pn );
      FreeArgs(&rdargs);
    } else {
      //intf("bad rdargs\n");
//      reading=FALSE;
//      break;
    }
  } // for
  Close(fh);

  return 1;
} // ReadFile

//**** End of file
