/*
What's new:
C44 - [Martin Zoubek] first public release
*/

#include <time.h>
#ifdef WIN32
  #include <windows.h>
#endif
#ifdef UNIX
  #include <sys/times.h>
#endif

  int vTime_State=0;  //0=not initialized, 1=use time, 2=machine specific
#ifdef WIN32
  HANDLE vTime_CurrentProcess;
#endif

double Time_CPUGet(void){
#ifdef UNIX
  struct tms stms;
#endif
#ifdef WIN32
  FILETIME kt;
#endif
  if(!vTime_State){
  #ifdef UNIX
    if(times(&stms)!=-1)
      vTime_State=2;
    else
  #endif
  #ifdef WIN32
    OSVERSIONINFO osvi;
    osvi.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
    vTime_CurrentProcess=GetCurrentProcess();
    if(GetVersionEx(&osvi)&&osvi.dwPlatformId==VER_PLATFORM_WIN32_NT&&
      GetProcessTimes(vTime_CurrentProcess,0,0,&kt,0))
      vTime_State=2;
    else
  #endif
      vTime_State=1;
    }
  if(vTime_State==2){
  #ifdef UNIX
    times(&stms); 
    return (1.0*stms.tms_utime)/CLK_TCK;
  #endif
  #ifdef WIN32
    GetProcessTimes(vTime_CurrentProcess,0,0,&kt,0);
    return 1e-7*kt.dwLowDateTime+429.4967296*kt.dwHighDateTime;
  #endif
    }
  return time(0);
  }

