From 64dbd97731bfd1cf6ff51e924d7e3cf3018f59ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 26 Jul 2021 22:11:07 +0200 Subject: [PATCH] Address feedback --- Common/Data/Text/Parsers.cpp | 17 +++++++++++++++++ Common/Data/Text/Parsers.h | 2 ++ Common/File/DiskFree.cpp | 4 +++- Core/Dialog/SavedataParam.cpp | 5 ++--- UI/MemStickScreen.cpp | 6 ++++-- UI/MemStickScreen.h | 4 +++- UI/NativeApp.cpp | 2 +- Windows/W32Util/Misc.cpp | 18 ------------------ Windows/W32Util/Misc.h | 2 -- 9 files changed, 32 insertions(+), 28 deletions(-) diff --git a/Common/Data/Text/Parsers.cpp b/Common/Data/Text/Parsers.cpp index 427fb74316..b6ce78d958 100644 --- a/Common/Data/Text/Parsers.cpp +++ b/Common/Data/Text/Parsers.cpp @@ -6,6 +6,23 @@ #include "Common/StringUtils.h" +// Not strictly a parser... +void NiceSizeFormat(size_t size, char *out, size_t bufSize) { + const char *sizes[] = { "B","KB","MB","GB","TB","PB","EB" }; + int s = 0; + int frac = 0; + while (size >= 1024) { + s++; + frac = (int)size & 1023; + size /= 1024; + } + float f = (float)size + ((float)frac / 1024.0f); + if (s == 0) + snprintf(out, bufSize, "%d B", (int)size); + else + snprintf(out, bufSize, "%3.1f %s", f, sizes[s]); +} + bool Version::ParseVersionString(std::string str) { if (str.empty()) return false; diff --git a/Common/Data/Text/Parsers.h b/Common/Data/Text/Parsers.h index bfa111774c..35b3839cfb 100644 --- a/Common/Data/Text/Parsers.h +++ b/Common/Data/Text/Parsers.h @@ -70,3 +70,5 @@ static bool TryParse(const std::string &str, N *const output) { } else return false; } + +void NiceSizeFormat(size_t size, char *out, size_t bufSize); diff --git a/Common/File/DiskFree.cpp b/Common/File/DiskFree.cpp index f1412a35be..ea8e0f0d39 100644 --- a/Common/File/DiskFree.cpp +++ b/Common/File/DiskFree.cpp @@ -41,9 +41,11 @@ bool free_disk_space(const Path &path, int64_t &space) { int res = statvfs(path.c_str(), &diskstat); if (res == 0) { + // Not sure why we're excluding Android here anyway... #ifndef __ANDROID__ if (diskstat.f_flag & ST_RDONLY) { - space = -1; + // No space to write. + space = 0; return true; } #endif diff --git a/Core/Dialog/SavedataParam.cpp b/Core/Dialog/SavedataParam.cpp index 1e26486e2c..0558dacc60 100644 --- a/Core/Dialog/SavedataParam.cpp +++ b/Core/Dialog/SavedataParam.cpp @@ -1045,11 +1045,11 @@ int SavedataParam::BuildHash(unsigned char *output, return 0; } +// TODO: Merge with NiceSizeFormat? That one has a decimal though. std::string SavedataParam::GetSpaceText(u64 size, bool roundUp) { - static const char *suffixes[] = {"B", "KB", "MB", "GB"}; char text[50]; - + static const char *suffixes[] = {"B", "KB", "MB", "GB"}; for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) { if (size < 1024) @@ -1063,7 +1063,6 @@ std::string SavedataParam::GetSpaceText(u64 size, bool roundUp) size /= 1024; } } - snprintf(text, sizeof(text), "%llu TB", size); return std::string(text); } diff --git a/UI/MemStickScreen.cpp b/UI/MemStickScreen.cpp index 2696ed4004..65a8a85b76 100644 --- a/UI/MemStickScreen.cpp +++ b/UI/MemStickScreen.cpp @@ -29,6 +29,7 @@ #include "Common/System/NativeApp.h" #include "Common/System/Display.h" #include "Common/Data/Text/I18n.h" +#include "Common/Data/Text/Parsers.h" #include "Common/File/AndroidStorage.h" #include "Common/File/FileUtil.h" @@ -91,8 +92,9 @@ static bool SwitchMemstickFolderTo(Path newMemstickFolder) { static std::string FormatSpaceString(int64_t space) { if (space >= 0) { - // TODO: Smarter display (MB, GB as appropriate). Don't we have one of these somewhere? - return StringFromFormat("%lld MB", space / (1024 * 1024)); + char buffer[50]; + NiceSizeFormat(space, buffer, sizeof(buffer)); + return buffer; } else { return "N/A"; } diff --git a/UI/MemStickScreen.h b/UI/MemStickScreen.h index 84d6d4d84c..6fb9df31d3 100644 --- a/UI/MemStickScreen.h +++ b/UI/MemStickScreen.h @@ -34,9 +34,10 @@ public: ~MemStickScreen() {} std::string tag() const override { return "game"; } - void CreateViews() override; protected: + void CreateViews() override; + void sendMessage(const char *message, const char *value) override; void dialogFinished(const Screen *dialog, DialogResult result) override; void update() override; @@ -67,6 +68,7 @@ class ConfirmMemstickMoveScreen : public UIDialogScreenWithBackground { public: ConfirmMemstickMoveScreen(Path newMemstickFolder, bool initialSetup); +protected: void CreateViews() override; private: diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 6a0d731c0b..fd11d1ca5f 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -348,7 +348,7 @@ void PostLoadConfig() { } bool CreateDirectoriesAndroid() { - // TODO: Really not sure why this code is Android-exclusive, except the ".nomedia" part. + // TODO: We should probably simply use this as the shared function to create memstick directories. Path pspDir = g_Config.memStickDirectory; if (pspDir.GetFilename() != "PSP") { diff --git a/Windows/W32Util/Misc.cpp b/Windows/W32Util/Misc.cpp index d646302986..4f0ffb982f 100644 --- a/Windows/W32Util/Misc.cpp +++ b/Windows/W32Util/Misc.cpp @@ -53,24 +53,6 @@ namespace W32Util MoveWindow(hwnd, x, y, width, height, FALSE); } - void NiceSizeFormat(size_t size, char *out) - { - const char *sizes[] = {"B","KB","MB","GB","TB","PB","EB"}; - int s = 0; - int frac = 0; - while (size>=1024) - { - s++; - frac = (int)size & 1023; - size /= 1024; - } - float f = (float)size + ((float)frac / 1024.0f); - if (s==0) - sprintf(out, "%d B", (int)size); - else - sprintf(out, "%3.1f %s", f, sizes[s]); - } - BOOL CopyTextToClipboard(HWND hwnd, const char *text) { std::wstring wtext = ConvertUTF8ToWString(text); return CopyTextToClipboard(hwnd, wtext); diff --git a/Windows/W32Util/Misc.h b/Windows/W32Util/Misc.h index fa447de78d..0cddf63df9 100644 --- a/Windows/W32Util/Misc.h +++ b/Windows/W32Util/Misc.h @@ -6,8 +6,6 @@ namespace W32Util { void CenterWindow(HWND hwnd); - HBITMAP CreateBitmapFromARGB(HWND someHwnd, DWORD *image, int w, int h); - void NiceSizeFormat(size_t size, char *out); BOOL CopyTextToClipboard(HWND hwnd, const char *text); BOOL CopyTextToClipboard(HWND hwnd, const std::wstring &wtext); void MakeTopMost(HWND hwnd, bool topMost);