aboutsummaryrefslogtreecommitdiff
path: root/app/source/Cplt/Utils/IO/CstdioFile.cpp
blob: 0496fa1e173295b7700b830990c77afe466754da (plain)
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
#include "CstdioFile.hpp"

#include <Cplt/Utils/Macros.hpp>

#pragma push_macro("MODE_STRING")
#undef MODE_STRING

#if defined(_WIN32)
#	define MODE_STRING(x) L##x
#else
#	define MODE_STRING(x) x
#endif

namespace CPLT_UNITY_ID {
auto GetModeString(FileUtils::IoMode mode) {
	switch (mode) {
		case FileUtils::IM_Read: return MODE_STRING("rb");
		case FileUtils::IM_WriteAppend: return MODE_STRING("ab");
		case FileUtils::IM_WriteTruncate: return MODE_STRING("wb");
	}
	return MODE_STRING("");
}
} // namespace CPLT_UNITY_ID

#pragma pop_macro("MODE_STRING")

FILE* FileUtils::OpenCstdioFile(const std::filesystem::path& path, IoMode mode) {
#ifdef _WIN32
	// std::filesystem::path::c_str() returns `const wchar_t*` under Windows, because NT uses UTF-16 natively
	return _wfopen(path.c_str(), ::GetModeString(mode));
#else
	return fopen(path.c_str(), CPLT_UNITY_ID::GetModeString(mode));
#endif
}