//====================================================== file = merge.c =====
//=  Program to merge traffic files                                         =
//=   - Assumes delta time stamps                                           =
//===========================================================================
//=  Notes: 1) Reads from two files, outputs to a third file                =
//=             * File format is <interarrival time delta> and              =
//=               "&" bounds comments                                       =
//=-------------------------------------------------------------------------=
//= Example user input:                                                     =
//=                                                                         =
//=   ------------------------------------------ merge.c -----              =
//=   -  Program to merge traffic files containing           -              =
//=   -  <interarrival time delta> entries                   -              =
//=   --------------------------------------------------------              =
//=   Enter input file #1 name ======================> 1.out                =
//=   Enter input file #2 name ======================> 2.out                =
//=   Enter output file name ========================> 3.out                =
//=   --------------------------------------------------------              =
//=   -  Merging files to output file 3.out...                              =
//=   --------------------------------------------------------              =
//=   --------------------------------------------------------              =
//=   -  Done!                                               -              =
//=   --------------------------------------------------------              =
//=                                                                         =
//=   Where "1.out" and "2.out" contain...                                  =
//=                                                                         =
//=     & -- This is file 1.out -- &                                        =
//=     0.50                                                                =
//=     0.70                                                                =
//=     0.25                                                                =
//=                                                                         =
//=     & -- this is file 2.out -- &                                        =
//=     0.66                                                                =
//=     0.33                                                                =
//=     0.33                                                                =
//=     4.00                                                                =
//=     0.50                                                                =
//=-------------------------------------------------------------------------=
//= Example output file ("3.out" for above):                                =
//=                                                                         =
//=   & -- Merger of 1.out and 2.out -- &                                   =
//=   0.500000                                                              =
//=   0.660000                                                              =
//=   0.330000                                                              =
//=   0.700000                                                              =
//=   0.330000                                                              =
//=   0.250000                                                              =
//=   4.000000                                                              =
//=   0.500000                                                              =
//=-------------------------------------------------------------------------=
//=  Build: gcc merge.c, bcc32 merge.c, cl merge.c                          =
//=-------------------------------------------------------------------------=
//=  Execute: merge                                                         =
//=-------------------------------------------------------------------------=
//=  Author: Kenneth J. Christensen                                         =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (01/20/98) - Genesis                                      =
//===========================================================================

//----- Include files ---------------------------------------------------------
#include <stdio.h>                // Needed for printf() and feof()
#include <stdlib.h>               // Needed for exit(), atof(), and atoi()
#include <string.h>               // Needed for strcmp()

//----- Defines -------------------------------------------------------------
#define TRUE           1L         // Boolean "true"
#define FALSE          0L         // Boolean "false"

//===== Main program ==========================================================
void main(void)
{
  FILE     *fp1, *fp2, *fp3;      // File pointers
  char     file_name_1[256];      // Input file name #1 string
  char     file_name_2[256];      // Input file name #2 string
  char     file_name_3[256];      // Output file name string
  char     temp_string[256];      // Temporary string variable
  long int eof_1_flag;            // Flag for end of file #1
  long int eof_2_flag;            // Flag for end of file #2
  double   time1, time2;          // Times from each input file
  double   sum_time1, sum_time2;  // Sum of times

  //Output banner
  printf("------------------------------------------ merge.c ----- \n");
  printf("-  Program to merge traffic files containing           - \n");
  printf("-  <interarrival time delta> entries                   - \n");
  printf("-------------------------------------------------------- \n");

  // Prompt for input filename #1 and then open the file
  printf("Enter input file #1 name ======================> ");
  scanf("%s", file_name_1);
  fp1 = fopen(file_name_1, "r");
  if (fp1 == NULL)
  {
    printf("ERROR in opening input file (%s) \n", file_name_1);
    exit(1);
  }

  // Prompt for input filename #2 and then open the file
  printf("Enter input file #2 name ======================> ");
  scanf("%s", file_name_2);
  fp2 = fopen(file_name_2, "r");
  if (fp2 == NULL)
  {
    printf("ERROR in opening input file (%s) \n", file_name_2);
    exit(1);
  }

  // Prompt for output filename and then create and open file
  printf("Enter output file name ========================> ");
  scanf("%s", file_name_3);
  fp3 = fopen(file_name_3, "w");
  if (fp3 == NULL)
  {
    printf("ERROR in opening output file (%s) \n", file_name_3);
    exit(1);
  }

  //Output message and merge
  printf("-------------------------------------------------------- \n");
  printf("-  Merging files to output file %s... \n", file_name_3);
  printf("-------------------------------------------------------- \n");
  fprintf(fp3,"& -- Merger of %s and %s -- & \n", file_name_1, file_name_2);

  // Get first entry in file #1
  fscanf(fp1, "%s", temp_string);
  if (strcmp(temp_string, "&") == 0)
  {
    do
    {
      fscanf(fp1, "%s", temp_string);
    } while (strcmp(temp_string, "&") != 0);
    fscanf(fp1, "%s", temp_string);
  }
  time1 = atof(temp_string);

  // Get first entry in file #2
  fscanf(fp2, "%s", temp_string);
  if (strcmp(temp_string, "&") == 0)
  {
    do
    {
      fscanf(fp2, "%s", temp_string);
    } while (strcmp(temp_string, "&") != 0);
    fscanf(fp2, "%s", temp_string);
  }
  time2 = atof(temp_string);

  // Merge until feof() is reached in one of the input files
  sum_time1 = time1;
  sum_time2 = time2;
  eof_1_flag = eof_2_flag = FALSE;
  while(1)
  {
    if (sum_time1 < sum_time2)
    {
      fprintf(fp3, "%f \n", time1);
      fscanf(fp1, "%s", temp_string);
      if (strcmp(temp_string, "&") == 0)
      {
        do
        {
          fscanf(fp1, "%s", temp_string);
        } while (strcmp(temp_string, "&") != 0);
        fscanf(fp1, "%s", temp_string);
      }
      if (feof(fp1))
      {
        eof_1_flag = TRUE;
        break;
      }
      time1 = atof(temp_string);
      sum_time1 = sum_time1 + time1;
    }
    else
    {
      fprintf(fp3, "%f \n", time2);
      fscanf(fp2, "%s", temp_string);
      if (strcmp(temp_string, "&") == 0)
      {
        do
        {
          fscanf(fp2, "%s", temp_string);
        } while (strcmp(temp_string, "&") != 0);
        fscanf(fp2, "%s", temp_string);
      }
      if (feof(fp2))
      {
        eof_2_flag = TRUE;
        break;
      }
      time2 = atof(temp_string);
      sum_time2 = sum_time2 + time2;
    }
  }

  // if end-of-file in file #1 then copy over rest of file #2
  if (eof_1_flag == TRUE)
    while(1)
    {
      fprintf(fp3, "%f \n", time2);
      fscanf(fp2, "%s", temp_string);
      if (strcmp(temp_string, "&") == 0)
      {
        do
        {
          fscanf(fp2, "%s", temp_string);
        } while (strcmp(temp_string, "&") != 0);
        fscanf(fp2, "%s", temp_string);
      }
      if (feof(fp2)) break;
      time2 = atof(temp_string);
    }

  // if end-of-file in file #2 then copy over rest of file #1
  if (eof_2_flag == TRUE)
    while(1)
    {
      fprintf(fp3, "%f \n", time1);
      fscanf(fp1, "%s", temp_string);
      if (strcmp(temp_string, "&") == 0)
      {
        do
        {
          fscanf(fp1, "%s", temp_string);
        } while (strcmp(temp_string, "&") != 0);
        fscanf(fp1, "%s", temp_string);
      }
      if (feof(fp1)) break;
      time1 = atof(temp_string);
    }

  //Output done message and close the files
  printf("-------------------------------------------------------- \n");
  printf("-  Done!                                               - \n");
  printf("-------------------------------------------------------- \n");
  fclose(fp1);
  fclose(fp2);
  fclose(fp3);
}

