Skip to content

Commit 4742739

Browse files
committedDec 22, 2010
Adding position() function to File class and replacing FILE_TRUNCATE and FILE_APPEND with FILE_WRITE (SD library). Updating examples and keywords.txt accordingly.
1 parent 5057d5b commit 4742739

File tree

7 files changed

+18
-9
lines changed

7 files changed

+18
-9
lines changed
 

‎libraries/SD/File.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ boolean File::seek(uint32_t pos) {
5555
return SD.file.seekSet(pos);
5656
}
5757

58+
uint32_t File::position() {
59+
return SD.file.curPosition();
60+
}
61+
5862
uint32_t File::size() {
5963
return SD.file.fileSize();
6064
}

‎libraries/SD/SD.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
297297
if (isLastComponent) {
298298
SDClass *p_SD = static_cast<SDClass*>(object);
299299
p_SD->file.open(parentDir, filePathComponent, p_SD->fileOpenMode);
300+
if (p_SD->fileOpenMode == FILE_WRITE) {
301+
p_SD->file.seekSet(p_SD->file.fileSize());
302+
}
300303
p_SD->c = -1;
301304
// TODO: Return file open result?
302305
return false;

‎libraries/SD/SD.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
#include <utility/SdFatUtil.h>
2222

2323
#define FILE_READ O_READ
24-
#define FILE_TRUNCATE (O_WRITE | O_CREAT | O_TRUNC)
25-
#define FILE_APPEND (O_WRITE | O_CREAT | O_APPEND)
24+
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT | O_SYNC)
2625

2726
class File : public Stream {
2827
public:
@@ -34,6 +33,7 @@ class File : public Stream {
3433
virtual int available();
3534
virtual void flush();
3635
boolean seek(uint32_t pos);
36+
uint32_t position();
3737
uint32_t size();
3838
void close();
3939
operator bool();

‎libraries/SD/examples/Datalogger/Datalogger.pde

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ void loop()
5959
}
6060
}
6161

62-
// open the file:
63-
File dataFile = SD.open("datalog.txt", FILE_APPEND);
62+
// open the file. note that only one file can be open at a time,
63+
// so you have to close this one before opening another.
64+
File dataFile = SD.open("datalog.txt", FILE_WRITE);
6465

6566
// if the file is available, write to it:
6667
if (dataFile) {

‎libraries/SD/examples/Files/Files.pde

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void setup()
4646

4747
// open a new file and immediately close it:
4848
Serial.println("Creating example.txt...");
49-
myFile = SD.open("example.txt", FILE_TRUNCATE);
49+
myFile = SD.open("example.txt", FILE_WRITE);
5050
myFile.close();
5151

5252
// Check to see if the file exists:

‎libraries/SD/examples/ReadWrite/ReadWrite.pde

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void setup()
3939
Serial.println("initialization done.");
4040

4141
// open a file:
42-
myFile = SD.open("test.txt", FILE_TRUNCATE);
42+
myFile = SD.open("test.txt", FILE_WRITE);
4343

4444
// if the file opened okay, write to it:
4545
if (myFile) {

‎libraries/SD/keywords.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ remove KEYWORD2
1919
rmdir KEYWORD2
2020
open KEYWORD2
2121
close KEYWORD2
22-
22+
seek KEYWORD2
23+
position KEYWORD2
24+
size KEYWORD2
2325

2426
#######################################
2527
# Constants (LITERAL1)
2628
#######################################
2729
FILE_READ LITERAL1
28-
FILE_TRUNCATE LITERAL1
29-
FILE_APPEND LITERAL1
30+
FILE_WRITE LITERAL1

0 commit comments

Comments
 (0)