From d855c847734e1050cb76352b8bd99ecaca1fa8d8 Mon Sep 17 00:00:00 2001 From: RocketRobz Date: Sun, 21 Oct 2018 19:28:41 -0600 Subject: [PATCH] Show size of SD and flashcards in [root] menu Credit to @JeffRuLz for the printBytes code and size grabbing code from TMFH --- arm9/source/driveMenu.cpp | 9 +++++++-- arm9/source/driveOperations.cpp | 18 ++++++++++++++++++ arm9/source/driveOperations.h | 3 +++ arm9/source/fileOperations.cpp | 15 +++++++++++++++ arm9/source/fileOperations.h | 2 ++ 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/arm9/source/driveMenu.cpp b/arm9/source/driveMenu.cpp index 1e5cfa5..c17d7be 100644 --- a/arm9/source/driveMenu.cpp +++ b/arm9/source/driveMenu.cpp @@ -30,6 +30,7 @@ #include "date.h" #include "screenshot.h" #include "driveOperations.h" +#include "fileOperations.h" #define SCREEN_COLS 32 #define ENTRIES_PER_SCREEN 22 @@ -216,13 +217,17 @@ void dm_drawBottomScreen(void) { if (sdLabel[0] != '\0') { iprintf (" (%s)", sdLabel); } - printf ("\n(SD FAT)"); + printf ("\n(SD FAT, "); + printBytes(sdSize); + printf(")"); } else if (dmAssignedOp[dmCursorPosition] == 1) { printf ("[fat:] FLASHCART"); if (fatLabel[0] != '\0') { iprintf (" (%s)", fatLabel); } - printf ("\n(Slot-1 SD FAT)"); + printf ("\n(Slot-1 SD FAT, ", fatSize); + printBytes(fatSize); + printf(")"); } else if (dmAssignedOp[dmCursorPosition] == 2) { printf ("GBA GAMECART\n"); printf ("(GBA Game)"); diff --git a/arm9/source/driveOperations.cpp b/arm9/source/driveOperations.cpp index 67ce524..e12b505 100644 --- a/arm9/source/driveOperations.cpp +++ b/arm9/source/driveOperations.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "main.h" @@ -24,6 +25,9 @@ bool nitroSecondaryDrive = false; // false == SD card, true == Flashcard char sdLabel[12]; char fatLabel[12]; +int sdSize = 0; +int fatSize = 0; + void fixLabel(bool fat) { if (fat) { for (int i = 0; i < 12; i++) { @@ -76,6 +80,10 @@ TWL_CODE bool sdMount(void) { sdMountedDone = true; fatGetVolumeLabel("sd", sdLabel); fixLabel(false); + struct statvfs st; + if (statvfs("sd:/", &st) == 0) { + sdSize = st.f_bsize * st.f_blocks; + } return true; } return false; @@ -84,6 +92,7 @@ TWL_CODE bool sdMount(void) { TWL_CODE void sdUnmount(void) { fatUnmount("sd"); sdLabel[0] = '\0'; + sdSize = 0; sdMounted = false; } @@ -210,6 +219,10 @@ TWL_CODE bool twl_flashcardMount(void) { if (flashcardFound()) { fatGetVolumeLabel("fat", fatLabel); fixLabel(true); + struct statvfs st; + if (statvfs("fat:/", &st) == 0) { + fatSize = st.f_bsize * st.f_blocks; + } return true; } } @@ -222,6 +235,10 @@ bool flashcardMount(void) { if (flashcardFound()) { fatGetVolumeLabel("fat", fatLabel); fixLabel(true); + struct statvfs st; + if (statvfs("fat:/", &st) == 0) { + fatSize = st.f_bsize * st.f_blocks; + } return true; } return false; @@ -233,5 +250,6 @@ bool flashcardMount(void) { void flashcardUnmount(void) { fatUnmount("fat"); fatLabel[0] = '\0'; + fatSize = 0; flashcardMounted = false; } diff --git a/arm9/source/driveOperations.h b/arm9/source/driveOperations.h index acc0236..c5f122a 100644 --- a/arm9/source/driveOperations.h +++ b/arm9/source/driveOperations.h @@ -14,6 +14,9 @@ extern bool nitroSecondaryDrive; // false == SD card, true == Flashcard extern char sdLabel[12]; extern char fatLabel[12]; +extern int sdSize; +extern int fatSize; + extern bool sdFound(void); extern bool flashcardFound(void); extern bool bothSDandFlashcard(void); diff --git a/arm9/source/fileOperations.cpp b/arm9/source/fileOperations.cpp index 3f9e01e..304f670 100644 --- a/arm9/source/fileOperations.cpp +++ b/arm9/source/fileOperations.cpp @@ -20,6 +20,21 @@ bool clipboardUsed = false; bool clipboardDrive = false; // false == SD card, true == Flashcard bool clipboardInNitro = false; +void printBytes(int bytes) +{ + if (abs(bytes) < 1024) + iprintf("%d B", bytes); + + else if (abs(bytes) < 1024 * 1024) + printf("%.1f KB", (float)bytes / 1024); + + else if (abs(bytes) < 1024 * 1024 * 1024) + printf("%.1f MB", (float)bytes / 1024 / 1024); + + else + printf("%.1f GB", (float)bytes / 1024 / 1024 / 1024); +} + off_t getFileSize(const char *fileName) { FILE* fp = fopen(fileName, "rb"); diff --git a/arm9/source/fileOperations.h b/arm9/source/fileOperations.h index d574b5c..5dfd2be 100644 --- a/arm9/source/fileOperations.h +++ b/arm9/source/fileOperations.h @@ -11,6 +11,8 @@ extern bool clipboardUsed; extern bool clipboardDrive; // false == SD card, true == Flashcard extern bool clipboardInNitro; +extern void printBytes(int bytes); + extern off_t getFileSize(const char *fileName); extern int fcopy(const char *sourcePath, const char *destinationPath);