/* 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 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, by pressing Y or N to continue after each iteration.
 *
 * Cristina Cifuentes
 * 3 August 1997
*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include <assert.h>

#include "strlib.h"
#include "cities.h"
#include "readfile.h"	/* inputFlightRoutes() */

#include "cityroutes.h"

int main(void)
{ 
     FlightRouteType flightRoute;	/* Array of flight distances */
     String fileName;		/* Name of the input file */
     char ch, *pch;
     int exitCode;

     /* Read in data file name (e.g. routes.txt) */
     printf ("\nPlease enter the name of the route file: ");
     fgets(fileName, sizeof(fileName), stdin);
     pch = fileName + stringLength(fileName) - 1;
     while ( *pch == '\n' || *pch == '\r' || isspace(*pch) ) 
	  *(pch--) = '\0';

     /* Read in data file into 2-dimensional array flightRoute */
     exitCode = inputFlightRoutes (fileName, flightRoute);

     /* Check exitCode & exit or continue */
     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");
	  do {
	       String input_buffer;
	       City src, dest;

	       /* find out source city */
	       do {
		    printf( "Where will you be travelling from? " );

		    fgets( input_buffer, sizeof( input_buffer ), stdin );
		    pch = input_buffer + stringLength(input_buffer) - 1;
		    while ( *pch == '\n' || *pch == '\r' || isspace(*pch) ) 
			 *(pch--) = '\0';

		    src = stringToCity( input_buffer );

		    if ( src == NoCity ) {
			 printf( "That's not a city.\n" );
		    }
	       } while ( src == NoCity );

	       /* find out destination city */
	       do {
		    printf( "Where will you be travelling to? " );

		    fgets( input_buffer, sizeof( input_buffer ), stdin );
		    pch = input_buffer + stringLength(input_buffer) - 1;
		    while ( *pch == '\n' || *pch == '\r' || isspace(*pch) ) 
			 *(pch--) = '\0';

		    dest = stringToCity( input_buffer );

		    if ( dest == NoCity ) {
			 printf( "That's not a city.\n" );
		    } else if ( dest == src ) {
			 printf( "That's the same city you're departing.\n" );
		    }
	       } while ( dest == NoCity || dest == src );
	       
	       /* check they're connected */
	       if ( !connected( src, dest, flightRoute ) ) {
		    printf( "But those cities aren't connected!\n" );
	       } else {
		    cityPath path;
		    int i;
		    int distance;

		    /* work out the shortest path */
		    path = shortest_path( src, dest, flightRoute );
		    
		    printf( "The shortest route from %s to %s is:\n", 
			    cityName[ path.stop[0] ], 
			    cityName[ path.stop[path.n_visited - 1] ]);

		    /* display path, plus associated distances */
		    distance = 0;
		    for ( i = 1; i < path.n_visited; i++ ) {
			 printf( "\t%s to %s (distance %d kms)\n", 
				 cityName[ path.stop[i-1] ], 
				 cityName[ path.stop[i] ], 
				 flightRoute[path.stop[i-1]][path.stop[i]] );
			 distance += flightRoute[path.stop[i-1]][path.stop[i]];
		    }
		    printf( "The total distance is %d kms.\n", distance );

		    /* we did do the calculation correctly, right? */
		    assert( distance == path_distance( path, flightRoute ) );
	       }

	       /* Check if new iteration is needed */
	       printf ("\nDo you wish to continue (Y/N)? ");
	       
	       fgets( input_buffer, sizeof( input_buffer ), stdin );
	       ch = input_buffer[0];
	       if ( ch=='N' || ch=='n' ) {
		    exitCode = -1;
	       } else {
		    exitCode=0;
	       } 
	  } while (exitCode==0);
     }

     return EXIT_SUCCESS;
}


