#define ctimer_CPP

#ifdef RCSID
static char rcsid[] = "$Id: timer.C,v 1.2 2003/05/19 12:35:01 wilder Exp $";
#endif

// __________________________________________________________________________
// Includes

#include <ctime>
#include <iostream>
#include <iomanip>
#include <time.h>

#include "cTimer.h"

// __________________________________________________________________________
// Determine how much time has passed since the timer was started.

float cTimer::update()
{
  clock_t curr_time = clock();
  float time_dif = float(curr_time - start_time) / float(CLOCKS_PER_SEC);
  start_time = curr_time;
  return time_dif;

} // timer::elapsed_time

// __________________________________________________________________________
// Start a timer.  Return accumulated time.

void cTimer::start(const char* timer_name)
{
  // Return immediately if the timer is already running
  if (running)
    return;

  // Report the timer message
  if (timer_name)
    cout << "Timing [" << timer_name << "]...\n";

  // Change timer status to running
  running = 1;

  // Determine the current time;
  start_time = clock();

} // timer::start

// __________________________________________________________________________
// Check how much time has passed.

double cTimer::check(const char* timer_name)
{
  // Report the timer message
  if (timer_name)
    cout << "Checking timer [" << timer_name << "]...\n";

  if (running)
    return accumulated_time + update();

  return accumulated_time;

} // timer::check

// __________________________________________________________________________

void cTimer::restart(const char* timer_name)
{
  // Report the timer message
  if (timer_name)
    cout << "Restarting [" << timer_name << "]...\n";

  accumulated_time = 0;

  // Change timer status to running
  running = 1;

  // Determine the start time;
  start_time = clock();

} // timer::restart

// __________________________________________________________________________

void cTimer::stop(const char* timer_name)
{
  // Recalculate the total accumulated time the timer has run
  if (running)
    accumulated_time += update();

  running = 0;

  cout.setf(ios::fixed, ios::floatfield);
  setprecision(4);

  if (timer_name)
    cout << "Ending [" << timer_name << "] : " 
         << accumulated_time << endl;

} // timer::stop

// __________________________________________________________________________
// cTimer.cpp