Beispiel #1
0
void DisplayTrip(const int BORDER,
				  string username,
			      Trip &trip)
{
	const int NUM_OF_OPTIONS = trip.getDestinations().size();

	//Data Structures
	ListNode<tripNode>* 	   selected[NUM_OF_OPTIONS];
	int            parentAr   [NUM_OF_OPTIONS];
	int            dist       [NUM_OF_OPTIONS];

	List<tripNode> destinations;
	avltree<User>  users;

	//Input-Output
	stringstream   ss;
	stringstream   ss2;

	//Variables
	int input;
	int option;
	int index;
	int distance;
	int totalDistance;

	string tripStr;
	string tripName;

	ifstream inFile;
	ofstream outFile;
	User     tempUser;

	tripNode tempTrip;
	tripNode lastTrip;
	tripNode source;
	ListNode<tripNode> *node;

	DWORD mode;

	//Initialize Variables
	input         = 0;
	option        = 1;
	index         = 0;
	distance      = 0;
	totalDistance = 0;

	destinations = trip.getDestinations();
	destinations.to_front();

	CenterStrings(ss, "Press Enter to Select a Trip", BORDER);
	CenterStrings(ss, "Use Up or Down Arrow Keys to Navigate list", BORDER);
	CenterStrings(ss, "Press Ctrl-S to Save and Exit \n\n", BORDER);

	if(destinations.currentNode(source)){
		destinations.to_front();
		source = trip.getSource();
		fastRoute(destinations,
				  source,
				  parentAr,
				  dist,
				  NUM_OF_OPTIONS);
	}

	//Initialize all indices of currentTrip[] to 0 (All options unselected)
	for(int pos = 0; pos < NUM_OF_OPTIONS; pos++){
		selected[pos] = NULL;
	}


	//Main Loop
	while(input != -1)
	{
		// Clear Console.
		system("CLS");

		//Reset Index
		index = 0;
		//PrintInput("MainMenu.txt");
		cout << (MenuString());
		cout << ss.str() << '\n';

		destinations.to_front();

		// Prints Selection.  Passes parameter based on
		// current value of option.
		node = destinations.currentNode();
		while(node != NULL){
			if(index == 0)
			{
				distance = 0;
			}
			else
			{
				distance = node->getData().s.getLocation().distanceTo
													(lastTrip.s.getLocation());
			}
			totalDistance += distance;
			lastTrip = node->getData();

			ss2 << node->getData().s.getStadiumName().str
				<< " | "
				<< node->getData().s.getCity()
				<< ", "
			    << node->getData().s.getState()
				<< " | "
				<< distance
				<< " miles";

			tripStr = NumberedLine(ss2.str(), 5, index + 1, BORDER);
			tripStr += '\n';

			if(selected[index] != NULL || index == option - 1)
			{
					PrintSelection(tripStr);
			}
			else
			{
				cout << tripStr;
			}
			destinations.next(tempTrip);
			node = destinations.currentNode();
			index++;
			ss2.str("");
		}
		cout << "TOTAL DISTANCE: " << totalDistance << " miles\n";
		totalDistance = 0;

		// Get user's key stroke.
		input = PressAnyKey("", mode);


		// If user makes selection (Presses Enter Key)
		tripNode newStop;
		if(input == VK_RETURN)
		{
			if(selected[option - 1] == NULL){
				selected[option - 1] = node;
			}
			else{
				selected[option - 1] = NULL;
			}

		}else if((input == 0x53 && (mode & LEFT_CTRL_PRESSED)) ||
				(input == 0x53 && (mode & RIGHT_CTRL_PRESSED)))
		{
				input = -1;
		}
		else
		{
			if(input == VK_UP){
				ChangeSelection(VK_LEFT, option, NUM_OF_OPTIONS);
			}
			else if(input == VK_DOWN){
				ChangeSelection(VK_RIGHT, option, NUM_OF_OPTIONS);
			}
		}
	}
}
Beispiel #2
0
int                  main(int argc, char **argv)
{
   int                  CheckDuplicateDescriptions;
   int                  SortBy;
   int                  SkipTilde;
   int                  SkipMovedto;
   char                *arg;
   int                  c;
   int                  i;
   int                  j;
   FILE                *fp;
   struct ffblk         ffb;
   int                  done;
   int                  Len;
   char               **Lines;
   int                  maxLines;
   int                  nLines;
   int                  pathlen;
   char                 Line[200];
   char                 Dirname[MAX_PATH];
   char                 Filename[MAX_PATH];
   char                 OutFilename[MAX_PATH];
   char                *s;
   char                *Description;
   char                *FormattedLine;
   unsigned long        farcoreleftStart;
   unsigned long        farcoreleftEnd;
   long                 FileSize;
   struct stat		statbuf;
   int 			verbose;
   int 			ragged;
   int 			terminalsized;

   printf("%s\n", ProgVer);
   printf("Replacement for James Jessiman's makelist\n");
   printf("Call with -h to see a list of options.\n\n");

   strcpy(Dirname, "PARTS"); /* Default input directory path */
   strcpy(OutFilename, "parts.lst"); /* Default output filename */

   verbose = 0;
   ragged = 1; /* Steve wants it ragged by default */
   terminalsized = 1; /* Steve wants it to fit in 80 chars by default */

   CheckDuplicateDescriptions = 0;
   SortBy = 0;
   SkipTilde = 0;
   SkipMovedto = 1;
   while (--argc > 0)
   {
      arg = *++argv;
      if (arg[0] == '-')
      {
         switch (arg[1])
         {
            case '?':
            case 'h':
               PrintUsage();
               exit(1);
               break;
            case '8':
               namelen = 12;
               break;
            case 'c':
               CheckDuplicateDescriptions = 1;
               break;
            case 'n':
            case 'd':
               SortBy = arg[1];
               break;
            case 'm':
               SkipMovedto = 0;
               break;
            case '~':
               SkipTilde = 1;
               break;
            case 'i':
	       if (--argc > 0)
		 strcpy(Dirname, *++argv);
	       else
	       {
		 PrintUsage();
		 printf("*** input directory expected as next argument after -i.\n");
		 exit(1);
	       }
               break;
            case 'o':
	       if (--argc > 0)
		 strcpy(OutFilename, *++argv);
	       else
	       {
		 PrintUsage();
		 printf("*** output filename expected as next argument after -o.\n");
		 exit(1);
	       }
               break;
            case 'q':
               quiet = 1;
            case 'f':
               forceit = 1;
               break;
            case 'r':
               ragged = ragged ^ 1;
               break;
            case 't':
               terminalsized = terminalsized ^ 1;
               break;
            case 'v':
               verbose = 1;
               break;
            default:
               PrintUsage();
               printf("*** Unknown option '%s'.\n", arg);
               exit(1);
               break;
         }
      }
      else
      {
         PrintUsage();
         exit(1);
      }
   }

   /* Do a stat to see if Dirname exists and is a directory. */
   if (stat(Dirname, &statbuf) < 0)
   {
     printf("*** Could not stat input directory \"%s\".\n", Dirname);
     exit(1);
   }

   if ((statbuf.st_mode & S_IFDIR) == 0) 
   {
     printf("*** Input directory \"%s\" is not a directory.\n", Dirname);
     exit(1);
   }

   if (CheckDuplicateDescriptions)
      SortBy = 'd';
   if (!SortBy)
   {
      if (forceit)
        SortBy = 'd';
      if (!quiet)
         printf("Sorting by [D]escription.\n");
   }

   if (!SortBy)
   {
      printf("Sort by [N]umber or [D]escription: ");
      c = getch();
      printf("%c\n", c);

      if (c == 'N' || c == 'n')
         SortBy = 'n';
      else if (c == 'D' || c == 'd')
         SortBy = 'd';
      else
      {
         printf("Nothing done.\n");
         exit(0);
      }
   }

   farcoreleftStart = farcoreleft();

   nLines = 0;
   maxLines = numlines;
   Lines = farmalloc(maxLines * sizeof(char *));
   if (!Lines)
   {
      printf("Out of memory after %d parts\n", nLines);
      printf("Memory available at beginning: %ld kBytes\n",
             (farcoreleftStart + 1023) / 1024);
      exit(1);
   }
   strcpy(Filename, Dirname);
   strcat(Filename, "\\");
   pathlen = strlen(Filename);
   strcat(Filename, "*.*");
   for (done = findfirst(Filename, &ffb, 0); !done; done = findnext(&ffb))
   {
      if (verbose)
      {
         printf("Processing file: \"%s\"\n", ffb.ff_name);
      }
      strcpy(Filename + pathlen, ffb.ff_name);
      fp = fopen(Filename, "rt");
      if (!fp)
      {
         if (!quiet) printf("Cannot open \"%s\"", ffb.ff_name);
         PressAnyKey();
         continue;
      }
      fgets(Line, sizeof(Line), fp);
      fclose(fp);

      s = Line + strlen(Line) - 1;
      while (s >= Line && (*s == '\n' || *s == '\r' || *s == '\t' || *s == ' '))
         *s-- = '\0';                     /* clear newline and trailing tabs
                                             and spaces                      */
      s = Line;
      while (*s == '\t' || *s == ' ')
         *s++;
      if (*s++ != '0')
      {
         if (!quiet)          printf("Line type 0 expected in \"%s\", skipping...", ffb.ff_name);
         PressAnyKey();
         continue;
      }
      while (*s == '\t' || *s == ' ')
         *s++;
      Description = s;
      if (SkipTilde && Description[0] == '~')
         continue;
      if (SkipMovedto && strncmp(Description, "~Moved to", 9) == 0)
         continue;
      Len = strlen(Description);
      if (Len == 0)
      {
         if (!quiet)          printf("Empty description in \"%s\"", ffb.ff_name);
         PressAnyKey();
      }
      if (Len > 64)
      {
         /* Original makelist truncates to 64 characters. */
         if (!quiet) 
         {
            printf("Description in \"%s\" will be truncated to 64 characters:\n",
                   ffb.ff_name);
            printf("Before: \"%s\"\n", Description);
            printf("After:  \"%-64.64s\"\n", Description);
         }
         PressAnyKey();
      }
      if (namelen == 12)
         FormattedLine = farmalloc(79);
      else
         FormattedLine = farmalloc(128);
      if (!FormattedLine)
      {
         printf("Out of memory after %d parts\n", nLines);
         printf("Memory available at beginning: %ld kBytes\n",
                (farcoreleftStart + 1023) / 1024);
         exit(1);
      }


      if (namelen > 12) 
         strcpy(shortfilename, ffb.ff_name);
      else
      {
         GetShortPathName(Filename, shortfilepath, MAX_PATH);
         s = basename(shortfilepath);
         strcpy(shortfilename, s);    
         if (s != NULL)
            free(s);
         if (strcmp(ffb.ff_name, shortfilename))
         {
            if (!quiet) 
               printf("Filename \"%s\" will be shortened to %s\n", 
                      ffb.ff_name, shortfilename);
            PressAnyKey();
         }
      }

      Len = strlen(shortfilename); 
      if (Len > namelen)
      {
         if (!quiet)          
            printf("Filename \"%s\" will be truncated to %d characters.\n",
                   shortfilename, namelen);
         PressAnyKey();
      }
      shortfilename[namelen] = 0;
      if (namelen == 12)
         sprintf(FormattedLine, "%-12s  %-64.64s", shortfilename, Description);
      else if (ragged && terminalsized)
      {
         if (Len > 14) /* Squeeze every last char out of the 80. */
            sprintf(FormattedLine, "%s %-64.64s", shortfilename, Description);
         else
            sprintf(FormattedLine, "%-14s  %-64.64s", shortfilename, Description);
      }
      else if (ragged)
      {
         if (Len > 12)
            sprintf(FormattedLine, "%s %-64.64s", shortfilename, Description);
         else
            sprintf(FormattedLine, "%-12s  %-64.64s", shortfilename, Description);
      }
      else
         sprintf(FormattedLine, "%-25s  %-64.64s", shortfilename, Description);
    
      if (terminalsized)
      {
         if (namelen == 12)
            FormattedLine[78] = 0;
         else
            FormattedLine[80] = 0;
      }

      if (verbose)
      {
         printf("%d:\t%s\n", nLines, FormattedLine);
      }
      if (nLines >= maxLines)
      {
         /* Let's have another 1000 pointers */
         maxLines += numlines;
         Lines = farrealloc(Lines, maxLines * sizeof(char *));
         if (!Lines)
         {
            printf("Out of memory after %d parts\n", nLines);
            printf("Memory available at beginning: %ld kBytes\n",
                   (farcoreleftStart + 1023) / 1024);
            exit(1);
         }
      }
      Lines[nLines++] = FormattedLine;
      if (nLines % 100 == 0)
         printf("%d parts so far...\r", nLines);
   }
   printf("%d parts found in %s.\n", nLines, Dirname);
   if (nLines == 0)
   {
      printf("No parts found, nothing done.\n");
      exit(0);
   }

   printf("Sorting...\n");
   qsort(Lines, nLines, sizeof(Lines[0]),
         (SortBy == 'n') ? CmpNumber : CmpDescription);

   if (CheckDuplicateDescriptions)
   {
      printf("Checking for duplicate descriptions. \"%s\" unchanged.\n", OutFilename);
      for (i = 0; i < nLines; i += j)
      {
         for (j = 1; i + j < nLines; j++)
         {
            if (stricmp(Lines[i] + (namelen+2), Lines[i + j] + (namelen+2)) != 0)
               break;                     /* OK to break, lines are sorted   */
            if (j == 1)                   /* First duplicate                 */
               printf("%s\n", Lines[i]);
            printf("%s\n", Lines[i + j]);
         }
         if (j > 1)                       /* Duplicates found                */
            PressAnyKey();
      }
   }
   else
   {
      fp = fopen(OutFilename, "wt");
      if (!fp)
      {
         printf("Cannot open \"%s\" for writing.\n", OutFilename);
         exit(1);
      }
      for (i = 0; i < nLines; i++)
         fprintf(fp, "%s\n", Lines[i]);
      FileSize = ftell(fp);
      fclose(fp);
      printf("\"%s\" successfully written, %ld kBytes\n", OutFilename,
             (FileSize + 1023) / 1024);
   }

   if (numlines > 1000) /* if not Borland DOS compiler then skip the mem msg. */
   {
       return (0);
   }

   farcoreleftEnd = farcoreleft();

   printf("Maximum memory usage: %ld kBytes of %ld kBytes available\n",
          (farcoreleftStart - farcoreleftEnd + 1023) / 1024,
          (farcoreleftStart + 1023) / 1024);

   return (0);
}