/*
  This program looks at 100's of files created from a redirected output of ping.
  It looks at the file and converts it into a more convienient format that can be
  copied and pasted straight into excel. The 'd' stands for date and it is in the format:-
  Wed Apr 10 15:43:30 BST 2002
  the first set of data would be d1,l1,s1,e1,u1
  l stands for a ping to london
  s stands for a ping to scotland
  e stands for a ping to europe
  u stands for a ping to usa
  the redirected output of ping is put into the each file and is in the format:-
PING 194.66.233.23 (194.66.233.23) from 131.227.74.105 : 56(84) bytes of data.
64 bytes from 194.66.233.23: icmp_seq=0 ttl=49 time=13.248 msec
64 bytes from 194.66.233.23: icmp_seq=1 ttl=49 time=19.181 msec
64 bytes from 194.66.233.23: icmp_seq=2 ttl=49 time=12.652 msec
64 bytes from 194.66.233.23: icmp_seq=3 ttl=49 time=13.352 msec
64 bytes from 194.66.233.23: icmp_seq=4 ttl=49 time=59.931 msec
64 bytes from 194.66.233.23: icmp_seq=5 ttl=49 time=13.143 msec
64 bytes from 194.66.233.23: icmp_seq=6 ttl=49 time=13.411 msec
64 bytes from 194.66.233.23: icmp_seq=7 ttl=49 time=14.102 msec
64 bytes from 194.66.233.23: icmp_seq=8 ttl=49 time=12.386 msec
64 bytes from 194.66.233.23: icmp_seq=9 ttl=49 time=59.745 msec

--- 194.66.233.23 ping statistics ---
10 packets transmitted, 10 packets received, 0% packet loss
round-trip min/avg/max/mdev = 12.386/23.115/59.931/18.452 ms

  The program looks for data in my directory but is obvious where to change this.
  a few files are created for workings
  the final file is ping.txt.
  I used this program to work out the RTT around the world for every hour for one
  week. This is a second year module, Computer and data networks for Surrey University
  doing a Beng in Electrical and Electronic Engineering.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
	char d1[10],d2[10],d3[10],d4[10],d5[10],d6[10];
    char str1[80],s[100];
    int i,j,k,l,p,q,t;
    double min,max,av;
    FILE *fp,*date,*londonmax,*londonmin,*londonav,*scotlandmax,*scotlandmin,
    *scotlandav,*europemax,*europemin,*europeav,*usamax,*usamin,*usaav;
    date = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/date","w");
    londonmax   = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/londonmax"  ,"w");
    londonmin   = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/londonmin"  ,"w");
    londonav    = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/londonav"   ,"w");
    scotlandmax = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/scotlandmax","w");
    scotlandmin = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/scotlandmin","w");
    scotlandav  = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/scotlandav" ,"w");
    europemax   = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/europemax"  ,"w");
    europemin   = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/europemin"  ,"w");
    europeav    = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/europeav"   ,"w");
    usamax      = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/usamax"     ,"w");
    usamin      = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/usamin"     ,"w");
    usaav       = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/usaav"      ,"w");
    for(j = 1; j <= 5; j++)
    {
    if(j==1) strcpy(str1,"C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/d");
    if(j==2) strcpy(str1,"C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/l");
    if(j==3) strcpy(str1,"C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/s");
    if(j==4) strcpy(str1,"C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/e");
    if(j==5) strcpy(str1,"C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/u");
    for(i = 1; i <= 168; i++)
    {
     if(i<10)
     {
      str1[63] = i+48;
      str1[64] = '\0';
     }
     if(i>9 && i<100)
     {
      str1[63] = (i/10)+48;
      str1[64] = i-((i/10)*10)+48;
      str1[65] = '\0';
     }
     if(i>99)
     {
      str1[63] = (i/100)+48;
      str1[64] = ((i-((i/100)*100))/10)+48;
      str1[65] = i-((i/100)*100)-(((i-((i/100)*100))/10)*10)+48;
      str1[66] = '\0';
     }
     fp = fopen(str1,"r");
     if(fp == NULL)
     {
      if(j==1) printf("%i dates\n",i-1);
      if(j==2) printf("%i for London\n",i-1);
      if(j==3) printf("%i for Scotland\n",i-1);
      if(j==4) printf("%i for Europe\n",i-1);
      if(j==5) printf("%i for USA\n",i-1);
      p = i-1;      
      break;
     }
     if(j==1)
     {
      fscanf(fp,"%s",&d1);
      fscanf(fp,"%s",&d2);
      fscanf(fp,"%s",&d3);
      fscanf(fp,"%s",&d4);
      fscanf(fp,"%s",&d5);
      fscanf(fp,"%s",&d6);
      fclose(fp);
 	  fprintf(date,"%s %s %s %s %s %s\n",d1,d2,d3,d4,d5,d6);
     }
     else
     {
      do{
      fscanf(fp,"%s",&d1);
      fscanf(fp,"%s",&s);
      q=0;
      if(feof(fp) && s[0]=='l' && s[1]=='o' && s[2]=='s' && s[3]=='s')
      {
       q=1;
       break;
      }
      }while( (d1[0] != 'm' && d1[1] != 's') );
      if(q)
      {
       min=max=av=0;
      }
      else
      {
       fscanf(fp,"%s",&s);
       k=l=-1;
       do{
        k++;l++;
        d1[l] = s[k];
       }while(d1[l]!='/');
       d1[l]='\0';
       min = atof(d1);
       l=-1;
       do{
        k++;l++;
        d1[l] = s[k];
       }while(d1[l]!='/');
       d1[l]='\0';
       av = atof(d1);
       l=-1;
       do{
        k++;l++;
        d1[l] = s[k];
       }while(d1[l]!='/');
       d1[l]='\0';
       max = atof(d1);
      }
      if(j==2)
      {
       fprintf(londonmin,"%f\n",min);
       fprintf(londonmax,"%f\n",max);
       fprintf(londonav,"%f\n",av);
      }
      if(j==3)
      {
       fprintf(scotlandmin,"%f\n",min);
       fprintf(scotlandmax,"%f\n",max);
       fprintf(scotlandav,"%f\n",av);
      }
      if(j==4)
      {
       fprintf(europemin,"%f\n",min);
       fprintf(europemax,"%f\n",max);
       fprintf(europeav,"%f\n",av);
      }
      if(j==5)
      {
       fprintf(usamin,"%f\n",min);
       fprintf(usamax,"%f\n",max);
       fprintf(usaav,"%f\n",av);
      }
      fclose(fp);
     }
    }
    }
    fclose(date);
    fclose(londonmin);
    fclose(londonmax);
    fclose(londonav);
    fclose(scotlandmin);
    fclose(scotlandmax);
    fclose(scotlandav);
    fclose(europemin);
    fclose(europemax);
    fclose(europeav);
    fclose(usamin);
    fclose(usamax);
    fclose(usaav);
    fp = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/ping.txt","w");
    date        = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/date"       ,"r");
    londonmax   = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/londonmax"  ,"r");
    londonmin   = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/londonmin"  ,"r");
    londonav    = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/londonav"   ,"r");
    scotlandmax = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/scotlandmax","r");
    scotlandmin = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/scotlandmin","r");
    scotlandav  = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/scotlandav" ,"r");
    europemax   = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/europemax"  ,"r");
    europemin   = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/europemin"  ,"r");
    europeav    = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/europeav"   ,"r");
    usamax      = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/usamax"     ,"r");
    usamin      = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/usamin"     ,"r");
    usaav       = fopen("C:/Documents and Settings/Mathew Fisher/My Documents/Uni/Ping/usaav"      ,"r");
    t=9;
    fprintf(fp,"date");
    fprintf(fp,"%c",t);
    fprintf(fp,"london min");
    fprintf(fp,"%c",t);
    fprintf(fp,"london average");
    fprintf(fp,"%c",t);
    fprintf(fp,"london max");
    fprintf(fp,"%c",t);
    fprintf(fp,"scotland min");
    fprintf(fp,"%c",t);
    fprintf(fp,"scotland average");
    fprintf(fp,"%c",t);
    fprintf(fp,"scotland max");
    fprintf(fp,"%c",t);
    fprintf(fp,"europe min");
    fprintf(fp,"%c",t);
    fprintf(fp,"europe average");
    fprintf(fp,"%c",t);
    fprintf(fp,"europe max");
    fprintf(fp,"%c",t);
    fprintf(fp,"usa min");
    fprintf(fp,"%c",t);
    fprintf(fp,"usa average");
    fprintf(fp,"%c",t);
    fprintf(fp,"usa max\n");
    for(i=1;i<=p;i++)
    {
    fscanf(date,"%s",&str1);
    fprintf(fp,"%s ",str1);
    fscanf(date,"%s",&str1);
    fprintf(fp,"%s ",str1);
    fscanf(date,"%s",&str1);
    fprintf(fp,"%s ",str1);
    fscanf(date,"%s",&str1);
    fprintf(fp,"%s ",str1);
    fscanf(date,"%s",&str1);
    fprintf(fp,"%s ",str1);
    fscanf(date,"%s",&str1);
    fprintf(fp,"%s",str1);
    fprintf(fp,"%c",t);
    fscanf(londonmin,"%s",&str1);
    fprintf(fp,"%s",str1);
    fprintf(fp,"%c",t);
    fscanf(londonav,"%s",&str1);
    fprintf(fp,"%s",str1);
    fprintf(fp,"%c",t);
    fscanf(londonmax,"%s",&str1);
    fprintf(fp,"%s",str1);
    fprintf(fp,"%c",t);
    fscanf(scotlandmin,"%s",&str1);
    fprintf(fp,"%s",str1);
    fprintf(fp,"%c",t);
    fscanf(scotlandav,"%s",&str1);
    fprintf(fp,"%s",str1);
    fprintf(fp,"%c",t);
    fscanf(scotlandmax,"%s",&str1);
    fprintf(fp,"%s",str1);
    fprintf(fp,"%c",t);
    fscanf(europemin,"%s",&str1);
    fprintf(fp,"%s",str1);
    fprintf(fp,"%c",t);
    fscanf(europeav,"%s",&str1);
    fprintf(fp,"%s",str1);
    fprintf(fp,"%c",t);
    fscanf(europemax,"%s",&str1);
    fprintf(fp,"%s",str1);
    fprintf(fp,"%c",t);
    fscanf(usamin,"%s",&str1);
    fprintf(fp,"%s",str1);
    fprintf(fp,"%c",t);
    fscanf(usaav,"%s",&str1);
    fprintf(fp,"%s",str1);
    fprintf(fp,"%c",t);
    fscanf(usamax,"%s",&str1);
    fprintf(fp,"%s\n",str1);
    }
    fclose(fp);
    fclose(date);
    fclose(londonmin);
    fclose(londonmax);
    fclose(londonav);
    fclose(scotlandmin);
    fclose(scotlandmax);
    fclose(scotlandav);
    fclose(europemin);
    fclose(europemax);
    fclose(europeav);
    fclose(usamin);
    fclose(usamax);
    fclose(usaav);
    printf("end\n");
	system("PAUSE");
	return 0;
}
