//====================================================== file = ttoc1.c =====
//=  Program to convert a time-based file into a count-based file           =
//=    - Assumes cumulative time stamps                                     =
//===========================================================================
//=  Notes:                                                                 =
//=    1) Input from input file "in.dat" to stdin (see example below)       =
//=        * Comments are bounded by "&" characters at the beginning and    =
//=          end of the comment block                                       =
//=    2) The time block size, T, is specified in the #define section       =
//=    3) Assumes cumulative and not delta time                             =
//=    4) May not handle the last block correctly if last time increment    =
//=       is greater than T                                                 =
//=    5) Output is to stdout                                               =
//=-------------------------------------------------------------------------=
//= Example "in.dat" file:                                                  =
//=                                                                         =
//=   & --- Sample input file of cumulative time stamps --- &               =
//=   0.09                                                                  =
//=   0.11                                                                  =
//=   0.46                                                                  =
//=   0.48                                                                  =
//=   0.51                                                                  =
//=   0.52                                                                  =
//=   0.62                                                                  =
//=   0.622                                                                 =
//=   0.6222                                                                =
//=   0.80                                                                  =
//=   0.801                                                                 =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat" and T = 0.10):                       =
//=                                                                         =
//=   & -------------------------------------------- ttoc1.c ----- &        =
//=   1                                                                     =
//=   1                                                                     =
//=   0                                                                     =
//=   0                                                                     =
//=   2                                                                     =
//=   2                                                                     =
//=   3                                                                     =
//=   0                                                                     =
//=   2                                                                     =
//=   &  Output 9 values with time block = 0.100000 sec                     =
//=   ------------------------------------------------------------ &        =
//=-------------------------------------------------------------------------=
//=  Build:  gcc ttoc1.c, bcc32 ttoc1.c, cl ttoc1.c                         =
//=-------------------------------------------------------------------------=
//=  Execute: ttoc1 < in.dat                                                =
//=-------------------------------------------------------------------------=
//=  Author: Kenneth J. Christensen                                         =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (10/02/98) - Genesis                                      =
//===========================================================================

//----- Include files -------------------------------------------------------
#include <stdio.h>                 // Needed for printf() and feof()
#include <stdlib.h>                // Needed for atof()
#include <string.h>                // Needed for strcmp()

//----- Defines -------------------------------------------------------------
#define T            0.10          // Time block size in seconds

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main(void)
{
  long int  total_count;           // Total count of number of blocks
  long int  block_count;           // Count of number packets in a time block
  double    block_time;            // Accumulated time for this block
  double    new_time;              // Current time stamp value
  double    old_time;              // Old time stamp value
  char      temp_string[256];      // Temporary string variable

  // Output banner
  printf("& -------------------------------------------- ttoc1.c ----- & \n");

  // Loop for entire input file
  block_time = new_time = old_time = 0.0;
  total_count = block_count = 0;
  while(1)
  {
    // Input a value
    scanf("%s", temp_string);

    // This handles a comment bounded by "&" symbols
    if (strcmp(temp_string, "&") == 0)
    {
      do
      {
        scanf("%s", temp_string);
      } while (strcmp(temp_string, "&") != 0);
      scanf("%s", temp_string);
    }

    // Check if end-of-file
    if (feof(stdin))
    {
      total_count = total_count + 1;
      printf("%ld \n", block_count);
      break;
    }

    // Set new_time to current time stamp
    new_time = atof(temp_string);

    // Do our counting and outputting here
    block_time = block_time + (new_time - old_time);
    if (block_time > T)
    {
      total_count = total_count + 1;
      printf("%ld \n", block_count);
      block_time = block_time - T;
      while (block_time > T)
      {
        total_count = total_count + 1;
        block_time = block_time - T;
        printf("0 \n");
      }
      block_count = 1;
    }
    else
      block_count = block_count + 1;

    // The current time stamp is now old
    old_time = new_time;
  }

  // Output closing message
  printf("&  Output %ld values with time block = %f sec \n", total_count, T);
  printf("------------------------------------------------------------ & \n");
}
