foxygit / fake-08-xbox360 Log in
commit 23db7212e5dfc376920269ebf0c3c0554426f386
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sun Sep 5 15:30:30 2021 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sun Sep 5 15:30:30 2021 -0600

    Hide macOS filesystem hidden files from cart lists
---
 platform/3ds/source/3dsHost.cpp          |  5 ++-
 platform/SDL2Desktop/source/SDL2Host.cpp |  2 +-
 platform/bittboy/source/BittBoyHost.cpp  |  5 ++-
 platform/switch/source/SwitchHost.cpp    |  5 ++-
 platform/vita/source/VitaHost.cpp        |  5 ++-
 platform/wiiu/source/WiiUHost.cpp        |  5 ++-
 source/filehelpers.cpp                   | 13 ++++++++
 source/filehelpers.h                     |  4 +++
 test/filehelperstests.cpp                | 57 ++++++++++++++++++++++++++++++++
 9 files changed, 95 insertions(+), 6 deletions(-)

diff --git a/platform/3ds/source/3dsHost.cpp b/platform/3ds/source/3dsHost.cpp
index 357b799..24bf2fd 100644
--- a/platform/3ds/source/3dsHost.cpp
+++ b/platform/3ds/source/3dsHost.cpp
@@ -18,6 +18,7 @@ using namespace std;
 #include "../../../source/hostVmShared.h"
 #include "../../../source/nibblehelpers.h"
 #include "../../../source/PicoRam.h"
+#include "../../../source/filehelpers.h"

 #define SCREEN_WIDTH 400;
 #define SCREEN_HEIGHT 240;
@@ -708,7 +709,9 @@ vector<string> Host::listcarts(){
     if (dir) {
         /* print all the files and directories within directory */
         while ((ent = readdir (dir)) != NULL) {
-            carts.push_back(_cartDirectory + ent->d_name);
+            if (isCartFile(ent->d_name)){
+                carts.push_back(_cartDirectory + ent->d_name);
+            }
         }
         closedir (dir);
     }
diff --git a/platform/SDL2Desktop/source/SDL2Host.cpp b/platform/SDL2Desktop/source/SDL2Host.cpp
index 879d1a1..9a64fab 100644
--- a/platform/SDL2Desktop/source/SDL2Host.cpp
+++ b/platform/SDL2Desktop/source/SDL2Host.cpp
@@ -159,7 +159,7 @@ vector<string> Host::listcarts(){
     if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
         /* print all the files and directories within directory */
         while ((ent = readdir (dir)) != NULL) {
-            if (hasEnding(ent->d_name, ".p8") || hasEnding(ent->d_name, ".png")){
+            if (isCartFile(ent->d_name)){
                 carts.push_back(ent->d_name);
             }
         }
diff --git a/platform/bittboy/source/BittBoyHost.cpp b/platform/bittboy/source/BittBoyHost.cpp
index 6f388b6..640b75c 100644
--- a/platform/bittboy/source/BittBoyHost.cpp
+++ b/platform/bittboy/source/BittBoyHost.cpp
@@ -12,6 +12,7 @@ using namespace std;
 #include "../../../source/hostVmShared.h"
 #include "../../../source/nibblehelpers.h"
 #include "../../../source/logger.h"
+#include "../../../source/filehelpers.h"

 // sdl
 #include <SDL/SDL.h>
@@ -536,7 +537,9 @@ vector<string> Host::listcarts(){
     if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
         /* print all the files and directories within directory */
         while ((ent = readdir (dir)) != NULL) {
-            carts.push_back(_cartDirectory + "/" + ent->d_name);
+            if (isCartFile(ent->d_name)){
+                carts.push_back(_cartDirectory + "/" + ent->d_name);
+            }
         }
         closedir (dir);
     } else {
diff --git a/platform/switch/source/SwitchHost.cpp b/platform/switch/source/SwitchHost.cpp
index e9d4b95..9b050e8 100644
--- a/platform/switch/source/SwitchHost.cpp
+++ b/platform/switch/source/SwitchHost.cpp
@@ -14,6 +14,7 @@ using namespace std;
 #include "../../../source/hostVmShared.h"
 #include "../../../source/nibblehelpers.h"
 #include "../../../source/logger.h"
+#include "../../../source/filehelpers.h"

 // sdl
 #include <SDL2/SDL.h>
@@ -164,7 +165,9 @@ vector<string> Host::listcarts(){
     if (dir) {
         /* print all the files and directories within directory */
         while ((ent = readdir (dir)) != NULL) {
-            carts.push_back(_cartDirectory + "/" + ent->d_name);
+            if (isCartFile(ent->d_name)){
+                carts.push_back(_cartDirectory + "/" + ent->d_name);
+            }
         }
         closedir (dir);
     }
diff --git a/platform/vita/source/VitaHost.cpp b/platform/vita/source/VitaHost.cpp
index 38cf330..58eed03 100644
--- a/platform/vita/source/VitaHost.cpp
+++ b/platform/vita/source/VitaHost.cpp
@@ -15,6 +15,7 @@ using namespace std;
 #include "../../../source/hostVmShared.h"
 #include "../../../source/nibblehelpers.h"
 #include "../../../source/logger.h"
+#include "../../../source/filehelpers.h"

 #include <psp2/kernel/processmgr.h>
 #include <psp2/io/fcntl.h>
@@ -290,7 +291,9 @@ vector<string> Host::listcarts(){
                 continue;
             }

-            carts.push_back(_cartDirectory + "/" + dirent.d_name);
+            if (isCartFile(ent->d_name)){
+                carts.push_back(_cartDirectory + "/" + dirent.d_name);
+            }
         } while (ret > 0);

         sceIoDclose(dir);
diff --git a/platform/wiiu/source/WiiUHost.cpp b/platform/wiiu/source/WiiUHost.cpp
index f91c87e..c46d8f6 100644
--- a/platform/wiiu/source/WiiUHost.cpp
+++ b/platform/wiiu/source/WiiUHost.cpp
@@ -17,6 +17,7 @@ using namespace std;
 #include "../../../source/hostVmShared.h"
 #include "../../../source/nibblehelpers.h"
 #include "../../../source/logger.h"
+#include "../../../source/filehelpers.h"

 // sdl
 #include <SDL2/SDL.h>
@@ -553,7 +554,9 @@ vector<string> Host::listcarts(){
     if (dir != NULL) {
         /* print all the files and directories within directory */
         while ((ent = readdir (dir)) != NULL) {
-            carts.push_back(_cartDirectory + "/" + ent->d_name);
+            if (isCartFile(ent->d_name)){
+                carts.push_back(_cartDirectory + "/" + ent->d_name);
+            }
         }
         closedir (dir);
     } else {
diff --git a/source/filehelpers.cpp b/source/filehelpers.cpp
index a54f55c..ab3b122 100644
--- a/source/filehelpers.cpp
+++ b/source/filehelpers.cpp
@@ -70,3 +70,16 @@ bool hasEnding (std::string const &fullString, std::string const &ending) {
     }
 }

+bool isHiddenFile (std::string const &fullString) {
+    if (fullString.length() >= 0) {
+        return (0 == fullString.compare (0, 2, "._"));
+    } else {
+        return false;
+    }
+}
+
+bool isCartFile (std::string const &fullString) {
+    return !isHiddenFile(fullString) &&
+        (hasEnding(fullString, ".p8") || hasEnding(fullString, ".png"));
+}
+
diff --git a/source/filehelpers.h b/source/filehelpers.h
index 94e155e..c4221ee 100644
--- a/source/filehelpers.h
+++ b/source/filehelpers.h
@@ -6,6 +6,10 @@ std::string getDirectory(const std::string& fname);

 bool hasEnding (std::string const &fullString, std::string const &ending);

+bool isCartFile (std::string const &fullString);
+
+bool isHiddenFile (std::string const &fullString);
+
 bool isAbsolutePath (std::string const &path);

 std::string getFileExtension(std::string const &path);
diff --git a/test/filehelperstests.cpp b/test/filehelperstests.cpp
index c4d885d..efb32c7 100644
--- a/test/filehelperstests.cpp
+++ b/test/filehelperstests.cpp
@@ -63,3 +63,60 @@ TEST_CASE("getFileExtension") {
         CHECK_EQ(getFileExtension("ux0:/data/di.r2/ca.rt.p8.longer"), ".longer");
     }
 }
+
+TEST_CASE("isHidden") {
+    SUBCASE("empty string") {
+        CHECK_EQ(isHiddenFile(""), false);
+    }
+    SUBCASE("filename no etension") {
+        CHECK_EQ(isHiddenFile("file"), false);
+    }
+    SUBCASE("filename with etension") {
+        CHECK_EQ(isHiddenFile("cart.p8"), false);
+    }
+    SUBCASE("filename with extension in folder") {
+        CHECK_EQ(isHiddenFile("dir/cart.p8"), false);
+    }
+    SUBCASE("path starting with slash") {
+        CHECK_EQ(isHiddenFile("/dir1/dir2/cart.p8"), false);
+    }
+    SUBCASE("vita path") {
+        CHECK_EQ(isHiddenFile("ux0:/data/dir2/cart.p8"), false);
+    }
+    SUBCASE("hidden file") {
+        CHECK_EQ(isHiddenFile(".bash_rc"), false);
+    }
+    SUBCASE("macOS filesystem thing") {
+        CHECK_EQ(isHiddenFile("._cart.p8"), true);
+    }
+}
+
+TEST_CASE("isCartFile") {
+    SUBCASE("empty string") {
+        CHECK_EQ(isCartFile(""), false);
+    }
+    SUBCASE("filename no etension") {
+        CHECK_EQ(isCartFile("file"), false);
+    }
+    SUBCASE("filename with etension") {
+        CHECK_EQ(isCartFile("cart.p8"), true);
+    }
+    SUBCASE("filename with extension in folder") {
+        CHECK_EQ(isCartFile("dir/cart.p8"), true);
+    }
+    SUBCASE("path starting with slash") {
+        CHECK_EQ(isCartFile("/dir1/dir2/cart.p8.png"), true);
+    }
+    SUBCASE("vita path") {
+        CHECK_EQ(isCartFile("ux0:/data/dir2/cart.p8"), true);
+    }
+    SUBCASE("hidden file") {
+        CHECK_EQ(isCartFile(".bash_rc"), false);
+    }
+    SUBCASE("macOS filesystem thing p8") {
+        CHECK_EQ(isCartFile("._cart.p8"), false);
+    }
+    SUBCASE("macOS filesystem thing png") {
+        CHECK_EQ(isCartFile("._cart.p8.png"), false);
+    }
+}