// These are the commands for effecting the types list.

#include "standard.h"

struct node
        {
         FileType type;
         MTEXT file;
	 MTEXT arg;
        };

struct link
        {
         struct node * thisnode;
         struct link * rest;
        };

typedef struct node  *Citem;
typedef struct link *Clist;
static Clist nodelist=NULL;
static Clist pagelist;
#define NUMTYPE 50

static BOOL typeX[NUMTYPE];
static MTEXT current_arg;
static int pagenum = 0;

set_pagelist()
 {
  pagelist = nodelist;
 }

int page_num()
 {
  return pagenum;
 }

init_typearray()
 {
  int i;
  for(i=0;i<NUMTYPE;i++)
	{
	typeX[i] = FALSE;
	}
  strcpy(current_arg,"");
 }

Clist cons( Citem x, Clist xl)
        {
        Clist cnew;
        cnew=malloc(sizeof(struct link));
        cnew->thisnode = x;
        cnew->rest = xl;
        return(cnew);
        }


add_type(FileType type, char * file, char * arg)
 {
  Clist currentlist;
  currentlist = nodelist;
  Citem cn;

  NOTE("Adding file: ",file);
  space_control(arg); // This will enable the contents to look neater.
 
  if((typeX[type]) && (type !=PAGE))
	{ REM ("Duplicate type"); return;}
  else
	{typeX[type]=TRUE;} 
		
  if(type ==PAGE)
	pagenum++;

   cn = malloc(sizeof(struct node));
   /* Copy data into command structure */
   strcpy(cn->file,file);
   strcpy(cn->arg,arg);
   cn->type=type;

 nodelist=cons(cn,currentlist);
 }

BOOL exist_type(FileType type)
 {
  // For the purposes of wheth
  return typeX[type];
 }

check_guidefile()
 {
  if(typeX[GUIDE])
	{
	 REM ("Guidefile exists");
	}
  else
	{
	 REM ("Guidefile NOT exists: using default file");
	 add_type(GUIDE,"output.guide","");
	}
 }

char * get_nodedesc()
 {
	NOTE ("Current node description: ",current_arg);
	return current_arg;
 }

char * getfilename(FileType type)
 {
   NOTE ("NameRequest: ",txt_type(type));
   if(type != PAGE)
 	{
	 Clist temp;
	 temp = nodelist;

	 while(temp != NULL)
	 {	
	 	if(type == temp->thisnode->type)
			{
			strcpy(current_arg,temp->thisnode->arg);
			return temp->thisnode->file;
			}
		else
			temp = temp->rest;
	 }
	 return ""; // Failed	
	
	}
   else
	{
	 return "";  // Fail
	}
 }

char * page_file(int number)
 {
  Clist temp;
  temp = nodelist;
  FileType type = PAGE;
  int i = 1;

  REM("Page file request");

  while(temp != NULL)
         {
                if(type == temp->thisnode->type)
                        {
			 if(i==number)
			  {
                           MTEXT current_file;
                           strcpy(current_arg,temp->thisnode->arg);
                           strcpy(current_file,temp->thisnode->file);
                           return current_file;
			  }
			 else
			  {
			   i++;
			   temp = temp->rest;
			  }
                        }
                else
			{
                           temp = temp->rest;
			}
         }
         strcpy(current_arg,"");
         return "";  // Fail
 }
