//************************************
//
// Name : Node.c
//
//************************************


//**** Header files

//** OS Include files

#include <exec/lists.h>
#include <exec/nodes.h>
#include <exec/memory.h>

//** OS function prototypes

#include <clib/alib_protos.h>
#include <clib/exec_protos.h>

//** OS function inline calls

#include <pragmas/exec_pragmas.h>

//** ANSI C includes

#include <string.h>

//** application include

#include "Node.h"


//**** Local storage

struct List *List=NULL;
struct List Sticky;


//**** Aux List functions

int InitList(void) {
  if (NULL==List) {
    if (List=AllocMem(sizeof(struct List),MEMF_PUBLIC|MEMF_REVERSE|MEMF_CLEAR)) {
      NewList(List);
      NewList(&Sticky);
      return 1;
    }
  }
  return 0;
} // InitList


int FreeList(int all) {
  struct Node *mynode;

  if (NULL!=List) {
    while (mynode=RemHead(List)) {
      DisposeNode((struct ProgNode *)mynode);
    } // while

    while (mynode=RemHead(&Sticky)) {
      if (mynode->ln_Name) FreeVec(mynode->ln_Name);
      FreeVec(mynode);
    } // while

    if (1==all) {
      FreeMem(List,sizeof(struct List));
      List=NULL;
    }
    return 1;
  }
  return 0;
} // FreeList
/*
long ListLength(void) {
  struct Node *myn;
  long count=0;
  if (NULL==List->lh_Head->ln_Succ) return 0;
  for ( myn=List->lh_Head; myn->ln_Succ ; myn=myn->ln_Succ,count++ );
  return count;
} // ListLength
*/

//**** Node Con/De-Structors


struct ProgNode *NewNode(void) {
  struct ProgNode *mynode;
  mynode = AllocMem(sizeof(struct ProgNode),MEMF_CLEAR|MEMF_PUBLIC|MEMF_CLEAR);
  if (mynode) AddTail( List, (struct Node *)mynode );
  return mynode;
} // NewNode


int DisposeNode(struct ProgNode *pn) {
  if (NULL!=pn) {
    if (NULL!=pn->pn_Node.ln_Name) FreeVec(pn->pn_Node.ln_Name);
    if (NULL!=pn->pn_Filename) FreeVec(pn->pn_Filename);
    if (NULL!=pn->pn_Directory) FreeVec(pn->pn_Directory);

    FreeMem(pn,sizeof(struct ProgNode));
    return 1;
  }
  return 0;
} // DisposeNode


BOOL NewNodeSticky(char *st) {
  struct Node *n;

  n=(struct Node *)AllocVec(sizeof(struct Node),MEMF_PUBLIC|MEMF_REVERSE);
  if (n) {
    if (st) n->ln_Name=strcpy(AllocVec(strlen(st)+1,MEMF_PUBLIC|MEMF_REVERSE),st);
    else n->ln_Name=NULL;
    AddTail(&Sticky,n);
    return TRUE;
  }
  return FALSE;
} // NewNodeSticky


//**** Node Search functions
/*
struct Node *FindNthNode(long n) {
  struct Node *myn;

  for ( myn=List->lh_Head; (n--) && (myn) ; myn=myn->ln_Succ );
  return myn;
} // FindNthNode
*/

//**** Node setting

void pnSetItem(struct ProgNode *pn, char *item) {
#define ITEM pn->pn_Node.ln_Name
  if (pn) {
    if (NULL!=ITEM) FreeVec(ITEM);
    if (item) ITEM=strcpy( AllocVec(strlen(item)+1,MEMF_PUBLIC|MEMF_REVERSE), item);
    else ITEM=NULL;
  }
#undef ITEM
} // pnSetItem

void pnSetFilename(struct ProgNode *pn, char *item) {
#define ITEM pn->pn_Filename
  if (pn) {
    if (NULL!=ITEM) FreeVec(ITEM);
    if (item) ITEM=strcpy( AllocVec(strlen(item)+1,MEMF_PUBLIC|MEMF_REVERSE), item);
    else ITEM=NULL;
  }
#undef ITEM
} // pnSetItem

void pnSetDirectory(struct ProgNode *pn, char *item) {
#define ITEM pn->pn_Directory
  if (pn) {
    if (NULL!=ITEM) FreeVec(ITEM);
    if (item) ITEM=strcpy( AllocVec(strlen(item)+1,MEMF_PUBLIC|MEMF_REVERSE), item);
    else ITEM=NULL;
  }
#undef ITEM
} // pnSetItem

//**** End of file
