#include"mbasic.h"

#define MAX_INCLUDES		32

static char * inc_names[MAX_INCLUDES];

static void register_include(char * name)
{
 int i;
 for(i=0;i<MAX_INCLUDES;i++)
 {
  if(inc_names[i])
   if(!strncmp(name,inc_names[i],strlen(name)) && strlen(name)==strlen(inc_names[i]))
    return;
 }
 for(i=0;i<MAX_INCLUDES;i++)
 {
  if(!inc_names[i])
  {
   inc_names[i]=malloc(strlen(name)+1);
   memcpy(inc_names[i],name,strlen(name)+1);
   return;
  }
 }
 Error("Internal error: Grow MAX_INCLUDES in file %s",__FILE__);
}
      
void InitIncludes(void)
{
 int i;
 for(i=0;i<MAX_INCLUDES;i++) inc_names[i]=NULL;
}

void EmitIncludes(void)
{
 int i;
 for(i=0;i<MAX_INCLUDES;i++)
 {
  if(inc_names[i])
   _emit("include \"%s\"\n",inc_names[i]);
 }
}

void ParseInclude(void)
{
 char buf[128];
 GetName();
 if(Look=='.')
 {
  char buf2[128];
  STORE_TMP(buf);
  GetChar();
  GetName();
  STORE_TMP(buf2);
  sprintf(current_token,"%s.%s",buf,buf2);
 }
 register_include(current_token);
}
