/* File: iotravel.c
 * Description: this is the implementation of the module which handles
 * the input and output of data for travel.c.
 * Cristina Cifuentes
 * 3 August 1997
 * 18 August 1997 - replaced type FlightRouteType for Graph.
 *    Updates uses of variables previously of type FlightRouteType
 *    to their Graph counterpart.
*/

#include <stdio.h>
#include "bool.h"
#include "cities.h"
#include "strlib.h"
#include "iotravel.h"


/* IMPLEMENTATION */

void displayTitleAndInformation()
{
	printf ("\nSHORTEST TRAVELLING PATH PROGRAM\n");
	printf ("This program allows the user to specify a text file which contains\n");
	printf ("a list of the distances between pairs of major Australian capital cities.\n");
	printf ("The user then specifies a starting city and a destination city, and the \n");
	printf ("program finds the shortest path between those cities, if such a path exists.\n");
	printf ("The user is able to enter unlimited trips for any numnber of route files.\n\n");
	printf ("The only restriction is on the cities which can be used; they must be one of\n");
	printf ("the major population centres listed below.\n\n");
	printf ("\n");
}


static void dispC (City C)
{
	printf ("%s", cityName[(int)C]);
}


void inputRouteFileName (String S)
{
	printf ("Please enter the name of the route file: ");
	scanf ("%s", S);
}


int inputFlightRoutes (String routeFileName, Graph fr)
/* Opens the given file, and reads the stored distances into the
 * graph fr.	*/
{ FILE *routeFile;
  String sFrom, sTo;
  cityPair cPair;
  int nLength,
      exitCode = 0;	/* Default exit code is 0  */

	routeFile = fopen (routeFileName, "r");
	if (routeFile == NULL)
		exitCode = 1;		/* failed to open file */
	else
	{	/* Traverse the input data file */
		while (! feof (routeFile))
		{
			fscanf (routeFile, "%s %s %d", sFrom, sTo, &nLength);
#ifdef DEBUG
printf ("Read from file\n\tsFrom=%s, sTo=%s, length=%d\n", sFrom,sTo,nLength);
#endif

			cPair.from = stringToCity (sFrom);
			cPair.to = stringToCity (sTo);

			if ((cPair.from != NoCity) && (cPair.to != NoCity) &&
			    (nLength > 0))
				addCityPairDistance (fr, cPair, nLength);
			else	/* error code indicating an error in the file */
				exitCode = 2;
		}

		fclose (routeFile);
	}
	return exitCode;
}


void inputTrip (cityPair *cPair)
/* Pass the city pair by reference, asks the user to enter a starting
 * city and a destination city of known name.  */
{ String s;

	cPair->from = cPair->to = NoCity;

	printf ("\nPlease enter your starting city: ");
	scanf ("%s", s);
	cPair->from = stringToCity (s);
	while (cPair->from == NoCity)
	{
		printf ("\n'%s' is not a recognized city.\n", s);
		outputAvailableRoutes();
		printf ("\nPlease enter one of the above for your starting city: ");
		scanf ("%s", s);
		cPair->from = stringToCity (s);
	}

	printf ("Please enter your destination city: ");
	scanf ("%s", s);
	cPair->to = stringToCity (s);
	while (cPair->to == NoCity)
	{
		printf ("\n'%s' is not a recognised city.\n", s);
		outputAvailableRoutes();
		printf ("\nPlease enter one of the above for your destination city: ");
		scanf ("%s", s);
		cPair->to = stringToCity (s);
	}

#ifdef DEBUG
printf ("From city = %s, to city = %s\n", cityName[(int)cPair->from],
	cityName[(int)cPair->to]);
#endif

}


void outputAvailableRoutes()
/* scans through the available cities, and displays as output the
 * names of each in a list format */
{ int i;

	printf ("The cities on the map are:\n");
	for (i=0; i<=NUMCITIES; i++)
		printf ("    %s\n", cityName[i]);
}


void outputRoute (cityPair cPair, cityPath P, Graph fr,
		  int routeLength)
/* Displays a route. There are three cases:
 *	1. No route exists
 * 	2. The starting point is the end point
 *	3. The route is of any other non-degenerate type
 */
{ int i=0;
  cityPair pair;

	if (P[0] == NoCity)
	{
		printf ("\nThere is no way to get from ");
		dispC (cPair.from);
		printf (" to ");
		dispC (cPair.to);
		printf (".\n");
	}
	else
	{
		printf ("\nTo get from ");
		dispC (cPair.from);
		printf (" to ");
		dispC (cPair.to);

		if (cPair.from == cPair.to)
		{
			printf (", just stay put. You are already there!\n");
		}
		else
		{
			printf (", take the following route:\n");

			/* display the path until a NoCity is reached */
			while ((i <= NUMCITIES-1) && (P[i+1] != NoCity))
			{
				printf ("\t");
				dispC (P[i]);
				printf (" to ");
				dispC (P[i+1]);
				pair.to = P[i];
				pair.from = P[i+1];
				printf (": length %d kms\n",
					distanceBetweenCities (fr, pair));
				i++;
			}
			printf ("This route has total (minimised) length of %d kms.\n",
				routeLength);
		}
	}
}



