
/*************************************************************************/
/*				 Lists.c				 */
/*									 */
/* This file contains linked-list handling functions that provide an	 */
/* interface between the needs of FileFind, and the Exec Node/List	 */
/* system								 */
/*									 */
/* This source file is Copyright 1992 by Dave Schreiber.  All Rights	 */
/* Reserved.								 */
/*************************************************************************/


#include <exec/types.h>
#include <exec/lists.h>
#include <exec/exec.h>
#include "Lists.h"


/*Initialize a list*/
BOOL initList(struct List **listHead)
{
   /*If the head of the lists exists, free it*/
   if(*listHead!=NULL)
      FreeMem(*listHead,sizeof(struct List));

   /*Allocate the head of the list*/
   *listHead=(struct List *)AllocMem(sizeof(struct List),MEMF_CLEAR);

   /*Initialize the list if the allocation was successful*/
   if(*listHead!=NULL)
      NewList(*listHead);

   return(listHead!=NULL);
}

/*Add a name to the list*/
BOOL addItem(struct List *listHead,char *name,char *prefixName,BOOL fileOnly)
{
   dosNameNode *nameNode;
   char *nameOnly;
   ULONG nodeSize;

   nodeSize=strlen(name)+sizeof(dosNameNode)+1;

   /*Allocate enough memory for the node*/
   if((nameNode=(dosNameNode *)AllocMem(nodeSize,MEMF_CLEAR))!=NULL)
   {
      /*Copy the filename into the node's buffer*/
      strcpy(nameNode->fullFilename,name);

      /*Store a pointer to the filename (or whole name)*/
      if(fileOnly)
	 nameOnly=(char *)FilePart(nameNode->fullFilename);
      else
	 nameOnly=nameNode->fullFilename;

      if(prefixName!=NULL)
      {
	 if((nameNode->node.ln_Name=(char *)
		   AllocMem(strlen(prefixName)+1,0L))!=NULL)
	    strcpy(nameNode->node.ln_Name,prefixName);
      }
      else
	 if((nameNode->node.ln_Name=(char *)AllocMem(strlen(nameOnly)+1,0L))!=NULL)
	    strcpy(nameNode->node.ln_Name,nameOnly);

      if(nameNode->node.ln_Name==NULL)
      {
	 FreeMem(nameNode,nodeSize);
	 nameNode=NULL;
      }


   }
   if(nameNode!=NULL)
   {
      /*Other initializations*/
      nameNode->node.ln_Type=NAMENODE_ID;
      nameNode->node.ln_Pri=0;

      /*Add to the end of the list*/
      AddTail(listHead,(struct Node *)nameNode);
      return(TRUE);
   }
   else
      return(FALSE);
}

/*Get the item at ordinal number 'nodeNum'*/
dosNameNode *getItem(struct List *listHead,ULONG nodeNum)
{
   dosNameNode *node;

   /*Return NULL if the list is empty*/
   if(listHead->lh_Head->ln_Succ==NULL)
      return(NULL);

   /*Otherwise, find the appropriate node*/
   for(node=(dosNameNode *)listHead->lh_Head; node->node.ln_Succ && nodeNum!=0;
	node=(dosNameNode *)node->node.ln_Succ,nodeNum--);

   return(node);
}

/*Free a linked list*/
void freeList(struct List **listHead)
{
   dosNameNode *curNode;
   dosNameNode *nextNode;

   /*Get the head of the list*/
   curNode=(dosNameNode *)((*listHead)->lh_Head);

   /*Loop while there are still nodes*/
   while((nextNode=(dosNameNode *)curNode->node.ln_Succ)!=NULL)
   {
      /*Free a node's memory*/
      FreeMem(curNode->node.ln_Name,strlen(curNode->node.ln_Name)+1);
      FreeMem(curNode,sizeof(dosNameNode)+strlen(curNode->fullFilename)+1);
      curNode=nextNode;
   }

   /*Free the head of the list*/
   FreeMem(*listHead,sizeof(struct List));
   *listHead=NULL;

   return;
}

/*End of Lists.c*/


