Fix getFileSize failing (#145)

* Try to fix getFileSize failing

* Fix file sizes over 2 GB
This commit is contained in:
Pk11 2022-01-08 16:31:41 -06:00 committed by GitHub
parent 3e92e876e9
commit 750e3cbf99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 105 additions and 130 deletions

View File

@ -47,7 +47,7 @@ GRAPHICS := ../gfx
#---------------------------------------------------------------------------------
ARCH := -mthumb-interwork
CFLAGS := -g -Wall -O2\
CFLAGS := -g -Wall -Wno-psabi -O2\
-march=armv5te -mtune=arm946e-s -fomit-frame-pointer\
-ffast-math \
$(ARCH)

View File

@ -159,13 +159,13 @@ void dm_drawBottomScreen(void) {
switch(dmOperations[dmCursorPosition]) {
case DriveMenuOperation::sdCard:
font->printf(0, 0, false, Alignment::left, Palette::white, STR_SDCARD_LABEL.c_str(), sdLabel[0] == 0 ? STR_UNTITLED.c_str() : sdLabel);
font->printf(0, 1, false, Alignment::left, Palette::white, STR_SD_FAT.c_str(), getDriveBytes(sdSize).c_str());
font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getDriveBytes(getBytesFree("sd:/")).c_str());
font->printf(0, 1, false, Alignment::left, Palette::white, STR_SD_FAT.c_str(), getBytes(sdSize).c_str());
font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getBytes(getBytesFree("sd:/")).c_str());
break;
case DriveMenuOperation::flashcard:
font->printf(0, 0, false, Alignment::left, Palette::white, STR_FLASHCARD_LABEL.c_str(), fatLabel[0] == 0 ? STR_UNTITLED.c_str() : fatLabel);
font->printf(0, 1, false, Alignment::left, Palette::white, STR_SLOT1_FAT.c_str(), getDriveBytes(fatSize).c_str());
font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getDriveBytes(getBytesFree("fat:/")).c_str());
font->printf(0, 1, false, Alignment::left, Palette::white, STR_SLOT1_FAT.c_str(), getBytes(fatSize).c_str());
font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getBytes(getBytesFree("fat:/")).c_str());
break;
case DriveMenuOperation::gbaCart:
font->printf(0, 0, false, Alignment::left, Palette::white, STR_GBA_GAMECART.c_str(), romTitle[1]);
@ -185,17 +185,17 @@ void dm_drawBottomScreen(void) {
break;
case DriveMenuOperation::ramDrive:
font->print(0, 0, false, STR_RAMDRIVE_LABEL);
font->printf(0, 1, false, Alignment::left, Palette::white, STR_RAMDRIVE_FAT.c_str(), getDriveBytes(ramdSize).c_str());
font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getDriveBytes(getBytesFree("ram:/")).c_str());
font->printf(0, 1, false, Alignment::left, Palette::white, STR_RAMDRIVE_FAT.c_str(), getBytes(ramdSize).c_str());
font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getBytes(getBytesFree("ram:/")).c_str());
break;
case DriveMenuOperation::sysNand:
font->print(0, 0, false, STR_SYSNAND_LABEL);
font->printf(0, 1, false, Alignment::left, Palette::white, STR_SYSNAND_FAT.c_str(), getDriveBytes(nandSize).c_str());
font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getDriveBytes(getBytesFree("nand:/")).c_str());
font->printf(0, 1, false, Alignment::left, Palette::white, STR_SYSNAND_FAT.c_str(), getBytes(nandSize).c_str());
font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getBytes(getBytesFree("nand:/")).c_str());
break;
case DriveMenuOperation::fatImage:
font->printf(0, 0, false, Alignment::left, Palette::white, STR_FAT_LABEL.c_str(), imgLabel[0] == 0 ? STR_UNTITLED.c_str() : imgLabel);
font->printf(0, 1, false, Alignment::left, Palette::white, STR_FAT_IMAGE.c_str(), getDriveBytes(imgSize).c_str());
font->printf(0, 1, false, Alignment::left, Palette::white, STR_FAT_IMAGE.c_str(), getBytes(imgSize).c_str());
break;
case DriveMenuOperation::none:
break;

View File

@ -50,40 +50,6 @@ u64 fatSize = 0;
u64 imgSize = 0;
u32 ramdSize = 0;
static float getGbNumber(u64 bytes) {
float gbNumber = 0.0f;
for (u64 i = 0; i <= bytes; i += 0x6666666) {
gbNumber += 0.1f;
}
return gbNumber;
}
static float getTbNumber(u64 bytes) {
float tbNumber = 0.0f;
for (u64 i = 0; i <= bytes; i += 0x1999999999) {
tbNumber += 0.01f;
}
return tbNumber;
}
std::string getDriveBytes(u64 bytes)
{
char buffer[32];
if (bytes < (1024 * 1024))
sniprintf(buffer, sizeof(buffer), STR_N_KB.c_str(), (int)bytes >> 10);
else if (bytes >= (1024 * 1024) && bytes < (1024 * 1024 * 1024))
sniprintf(buffer, sizeof(buffer), STR_N_MB.c_str(), (int)bytes >> 20);
else if (bytes >= 0x40000000 && bytes < 0x10000000000)
snprintf(buffer, sizeof(buffer), STR_N_GB_FLOAT.c_str(), getGbNumber(bytes));
else
snprintf(buffer, sizeof(buffer), STR_N_TB_FLOAT.c_str(), getTbNumber(bytes));
return buffer;
}
const char* getDrivePath(void) {
switch (currentDrive) {
case Drive::sdCard:

View File

@ -34,7 +34,6 @@ extern u64 sdSize;
extern u64 fatSize;
extern u64 imgSize;
extern u32 ramdSize;
extern std::string getDriveBytes(u64 bytes);
extern const char* getDrivePath(void);

View File

@ -20,7 +20,23 @@ std::vector<ClipboardFile> clipboard;
bool clipboardOn = false;
bool clipboardUsed = true;
std::string getBytes(int bytes) {
static float getGbNumber(u64 bytes) {
float gbNumber = 0.0f;
for (u64 i = 0; i <= bytes; i += 0x6666666) {
gbNumber += 0.1f;
}
return gbNumber;
}
static float getTbNumber(u64 bytes) {
float tbNumber = 0.0f;
for (u64 i = 0; i <= bytes; i += 0x1999999999) {
tbNumber += 0.01f;
}
return tbNumber;
}
std::string getBytes(off_t bytes) {
char buffer[32];
if (bytes == 1)
sniprintf(buffer, sizeof(buffer), STR_1_BYTE.c_str());
@ -34,23 +50,19 @@ std::string getBytes(int bytes) {
else if (bytes < (1024 * 1024 * 1024))
sniprintf(buffer, sizeof(buffer), STR_N_MB.c_str(), bytes >> 20);
else if (bytes < 0x10000000000)
snprintf(buffer, sizeof(buffer), STR_N_GB_FLOAT.c_str(), getGbNumber(bytes));
else
sniprintf(buffer, sizeof(buffer), STR_N_GB.c_str(), bytes >> 30);
snprintf(buffer, sizeof(buffer), STR_N_TB_FLOAT.c_str(), getTbNumber(bytes));
return buffer;
}
off_t getFileSize(const char *fileName) {
FILE* fp = fopen(fileName, "rb");
off_t fsize = 0;
if (fp) {
fseek(fp, 0, SEEK_END);
fsize = ftell(fp); // Get source file's size
fseek(fp, 0, SEEK_SET);
}
fclose(fp);
return fsize;
struct stat st;
stat(fileName, &st);
return st.st_size;
}
bool calculateSHA1(const char *fileName, u8 *sha1) {

View File

@ -19,7 +19,7 @@ extern std::vector<ClipboardFile> clipboard;
extern bool clipboardOn;
extern bool clipboardUsed;
extern std::string getBytes(int bytes);
extern std::string getBytes(off_t bytes);
extern off_t getFileSize(const char *fileName);
extern bool calculateSHA1(const char *fileName, u8 *sha1);

View File

@ -26,11 +26,11 @@
#include <vector>
struct DirEntry {
DirEntry(std::string name, size_t size, bool isDirectory, bool isApp, bool selected = false) : name(name), size(size), isDirectory(isDirectory), isApp(isApp), selected(selected) {}
DirEntry(std::string name, off_t size, bool isDirectory, bool isApp, bool selected = false) : name(name), size(size), isDirectory(isDirectory), isApp(isApp), selected(selected) {}
DirEntry() {}
std::string name;
int size;
off_t size;
bool isDirectory;
bool isApp;
bool selected = false;

View File

@ -182,10 +182,9 @@ STRING(START_RETURN_B_BACKSPACE, "(START Return, \\B Backspace)")
// Byte counts
STRING(1_BYTE, "1 Byte")
STRING(N_BYTES, "%d Bytes")
STRING(N_KB, "%d KB")
STRING(N_MB, "%d MB")
STRING(N_GB, "%d GB")
STRING(N_BYTES, "%lld Bytes")
STRING(N_KB, "%lld KB")
STRING(N_MB, "%lld MB")
STRING(N_GB_FLOAT, "%.1f GB")
STRING(N_TB_FLOAT, "%.1f TB")

View File

@ -172,10 +172,9 @@ A_APPLY_B_CANCEL=(\A apply, \B cancel)
START_RETURN_B_BACKSPACE=(START Return, \B Backspace)
1_BYTE=1 Byte
N_BYTES=%d Bytes
N_KB=%d KB
N_MB=%d MB
N_GB=%d GB
N_BYTES=%lld Bytes
N_KB=%lld KB
N_MB=%lld MB
N_GB_FLOAT=%.1f GB
N_TB_FLOAT=%.1f TB