forked from OpenNMT/CTranslate2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.cc
49 lines (39 loc) · 1.29 KB
/
filesystem.cc
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "ctranslate2/filesystem.h"
#include <stdexcept>
#ifdef _WIN32
# include <windows.h>
#endif
namespace ctranslate2 {
#ifdef _WIN32
static std::wstring convert_to_wstring(const std::string& str) {
int count = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
std::wstring wstr(count, 0);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], count);
return wstr;
}
#endif
template <typename Stream>
static Stream open_file(const std::string& path,
std::ios_base::openmode mode,
bool check) {
#ifdef _WIN32
const std::wstring wpath = convert_to_wstring(path);
Stream stream(wpath.c_str(), mode);
#else
Stream stream(path, mode);
#endif
if (check && !stream)
throw std::runtime_error("Failed to open file: " + path);
return stream;
}
std::ifstream open_file_read(const std::string& path,
std::ios_base::openmode mode,
bool check) {
return open_file<std::ifstream>(path, mode, check);
}
std::ofstream open_file_write(const std::string& path,
std::ios_base::openmode mode,
bool check) {
return open_file<std::ofstream>(path, mode, check);
}
}