/* File: travel.c
 * Description:
 * This program finds the general shortest route between two cities
 * chosen by the user. It calculates the shortest route using a text file
 * containing all possible routes together with the distances involved.
 *
 * The user is prompted to enter the text file containing the distances
 * between cities. The user is then asked for a starting point and a
 * destination point, selected from one of the available cities defined
 * in cities.h.
 *
 * The program then uses stacks (implemented in the module defined by
 * search.h and search.cpp) to find the shortest path, if a path exists.
 *
 * The user is able to enter as many pair of cities as they wish, and is
 * able to process as many route files as they wish, in one instance of
 * the program.
 *
 * Cristina Cifuentes
 * 3 August 1997
 * 18 August 1997 - added citygraph.h include, plus changed the type
 *    of flightRoutes to Graph instead of 2-D array.
*/

#include <stdio.h>
#include "strlib.h"
#include "cities.h"
#include "citystk.h"
#include "citygraph.h"
#include "search.h"	/* implementation of the search algorithm */
#include "iotravel.h"	/* module which scans a given text file and returns
			   the flight route array.  It also handles user io
			   functions.  */



int main (void)
{ int exitCode;
  Graph flightRoute;		/* Graph of city pairs and flight distances */
  cityPath bestPath;		/* Stores the best route between the cities */
  cityPair trip;		/* Stores the overall trip (starting city
				 * and destination city)  */
  int routeLength;		/* Stores the length of the shortest route. */
  String fileName;		/* Name of the input file */
  char ch;

	displayTitleAndInformation();

	inputRouteFileName(fileName);
	flightRoute = graphNew();
	exitCode = inputFlightRoutes(fileName, flightRoute);

#ifdef DEBUG
	displayGraphContents (flightRoute);
#endif

	switch (exitCode) {
	  case 1:	/* non-existent file */
			printf ("\nERROR: The file '%s' could not be found!\n",
					fileName);
			break;

	  case 2: 	/* errors in file */
			printf ("\nWARNING: The route file '%s' contains errors!\n",
					fileName);

			/* This case does not contain a break. If the file
			   does contain errors, the program ignores them
			   but notifies the user of their existence. However,
			   it does permit the program to continue operating.
			*/

	  case 0: 	/* Valid file */
			printf ("\n");
			outputAvailableRoutes();
			do {
				inputTrip (&trip);
				exitCode = findShortestPath (
						flightRoute, trip,
						bestPath, &routeLength);

				if (exitCode==0)
				{
					outputRoute(trip, bestPath,
						flightRoute, routeLength);
					printf ("\nDo you wish to enter another trip for the file '%s'? (enter Y/N)",
							fileName);
					fflush (stdin);
					scanf ("%c", &ch);

					if (ch=='N' || ch=='n')
						exitCode = -1;
					else
						exitCode=0;
				}
			} while (exitCode==0);  /* the trip loop */
	}

	graphFree (&flightRoute);
	return exitCode;
}


