Adding position() function to File class and replacing FILE_TRUNCATE and FILE_APPEND with FILE_WRITE (SD library). Updating examples and keywords.txt accordingly.

This commit is contained in:
David A. Mellis 2010-12-22 15:52:09 -06:00
parent 5057d5b109
commit 4742739d6e
7 changed files with 18 additions and 9 deletions

View File

@ -55,6 +55,10 @@ boolean File::seek(uint32_t pos) {
return SD.file.seekSet(pos);
}
uint32_t File::position() {
return SD.file.curPosition();
}
uint32_t File::size() {
return SD.file.fileSize();
}

View File

@ -297,6 +297,9 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
if (isLastComponent) {
SDClass *p_SD = static_cast<SDClass*>(object);
p_SD->file.open(parentDir, filePathComponent, p_SD->fileOpenMode);
if (p_SD->fileOpenMode == FILE_WRITE) {
p_SD->file.seekSet(p_SD->file.fileSize());
}
p_SD->c = -1;
// TODO: Return file open result?
return false;

View File

@ -21,8 +21,7 @@
#include <utility/SdFatUtil.h>
#define FILE_READ O_READ
#define FILE_TRUNCATE (O_WRITE | O_CREAT | O_TRUNC)
#define FILE_APPEND (O_WRITE | O_CREAT | O_APPEND)
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT | O_SYNC)
class File : public Stream {
public:
@ -34,6 +33,7 @@ public:
virtual int available();
virtual void flush();
boolean seek(uint32_t pos);
uint32_t position();
uint32_t size();
void close();
operator bool();

View File

@ -59,8 +59,9 @@ void loop()
}
}
// open the file:
File dataFile = SD.open("datalog.txt", FILE_APPEND);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {

View File

@ -46,7 +46,7 @@ void setup()
// open a new file and immediately close it:
Serial.println("Creating example.txt...");
myFile = SD.open("example.txt", FILE_TRUNCATE);
myFile = SD.open("example.txt", FILE_WRITE);
myFile.close();
// Check to see if the file exists:

View File

@ -39,7 +39,7 @@ void setup()
Serial.println("initialization done.");
// open a file:
myFile = SD.open("test.txt", FILE_TRUNCATE);
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {

View File

@ -19,11 +19,12 @@ remove KEYWORD2
rmdir KEYWORD2
open KEYWORD2
close KEYWORD2
seek KEYWORD2
position KEYWORD2
size KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
FILE_READ LITERAL1
FILE_TRUNCATE LITERAL1
FILE_APPEND LITERAL1
FILE_WRITE LITERAL1