/* 
 * File:   testMain.cpp
 * Author: macino
 *
 * Created on Piatok, 2008, november 7, 19:52
 */
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include "Reader.h"
#include "Circuit.h"
#include "Piece.h"
#include "Placer.h"
#include "Printer.h"

using namespace std;

int main(int argc, char** argv) {
    
    //cout << "The dev testing.." << endl;
    //otvorime si testovaci subor
    string s;
    stringstream ss;
	if(argc != 2){
		cout << "Usage: betopox benchFileName" << endl;
		cout << "\t benchFileName is name of .bench file withou extension" << endl;
		return 1;
	}
	s = argv[1];
	ifstream testFile((s + ".bench").c_str());
    char ch;
    
	//otestujeme jeho existenciu
	if(!testFile.is_open()){
		cout << "Input file does not exist or the access was denied." << endl;
	    cin.get();
		return EXIT_FAILURE;
	}
	ss.clear();
	//cely obsah suboru prekopirujeme do streamu
    while(!testFile.eof()){
        testFile.get(ch);
        ss << ch;
    }
    testFile.close();
    //zo streamu vytvorime string pre dalsie parsovanie
    s=ss.str();
    
	//vytvorime si novy citac, ktory nam rozparsuje obsah 
	//suboru a vytvori reprezentaciu obvodu
	Reader* reader=new Reader(&s);  
    //cout << "Parsing the input file" << endl;
    Circuit* circuit=reader->parse();
    //cout << "The parsed objects" << endl;
    cout << circuit->toString();
	//vytvorime si novy umiestnovac a rozmiestnime prvky obvodu ako hradla a spoje
    Placer* placer=new Placer(circuit);
    placer->placeCircuit();
    //cout << "The placed pieces" << endl;
    cout << circuit->toString();
	//cout << "Printing circuit." << endl;
	Printer* prnt=new Printer(circuit);
	prnt->printXml(argv[1]);

	cout << "[Press amy key to end.]" << endl;
    cin.get();
    
	//TODO je potreba uvolnit vsetky zabrate prostriedky
    return (EXIT_SUCCESS);
}

