#include "myfile.h"
#define long unsigned long

char IsUndefined(FILE*, long);
char* MakeString(FILE*, long);
void ConvertObj(FILE*, char*);
void KillAt(FILE*, long);

int main(int argc, char *argv[])
{
  FILE *MapFile;
  FILE *ObjFile;
  long FilePointer;
  long Size;
  char FileName[255];
  int Counter=0;
  char *Found[255];
  int i;

  for (i=0; i<=254; i++)
  {
    Found[i]=(char*) malloc(30*sizeof(char));
  }


  if (argc==1)
  {
    printf ("Wrong Syntax: Use map2obj [objfilename] without .map\n");
  }
  else
  {
    strncpy (FileName, argv[1], 250);
    strncat (FileName, ".map", 4);
    MapFile=fopen(FileName, "r+");
    if (MapFile==NULL)
    {
      printf ("Error: File %s not found!", FileName);
    }
    else
    {
      Size=GetSizeOfFile(MapFile);
      FilePointer=0;
      while (FilePointer<=Size)
      {
        if (IsUndefined(MapFile, FilePointer))
        {
          printf("Found Nr. %d\n",Counter);
          FilePointer+=17;
          Found[Counter]=MakeString(MapFile, FilePointer);
          Counter++;
        }
        FilePointer++;
      }
      fclose(MapFile);
      printf("Found %d undefined symbols\n", Counter);
      printf("%s was %d bytes big\n", FileName, Size);
      printf("Converting %s ...\n", FileName);

      strncpy(FileName, argv[1], 250);
      strncpy(FileName, ".obj", 4);

      ObjFile=fopen(FileName, "rb+");

      if (ObjFile==NULL)
      {
        printf("Error: File %s not found!\n", FileName);
      }
      else
      {
        for (i=0; i<=Counter; i++)
        {
          printf("Converting symbol %s\n", Found[i]);
          ConvertObj (ObjFile, Found[i]);
        }
        printf("All done...\n");
        fclose(ObjFile);
      }
    }
  }

  for (i=0; i<=254; i++)
  {
    free(Found[i]);
  }
}

char IsUndefined(FILE* WhatFile, long Where)
{
  if (  ReadChar(WhatFile, Where+0)=='S' &&
        ReadChar(WhatFile, Where+1)=='y' &&
        ReadChar(WhatFile, Where+2)=='m' &&
        ReadChar(WhatFile, Where+3)=='b' &&
        ReadChar(WhatFile, Where+4)=='o' &&
        ReadChar(WhatFile, Where+5)=='l' &&
        ReadChar(WhatFile, Where+6)==' ' &&
        ReadChar(WhatFile, Where+7)=='U' &&
        ReadChar(WhatFile, Where+8)=='n' &&
        ReadChar(WhatFile, Where+9)=='d' &&
        ReadChar(WhatFile, Where+10)=='e' &&
        ReadChar(WhatFile, Where+11)=='f' &&
        ReadChar(WhatFile, Where+12)=='i' &&
        ReadChar(WhatFile, Where+13)=='n' &&
        ReadChar(WhatFile, Where+14)=='e' &&
        ReadChar(WhatFile, Where+15)=='d' &&
        ReadChar(WhatFile, Where+16)==' ' )
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

char* MakeString(FILE* WhatFile, long Where)
{
  int Temp=0;
  char* String;
  char* Address;

  String=(char*) malloc(255*sizeof(char));
  Address=String;

  while (ReadChar(WhatFile, Where+Temp)!=0x0D)
  {
    *String=ReadChar(WhatFile, Where+Temp);
    Temp++;
    String++;
  }
  *String='\0';
  String=Address;
  return String;
}

void ConvertObj (FILE* WhatFile, char* WhatString)
{
  long FileOffset=0;
  long FileSize=GetSizeOfFile(WhatFile);
  char *StartOfString=WhatString;
  int Length=strlen(WhatString);
  int Counter;
  int i;

  while (FileOffset<=FileSize)
  {
    if (*WhatString==ReadChar(WhatFile, FileOffset))
    {
      Counter=0;
      for (i=0; i<=Length; i++)
      {
        if (*WhatString==ReadChar(WhatFile, FileOffset+i))
        {
          Counter++;
        }
      }
      if (Counter==Length)
      {
        KillAt (WhatFile, FileOffset);
      }
    }
  }
}
        
void KillAt (FILE* WhatFile, long Where)
{
  // Insert code for overwrting @xx with 0x00s
}

