/**\file n_common.c
 *  common function
 *\author Castagnier Mickael
 *\version 1.0
 *\date 24/03/05
 */

#include "n_common.h"

/*!\fn void PAUSE()
 *\brief make a pause in a terminal
 */
void PAUSE()
{

   char k[ 2 ] = "" ;

   printf( "Press a enter to continue..." );

   fflush( stdin );
   scanf( "%c", k );

   printf( "\n" );
} /* PAUSE(...) */



/*!\fn int start_HiTimer( N_TIME *timer )
 *\brief Initialize or restart from zero any N_TIME HiTimer
 *\param timer Any N_TIMER *timer you wanna start or reset
 *\return TRUE or FALSE
 */
int start_HiTimer( N_TIME *timer )
{
   /* set delta to 0 */
   timer -> delta = 0;

#if !defined( LINUX ) && !defined( SOLARIS ) && !defined( AIX )
   if( QueryPerformanceFrequency( ( LARGE_INTEGER * ) & timer -> freq ) == 0 )
      return FALSE ;
   if( QueryPerformanceCounter( &timer -> startTime ) == 0 )
      return FALSE ;
#else
   if( gettimeofday( &timer -> startTime, 0 ) != 0 )
      return FALSE ;
#endif

   return TRUE ;      
} /* init_HiTimer(...) */



/*!\fn time_t get_usec( N_TIME  *timer )
 *\brief Poll any N_TIME HiTimer, returning usec, and moving currentTime to startTime
 *\param timer Any N_TIMER *timer you wanna poll
 *\return The elapsed number of usec for the given N_TIME *timer
 */
time_t get_usec( N_TIME *timer )
{
#ifdef WIN32
   QueryPerformanceCounter( ( LARGE_INTEGER * ) & timer -> currentTime );
   timer -> delta = 1000000 * ( timer -> currentTime . QuadPart - timer -> startTime . QuadPart ) / timer -> freq . QuadPart ;
   timer -> startTime = timer -> currentTime ;
#else
   gettimeofday( &timer -> currentTime, 0 );
   timer -> delta = ( timer -> currentTime . tv_sec   - timer -> startTime . tv_sec  ) * 1000000 + ( timer -> currentTime . tv_usec  - timer -> startTime . tv_usec );
   timer -> startTime . tv_sec  = timer -> currentTime . tv_sec ;
   timer -> startTime . tv_usec = timer -> currentTime . tv_usec ;
#endif

   return timer -> delta;
} /* get_usec( ... ) */



/*!\fn time_t get_msec( N_TIME  *timer )
 *\brief Poll any N_TIME HiTimer, returning msec, and moving currentTime to startTime
 *\param timer Any N_TIMER *timer you wanna poll
 *\return The elapsed number of msec for the given N_TIME *timer
 */
time_t get_msec( N_TIME *timer )
{
#ifdef WIN32
   QueryPerformanceCounter( ( LARGE_INTEGER * ) & timer -> currentTime );
   timer -> delta = 1000 * ( timer -> currentTime . QuadPart - timer -> startTime . QuadPart ) / timer -> freq . QuadPart ;
   timer -> startTime = timer -> currentTime ;
#else
   gettimeofday( &timer -> currentTime, 0 );
   timer -> delta = ( timer -> currentTime . tv_sec   - timer -> startTime . tv_sec  ) * 1000 + ( timer -> currentTime . tv_usec  - timer -> startTime . tv_usec ) / 1000 ;
   timer -> startTime . tv_sec  = timer -> currentTime . tv_sec ;
   timer -> startTime . tv_usec = timer -> currentTime . tv_usec ;
#endif

   return timer -> delta;
} /* get_msec(...) */



/*!\fn time_t get_sec( N_TIME  *timer )
 *\brief Poll any N_TIME HiTimer, returning sec, and moving currentTime to startTime
 *\param timer Any N_TIMER *timer you wanna poll
 *\return The elapsed number of sec for the given N_TIME *timer
 */
time_t get_sec( N_TIME *timer ){
#ifdef WIN32
   QueryPerformanceCounter( ( LARGE_INTEGER * ) & timer -> currentTime );
   timer -> delta = ( timer -> currentTime . QuadPart - timer -> startTime . QuadPart ) / timer -> freq . QuadPart ;
   timer -> startTime = timer -> currentTime ;
#else
   gettimeofday( &timer -> currentTime, 0 );
   timer -> delta = ( timer -> currentTime . tv_sec   - timer -> startTime . tv_sec  )  + ( timer -> currentTime . tv_usec  - timer -> startTime . tv_usec ) / 1000000 ;
   timer -> startTime . tv_sec  = timer -> currentTime . tv_sec ;
   timer -> startTime . tv_usec = timer -> currentTime . tv_usec ;
#endif
   return timer -> delta;
}/* get_sec(...) */



#ifndef NOALLEGRO
/*!\fn void get_keyboard( char *keybuf , int *cur , int min , int max )
 *\brief Function for getting a keyboard input, unicode
 *\param keybuf a valid char buffer
 *\param cur a pointer to the current_cursor_position
 *\param min the minimum carret position in byte
 *\param max the maximim carret position in byte
 */
void get_keyboard( char *keybuf , int *cur , int min , int max )
{
   int k = 0;

   if ( keypressed() ) 
   {
      k = ureadkey( NULL );

      if ( k != 0x1b && k != 0x09 && k != 0x00 && k != 0x0d && k != 0x08 && ( *cur ) < max ) 
      {
         usprintf( keybuf + ustrsize( keybuf ) , "%c" , k );
         ( *cur ) ++;
      } 

      if ( k == 0x08 && (*cur) > min )  /* backspace */
      {
         ( *cur ) --;
         uremove( keybuf , ( *cur ) );
      }
   } /* if( keypressed( ... ) ) */
} /* get_keyboard(...) */

/* #ifndef NOALLEGRO */
#endif 

