/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <b.h>

#ifdef WIN32
static int
gettimeofday (struct timeval *tv, void *tz)
{
  struct _timeb timebuffer;

  _ftime (&timebuffer);
  tv->tv_sec = (long) timebuffer.time;
  tv->tv_usec = timebuffer.millitm * 1000;

  return 0;
}
#endif

/**
 * b_timer_new:
 * @timer: The return pointer to a b_timer_t struct.
 * @returns: The error code.
 *
 * Creates a timer. This function execs the b_timer_start().
 **/
b_error_t
b_timer_new (b_timer_t ** timer)
{
  if (!timer)
    return B_ERROR_DATA;

  if (!(*timer = (b_timer_t *) calloc (1, sizeof (b_timer_t))))
    return B_ERROR_MEMORY;

  b_timer_start (*timer);
  return B_OK;
}

/**
 * b_timer_destroy:
 * @timer: The timer you want destroy.
 * @nanosec: If it != NULL, It will contain the number of nanosec from the
 * start moment and the stop event. If no stop was execed, the b_timer_stop() 
 * will be executed.
 * @returns: The error code.
 *
 * Destroies the b_timer_t data struct and returns the nanonsec from the start 
 * moment.
 **/
b_error_t
b_timer_destroy (b_timer_t * timer, b_uint64 * nanosec)
{
  if (!timer)
    return B_ERROR_DATA;

  if (nanosec)
    {
      if (!timer->stopped)
	b_timer_stop (timer);

      b_timer_diff (timer, nanosec);
    }

  free (timer);
  return B_OK;
}

/**
 * b_timer_start:
 * @timer: The b_timer_t data struct.
 * @returns: The error code.
 *
 * starts the timer.
 **/
b_error_t
b_timer_start (b_timer_t * timer)
{
  if (!timer)
    return B_ERROR_DATA;

  timer->stopped = 0;
  gettimeofday (&timer->start, NULL);

  return B_OK;
}

/**
 * b_timer_stop:
 * @timer: The b_timer_t data struct.
 * @returns: The error code.
 *
 * stops the timer.
 **/
b_error_t
b_timer_stop (b_timer_t * timer)
{
  if (!timer)
    return B_ERROR_DATA;

  if (!timer->stopped)
    {
      timer->stopped = 1;
      gettimeofday (&timer->stop, NULL);
    }

  return B_OK;
}

/**
 * b_timer_diff:
 * @timer: The b_timer_t data struct.
 * @nanosec: The nanosec variable'll contain the nanosec from the start moment
 * and the stop moment. If no b_timer_stop() was executed, the stop moment is
 * now.
 * @returns: The error code.
 *
 * checks and returns the differences from the start and the stop (or now).
 **/
b_error_t
b_timer_diff (b_timer_t * timer, b_uint64 * nanosec)
{
  b_uint64 start, stop;
  if (!timer || !nanosec)
    return B_ERROR_DATA;

  if (!timer->stopped)
    gettimeofday (&timer->stop, NULL);

  start = timer->start.tv_sec * 1000000 + timer->start.tv_usec;
  stop = timer->stop.tv_sec * 1000000 + timer->stop.tv_usec;
  *nanosec = stop - start;

  return B_OK;
}

/* EOF */
