//====================================================== file = ttoc2.c =====
//=  Program to convert a time-based file into a count-based file           =
//=    - Assumes delta 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 delta time stamps                                         =
//=    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.001                                                                 =
//=   0.21                                                                  =
//=   0.04                                                                  =
//=   0.05                                                                  =
//=   0.11                                                                  =
//=   0.006                                                                 =
//=   0.01                                                                  =
//=   0.1222                                                                =
//=   0.06                                                                  =
//=   0.01                                                                  =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat" and T = 0.10):                       =
//=                                                                         =
//=   & -------------------------------------------- ttoc1.c ----- &        =
//=   2                                                                     =
//=   0                                                                     =
//=   0                                                                     =
//=   3                                                                     =
//=   0                                                                     =
//=   3                                                                     =
//=   2                                                                     =
//=   1                                                                     =
//=   &  Output 8 values with time block = 0.100000 sec                     =
//=   ------------------------------------------------------------ &        =
//=-------------------------------------------------------------------------=
//=  Build:  gcc ttoc2.c, bcc32 ttoc2.c, cl ttoc2.c                         =
//=-------------------------------------------------------------------------=
//=  Execute: ttoc2 < 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    delta_time;            // Current delta time stamp value
  char      temp_string[256];      // Temporary string variable

  // Output banner
  printf("& -------------------------------------------- ttoc2.c ----- & \n");

  // Loop for entire input file
  block_time = delta_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 delta_time to current time stamp
    delta_time = atof(temp_string);

    // Do our counting and outputting here
    block_time = block_time + delta_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;
  }

  // Output closing message
  printf("&  Output %ld values with time block = %f sec \n", total_count, T);
  printf("------------------------------------------------------------ & \n");
}

