Skip to content

Commit 3913a0b

Browse files
authored
Merge d5545f8 into 399f740
2 parents 399f740 + d5545f8 commit 3913a0b

6 files changed

+45
-20
lines changed

app/src/filesystem.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include <string>
2121

22+
#include "app/src/include/firebase/internal/platform.h"
23+
2224
namespace firebase {
2325

2426
/**
@@ -51,8 +53,25 @@ namespace firebase {
5153
//
5254
// TODO(b/171738655): use a separate function instead of the `should_create`
5355
// flag. Use `StatusOr` for returning errors.
54-
std::string AppDataDir(const char* app_name, bool should_create = true,
55-
std::string* out_error = nullptr);
56+
57+
#if FIREBASE_PLATFORM_WINDOWS
58+
typedef std::wstring PathString;
59+
#define PathStringLiteral(x) PathString(L##x)
60+
#define kPathStringEmpty PathString(L"")
61+
#define kPathStringSep PathString(L"\\")
62+
#define PathStringChar wchar_t
63+
#define PathStringLiteralPrefix L
64+
#else
65+
typedef std::string PathString;
66+
#define PathStringLiteral(x) PathString(x)
67+
#define kPathStringEmpty PathString("")
68+
#define kPathStringSep PathString("/")
69+
#define PathStringChar char
70+
#define PathStringLiteralPrefix
71+
#endif // FIREBASE_PLATFORM_WINDOWS
72+
73+
PathString AppDataDir(const char* app_name, bool should_create = true,
74+
std::string* out_error = nullptr);
5675

5776
} // namespace firebase
5877

app/src/filesystem_apple.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ bool Mkdir(const std::string& path, std::string* out_error) {
4444

4545
} // namespace
4646

47-
std::string AppDataDir(const char* app_name, bool should_create, std::string* out_error) {
47+
PathString AppDataDir(const char* app_name, bool should_create, std::string* out_error) {
4848
if (!app_name || std::strlen(app_name) == 0) {
4949
if (out_error) {
5050
*out_error = "AppDataDir failed: no app_name provided";

app/src/filesystem_desktop_linux.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ bool PathExists(const std::string& path) {
8080

8181
} // namespace
8282

83-
std::string AppDataDir(const char* app_name, bool should_create,
84-
std::string* out_error) {
83+
PathString AppDataDir(const char* app_name, bool should_create,
84+
std::string* out_error) {
8585
if (!app_name || strlen(app_name) == 0) {
8686
if (out_error) {
8787
*out_error = "AppDataDir failed: no app_name provided";

app/src/filesystem_desktop_windows.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ bool Mkdir(const std::wstring& path, std::string* out_error) {
164164

165165
} // namespace
166166

167-
std::string AppDataDir(const char* app_name, bool should_create,
168-
std::string* out_error) {
167+
PathString AppDataDir(const char* app_name, bool should_create,
168+
std::string* out_error) {
169169
if (!app_name || std::strlen(app_name) == 0) {
170170
if (out_error) {
171171
*out_error = "AppDataDir failed: no app_name provided";
@@ -210,14 +210,14 @@ std::string AppDataDir(const char* app_name, bool should_create,
210210
if (!created) return "";
211211
}
212212

213-
return NativeToUtf8(current_path, out_error);
213+
return PathString(current_path);
214214

215215
} else {
216216
auto app_name_utf16 = Utf8ToNative(app_name, out_error);
217217
if (app_name_utf16.empty()) {
218218
return "";
219219
}
220-
return NativeToUtf8(base_dir + L"/" + app_name_utf16, out_error);
220+
return PathString(base_dir + L"/" + app_name_utf16);
221221
}
222222
}
223223

app/src/heartbeat/heartbeat_storage_desktop.cc

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,30 @@ using com::google::firebase::cpp::heartbeat::VerifyLoggedHeartbeatsBuffer;
3939
namespace {
4040

4141
const char kHeartbeatDir[] = "firebase-heartbeat";
42+
#if FIREBASE_PLATFORM_WINDOWS
43+
const wchar_t kHeartbeatFilenamePrefix[] = L"heartbeats-";
44+
#else
4245
const char kHeartbeatFilenamePrefix[] = "heartbeats-";
46+
#endif
4347

44-
std::string CreateFilename(const std::string& app_id, const Logger& logger) {
48+
PathString CreateFilename(const std::string& app_id, const Logger& logger) {
4549
std::string error;
46-
std::string app_dir =
50+
PathString app_dir =
4751
AppDataDir(kHeartbeatDir, /*should_create=*/true, &error);
4852
if (!error.empty()) {
4953
logger.LogError(error.c_str());
50-
return "";
54+
return kPathStringEmpty;
5155
}
5256
if (app_dir.empty()) {
53-
return "";
57+
return kPathStringEmpty;
5458
}
5559

5660
// Remove any symbols from app_id that might not be allowed in filenames.
57-
auto app_id_without_symbols =
58-
std::regex_replace(app_id, std::regex("[/\\\\?%*:|\"<>.,;=]"), "");
59-
// Note: fstream will convert / to \ if needed on windows.
60-
return app_dir + "/" + kHeartbeatFilenamePrefix + app_id_without_symbols;
61+
auto app_id_without_symbols = std::regex_replace(
62+
app_id, std::regex(PathStringLiteral("[/\\\\?%*:|\"<>.,;=]")),
63+
kPathStringEmpty);
64+
return app_dir + kPathStringSep + kHeartbeatFilenamePrefix +
65+
app_id_without_symbols;
6166
}
6267

6368
} // namespace
@@ -127,7 +132,7 @@ bool HeartbeatStorageDesktop::Write(const LoggedHeartbeats& heartbeats) const {
127132
return !file.fail();
128133
}
129134

130-
const char* HeartbeatStorageDesktop::GetFilename() const {
135+
const PathStringChar* HeartbeatStorageDesktop::GetFilename() const {
131136
return filename_.c_str();
132137
}
133138

app/src/heartbeat/heartbeat_storage_desktop.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <vector>
2424

2525
#include "app/logged_heartbeats_generated.h"
26+
#include "app/src/filesystem.h"
2627
#include "app/src/logger.h"
2728

2829
namespace firebase {
@@ -52,7 +53,7 @@ class HeartbeatStorageDesktop {
5253
// write operation fails.
5354
bool Write(const LoggedHeartbeats& heartbeats) const;
5455

55-
const char* GetFilename() const;
56+
const PathStringChar* GetFilename() const;
5657

5758
private:
5859
LoggedHeartbeats LoggedHeartbeatsFromFlatbuffer(
@@ -61,7 +62,7 @@ class HeartbeatStorageDesktop {
6162
const LoggedHeartbeats& heartbeats_struct) const;
6263

6364
// local variables for state
64-
std::string filename_;
65+
PathString filename_;
6566
const Logger& logger_;
6667
};
6768

0 commit comments

Comments
 (0)