From ab9e36e12e94a177ba7f619f584ac79f047cc7f6 Mon Sep 17 00:00:00 2001 From: Vincent Pelletier Date: Sat, 5 Nov 2022 00:31:51 +0000 Subject: [PATCH] Cart_Reader.ino: Comment rewind_line implementation The logic of this function is not immediately clear, so comment it extensively. --- Cart_Reader/Cart_Reader.ino | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Cart_Reader/Cart_Reader.ino b/Cart_Reader/Cart_Reader.ino index f294bdf..91a1acc 100644 --- a/Cart_Reader/Cart_Reader.ino +++ b/Cart_Reader/Cart_Reader.ino @@ -675,15 +675,26 @@ void get_line(char* str_buf, FsFile* readfile, uint8_t maxi) { void rewind_line(FsFile& readfile, byte count = 1) { 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 from how-many-lines-back into how-many-newlines-to-look-for + // by incrementing it by 1. count++; for (byte count_newline = 0; count_newline < count; count_newline++) { + // Go to the strictly previous '\n', or file start. while (position) { + // Seek back first (keeping position updated)... position--; 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') break; } } + // If not at file start, the current character is the '\n' just before the + // desired line, so advance by one. if (position) readfile.seekCur(1); }