#pragma once

#include "stdafx.h"

#include "Includes.h"

#include "Defines.h"

#include "BringUp.h"

#include "GUI_DefineFunctions.cpp"

using namespace std;

int translate()
{
   ifstream Rules(txt_fname,ios::in|ios::out);
   if(Rules.bad()) {
	   CallMessageBox("Error opening ",txt_fname);
   }
	ofstream Build(build_fname,ios::out|ios::binary|ios::in);
	if(Build.bad()) {
		CallMessageBox("Error opening ",build_fname);
	}
   ofstream Item(item_fname,ios::out|ios::binary|ios::in);
	if(Item.bad()) {
      CallMessageBox("Error opening ",item_fname);
   }
   ofstream Spell(spell_fname,ios::out|ios::binary|ios::in);
	if(Spell.bad()) {
		CallMessageBox("Error opening ",spell_fname);
   }
   ofstream Wizard(exe_fname,ios::out|ios::binary|ios::in);
   if(Wizard.bad()) {
	   CallMessageBox("Error opening ",exe_fname);
   } 
	ofstream Magic(exe2_fname,ios::out|ios::binary|ios::in);
   if(Magic.bad()) {
	   CallMessageBox("Error opening ",exe2_fname);
   } 
	ofstream Itemmake(exe3_fname,ios::out|ios::binary|ios::in);
   if(Itemmake.bad()) {
	   CallMessageBox("Error opening ",exe3_fname);
   } 
   for (int i=0;i < rec_t_cnt - testing;i++) {
		if (ReturnViableSections(i)) {
			WriteDataTableToFiles(i,Item,Build,Spell,Magic,Wizard,Itemmake,Rules);
			m_progress->StepIt();
			wait ( 1 );
		}
   }

	Build.close();
	Item.close();
	Spell.close();
   Wizard.close();
	Magic.close();
	Itemmake.close();
   Rules.close();

	return 0;
}

int extract()
{
   ofstream Rules(txt_fname,ios::in|ios::out|ios::trunc);
   if(Rules.bad()) {
	   CallMessageBox("Error opening ",txt_fname);
   }
   Rules.put('"');
   Rules.write(ver_info,strlen(ver_info));
   Rules.put('"');
   Rules.put('\n');
	ifstream Build(build_fname,ios::out|ios::binary|ios::in);
	if(Build.bad()) {
		CallMessageBox("Error opening ",build_fname);
	}
   ifstream Item(item_fname,ios::out|ios::binary|ios::in);
	if(Item.bad()) {
      CallMessageBox("Error opening ",item_fname);
   }
   ifstream Spell(spell_fname,ios::out|ios::binary|ios::in);
	if(Spell.bad()) {
		CallMessageBox("Error opening ",spell_fname);
   }
   ifstream Wizard(exe_fname,ios::out|ios::binary|ios::in);
   if(Wizard.bad()) {
	   CallMessageBox("Error opening ",exe_fname);
   } 
	ifstream Magic(exe2_fname,ios::out|ios::binary|ios::in);
   if(Magic.bad()) {
	   CallMessageBox("Error opening ",exe2_fname);
   } 
	ifstream Itemmake(exe3_fname,ios::out|ios::binary|ios::in);
   if(Itemmake.bad()) {
	   CallMessageBox("Error opening ",exe3_fname);
   } 
   for (int i=0;i < rec_t_cnt - testing;i++) {
		if (ReturnViableSections(i)) {
			Rules << '\n';
			WriteLableRules(Rules,directive[i]);
			WriteDataTable(i,Item,Build,Spell,Magic,Wizard,Itemmake,Rules);
			WriteLableRules(Rules,directive[rec_t_cnt]);
			m_progress->StepIt();
			wait ( 1 );
		}
   }

	Build.close();
	Item.close();
	Spell.close();
   Wizard.close();
	Magic.close();
	Itemmake.close();
   Rules.close();

   return 0;
}	