#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "Popcorn:h.Popcorn"

char   			symbol_table_filename[255];
char			*symbol_table = NULL;
int			st_length = 0;

object_handler Popcorn_FindSymbol(char *name)
{
  FILE           *st_handle;
  char		 *cmp;
  int		 namelen = strlen(name);
  object_handler return_func;

  if (symbol_table == NULL)
  {
    st_handle = fopen(symbol_table_filename, "rb");
    if (st_handle == NULL)
      return(NULL);
    fseek(st_handle, 0, 2); /* End of file */
    st_length = (int) ftell(st_handle);
    symbol_table = malloc(st_length);
    rewind(st_handle);
    fread(symbol_table, 1, st_length, st_handle);
    fclose(st_handle);
  }

  cmp = symbol_table;
  while (memcmp(cmp, name, namelen) != 0 && cmp != (symbol_table+st_length) )
  {
    while (*cmp >= 32)
      cmp++;
    cmp++;
  }

  if (cmp == (symbol_table + st_length))
    return(NULL);

  while (*cmp != 32)
    cmp++;
  while (*cmp == 32)
    cmp++;

  sscanf(cmp, "%x\n", (void*) &return_func);
  return(return_func);
}
