#ifdef _WIN32_
#include <memory.h>
#include <conio.h>
#include <stdafx.h>
#endif

#ifdef _VXWORKS_
#include <string.h>
#include <stdio.h>
#include <ioLib.h>
#endif

#include <stdlib.h>
#include <time.h>
#include "osutils.h"
#include "ostime.h"

#ifdef _VXWORKS_
// VxWorks headers fail to define this
extern "C" { 
int sysClkRateGet();
};
#endif

#ifdef _VXWORKS_
extern "C" {
char *strdup(const char *src)
{
  int nbytes;
  char *buf;
  
  if ((nbytes = strlen(src)) <= 0)
    return (char *)NULL;
  
  /* Include space for terminating null byte */
  nbytes++;
  
  if ((buf = (char *)malloc(nbytes)) == (char *)NULL)
    return (char *)NULL;
  
  strcpy(buf, src);
  return buf;
}
}
#endif
//////////////////////////////////////////////////////////////////////
// utility.cpp: implementation of the utility class.
//////////////////////////////////////////////////////////////////////
Utility::Utility(){};
Utility::~Utility(){};
void Utility::bzero( void *s, int n )
{
	memset(s, NULL, n);
}
void Utility::sleep( Nat32 secs )
{
#ifdef _VXWORKS_
  timespec t;
  t.tv_sec = secs;
  t.tv_nsec = 0;
  nanosleep(&t, 0);
#endif
#ifdef _WIN32
  Sleep(secs * 1000);
#endif
}
void Utility::delay( Nat32 milliSecs )
{
#ifdef _WIN32_
	Sleep(milliSecs);
#endif
#ifdef _VXWORKS_
  	 //TODO: test this snippet
 	 taskDelay(milliSecs*sysClkRateGet()/1000); 	   
#endif
}
#ifdef _VXWORKS_
extern "C" { 
int sysClkRateGet();
}
#endif
int Utility::keyhit()
{
#ifdef _VXWORKS_
  int nBytes;
  ioctl(0, FIONREAD, (int )&nBytes);
  nBytes += (stdin)->_r;
  return nBytes;
#endif
#ifdef _WIN32
  return _kbhit();
#endif
}
int Utility::getChar()
{
#ifdef _VXWORKS_  
 return getc(stdin);
#endif
#ifdef _WIN32
  return _getche();
#endif
}
