//#include <stdafx.h>
#include <string>
#include <vector>
#include <windows.h>
#include <fstream>
#include <windows.h>
#include "baseland.h" //TODO: Rename
using namespace std;
double parser2::myatof(string s)
{
double d = atof(s.c_str());
if (d == 0 && s.at(0) != '0')
{
errorOut("error converting " + s + " to double.");
return -1;
}
return d;
}
vector<waypoint> parser2::getRoute(string fileName)
{
//this->readFileLocal(fileName.c_str());
fileString = readFileLocal(fileName.c_str());
fileString = this->replaceAll();
vector<point> points = getPoints();
runways = getRunways(points);
meineroute = getRoute(points,runways);
return meineroute;
}
int parser2::myatoi(string s)
{
int i = atoi(s.c_str());
if (i == 0 && s.at(0) != '0')
{
errorOut("error converting " + s + " to int.");
return -1;
}
return i;
}
vector<waypoint> parser2::getRoute(vector<point> points, vector<runway> runways)
{
vector<waypoint> waypoints;
string str = getElements("WAYPOINTS");
if (str.size() == 0)
{
errorOut("Error parsing file!\nEND_WAYPOINTS not found after BEGIN_WAYPOINTS?\n");
return waypoints;
}
vector<string> ptbuff = split(str,"\n");
for (int ip =0; ip<ptbuff.size(); ip++)
{
waypoint wp;
//parse comment
int startComment = ptbuff.at(ip).find("\"");
if (startComment > 0)
{
int endComment = ptbuff.at(ip).find("\"",startComment+1);
if (endComment > startComment)
{
wp.comment = ptbuff.at(ip).substr(startComment+1,endComment-startComment-1);
printf("gut!");
}
}
else
wp.comment = "";
vector<string> elementStr = split(ptbuff.at(ip)," ");
int id = myatoi(elementStr.at(1));
if (elementStr.at(2) == "TURN") wp.type = 0;
else if (elementStr.at(2) == "LOOP") wp.type = 1;
else if (elementStr.at(2) == "TAKEOFF") wp.type= 2;
else if (elementStr.at(2) == "LAND") wp.type= 3;
wp.radius = myatof (elementStr.at(3));
if (elementStr.size() > 4)
{
wp.alt = myatof(elementStr.at(4));
wp.speed = myatof(elementStr.at(5));
}
else
{
wp.alt = 0;
wp.speed = 0;
}
if (elementStr.at(0) == "RUNWAY")
{
wp.lat = (runways.at(id).center.lat);
wp.lon = (runways.at(id).center.lon);
//runways.at(id).wpIds.push_back(ip);
}
else if (elementStr.at(0) == "POINT")
{
wp.lat = degToRad(points.at(id).lat);
wp.lon = degToRad(points.at(id).lon);
}
waypoints.push_back(wp);
}
printf("%s",str.c_str());
return waypoints;
}
void parser2::errorOut(string errormessage)
{
printf(errormessage.c_str());
}
vector<runway> parser2::getRunways(vector<point> points)
{
vector<runway> runways;
string runwaysStr = getElements("RUNWAYS");
if (runwaysStr.size() == 0)
{
errorOut("Error parsing file!\nEND_RUNWAYS not found after BEGIN_RUNWAYS?\n");
return runways;
}
vector<string> ptbuff = split(runwaysStr,"\n");
for (int ip =0; ip<ptbuff.size(); ip++)
{
vector<string> elementStr = split(ptbuff.at(ip)," ");
int ip1 = myatoi(elementStr.at(0));
int ip2 = myatoi(elementStr.at(1));
if (ip1 == -1|| ip2 == -1)
return runways;
point p1,p2;
p1.lon = degToRad(points.at(ip1).lon);
p1.lat = degToRad(points.at(ip1).lat);
p2.lon = degToRad(points.at(ip2).lon);
p2.lat = degToRad(points.at(ip2).lat);
runway rw;
rw.p1 = p1;
rw.p2 = p2;
double delta = 0;
if (rw.p1.lat > rw.p2.lat)
{
delta = rw.p1.lat - rw.p2.lat;
rw.center.lat = rw.p2.lat + (delta / 2);
}
if (rw.p1.lat < rw.p2.lat)
{
delta = rw.p2.lat - rw.p1.lat;
rw.center.lat = rw.p1.lat + (delta / 2);
}
if (rw.p1.lon > rw.p2.lon)
{
delta = rw.p1.lon - rw.p2.lon;
rw.center.lon = rw.p2.lon + (delta / 2);
}
if (rw.p1.lon < rw.p2.lon)
{
delta = rw.p2.lon - rw.p1.lon;
rw.center.lon = rw.p1.lon + (delta / 2);
}
/*
rw.center.lat = p1.lat; //TODO: Calculateting Centerpoint
rw.center.lon = p1.lon; //TODO: Calculateting Centerpoint
*/
runways.push_back(rw);
}
return runways;
}
vector<point> parser2::getPoints()
{
string allpoints = getElements("POINTS");
vector<point> points;
if (allpoints.size() == 0)
{
errorOut("Error parsing file!\nEND_POINTS not found after BEGIN_POINTS?\n");
return points;
}
vector<string> ptbuff = split(allpoints,"\n");
vector<runway> runways;
for (int ip =0; ip<ptbuff.size(); ip++)
{
point p;
vector<string> elementStr = split(ptbuff.at(ip)," ");
p.lon = myatof(elementStr.at(0));
p.lat = myatof(elementStr.at(1));
points.push_back(p);
}
return points;
}
void parser2::readFile(std::string fileName)
{
this->readFileLocal(fileName.c_str());
}
parser2::parser2()
{
}
parser2::parser2(string fileName)
{
string testStr = "0";
fileString = readFileLocal(fileName.c_str());
fileString = this->replaceAll();
vector<point> points = getPoints();
runways = getRunways(points);
meineroute = getRoute(points,runways);
}
string parser2::replaceAll()
{
fileString = this->replace(fileString,"\t"," ");
fileString = this->replace(fileString," "," ");
fileString = this->replace(fileString,"\n ","\n");
fileString = this->replace(fileString,"\n\n","\n");
return fileString;
}
string parser2::readFileLocal(const char* filename)
{
try
{
string fstring;
ifstream fin(filename);
if (fin == NULL)
{
throw 0;
}
string zeile;
getline(fin, zeile);
fstring = fstring + zeile;
do
{
getline(fin, zeile);
zeile = zeile.substr(0,zeile.find("#"));
fstring = fstring + zeile + "\n";
} while (!fin.eof());
return fstring;
}
catch(...)
{
return "";
}
}
string parser2::replace(string source,string replace, string by)
{
int pos = source.find(replace);
while (pos >= 0)
{
source = source.replace(pos,replace.length(),by);
pos = source.find(replace);
}
return source;
}
vector<string> parser2::split(string sourceString,string delimitter)
{
std::vector<std::string> elements;
char * pch;
char * buffer1 = new char[sourceString.length()];
strcpy(buffer1,sourceString.c_str());
char * buffer2 = new char[delimitter.length()];
strcpy(buffer2,delimitter.c_str());
pch = strtok (buffer1,buffer2);
if (pch != NULL)
{
std::string buffer3(pch);
elements.push_back(buffer3);
}
while(pch != NULL)
{
pch = strtok (NULL,buffer2);
if (pch != NULL)
{
std::string buffer4(pch);
elements.push_back(buffer4);
}
}
return elements;
}
string parser2::getElements(string name)
{
try
{
string nodename = name;
string nodenameStart = "BEGIN_" + nodename;
string nodenameEnd = "END_" + nodename;
int start = fileString.find(nodenameStart) + nodenameStart.length();
int end = fileString.find(nodenameEnd);
if (end<start) throw 1;
if (end==-1 && start >= 0) throw 1;
string elements = fileString.substr(start,end-start);
if (elements.find("\n") == 0)
elements = elements.substr(1,elements.length());
int test1 = elements.find("\n");
if(elements.at(elements.length()-1) == '\n')
elements = elements.substr(0,elements.length()-1);
return elements;
}
catch(...)
{
return "";
}
}
/*
int _tmain(int argc, _TCHAR* argv[])
{
parser2 parser("C:\\Orbiter\\2 2.txt");
system("pause");
return 0;
}
*/