aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorVincent Pelletier <[email protected]>2022-10-23 02:54:59 +0000
committerVincent Pelletier <[email protected]>2022-10-28 05:29:20 +0000
commit27baa2b1646e60270ea9576dcf826c4ea2858c27 (patch)
tree250437aa0e52b748f30fdf0de04bb351e9b9e87b
parent0a627e5bd4010e36c32877a916da02d2213a2c1b (diff)
downloadcartreader-27baa2b1646e60270ea9576dcf826c4ea2858c27.tar.gz
cartreader-27baa2b1646e60270ea9576dcf826c4ea2858c27.zip
All: Factorise code seeking a file back N lines
This saves 890 bytes of program space.
-rw-r--r--Cart_Reader/COLV.ino30
-rw-r--r--Cart_Reader/Cart_Reader.ino14
-rw-r--r--Cart_Reader/GBA.ino30
-rw-r--r--Cart_Reader/INTV.ino30
-rw-r--r--Cart_Reader/NES.ino60
-rw-r--r--Cart_Reader/WSV.ino30
6 files changed, 26 insertions, 168 deletions
diff --git a/Cart_Reader/COLV.ino b/Cart_Reader/COLV.ino
index 4ea817e..070186b 100644
--- a/Cart_Reader/COLV.ino
+++ b/Cart_Reader/COLV.ino
@@ -394,20 +394,7 @@ void setCart_COL() {
}
// Rewind one line
- for (byte count_newline = 0; count_newline < 2; count_newline++) {
- while (1) {
- if (myFile.curPosition() == 0) {
- break;
- } else if (myFile.peek() == '\n') {
- myFile.seekCur(-1);
- break;
- } else {
- myFile.seekCur(-1);
- }
- }
- }
- if (myFile.curPosition() != 0)
- myFile.seekCur(2);
+ rewind_line(myFile);
}
// Display database
@@ -485,20 +472,7 @@ void setCart_COL() {
// Previous
else if (b == 2) {
- for (byte count_newline = 0; count_newline < 7; count_newline++) {
- while (1) {
- if (myFile.curPosition() == 0) {
- break;
- } else if (myFile.peek() == '\n') {
- myFile.seekCur(-1);
- break;
- } else {
- myFile.seekCur(-1);
- }
- }
- }
- if (myFile.curPosition() != 0)
- myFile.seekCur(2);
+ rewind_line(myFile, 6);
break;
}
diff --git a/Cart_Reader/Cart_Reader.ino b/Cart_Reader/Cart_Reader.ino
index 6415943..d5dd208 100644
--- a/Cart_Reader/Cart_Reader.ino
+++ b/Cart_Reader/Cart_Reader.ino
@@ -616,6 +616,20 @@ void get_line(char* str_buf, FsFile* readfile, uint8_t maxi) {
}
}
+void rewind_line(FsFile &readfile, byte count=1) {
+ uint32_t position = readfile.curPosition();
+ count++;
+ for (byte count_newline = 0; count_newline < count; count_newline++) {
+ while (position--) {
+ readfile.seekCur(-1);
+ if (readfile.peek() == '\n')
+ break;
+ }
+ }
+ if (position)
+ readfile.seekCur(1);
+}
+
// Calculate CRC32 if needed and compare it to CRC read from database
boolean compareCRC(const char* database, char* crcString, boolean renamerom, int offset) {
#ifdef nointro
diff --git a/Cart_Reader/GBA.ino b/Cart_Reader/GBA.ino
index 5b1054b..b010921 100644
--- a/Cart_Reader/GBA.ino
+++ b/Cart_Reader/GBA.ino
@@ -885,20 +885,7 @@ void getCartInfo_GBA() {
// Check if string is a match
if (strcmp(tempStr, cartID) == 0) {
// Rewind to start of entry
- for (byte count_newline = 0; count_newline < 2; count_newline++) {
- while (1) {
- if (myFile.curPosition() == 0) {
- break;
- } else if (myFile.peek() == '\n') {
- myFile.seekCur(-1);
- break;
- } else {
- myFile.seekCur(-1);
- }
- }
- }
- if (myFile.curPosition() != 0)
- myFile.seekCur(2);
+ rewind_line(myFile);
// Display database
while (myFile.available()) {
@@ -975,20 +962,7 @@ void getCartInfo_GBA() {
// Previous
else if (b == 2) {
- for (byte count_newline = 0; count_newline < 7; count_newline++) {
- while (1) {
- if (myFile.curPosition() == 0) {
- break;
- } else if (myFile.peek() == '\n') {
- myFile.seekCur(-1);
- break;
- } else {
- myFile.seekCur(-1);
- }
- }
- }
- if (myFile.curPosition() != 0)
- myFile.seekCur(2);
+ rewind_line(myFile, 6);
break;
}
diff --git a/Cart_Reader/INTV.ino b/Cart_Reader/INTV.ino
index 4acc8f9..549ff29 100644
--- a/Cart_Reader/INTV.ino
+++ b/Cart_Reader/INTV.ino
@@ -807,20 +807,7 @@ void setCart_INTV() {
}
// Rewind one line
- for (byte count_newline = 0; count_newline < 2; count_newline++) {
- while (1) {
- if (myFile.curPosition() == 0) {
- break;
- } else if (myFile.peek() == '\n') {
- myFile.seekCur(-1);
- break;
- } else {
- myFile.seekCur(-1);
- }
- }
- }
- if (myFile.curPosition() != 0)
- myFile.seekCur(2);
+ rewind_line(myFile);
}
// Display database
@@ -911,20 +898,7 @@ void setCart_INTV() {
// Previous
else if (b == 2) {
- for (byte count_newline = 0; count_newline < 7; count_newline++) {
- while (1) {
- if (myFile.curPosition() == 0) {
- break;
- } else if (myFile.peek() == '\n') {
- myFile.seekCur(-1);
- break;
- } else {
- myFile.seekCur(-1);
- }
- }
- }
- if (myFile.curPosition() != 0)
- myFile.seekCur(2);
+ rewind_line(myFile, 6);
break;
}
diff --git a/Cart_Reader/NES.ino b/Cart_Reader/NES.ino
index 301e3b7..5a8b313 100644
--- a/Cart_Reader/NES.ino
+++ b/Cart_Reader/NES.ino
@@ -581,20 +581,7 @@ boolean getMapping() {
if (((strcmp(crc_search, crcStr) == 0) || (strcmp(crc_search, crcStrMMC3) == 0)) && (strcmp(crc_search, "BD7BC39F") != 0)) {
// Rewind to start of entry
- for (byte count_newline = 0; count_newline < 4; count_newline++) {
- while (1) {
- if (myFile.curPosition() == 0) {
- break;
- } else if (myFile.peek() == '\n') {
- myFile.seekCur(-1);
- break;
- } else {
- myFile.seekCur(-1);
- }
- }
- }
- if (myFile.curPosition() != 0)
- myFile.seekCur(2);
+ rewind_line(myFile, 3);
// Display database
@@ -751,20 +738,7 @@ boolean getMapping() {
// Previous
else if (b == 2) {
- for (byte count_newline = 0; count_newline < 7; count_newline++) {
- while (1) {
- if (myFile.curPosition() == 0) {
- break;
- } else if (myFile.peek() == '\n') {
- myFile.seekCur(-1);
- break;
- } else {
- myFile.seekCur(-1);
- }
- }
- }
- if (myFile.curPosition() != 0)
- myFile.seekCur(2);
+ rewind_line(myFile, 6);
break;
}
@@ -874,20 +848,7 @@ void selectMapping() {
}
// Rewind one line
- for (byte count_newline = 0; count_newline < 2; count_newline++) {
- while (1) {
- if (myFile.curPosition() == 0) {
- break;
- } else if (myFile.peek() == '\n') {
- myFile.seekCur(-1);
- break;
- } else {
- myFile.seekCur(-1);
- }
- }
- }
- if (myFile.curPosition() != 0)
- myFile.seekCur(2);
+ rewind_line(myFile);
}
// Display database
@@ -1035,20 +996,7 @@ void selectMapping() {
// Previous
else if (b == 2) {
- for (byte count_newline = 0; count_newline < 7; count_newline++) {
- while (1) {
- if (myFile.curPosition() == 0) {
- break;
- } else if (myFile.peek() == '\n') {
- myFile.seekCur(-1);
- break;
- } else {
- myFile.seekCur(-1);
- }
- }
- }
- if (myFile.curPosition() != 0)
- myFile.seekCur(2);
+ rewind_line(myFile, 6);
break;
}
diff --git a/Cart_Reader/WSV.ino b/Cart_Reader/WSV.ino
index da409b6..080b19a 100644
--- a/Cart_Reader/WSV.ino
+++ b/Cart_Reader/WSV.ino
@@ -408,20 +408,7 @@ void setCart_WSV() {
}
// Rewind one line
- for (byte count_newline = 0; count_newline < 2; count_newline++) {
- while (1) {
- if (myFile.curPosition() == 0) {
- break;
- } else if (myFile.peek() == '\n') {
- myFile.seekCur(-1);
- break;
- } else {
- myFile.seekCur(-1);
- }
- }
- }
- if (myFile.curPosition() != 0)
- myFile.seekCur(2);
+ rewind_line(myFile);
}
// Display database
@@ -502,20 +489,7 @@ void setCart_WSV() {
// Previous
else if (b == 2) {
- for (byte count_newline = 0; count_newline < 7; count_newline++) {
- while (1) {
- if (myFile.curPosition() == 0) {
- break;
- } else if (myFile.peek() == '\n') {
- myFile.seekCur(-1);
- break;
- } else {
- myFile.seekCur(-1);
- }
- }
- }
- if (myFile.curPosition() != 0)
- myFile.seekCur(2);
+ rewind_line(myFile, 6);
break;
}