3131#include <unistd.h>
3232#include <time.h>
3333#include <assert.h>
34+ #ifdef _WIN32
35+ #include <windows.h>
36+ #endif
3437
3538#define GRAVITY 9.80665
3639
@@ -54,19 +57,41 @@ static void *user_ptr = NULL;
5457
5558static void sleep_highres (double tm )
5659{
60+ #ifdef _WIN32
61+ int msec = floor (tm * 1000 );
62+ if (msec > 0 ) {
63+ Sleep (msec );
64+ }
65+ #else
5766 int sec = floor (tm );
5867 int usec = (tm - sec ) * 1000000 ;
5968 if (tm > 0 ) {
6069 sleep (sec );
6170 usleep (usec );
6271 }
72+ #endif
6373}
6474
6575static double get_time ()
6676{
77+ #ifdef _WIN32
78+ SYSTEMTIME st ;
79+ GetSystemTime (& st );
80+ FILETIME ft ;
81+ SystemTimeToFileTime (& st , & ft );
82+ ULARGE_INTEGER li ;
83+ li .LowPart = ft .dwLowDateTime ;
84+ li .HighPart = ft .dwHighDateTime ;
85+ // FILETIME is given as a 64-bit value for the number of 100-nanosecond
86+ // intervals that have passed since Jan 1, 1601 (UTC). The difference between that
87+ // epoch and the POSIX epoch (Jan 1, 1970) is 116444736000000000 such ticks.
88+ uint64_t total_usecs = (li .QuadPart - 116444736000000000L ) / 10L ;
89+ return (total_usecs / 1000000. );
90+ #else
6791 struct timeval cur ;
6892 gettimeofday (& cur , NULL );
6993 return cur .tv_sec + cur .tv_usec / 1000000. ;
94+ #endif
7095}
7196
7297static char * one_line (FILE * fp )
@@ -107,7 +132,7 @@ static int parse_line(char *type, double *cur_time, unsigned int *timestamp, uns
107132 char * file_path = malloc (file_path_size );
108133 snprintf (file_path , file_path_size , "%s/%s" , input_path , line );
109134 // Open file
110- FILE * cur_fp = fopen (file_path , "r " );
135+ FILE * cur_fp = fopen (file_path , "rb " );
111136 if (!cur_fp ) {
112137 printf ("Error: Cannot open file [%s]\n" , file_path );
113138 exit (1 );
@@ -136,7 +161,7 @@ static void open_index()
136161 int index_path_size = strlen (input_path ) + 50 ;
137162 char * index_path = malloc (index_path_size );
138163 snprintf (index_path , index_path_size , "%s/INDEX.txt" , input_path );
139- index_fp = fopen (index_path , "r " );
164+ index_fp = fopen (index_path , "rb " );
140165 if (!index_fp ) {
141166 printf ("Error: Cannot open file [%s]\n" , index_path );
142167 exit (1 );
0 commit comments