Skip to content

Commit 071f51e

Browse files
committed
Make fakenect work on Windows.
We needed platform-specific functions for mkdir(), timers, and we need to open files in binary mode for compatibility. Signed-off-by: Drew Fisher <drew.m.fisher@gmail.com>
1 parent 5795655 commit 071f51e

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

fakenect/fakenect.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
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

5558
static 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

6575
static 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

7297
static 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);

fakenect/record.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ FILE *open_dump(char type, double cur_time, uint32_t timestamp, int data_size, c
7373
sprintf(fn, "%c-%f-%u.%s", type, cur_time, timestamp, extension);
7474
fprintf(index_fp, "%s\n", fn);
7575
sprintf(fn, "%s/%c-%f-%u.%s", out_dir, type, cur_time, timestamp, extension);
76-
FILE* fp = fopen(fn, "w");
76+
FILE* fp = fopen(fn, "wb");
7777
if (!fp) {
7878
printf("Error: Cannot open file [%s]\n", fn);
7979
exit(1);
@@ -254,7 +254,7 @@ FILE *open_index(const char *fn)
254254
"use a different directory.\n");
255255
return 0;
256256
}
257-
fp = fopen(fn, "w");
257+
fp = fopen(fn, "wb");
258258
if (!fp) {
259259
printf("Error: Cannot open file [%s]\n", fn);
260260
return 0;
@@ -333,7 +333,11 @@ int main(int argc, char **argv)
333333
if (rgb_stream) fclose(rgb_stream);
334334
fclose(index_fp);
335335
} else {
336+
#ifdef _WIN32
337+
_mkdir(out_dir);
338+
#else
336339
mkdir(out_dir, S_IRWXU | S_IRWXG | S_IRWXO);
340+
#endif
337341
char *fn = malloc(strlen(out_dir) + 50);
338342
sprintf(fn, "%s/INDEX.txt", out_dir);
339343
index_fp = open_index(fn);

0 commit comments

Comments
 (0)