


#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <list.h>
#include <string.h>

using namespace std;

// CGAL setup
#include <CGAL/Cartesian.h>
#include <CGAL/Point_2.h>
#include <CGAL/convex_hull_2.h>

typedef CGAL::Cartesian<float> ptRep;
typedef CGAL::Point_2<ptRep> Point_2;
typedef list<Point_2> ptList;

// maximum length for filenames and stuff
const int MAXLEN = 100;

bool readPolygon(ifstream& inpFile, char** polygonName, ptList** p);
void readRobot(char* fileName);
void readWorld(char* fileName);
void readProblem(char* fileName);

// returns true if a polygon has successfully been read.
// it creates storage for the polygon name and list of points.
bool readPolygon(ifstream& inpFile, char** polygonName, ptList** p) 
{
  *p = new ptList();
  *polygonName = new char[MAXLEN];

  char lineBuffer[MAXLEN];
  char *tokenA;
  char *tokenB;

  //this works for the sample file... not very robust but it does the trick
  while (true) {
    inpFile.getline(lineBuffer, MAXLEN);
    if (inpFile.eof())
      return false;

    //we break up each line into 1 or 2 pieces
    //we look at the pieces to figure out what the line is doing
    float x=0,y=0;
    tokenA=strtok(lineBuffer," ");
    if(tokenA) {
      tokenB=strtok(NULL," ");
    } 
    if(!tokenA) {

    } else if(tokenA[0]=='#') {
      //comment
    } else if(tokenA[0]=='}') {
      //we are done with this object
      break;
    } else if(tokenB[0]=='{') {
      //get the name from tokenA
      strcpy(*polygonName,tokenA);
    } else {
      //get the x and y coordinates
      x=atoi(tokenA);
      y=atoi(tokenB);
      //push onto pointlist
      (**p).push_back(Point_2(x,y));
    }
  }

  cout << "The polygon "<< *polygonName <<" has the points:\n";
  for (ptList::iterator k = (**p).begin(); k != (**p).end(); k++) {
    cout << "  (" << (*k).x() << ", " << (*k).y() << ")\n";
  }
  return true;
}


void readRobot(char* fileName)
{
  char* robotName;
  ptList *robotPoints;

  ifstream robotFile(fileName);

  if (robotFile == NULL) {
    cout << "Error opening the robot file: " << fileName << "\n";
    exit(-1);
  }

  cout << "\nReading the robot description...\n";
  if (!readPolygon( robotFile, &robotName, &robotPoints)) {
    // error reading the robot
    cout << "Error reading the robot file: " << fileName << "\n";
    exit(-1);
  }
}

void readWorld(char* fileName)
{
  char* polygonName;
  ptList *polygonPoints;
  int k = 0;

  ifstream worldFile(fileName);

  if (worldFile == NULL) {
    cout << "Error opening the world file: " << fileName << "\n";
    exit(-1);
  }

  cout << "\nReading the world boundary...\n";
  if (!readPolygon( worldFile, &polygonName, &polygonPoints)) {
    // error reading the robot
    cout << "Error reading the boundary from the world file: " 
	 << fileName << "\n";
    exit(-1);
  }

  cout << "\nReading the obstacles...\n";
  while (readPolygon( worldFile, &polygonName, &polygonPoints))
    k++;

  cout << "\nRead " << k << " obstacles from the world file: "
       << fileName << "\n";
}


void readProblem(char* fileName)
{
  char line[100];
  char robotfile[100];
  char worldfile[100];
  int sx,sy,fx,fy;
  
  ifstream infile(fileName);
  infile>>line;
  while(!infile.eof()) {
      //find the filenames, start and goal
      if(!strcmp("world=",line)) {
	infile>>worldfile;
      } 
      else if(!strcmp("robot=",line)) {
	infile>>robotfile;
      } 
      else if(!strcmp("start=",line)) {
	infile>>sx;
	infile>>sy;
      }
      else if(!strcmp("goal=",line)) {
        infile>>fx;
        infile>>fy;
      }
      infile>>line;
    }

  readRobot(robotfile);
  readWorld(worldfile);
}




int main(int argc, char* argv[])
{
  char fName[MAXLEN];

  cout << "Enter the problem filename\n";
  cin.getline(fName,MAXLEN);
  readProblem(fName);
}
