|
5 | 5 | * This file is part of GPUJPEG. |
6 | 6 | */ |
7 | 7 | /* |
8 | | - * Copyright (c) 2022-2024, CESNET z.s.p.o. |
| 8 | + * Copyright (c) 2022-2025, CESNET |
9 | 9 | * |
10 | 10 | * All rights reserved. |
11 | 11 | * |
@@ -74,8 +74,34 @@ static size_t y4m_get_data_len(const struct y4m_metadata *info) { |
74 | 74 | return ret * (info->bitdepth > 8 ? 2 : 1); |
75 | 75 | } |
76 | 76 |
|
| 77 | +#ifdef _WIN32 |
| 78 | +#include <wchar.h> |
| 79 | +#include <windows.h> |
| 80 | +static wchar_t* |
| 81 | +mbs_to_wstr_helper(const char* mbstr, wchar_t* wstr_buf, size_t wstr_len) |
| 82 | +{ |
| 83 | + const int size_needed = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbstr, 0 - 1, NULL, 0); |
| 84 | + if ( size_needed == 0 ) { |
| 85 | + fprintf(stderr, "[Y4M] MultiByteToWideChar error %d (0x%x)!\n", GetLastError(), GetLastError()); |
| 86 | + return NULL; |
| 87 | + } |
| 88 | + if ( size_needed > (int)wstr_len ) { |
| 89 | + fprintf(stderr, "[Y4M] buffer provided to %s too short - needed %d, got %zu!\n", __func__, size_needed, |
| 90 | + wstr_len); |
| 91 | + return NULL; |
| 92 | + } |
| 93 | + MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbstr, -1, wstr_buf, size_needed); |
| 94 | + return wstr_buf; |
| 95 | +} |
| 96 | +#define mbs_to_wstr(tstr) mbs_to_wstr_helper(tstr, (wchar_t[1024]){0}, 1024) |
| 97 | +#endif |
| 98 | + |
77 | 99 | size_t y4m_read(const char *filename, struct y4m_metadata *info, unsigned char **data, void *(*allocator)(size_t)) { |
| 100 | +#ifdef _WIN32 |
| 101 | + FILE *file = _wfopen(mbs_to_wstr(filename), L"rb"); |
| 102 | +#else |
78 | 103 | FILE *file = fopen(filename, "rb"); |
| 104 | +#endif |
79 | 105 | if (!file) { |
80 | 106 | fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno)); |
81 | 107 | return 0; |
@@ -134,7 +160,11 @@ size_t y4m_read(const char *filename, struct y4m_metadata *info, unsigned char * |
134 | 160 |
|
135 | 161 | bool y4m_write(const char *filename, const struct y4m_metadata *info, const unsigned char *data) { |
136 | 162 | errno = 0; |
| 163 | +#ifdef _WIN32 |
| 164 | + FILE *file = _wfopen(mbs_to_wstr(filename), L"wb"); |
| 165 | +#else |
137 | 166 | FILE *file = fopen(filename, "wb"); |
| 167 | +#endif |
138 | 168 | if (!file) { |
139 | 169 | fprintf(stderr, "Failed to open %s for writing: %s\n", filename, strerror(errno)); |
140 | 170 | return false; |
|
0 commit comments