#include "GPS_Sedlak.h"

char UTMZone[4];
int RefEllipsoid = 23;
Tserial *ts_GPS; 
Tserial *g_Init;
Tserial *ts_Poloha;
int cas = 0;
double ModusDelka = -1.0, ModusSirka = -1.0, ModusVyska = -1.0;


void ConnectAndGetSentence(string identificator, vector<string> &values) {
	int i_GPSErr;
	size_t cutAt;
	string CrcGps;
	string data;

	ts_GPS = new Tserial;											//alokace pameti pro Tserial
	do {
		i_GPSErr = ts_GPS->connect(MAGELLANBASE,9600,spNONE,0);		//propojeni se seriovym portem
	}while (i_GPSErr);

	if (!i_GPSErr) {
		GetSentence(ts_GPS, identificator, data);
	}
	CrcGps = data.substr(data.find("*")+1,2);
	data.erase(data.find("*"),3);
	while((cutAt = data.find_first_of(",")) != data.npos) {				//jednotliva data jsou oddelena carkou
		if(cutAt > 0) {
			values.push_back(data.substr(0, cutAt));					//ulozeni do jednotlivych stringu
		}
		data = data.substr(cutAt+1);									
	}	
	if(data.length() > 0) {
		values.push_back(data);
	}
	values.push_back(CrcGps);
	ts_GPS->disconnect();
	delete ts_GPS;
}
void ConnectAndSetSentence(string Sentence) {
	int i_GPSErr, j;
	char Conf[100] = "";

	ts_GPS = new Tserial;
	i_GPSErr = ts_GPS->connect(MAGELLANBASE,9600,spNONE,0);
	if (!i_GPSErr) {
		j = 0;	
		do {
			sprintf(Conf,"%s\r\n",Sentence.c_str());
			ts_GPS->sendArray(Conf,strlen(Conf));
			j++;
			if(j>5) {
				cout << "Neprovedeno - neposlano" <<endl;
				ts_GPS->disconnect();
				delete ts_GPS;
			}
		}while(GetSentence(ts_GPS,"$PASHR") != "3D");

		cout<<Conf<<endl;
		ts_GPS->disconnect();
		delete ts_GPS;
	}
	else {
		cout << "Neprovedeno - nepripojeno" <<endl;
		ts_GPS->disconnect();
		delete ts_GPS;
	}
}
string GetSentence(Tserial *Target, string identificator, string &data) {	//funkce pro nacitani vety z GPS prijimace
	int ready = 0;															//vystupni parametry jsou: Crc - kontrolni soucet, a data - nasctena veta
	int j = 0, k = 0;
	char c;
	string CrcSoucet;
	string CrcGps;
	char buffer[4] = "";

	while(!ready) {
		while((c = Target->getChar()) != '\n') {
			data.push_back(c);
			j++;
			//cout<<j<<endl;
			if(j>200) {												//osetreni v pripade nenalezeni konce vety
				//cout<<"ERROR1"<<endl;
				return "ERROR";
			}
		}
		if(data.find(identificator.c_str()) != string::npos) {		//nacitani probiha od nastaveneho identifikatoru			 
			ready = 1;
		}
		else {
			ready = 0;
			j = 0;
			data.clear();
		}
	}

	size_t position;
	position = data.find(identificator.c_str());							
	data.erase(0, position+1);											//odmazani dat pred identifikatorem
	//cout<<j<<endl;
	if (j<80) {															//delka spravne vety
		//cout<<"ERROR2"<<endl;
		return "ERROR";	
	}

	CrcGps = data.substr(data.find("*")+1,2);							//vycteni kontrolniho souctu z vety
	char soucet = data[0];
	for (int i=1; i<data.size()-4; i++) {
		soucet^=data[i];												//vypocet kontrolniho souctu
	}
	sprintf(buffer,"%X",soucet);
	CrcSoucet.append(buffer);
	if(CrcSoucet == CrcGps) {
		return CrcSoucet;
	}
	else {
		//cout<<"ERROR3"<<endl;
		return "ERROR";													//vraci ERROR pri rodilne hodnote kontrolniho souctu
	}
}
string GetSentence(Tserial *Target, string identificator) {				//pretizeni fce - nevraci parametr data a neprobiha kontrola delky vety
	int ready = 0;
	int j = 0;
	char c;
	string CrcSoucet;
	string CrcGps;
	string data;
	char buffer[4] = "";

	while(!ready) {
		while((c = Target->getChar()) != '\n') {
			data.push_back(c);
			j++;
			if(j>200) {														//osetreni v pripade nenalezeni konce vety
				return "ERROR";
			}
		}
		if(data.find(identificator.c_str()) != string::npos)				
			ready = 1;		
	}

	size_t position;
	position = data.find(identificator.c_str());
	data.erase(0, position+1);

	CrcGps = data.substr(data.find("*")+1,2);						
	char soucet = data[0];
	for (int i=1; i<data.size()-4; i++) {
		soucet^=data[i];
	}
	sprintf(buffer,"%X",soucet);
	CrcSoucet.append(buffer);
	if(CrcSoucet == CrcGps) {
		return CrcSoucet;
	}
	else {
		return "ERROR";		
	}
}

void Magellan_NacteniGPS(GPSSTRUCT &gpsData, string data) {				//fce pro jednotlive rozdeleni string data do vectoru values	
	vector<string> values;												//fce vraci strukturu GPSSTRUCT
	values.clear();
	GPSSTRUCT GPSm;
	size_t cutAt;

	while((cutAt = data.find_first_of(",")) != data.npos) {				//jednotliva data jsou oddelena carkou
		if(cutAt > 0) {
			values.push_back(data.substr(0, cutAt));					//ulozeni do jednotlivych stringu
		}
		data = data.substr(cutAt+1);									
	}	
	if(data.length() > 0) {
		values.push_back(data);
	}
	double tmpFlt;
	ostringstream tmpStr;
	tmpStr.precision(20);

	// D_SIRKA - prevod z formatu hhmm.mmmmm do hh.mmmmmm
	tmpFlt = atof(values[D_SIRKA].substr(0,2).c_str()) + (atof(values[D_SIRKA].substr(2).c_str())/60.0);
	tmpStr.str("");
	tmpStr << tmpFlt;
	values[D_SIRKA] = tmpStr.str();
	//cout<<values[D_SIRKA]<<endl;

	// D_DELKA prevod z formatu hhhmm.mmmmm do hhh.mmmmmm
	tmpFlt = atof(values[D_DELKA].substr(0,3).c_str()) + (atof(values[D_DELKA].substr(3).c_str())/60.0);
	tmpStr.str("");
	tmpStr << tmpFlt;
	values[D_DELKA] = tmpStr.str();
	//cout<<"Merr3"<<endl;

	// D_RYCHLOSTUZLY - prevod na rychlost km/h
	tmpFlt = atof(values[D_RYCHLOST].c_str()) * 1.852;
	tmpStr.str("");
	tmpStr << tmpFlt;
	values[D_RYCHLOST] = tmpStr.str();
	//cout<<"Merr4"<<endl;

	// UTM - prevod do UTM souradnic
	double utmX, utmY;
	LLtoUTM(RefEllipsoid, atof(values[D_SIRKA].c_str()), atof(values[D_DELKA].c_str()),
		utmY, utmX, UTMZone);
	//cout<<"Merr5"<<endl;
	
	//ulozeni do struktury gpsData
	gpsData.d_cas = atof(values[D_CAS].c_str());			
	gpsData.d_delka = atof(values[D_DELKA].c_str());
	gpsData.d_rychlost = atof(values[D_RYCHLOST].c_str());
	gpsData.d_sirka = atof(values[D_SIRKA].c_str());
	gpsData.d_vyska = atof(values[D_VYSKA].c_str());
	gpsData.d_uhel = atof(values[D_UHEL].c_str());
	gpsData.d_utmx = utmX;
	gpsData.d_utmy = utmY;
	gpsData.i_korekce = atoi(values[I_KOREKCE].c_str());
	gpsData.i_pocetsat = atoi(values[I_POCETSAT].c_str());
	gpsData.s_delkasektor=values[S_DELKASEKTOR];
	gpsData.s_sirkasektor=values[S_SIRKASEKTOR];
}
int Poloha(GPSSTRUCT &gpsData) {											//funkce pro nacteni polohy potrebne pro zadani do Base station a prevod do spravneho formatu
	double delka_min, sirka_min, delka_stup, sirka_stup;
	int i_PolohaErr;
	string data;
	GPSSTRUCT GPS1;

	ts_Poloha = new Tserial;												//alokace pameti pro Tserial
	do {
		i_PolohaErr = ts_Poloha->connect(MAGELLANBASE,9600,spNONE,0);		//propojeni se seriovym portem
	}while (i_PolohaErr);

	if (!i_PolohaErr) {
		//cout<<"err1"<<endl;
		if (GetSentence(ts_Poloha, "$PASHR,POS", data)!="ERROR") {		//nacteni vety
		//	cout<<"err2"<<endl;
			Magellan_NacteniGPS(GPS1, data);							//ulozeni do struktury GPS1
		//	cout<<"err3"<<endl;
		}
		else {
			ts_Poloha->disconnect();									//odpojeni seriveho portu								
			delete ts_Poloha;											//dealokace pameti
			return EXIT_FAILURE;										//navratova hodnota
		}
	}
	else { 
		ts_Poloha->disconnect();
		delete ts_Poloha;
		return EXIT_FAILURE;
	}
	gpsData.d_cas = GPS1.d_cas;
	gpsData.i_pocetsat = GPS1.i_pocetsat;

	delka_min = modf (GPS1.d_delka, &delka_stup);						//prevod do potrebneho formatu hhmm.mmmm
	gpsData.d_delka = delka_stup * 100 + delka_min * 60;
	sirka_min = modf (GPS1.d_sirka, &sirka_stup);
	gpsData.d_sirka = sirka_stup * 100 + sirka_min * 60;
	gpsData.d_vyska = GPS1.d_vyska;

	ts_Poloha->disconnect();
	delete ts_Poloha;	
	return EXIT_SUCCESS;
}
int Magellan_Init_Base(string COM, double delka, double sirka, double vyska) {	//funkce pro inicializaci Base station pokud zname presnou polohu
	int i = 0;																	//tvar hhmm.mmmmm, hhhmm.mmmmm, aaaaa.aa
	char Conf[40] = "";
	char buffer[5] = "";
	string data;

	g_Init = new Tserial();
	sprintf(buffer,"%s",COM.c_str());												//prevedeni vstupniho parametru COM do potrebneho tvaru

	int i_InitErr = g_Init->connect(buffer, 9600, spNONE, 0);

	//inicializace BASE station
	if (!i_InitErr) {
		cout<<"Probiha inicializace BASE STATION"<<endl;
		do {
			sprintf(Conf,"$PASHS,RST\r\n");											//reset zarizeni
			g_Init->sendArray(Conf,strlen(Conf));									//poslani vety do Base station
			i++;	
			if(i>5) {																//pokud se vyskytla chyba
				g_Init->disconnect();
				delete g_Init;
				return EXIT_FAILURE;
			}
		}while(GetSentence(g_Init,"$PASHR") != "3D");								//cekani na odpoved
		cout<<"RESET"<<endl;

		i = 0;	
		do {
			sprintf(Conf,"$PASHS,POS,%010.5f,N,%011.5f,E,%08.2f\r\n",delka, sirka, vyska);		//nastaveni posilani dat
			g_Init->sendArray(Conf,strlen(Conf));
			i++;
			if(i>5) {
				g_Init->disconnect();
				delete g_Init;
				return EXIT_FAILURE;
			}
		}while(GetSentence(g_Init,"$PASHR") != "3D");	
		cout<<Conf<<endl;

		i = 0;		
		do {
			sprintf(Conf,"$PASHS,RTC,BAS,B\r\n");												//nastaveni base station na port B - posilani korekci
			g_Init->sendArray(Conf,strlen(Conf));
			i++;
			if(i>5) {
				g_Init->disconnect();
				delete g_Init;
				return EXIT_FAILURE;
			}
		}while(GetSentence(g_Init,"$PASHR") != "3D");
		cout<<Conf<<endl;

		i = 0;	
		do {
			sprintf(Conf,"$PASHS,DYN,1\r\n");												//dynamicky model - staticky
			g_Init->sendArray(Conf,strlen(Conf));
			i++;
			if(i>5) {
				g_Init->disconnect();
				delete g_Init;
				return EXIT_FAILURE;
			}
		}while(GetSentence(g_Init,"$PASHR") != "3D");
		cout<<Conf<<endl;

		i = 0;	
		do {
			sprintf(Conf,"$PASHS,NME,POS,A,ON,0.5\r\n");									//nastaveni posilani dane vety na port A
			g_Init->sendArray(Conf,strlen(Conf));
			i++;
			if(i>5) {
				return EXIT_FAILURE;
			}
		}while(GetSentence(g_Init,"$PASHR") != "3D");
		cout<<Conf<<endl;

		i = 0;	
		do {
			sprintf(Conf,"$PASHS,SAV,Y\r\n");					//ulozeni do Base station
			g_Init->sendArray(Conf,strlen(Conf));
			i++;
			if(i>5) {
				g_Init->disconnect();
				delete g_Init;
				return EXIT_FAILURE;
			}
		}while(GetSentence(g_Init,"$PASHR") != "3D");
		cout<<"Ulozeni"<<endl;

		g_Init->disconnect();
		delete g_Init;
		cout<<"Dokoncena inicializace BASE STATION"<<endl;
		return EXIT_SUCCESS;
	}
	else {
		g_Init->disconnect();
		delete g_Init;
		cout<<"Dokoncena inicializace BASE STATION s CHYBOU"<<endl;
		return EXIT_FAILURE;
	}
}

int Magellan_Init_Base(string COM) {	//pretizeni fce pri nezname presne hodnote
	int i = 0;
	char Conf[40] = "";
	char buffer[5] = "";
	string data;
	g_Init = new Tserial();
	sprintf(buffer,"%s",COM.c_str());

	int i_InitErr = g_Init->connect(buffer, 9600, spNONE, 0);

	if (!i_InitErr) {
		cout<<"Probiha inicializace BASE STATION - neni zadana presna poloha"<<endl;
		do {
			sprintf(Conf,"$PASHS,RST\r\n");											//reset zarizeni
			g_Init->sendArray(Conf,strlen(Conf));
			i++;
			if(i>5) {
				g_Init->disconnect();
				delete g_Init;
				return EXIT_FAILURE;
			}
		}while(GetSentence(g_Init,"$PASHR") != "3D");
		cout<<"RESET"<<endl;

		i = 0;		
		do {
			sprintf(Conf,"$PASHS,RTC,BAS,B\r\n");				//nastaveni base station na port B - posilani korekci na port B
			g_Init->sendArray(Conf,strlen(Conf));
			i++;
			if(i>5) {
				g_Init->disconnect();
				delete g_Init;
				return EXIT_FAILURE;
			}
		}while(GetSentence(g_Init,"$PASHR") != "3D");
		cout<<Conf<<endl;

		i = 0;	
		do {
			sprintf(Conf,"$PASHS,DYN,1\r\n");					//dynamicky model
			g_Init->sendArray(Conf,strlen(Conf));
			i++;
			if(i>5) {
				g_Init->disconnect();
				delete g_Init;
				return EXIT_FAILURE;
			}
		}while(GetSentence(g_Init,"$PASHR") != "3D");
		cout<<Conf<<endl;

		i = 0;	
		do {
			sprintf(Conf,"$PASHS,NME,POS,A,ON,0.5\r\n");		//nastaveni posilani dat
			g_Init->sendArray(Conf,strlen(Conf));
			i++;
			if(i>5) {
				return EXIT_FAILURE;
			}
		}while(GetSentence(g_Init,"$PASHR") != "3D");
		cout<<Conf<<endl;

		i = 0;	
		do {
			sprintf(Conf,"$PASHS,SAV,Y\r\n");					//ulozeni
			g_Init->sendArray(Conf,strlen(Conf));
			i++;
			if(i>5) {
				g_Init->disconnect();
				delete g_Init;
				return EXIT_FAILURE;
			}
		}while(GetSentence(g_Init,"$PASHR") != "3D");
		cout<<"Ulozeni"<<endl;

		g_Init->disconnect();
		delete g_Init;
		cout<<"Dokoncena inicializace BASE STATION - neni zadana presna poloha"<<endl;
		return EXIT_SUCCESS;
	}
	else {
		g_Init->disconnect();
		delete g_Init;
		cout<<"Dokoncena inicializace BASE STATION s CHYBOU - neni zadana presna poloha"<<endl;
		return EXIT_FAILURE;
	}
}
int Base (GPSSTRUCT gpsData) {	//funkce pro zpresnovani polohy pomoci medianu a ulozeni do souboru
	GPSSTRUCT gpsm;
	double delka_min, sirka_min, delka_stup, sirka_stup, delka, sirka, count = 0;
	int i_DGPSErr, j;
	Tserial *ts_DGPS;
	char Conf[100] = "";
	char Ref[100] = "";
	char Text[100] = "";
	ofstream myfile;
	cout.precision(5);
	cout.setf(ios::fixed);
	myfile.precision(6);
	myfile.setf(ios::fixed);
	vector<double>::iterator vIt;
	map<double, int>::iterator cIt;
	
	if(!Poloha(gpsm)) {
		cas++;
		cout<<cas<<", ";

		vPoleDelka.push_back(gpsm.d_delka);
		vPoleSirka.push_back(gpsm.d_sirka);
		vPoleVyska.push_back(gpsm.d_vyska);
		
		if (cas > CEKACIDOBA) {			
			//Modus
			for(vIt = vPoleDelka.begin(); vIt != vPoleDelka.end(); vIt++) {
				// Pokud jeste neobsahuje double hodnotu tak se prida
				if(counter.find(*vIt) == counter.end()) {
					counter[*vIt] = 1;
				}
				else {
					counter[*vIt]++;
				}
			}
			// Vyhleda nejcasteji se vyskytujici hodnotu v te mape a vypise ji -> je v promenne max
			// v count je kolikrat tam byla
			for(cIt = counter.begin(); cIt != counter.end(); cIt++) {
				// Pokud je vyskyt prvku vetsi nez predchoziho maxima tak se nahradi (hleda jen max)
				if(cIt->second  > count) {
					ModusDelka = cIt->first;
					count = cIt->second;
				}
			}
			counter.clear();
			count = 0;

			for(vIt = vPoleSirka.begin(); vIt != vPoleSirka.end(); vIt++) {
				if(counter.find(*vIt) == counter.end()) {
					counter[*vIt] = 1;
				}
				else {
					counter[*vIt]++;
				}
			}
			for(cIt = counter.begin(); cIt != counter.end(); cIt++) {
				if(cIt->second  > count) {
					ModusSirka = cIt->first;
					count = cIt->second;
				}
			}
			counter.clear();
			count = 0;

			for(vIt = vPoleVyska.begin(); vIt != vPoleVyska.end(); vIt++) {
				if(counter.find(*vIt) == counter.end()) {
					counter[*vIt] = 1;
				}
				else {
					counter[*vIt]++;
				}
			}
			for(cIt = counter.begin(); cIt != counter.end(); cIt++) {
				if(cIt->second  > count) {
					ModusVyska = cIt->first;
					count = cIt->second;
				}
			}
			counter.clear();
			count = 0;

			//cout << "Modus Sirka: " << ModusSirka << ", Modus Delka: " << ModusDelka << ", Modus Vyska: " << ModusVyska<< endl;
			sirka_min = modf ((ModusSirka)/100, &sirka_stup);
			ModusSirka = sirka_stup + sirka_min * 100/60;
			delka_min = modf (ModusDelka/100, &delka_stup);
			ModusDelka = delka_stup + delka_min * 100/60;

			//Median
			sort(vPoleDelka.begin(),vPoleDelka.end());
			sort(vPoleSirka.begin(),vPoleSirka.end());
			sort(vPoleVyska.begin(),vPoleVyska.end());	
			if(vPoleDelka.size()%2) {
				MedianDelka=(vPoleDelka[floor((float)vPoleDelka.size()/2)]+vPoleDelka[ceil((float)vPoleDelka.size()/2)])/2;
				MedianSirka=(vPoleSirka[floor((float)vPoleSirka.size()/2)]+vPoleSirka[ceil((float)vPoleSirka.size()/2)])/2;
				MedianVyska=(vPoleVyska[floor((float)vPoleVyska.size()/2)]+vPoleVyska[ceil((float)vPoleVyska.size()/2)])/2;
				//cout<<"9"<<endl;
			}
			else {
				MedianDelka=vPoleDelka[vPoleDelka.size()/2];
				MedianSirka=vPoleSirka[vPoleSirka.size()/2];
				MedianVyska=vPoleVyska[vPoleVyska.size()/2];
				//cout<<"10"<<endl;
			}

			/*cout << MedianDelka << endl;
			cout << MedianSirka << endl;
			cout << MedianVyska << endl;*/

			gpsData.d_sirka = MedianSirka;
			gpsData.d_delka = MedianDelka;
			gpsData.d_vyska = MedianVyska;
			//cout<<"11"<<endl;

			cas = 0;

			delka_min = modf ((gpsData.d_delka)/100, &delka_stup);
			delka = delka_stup + delka_min * 100/60;
			//cout<<"12"<<endl;

			sirka_min = modf ((gpsData.d_sirka)/100, &sirka_stup);
			sirka = sirka_stup + sirka_min * 100/60;
			//cout<<"13"<<endl;

			//spravny tvar vety potrebny pro poslani
			sprintf(Conf,"$PASHS,POS,%010.5f,N,%011.5f,E,%08.2f\r\n", gpsData.d_sirka, gpsData.d_delka, gpsData.d_vyska);
			sprintf(Ref,"%010.5f, %011.5f, %08.2f", gpsData.d_sirka, gpsData.d_delka, gpsData.d_vyska);
			sprintf(Text,"Cas: %8.2f Sirka: %010.5f Delka: %011.5f Vyska: %08.2f",gpsm.d_cas, gpsData.d_sirka, gpsData.d_delka, gpsData.d_vyska);

			//vypis na obrazovku
			cout<<"Pocet satelitu: "<<gpsm.i_pocetsat<<endl;			
			cout<<Text<<endl;
			cout << "Modus Sirka: " << ModusSirka << ", Modus Delka: " << ModusDelka << ", Modus Vyska: " << ModusVyska <<endl;
			cout<<"Sirka: "<<sirka<<" Delka: "<<delka<<endl<<endl;
			

			//ulozeni do souboru - pouze posledni poloha
			myfile.open("ReferPoloha.txt");
			myfile<<"Pocatecni cas: "<<gpsData.d_cas<<endl;
			myfile<<"Koncovy cas: "<<gpsm.d_cas<<endl<<endl;
			//myfile<<"Delka trvani: "<<gpsm.d_cas - gpsData.d_cas<<endl;
			myfile<<"Pocet satelitu: "<<gpsm.i_pocetsat<<endl;
			myfile<<sirka<<", "<<delka<<endl;
			myfile<< "Modus: " << ModusSirka << ", " << ModusDelka <<", " << ModusVyska <<endl;
			myfile<<Ref<<endl;
			myfile<<Conf<<endl;
			myfile.close();

			//ulozeni do souboru - vsechny polohy
			myfile.open("VsePoloha.txt",ifstream::app);
			myfile<<"Pocet satelitu: "<<gpsm.i_pocetsat<<endl;
			myfile<<"Cas: "<<gpsm.d_cas<<endl;
			myfile<<Ref<<endl;
			myfile<< "Modus: " << ModusSirka << ", " << ModusDelka <<", " << ModusVyska <<endl;
			myfile<<sirka<<", "<<delka<<endl<<endl;
			myfile.close();

			//Ulozeni nove pozice do BASE STATION
			//ts_DGPS = new Tserial;
			//i_DGPSErr = ts_DGPS->connect(MAGELLANBASE,9600,spNONE,0);
			//if (!i_DGPSErr) {
			//	j = 0;	
			//	do {
			//		ts_DGPS->sendArray(Conf,strlen(Conf));
			//		j++;
			//		if(j>5) {
			//			ts_DGPS->disconnect();
			//			delete ts_DGPS;
			//			return EXIT_FAILURE;
			//		}
			//	}while(GetSentence(ts_DGPS,"$PASHR") != "3D");
			//	
			//	j = 0;	
			//	do {
			//		sprintf(Conf,"$PASHS,SAV,Y\r\n");					//ulozeni
			//		ts_DGPS->sendArray(Conf,strlen(Conf));
			//		j++;
			//		if(j>5) {
			//			ts_DGPS->disconnect();
			//			delete ts_DGPS;
			//			return EXIT_FAILURE;
			//		}
			//	}while(GetSentence(ts_DGPS,"$PASHR") != "3D");

			//	ts_DGPS->disconnect();
			//	delete ts_DGPS;
			//	return EXIT_SUCCESS;
			//}
			//else {
			//	ts_DGPS->disconnect();
			//	delete ts_DGPS;
			//	return EXIT_FAILURE;
			//}
		}
	}
	else {
		return EXIT_FAILURE;
	}
}
