-
Notifications
You must be signed in to change notification settings - Fork 8
/
brio.c
35 lines (30 loc) · 902 Bytes
/
brio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#ifdef _WIN32
#include <Rinternals.h>
#include <windows.h>
#endif
// This is needed to support wide character paths on windows
FILE* open_with_widechar_on_windows(const char* path, const char* mode) {
FILE* out;
#ifdef _WIN32
// First conver the mode to the wide equivalent
// Only usage is 2 characters so max 8 bytes + 2 byte null.
wchar_t mode_w[10];
MultiByteToWideChar(CP_UTF8, 0, mode, -1, mode_w, 9);
// Then convert the path
wchar_t* buf;
size_t len = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
if (len <= 0) {
error("Cannot convert file to Unicode: %s", path);
}
buf = (wchar_t*)R_alloc(len, sizeof(wchar_t));
if (buf == NULL) {
error("Could not allocate buffer of size: %ll", len);
}
MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, len);
out = _wfopen(buf, mode_w);
#else
out = fopen(path, mode);
#endif
return out;
}