Skip to content

Commit 575bb1d

Browse files
committed
Redoing peek() implementation (SD file class).
Now simply seeking backwards by a character in peek() rather than trying to keep track of the extra character read.
1 parent a7a8f3f commit 575bb1d

File tree

4 files changed

+7
-22
lines changed

4 files changed

+7
-22
lines changed

libraries/ArduinoTestSuite/examples/ATS_SD_Files/ATS_SD_Files.pde

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void setup()
1313
if (!b) goto done;
1414

1515
ATS_PrintTestStatus("!SD.exists()", !SD.exists("asdf.txt"));
16-
ATS_PrintTestStatus("SD.open()", f = SD.open("asdf.txt", FILE_TRUNCATE)); f.close();
16+
ATS_PrintTestStatus("SD.open()", f = SD.open("asdf.txt", FILE_WRITE)); f.close();
1717
ATS_PrintTestStatus("SD.exists()", SD.exists("asdf.txt"));
1818
ATS_PrintTestStatus("SD.exists()", SD.exists("/asdf.txt"));
1919
ATS_PrintTestStatus("SD.remove()", SD.remove("asdf.txt"));
@@ -48,13 +48,13 @@ void setup()
4848
ATS_PrintTestStatus("!SD.exists()", !SD.exists("x/y"));
4949
ATS_PrintTestStatus("!SD.exists()", !SD.exists("x/y/z"));
5050

51-
ATS_PrintTestStatus("!SD.open()", !(f = SD.open("asdf/asdf.txt", FILE_TRUNCATE))); f.close();
51+
ATS_PrintTestStatus("!SD.open()", !(f = SD.open("asdf/asdf.txt", FILE_WRITE))); f.close();
5252
ATS_PrintTestStatus("!SD.exists()", !SD.exists("asdf"));
5353
ATS_PrintTestStatus("!SD.exists()", !SD.exists("asdf.txt"));
5454
ATS_PrintTestStatus("!SD.exists()", !SD.exists("asdf/asdf.txt"));
5555
ATS_PrintTestStatus("SD.mkdir()", SD.mkdir("asdf"));
5656
ATS_PrintTestStatus("SD.exists()", SD.exists("asdf"));
57-
ATS_PrintTestStatus("SD.open()", f = SD.open("asdf/asdf.txt", FILE_TRUNCATE)); f.close();
57+
ATS_PrintTestStatus("SD.open()", f = SD.open("asdf/asdf.txt", FILE_WRITE)); f.close();
5858
ATS_PrintTestStatus("SD.exists()", SD.exists("asdf/asdf.txt"));
5959
ATS_PrintTestStatus("!SD.rmdir()", !SD.rmdir("asdf"));
6060
ATS_PrintTestStatus("SD.exists()", SD.exists("asdf"));

libraries/SD/File.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,40 @@
1515
#include <SD.h>
1616

1717
void File::write(uint8_t val) {
18-
SD.c = -1;
1918
SD.file.write(val);
2019
}
2120

2221
void File::write(const char *str) {
23-
SD.c = -1;
2422
SD.file.write(str);
2523
}
2624

2725
void File::write(const uint8_t *buf, size_t size) {
28-
SD.c = -1;
2926
SD.file.write(buf, size);
3027
}
3128

3229
int File::peek() {
33-
if (SD.c != -1) return SD.c;
34-
SD.c = SD.file.read();
35-
return SD.c;
30+
char c = SD.file.read();
31+
if (c != -1) SD.file.seekCur(-1);
32+
return c;
3633
}
3734

3835
int File::read() {
39-
if (SD.c != -1) {
40-
int tmp = SD.c;
41-
SD.c = -1;
42-
return tmp;
43-
}
4436
return SD.file.read();
4537
}
4638

4739
int File::available() {
48-
if (SD.c != -1) return 1;
49-
SD.c = SD.file.read();
50-
return SD.c != -1;
40+
return size() - position();
5141
}
5242

5343
void File::flush() {
5444
SD.file.sync();
5545
}
5646

5747
boolean File::seek(uint32_t pos) {
58-
SD.c = -1;
5948
return SD.file.seekSet(pos);
6049
}
6150

6251
uint32_t File::position() {
63-
if (SD.c != -1) return SD.file.curPosition() - 1;
6452
return SD.file.curPosition();
6553
}
6654

libraries/SD/SD.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
300300
if (p_SD->fileOpenMode == FILE_WRITE) {
301301
p_SD->file.seekSet(p_SD->file.fileSize());
302302
}
303-
p_SD->c = -1;
304303
// TODO: Return file open result?
305304
return false;
306305
}

libraries/SD/SD.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ class SDClass {
7979
// It shouldn't be set directly--it is set via the parameters to `open`.
8080
int fileOpenMode;
8181

82-
int c;
83-
8482
friend class File;
8583
friend boolean callback_openPath(SdFile&, char *, boolean, void *);
8684
};

0 commit comments

Comments
 (0)