All: Factorise code seeking a file back N lines

This saves 890 bytes of program space.
This commit is contained in:
Vincent Pelletier 2022-10-23 02:54:59 +00:00
parent 0a627e5bd4
commit 27baa2b164
6 changed files with 26 additions and 168 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}