#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    
    cout << "==================mapfixer.exe version 1.0======================" << endl;
    cout << "===========MAP Fixer, a program by Aaron Miller.================" << endl;
    cout << "========Currently fixing Tile Studio Generated Map File=========" << endl;
    system("PAUSE");

FILE *fp;
FILE *ofp;

char buffer1[4] = {' ',' ',' ','\0'}; 

cout << "Pass one:" << endl;

if (fp = fopen("maps.txt","r+"))
{
  while (!(feof(fp)))
  {
      buffer1[0] = buffer1[1];
      buffer1[1] = buffer1[2];
      buffer1[2] = getc(fp);
      
      if (buffer1[0] == ' ' && buffer1[1] == '0' && buffer1[2] == ',')
      {
         fseek(fp,-3,SEEK_CUR);
         fwrite(" , ",3,1,fp);
         fseek(fp,1,SEEK_CUR);
      }    
      
     
  }
  fclose(fp);
}
cout << "Pass two:" << endl;

if (fp = fopen("maps.txt","r+"))
{
  while (!(feof(fp)))
  {
      buffer1[0] = buffer1[1];
      buffer1[1] = buffer1[2];
      buffer1[2] = getc(fp);
      
      if (buffer1[0] == ' ' && buffer1[1] == '0' && buffer1[2] == '-')
      {
         fseek(fp,-3,SEEK_CUR);
         fwrite("  -",3,1,fp);
         fseek(fp,1,SEEK_CUR);
      }    
      
     
  }
  fclose(fp);
}

cout << "Pass three:" << endl;

if (fp = fopen("maps.txt","r+"))
{
  while (!(feof(fp)))
  {
      buffer1[0] = buffer1[1];
      buffer1[1] = buffer1[2];
      buffer1[2] = buffer1[3];
      buffer1[3] = getc(fp);
      
      if (buffer1[0] == '-' && buffer1[1] == '1' && buffer1[2] == '2' && buffer1[3] == '7')
      {
         fseek(fp,-4,SEEK_CUR);
         fwrite(" 127",4,1,fp);
         fseek(fp,1,SEEK_CUR);
      }     
  }
 fclose(fp);
}

cout << "Pass Four:" << endl;

if (fp = fopen("maps.txt","r+"))
{
  while (!(feof(fp)))
  {
      buffer1[0] = buffer1[1];
      buffer1[1] = buffer1[2];
      buffer1[2] = buffer1[3];
      buffer1[3] = getc(fp);
      
      if (buffer1[0] == '-' && buffer1[1] == '1' && buffer1[2] == '2' && buffer1[3] == '6')
      {
         fseek(fp,-4,SEEK_CUR);
         fwrite(" 126",4,1,fp);
         fseek(fp,1,SEEK_CUR);
      }     
  }
 fclose(fp);
}

cout << "Pass Five:" << endl;

if (fp = fopen("maps.txt","r+"))
{
  while (!(feof(fp)))
  {
      buffer1[0] = buffer1[1];
      buffer1[1] = buffer1[2];
      buffer1[2] = buffer1[3];
      buffer1[3] = getc(fp);
      
      if (buffer1[0] == '-' && buffer1[1] == '1' && buffer1[2] == '2' && buffer1[3] == '5')
      {
         fseek(fp,-4,SEEK_CUR);
         fwrite(" 125",4,1,fp);
         fseek(fp,1,SEEK_CUR);
      }     
  }
 fclose(fp);
}

char code_header[] = "#include <cstdlib> \n#include <iostream> \n using namespace std; \nint main(int argc, char *argv[])\n{\n system(\"PAUSE\");\n char maps[] = {\n";
char code_footer[] = "\n};\n FILE *fp; \n if (fp = fopen(\"doommaps.bin\",\"wb\")) \n { \n fwrite (maps,sizeof(char),sizeof(maps),fp); \n } \n return EXIT_SUCCESS;\n}\n";


if (fp = fopen("maps.txt","r"))
{
   if (ofp = fopen("maps.cpp","w+"));
   {
      fwrite(code_header,sizeof(code_header),1,ofp);
      fseek(ofp,-1,SEEK_CUR);
      while(!(feof(fp)))
      {
        fread(buffer1,4,1,fp); 
        for (int i = 0; i < 4; i++)
        {
           if (buffer1[i] != ' ')
           {
              putc(buffer1[i],ofp); 
           }  
        }
      }
      fseek(ofp,-2,SEEK_CUR);
      fwrite(code_footer,sizeof(code_footer),1,ofp);
      fseek(ofp,-1,SEEK_CUR);
      putc('\n',ofp);
      fclose(ofp);
   }
   fclose(fp);
}



 system("PAUSE");

  return EXIT_SUCCESS;
 
}
