// GS_merger.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{
  char buffer[1];
  int count = 0;
  int inSize_L, inSize_M;


  cout << "\n\nLooking for file named \"LSBs_dump.bin\" and \"MSBs_dump.bin\" as inputs.\n\n\n";
  ifstream inFile_L("LSBs_dump.bin", ios::in|ios::binary|ios::ate);
  ifstream inFile_M("MSBs_dump.bin", ios::in|ios::binary|ios::ate);
  if(inFile_L.is_open() && inFile_M.is_open()){
     cout << "splitted dumps opened, so far so good!\n";
     inSize_L = (int) inFile_L.tellg();
     inFile_L.seekg (0, ios::beg);
	 inSize_M = (int) inFile_M.tellg();
     inFile_M.seekg (0, ios::beg);
     if(inSize_L != (262144/2) && inSize_M != (262144/2)){
       cout << "One or both dump size mismatch!!\nYou do not have a proper splitted dumps.\n";
        return -1;
       }
    }
  else {
    cout << "Error opening one or both splitted dumps files.\n Make sure you have both files in the same directory as the executable.\naborting.\n";
    return -1;
    }


  ofstream LSBSFile ("merged.bin", ios::out | ios::binary);
  if(LSBSFile.is_open()){
     cout << "Output file created.\nMerging of the splitted dumps will begin.\n";
    }
  else{
     cout << "Could not create merged file, aborting.\n";
     return -1;
    }
 
  for(count =0; count < (262144/2); count++){

  inFile_M.read(buffer, 1);
  LSBSFile.write(buffer, 1);

  inFile_L.read(buffer, 1);
  LSBSFile.write(buffer, 1);

    }
	cout << "\n\n\"merged.bin\" was created.\nExiting\n";
  LSBSFile.close();
  inFile_L.close();
  inFile_M.close();
	return 0;
}


