#include "myfile.h"
#define long unsigned long

char IsUndefined(FILE*, long);
char* MakeString(FILE*, long);

int main(int argc, char *argv[])
{
  FILE *MapFile;
  long FilePointer;
  long Size;
  char FileName[255];
  long Counter=0;

  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, "rb+");
    if (MapFile==NULL)
    {
      printf ("Error: File %s not found!", FileName);
    }
    else
    {
      Size=GetSizeOfFile(MapFile);
      FilePointer=0;
      while (FilePointer<=Size)
      {
        if (IsUndefined(MapFile, FilePointer))
        {
          FilePointer+=17;
          printf(MakeString(MapFile, FilePointer)+'\n');
        }
        FilePointer++;
      }
      fclose(MapFile);
      printf("%s was %d bytes big", FileName, Size);
    }
  }
}

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[255];


  while (ReadChar(WhatFile, Where+Temp)!=0x0D)
  {
    String[Temp]=ReadChar(WhatFile, Where+Temp);
    Temp++;
  }
  return &String;
}
