#include "stdafx.h"

#include "InputReader.h"
#include "graph.h"
#include <iostream>
#include <string>
using namespace std;


InputReader::InputReader(const char* fileName, const char* stFileName)
{
	in.open(fileName);
	if(!in.is_open())
	{
		cout<<"Input file "<<fileName<<" doesn't exist!"<<endl;
		exit(1);
	}

	stIn.open(stFileName);
	if(!stIn.is_open())
	{
		cout<<"Input file "<<stFileName<<" doesn't exist!"<<endl;
		in.close();
		exit(1);
	}
}

InputReader::~InputReader()
{
	in.close();
	stIn.close();
}

void InputReader::ReadFirstLine()
{
	in>>strLine;
	assert(strLine[0] == 'g');
	
	in>>strLine;
	assert(strLine[0] == '#');

	in>> gId;
}


bool InputReader::ReadGraph(Graph &g)
{
	ReadFirstLine();
	if(gId == 0)
	{
		return false;                                         /*ÒÑ¾­Ã»ÓÐÍ¼*/
	}
	g.nId = gId;

	in>>strLine;
	assert(strLine[0] == 's');                                /*¶ÁÈ¡¶¥µãÊýºÍ±ßÊý*/
	in>>g.nV>>g.nE;

	assert(g.nV < MAX);                                       /*·ÀÖ¹ÁÚ½Ó¾ØÕó´óÐ¡²»¹»ÓÃ*/

    /*ÏÂÃæ¶ÁÈ¡±ßµÄÐÅÏ¢*/
	int u,v;                                                  /*u,vÊÇÒ»Ìõ±ßµÄÁ½¸ö¶¥µã*/
	for(int i = 1; i <= g.nE; i++)
	{
		in>>strLine;
		assert(strLine[0] == 'e');                            
		in>>u>>v;                                             /*×¢ÒâÕâ¸ö²»ÄÜÓëÏÂÃæµÄÐ´µ½Ò»Æð*/
		in>>g.matrix[u][v].iC>>g.matrix[u][v].dP>>g.matrix[u][v].iLabel;
	}

	return true;
}

void InputReader::ReadSourceSink(int &s, int &t)
{
	stIn>>s>>t;
}