Skip to content

Commit 9a4af62

Browse files
authored
Support international paths on Windows in app (#1319)
* Add support for wide paths to RequestFile in the rest library. * Write to UTF16 filename for log files.
1 parent 3053252 commit 9a4af62

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

app/rest/request_file.cc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,31 @@
3030
#if FIREBASE_PLATFORM_WINDOWS
3131
#define fseeko _fseeki64
3232
#define ftello _ftelli64
33+
34+
// Also include files needed for path conversion.
35+
#include <wchar.h> // NOLINT
36+
37+
#include <codecvt>
38+
#include <locale>
39+
#include <string>
3340
#endif // FIREBASE_PLATFORM_WINDOWS
3441

3542
namespace firebase {
3643
namespace rest {
3744

3845
// Create a request that will read from the specified file.
3946
// This file isn't opened until the first call to ReadBody().
40-
RequestFile::RequestFile(const char* filename, size_t offset)
41-
: file_(fopen(filename, "rb")), file_size_(0) {
47+
// Note that on Windows, the filename will be UTF-8 encoded
48+
// and needs to be converted to utf16.
49+
RequestFile::RequestFile(const char* filename, size_t offset) : file_size_(0) {
50+
#if FIREBASE_PLATFORM_WINDOWS
51+
std::string filename_utf8(filename);
52+
std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_to_utf16;
53+
std::wstring filename_utf16 = utf8_to_utf16.from_bytes(filename_utf8);
54+
file_ = _wfopen(filename_utf16.c_str(), L"rb");
55+
#else
56+
file_ = fopen(filename, "rb");
57+
#endif
4258
options_.stream_post_fields = true;
4359
// If the file exists, seek to the end of the file and get the size.
4460
if (file_ && fseeko(file_, 0, SEEK_END) == 0) {

app/src/log.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "app/src/assert.h"
2525
#include "app/src/include/firebase/internal/mutex.h"
26+
#include "app/src/include/firebase/internal/platform.h"
2627

2728
#if !defined(FIREBASE_LOG_DEBUG)
2829
#define FIREBASE_LOG_DEBUG 0
@@ -86,6 +87,8 @@ static void DefaultLogCallback(LogLevel log_level, const char* message,
8687
// Log a message to a log file.
8788
static void LogToFile(LogLevel log_level, const char* format, va_list args) {
8889
#define FIREBASE_LOG_FILENAME "firebase.log"
90+
// Wide string version for Windows.
91+
#define FIREBASE_LOG_FILENAME_W L"firebase.log"
8992
static FILE* log_file = nullptr;
9093
static bool attempted_to_open_log_file = false;
9194
static const char* kLogLevelToPrefixString[] = {
@@ -109,7 +112,11 @@ static void LogToFile(LogLevel log_level, const char* format, va_list args) {
109112
} else {
110113
MutexLock lock(*g_log_mutex);
111114
if (!log_file) {
115+
#if FIREBASE_PLATFORM_WINDOWS
116+
log_file = _wfopen(FIREBASE_LOG_FILENAME_W, L"wt");
117+
#else
112118
log_file = fopen(FIREBASE_LOG_FILENAME, "wt");
119+
#endif
113120
if (!log_file) {
114121
g_log_callback(kLogLevelError,
115122
"Unable to open log file " FIREBASE_LOG_FILENAME,

0 commit comments

Comments
 (0)