Cart_Reader.ino: Comment rewind_line implementation

The logic of this function is not immediately clear, so comment it
extensively.
This commit is contained in:
Vincent Pelletier 2022-11-05 00:31:51 +00:00
parent bfbbe922ad
commit ab9e36e12e

View File

@ -675,15 +675,26 @@ void get_line(char* str_buf, FsFile* readfile, uint8_t maxi) {
void rewind_line(FsFile& readfile, byte count = 1) { void rewind_line(FsFile& readfile, byte count = 1) {
uint32_t position = readfile.curPosition(); uint32_t position = readfile.curPosition();
// To seek one line back, this code must step over the first newline it finds
// in order to exit the current line and enter the end of the previous one.
// Convert <count> from how-many-lines-back into how-many-newlines-to-look-for
// by incrementing it by 1.
count++; count++;
for (byte count_newline = 0; count_newline < count; count_newline++) { for (byte count_newline = 0; count_newline < count; count_newline++) {
// Go to the strictly previous '\n', or file start.
while (position) { while (position) {
// Seek back first (keeping position updated)...
position--; position--;
readfile.seekCur(-1); readfile.seekCur(-1);
// ...and check current byte second.
// Note: this code assumed all files use ASCII with DOS-style newlines
// so \n is encountered first when seeking backwards.
if (readfile.peek() == '\n') if (readfile.peek() == '\n')
break; break;
} }
} }
// If not at file start, the current character is the '\n' just before the
// desired line, so advance by one.
if (position) if (position)
readfile.seekCur(1); readfile.seekCur(1);
} }