//=================================================== file = sleepSim.c =====
//=  Program to detemine sleep time for an idleCollect data series          =
//=   -
//===========================================================================
//=  Notes:                                                                 =
//=    1) Input from input file "in.dat" to stdin (see example below)       =
//=    2) Output is to stdout                                               =
//=    3) Input is of format 111000111... where a "1" signifies that the    =
//=       minute was on-active (or busy) and a "0" signifies that the       =
//=       minutes was on-idle (or idle).                                    =
//=    4) Must initialize timeOut to desired inactivity time value          =
//=-------------------------------------------------------------------------=
//= Example "in.dat" file:                                                  =
//=                                                                         =
//=  11100011000011000000110101                                             =
//=-------------------------------------------------------------------------=
//= Example output (for above "in.dat" and timeOut = 3):                    =
//=                                                                         =
//=  ---------------------------------------------- sleepSim.c -----        =
//=    Total time                   = 26 minutes                            =
//=    Initialized time out         = 3 minutes                             =
//=    Resulting sleep time         = 4 minutes                             =
//=    Resulting number of wake-ups = 3 events                              =
//=  ---------------------------------------------------------------        =
//=-------------------------------------------------------------------------=
//=  Build: bcc32 sleepSim.c                                                =
//=-------------------------------------------------------------------------=
//=  Execute: sleepSim < in.dat                                             =
//=-------------------------------------------------------------------------=
//=  Author: Ken Christensen                                                =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (07/17/12) - Genesis                                      =
//===========================================================================
//----- Include files -------------------------------------------------------
#include <stdio.h>                 // Needed for printf() and feof()
#include <stdlib.h>                // Needed for exit()

//----- Defines -------------------------------------------------------------
#define    FALSE       0           // Boolean false
#define     TRUE       1           // Boolean true
#define MAX_SIZE   50000           // Maximum size of input data in minutes

//===========================================================================
//=  Main program                                                           =
//===========================================================================
void main(void)
{
  int      X[MAX_SIZE];            // Time series read from "in.dat"
  int      N;                      // Number of values in "in.dat"
  int      timeOut;                // Inactivity timeout value
  int      idleState;              // Flag for idle state
  int      idleCount;              // Counter for idle state
  int      wakeUpCount;            // Counter for wake-up events
  int      sleepTime;              // Total sleep time
  char     value;                  // Value read-in
  int      i;                      // Loop counter

  // Initialize timeout value
  timeOut = 3;

  // Output banner
  printf("---------------------------------------------- sleepSim.c -----\n");

  // Load the series X and determine N
  i = 0;
  while(1)
  {
    value = getchar();
    if (feof(stdin)) break;
    if (value == '0')        // Idle
      X[i] = 0;
    else if (value == '1')   // Busy
      X[i] = 1;
    else
      X[i] = 2;              // Bogus (should not occur)
    i++;
  }
  N = i - 1;

  // Loop to process X for a given timeout value
  idleState = FALSE;
  for (i=0; i<N; i++)
  {
    // Check for unexpected value
    if (X[i] == 2)
    {
      printf("*** ERROR - illegal entry in X[] in position %d \n", i);
      exit(-1);
    }

    // Determine if start of next idle period
    if ((X[i] == 0) && (idleState == FALSE))
      idleState = TRUE;

    // Determine if start of next busy period
    if (X[i] == 1)
    {
      idleState = FALSE;
      idleCount = 0;
    }

    // Process while in an idle period
    if (idleState == TRUE)
      if (idleCount < timeOut)
      {
        X[i] = 1;
        idleCount++;
      }
  }

  // Loop to determine total sleep time and number of forced wake-ups
  sleepTime = wakeUpCount = 0;
  idleState = TRUE;
  for (i=0; i<N; i++)
  {
    // Determine if start of next busy period
    if ((X[i] == 1) && (idleState == TRUE))
    {
      idleState = FALSE;
      wakeUpCount++;
    }

    // Determine if in an idle period
    if (X[i] == 0)
      idleState = TRUE;

    // Tally the sleep
    if (X[i] == 0)
      sleepTime++;
  }

  // Output results
  printf("  Total time                   = %d minutes \n", N);
  printf("  Initialized time out         = %d minutes \n", timeOut);
  printf("  Resulting sleep time         = %d minutes \n", sleepTime);
  printf("  Resulting number of wake-ups = %d events  \n", wakeUpCount);
  printf("---------------------------------------------------------------\n");
}
