commit 3b0108fd3bbca26ddc3ee664721340e479e9564d
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sat Jan 29 18:13:54 2022 -0700
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sat Jan 29 18:13:54 2022 -0700
Use file header not extension to determine type
Now possible to load png files renamed to p8 and p8 files renamed to png
---
source/cart.cpp | 6 ++++--
source/filehelpers.cpp | 18 ++++++++++++++++++
source/filehelpers.h | 2 ++
test/filehelperstests.cpp | 15 +++++++++++++++
4 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/source/cart.cpp b/source/cart.cpp
index 83998d6..8a0f409 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -319,8 +319,10 @@ Cart::Cart(std::string filename, std::string cartDirectory){
initCartRom();
Logger_Write("getting file contents\n");
+
+ std::string firstFourChars = get_first_four_chars(FullCartPath);
- if (hasEnding(FullCartPath, ".p8")){
+ if (FullCartPath == "__FAKE08-BIOS.p8" || firstFourChars == "pico"){
std::string cartStr;
if (FullCartPath == "__FAKE08-BIOS.p8") {
@@ -395,7 +397,7 @@ Cart::Cart(std::string filename, std::string cartDirectory){
setSfx(SfxString);
setMusic(MusicString);
}
- else if (hasEnding(FullCartPath, ".png")) {
+ else if (firstFourChars == "\x89PNG") {
bool success = loadCartFromPng(FullCartPath);
if (!success){
diff --git a/source/filehelpers.cpp b/source/filehelpers.cpp
index ab3b122..267d928 100644
--- a/source/filehelpers.cpp
+++ b/source/filehelpers.cpp
@@ -19,6 +19,24 @@ std::string get_file_contents(std::string filename){
return "";
}
+std::string get_first_four_chars(std::string filename){
+ std::ifstream in(filename, std::ios::in | std::ios::binary);
+ if (in)
+ {
+ std::string contents;
+ in.seekg(0, std::ios::end);
+ int fileLength = in.tellg();
+ int bufSize = 4 < fileLength ? 4 : fileLength;
+ contents.resize(bufSize);
+ in.seekg(0, std::ios::beg);
+ in.read(&contents[0], bufSize);
+ in.close();
+ return(contents);
+ }
+
+ return "";
+}
+
//https://stackoverflow.com/questions/5420317/reading-and-writing-binary-file
std::vector<unsigned char> get_file_buffer(std::string filename){
std::ifstream in(filename, std::ios::binary);
diff --git a/source/filehelpers.h b/source/filehelpers.h
index c4221ee..e8ec73d 100644
--- a/source/filehelpers.h
+++ b/source/filehelpers.h
@@ -2,6 +2,8 @@
std::string get_file_contents(std::string filename);
+std::string get_first_four_chars(std::string filename);
+
std::string getDirectory(const std::string& fname);
bool hasEnding (std::string const &fullString, std::string const &ending);
diff --git a/test/filehelperstests.cpp b/test/filehelperstests.cpp
index efb32c7..2db6d1a 100644
--- a/test/filehelperstests.cpp
+++ b/test/filehelperstests.cpp
@@ -120,3 +120,18 @@ TEST_CASE("isCartFile") {
CHECK_EQ(isCartFile("._cart.p8.png"), false);
}
}
+
+TEST_CASE("get_first_four_chars") {
+ SUBCASE("empty string") {
+ CHECK_EQ(get_first_four_chars(""), "");
+ }
+ SUBCASE("not a file") {
+ CHECK_EQ(get_first_four_chars("file"), "");
+ }
+ SUBCASE("p8 cart file") {
+ CHECK_EQ(get_first_four_chars("carts/cartparsetest.p8"), "pico");
+ }
+ SUBCASE("png cart file") {
+ CHECK_EQ(get_first_four_chars("carts/test_legacypng_cart.p8.png"), "\x89PNG");
+ }
+}