#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int numOfPack32K[ 10000 ];
int numOfPack64K[ 10000 ];
int numOfPack96K[ 10000 ];
int curNumOfPack = 0;
int index32 = 0;
int index64 = 0;
int index96 = 0;
int thisOneThousand = 0;

int main( int argc, char * argv[] )
{

	FILE * srcFile = NULL;
	FILE * dstFile = NULL;
	char * oneline = NULL;
	int i;

  if ( argc < 3 )
  {
  	printf("Please input the source and destination file name \n ");
	exit( 1 );
  }

  for ( i = 0; i < 10000; i++ )
  {
  	numOfPack32K[ i ] = 0;
  }
	
  for ( i = 0; i < 10000; i++ )
  {
  	numOfPack64K[ i ] = 0;
  }
	
  if ( (srcFile = fopen( argv[ 1 ], "r" )) == NULL )
  {
  	printf("Error opening file: %s \n", argv[ 1 ] );
	exit( 1 );
  }

  if ( (dstFile = fopen( argv[ 2 ], "w" )) == NULL )
  {
  	printf("Error opening file: %s \n", argv[ 2 ] );
	exit( 1 );
  }

  oneline = malloc( 1024 );

  while( fgets( oneline, 1024, srcFile ) != NULL )
  {
	 if ( strstr( oneline, "(4)" ) != NULL )
	 {
		curNumOfPack = 0;
		fprintf( dstFile, "In the past 1000 interation, number of TCP packets send is: %d \n\n", thisOneThousand );
		thisOneThousand = 0;
	 }else

	 if ( strstr( oneline, "(1448)" ) != NULL )
	 {
	 	curNumOfPack++ ;
	 }else

	 if ( strstr( oneline, "(912)" ) != NULL )
	 {
	 	curNumOfPack++ ;
	 	numOfPack32K[ index32 ] = curNumOfPack;
		thisOneThousand += curNumOfPack;
  		fprintf( dstFile, "Iteration %d, Number of TCP packets sent: %d\n\n", index32, curNumOfPack );
		curNumOfPack = 0;
		index32 ++;
	 }else

	 if ( strstr( oneline, "(376)" ) != NULL )
	 {
	 	curNumOfPack++ ;
	 	numOfPack64K[ index64 ] = curNumOfPack;
		thisOneThousand += curNumOfPack;
  		fprintf( dstFile, "Iteration %d, Number of TCP packets sent: %d\n\n", index64, curNumOfPack );
		curNumOfPack = 0;
		index64 ++;
	 }

	 if ( strstr( oneline, "(1288)" ) != NULL )
	 {
	 	curNumOfPack++ ;
	 	numOfPack96K[ index96 ] = curNumOfPack;
		thisOneThousand += curNumOfPack;
  		fprintf( dstFile, "Iteration %d, Number of TCP packets sent: %d\n\n", index96, curNumOfPack );
		curNumOfPack = 0;
		index96 ++;
	 }


  }/* end of while */

fprintf( dstFile, "In the past 100 interation, number of TCP packets send is: %d \n\n", thisOneThousand );

  fclose( dstFile );
  fclose( srcFile );
  return 0;
  
}/*end of main*/