28#ifndef LMMS_IO_HELPER_H
29#define LMMS_IO_HELPER_H
45#if defined(_WIN32) && !defined(__WINE__)
47#elif __has_include(<unistd.h>)
61inline std::unique_ptr<wchar_t[]> toWString(std::string_view utf8)
63 std::unique_ptr<wchar_t[]> ret;
66 ret = std::make_unique<wchar_t[]>(1);
70 if (utf8.length() >
static_cast<std::size_t
>(std::numeric_limits<int>::max()))
72 throw std::overflow_error{
"toWString: input string is too long"};
74 const auto utf8Len =
static_cast<int>(utf8.length());
76 int result = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.data(), utf8Len,
nullptr, 0);
80 throw std::invalid_argument{
"toWString: failed to get size of result string. error code: " + std::to_string(
error)};
83 ret = std::make_unique<wchar_t[]>(
result + 1);
84 result = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.data(), utf8Len, ret.get(),
result);
88 throw std::invalid_argument{
"toWString: failed to convert. error code: " + std::to_string(
error)};
99#if defined(_WIN32) && !defined(__WINE__)
100 return _wfopen(toWString(
filename).get(), toWString(
mode).get());
109 if (
file ==
nullptr) {
return -1; }
111#if defined(_WIN32) && !defined(__WINE__)
112 int fh = _dup(_fileno(
file));
114 int fh = dup(fileno(
file));
117 if (closeFile) { std::fclose(
file); }
Definition AudioAlsa.cpp:35
std::FILE * fopenUtf8(const std::string &filename, const char *mode)
std::fopen wrapper that expects UTF-8 encoded filename and mode
Definition IoHelper.h:97
int fileToDescriptor(std::FILE *file, bool closeFile=true)
Returns the POSIX file descriptor of the given FILE.
Definition IoHelper.h:107
QString filename
Definition HydrogenImport.cpp:42
png_structrp int mode
Definition png.h:1139
unsigned int DWORD
Definition swell-types.h:164
int result
Definition process.c:1455
struct zdirent * file
Definition win32.c:1500