SdFat
|
Basic file class. More...
#include <FatFile.h>
Public Member Functions | |
uint32_t | available () |
void | clearError () |
void | clearWriteError () |
bool | close () |
bool | contiguousRange (uint32_t *bgnBlock, uint32_t *endBlock) |
bool | createContiguous (FatFile *dirFile, const char *path, uint32_t size) |
bool | createContiguous (const char *path, uint32_t size) |
uint32_t | curCluster () const |
uint32_t | curPosition () const |
bool | dirEntry (dir_t *dir) |
uint16_t | dirIndex () |
uint32_t | dirSize () |
void | dmpFile (print_t *pr, uint32_t pos, size_t n) |
bool | exists (const char *path) |
FatFile () | |
FatFile (const char *path, uint8_t oflag) | |
int16_t | fgets (char *str, int16_t num, char *delim=0) |
uint8_t | fileAttr () const |
uint32_t | fileSize () const |
uint32_t | firstBlock () |
uint32_t | firstCluster () const |
uint8_t | getError () |
bool | getName (char *name, size_t size) |
void | getpos (FatPos_t *pos) |
bool | getSFN (char *name) |
bool | getWriteError () |
bool | isDir () const |
bool | isFile () const |
bool | isHidden () const |
bool | isLFN () const |
bool | isOpen () const |
bool | isReadOnly () const |
bool | isRoot () const |
bool | isRoot32 () const |
bool | isRootFixed () const |
bool | isSubDir () const |
bool | isSystem () const |
void | ls (uint8_t flags=0) |
void | ls (print_t *pr, uint8_t flags=0, uint8_t indent=0) |
bool | mkdir (FatFile *dir, const char *path, bool pFlag=true) |
bool | open (FatFileSystem *fs, const char *path, uint8_t oflag) |
bool | open (FatFile *dirFile, uint16_t index, uint8_t oflag) |
bool | open (FatFile *dirFile, const char *path, uint8_t oflag) |
bool | open (const char *path, uint8_t oflag=O_READ) |
bool | openNext (FatFile *dirFile, uint8_t oflag=O_READ) |
bool | openRoot (FatVolume *vol) |
int | peek () |
bool | printCreateDateTime (print_t *pr) |
int | printField (float value, char term, uint8_t prec=2) |
int | printField (int16_t value, char term) |
int | printField (uint16_t value, char term) |
int | printField (int32_t value, char term) |
int | printField (uint32_t value, char term) |
size_t | printFileSize (print_t *pr) |
bool | printModifyDateTime (print_t *pr) |
size_t | printName () |
size_t | printName (print_t *pr) |
size_t | printSFN (print_t *pr) |
int | read () |
int | read (void *buf, size_t nbyte) |
int8_t | readDir (dir_t *dir) |
bool | remove () |
bool | rename (FatFile *dirFile, const char *newPath) |
void | rewind () |
bool | rmdir () |
bool | rmRfStar () |
bool | seekCur (int32_t offset) |
bool | seekEnd (int32_t offset=0) |
bool | seekSet (uint32_t pos) |
void | setpos (FatPos_t *pos) |
bool | sync () |
bool | timestamp (FatFile *file) |
bool | timestamp (uint8_t flags, uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) |
bool | truncate (uint32_t length) |
FatVolume * | volume () const |
int | write (const char *str) |
int | write (uint8_t b) |
int | write (const void *buf, size_t nbyte) |
Static Public Member Functions | |
static FatFile * | cwd () |
static void | dateTimeCallback (void(*dateTime)(uint16_t *date, uint16_t *time)) |
static void | dateTimeCallbackCancel () |
static uint8_t | dirName (const dir_t *dir, char *name) |
static bool | legal83Char (uint8_t c) |
static void | printFatDate (uint16_t fatDate) |
static void | printFatDate (print_t *pr, uint16_t fatDate) |
static void | printFatTime (uint16_t fatTime) |
static void | printFatTime (print_t *pr, uint16_t fatTime) |
static bool | remove (FatFile *dirFile, const char *path) |
static bool | setCwd (FatFile *dir) |
Basic file class.
|
inline |
Create an instance.
|
inline |
Create a file object and open it in the current working directory.
[in] | path | A path with a valid 8.3 DOS name for a file to be opened. |
[in] | oflag | Values for oflag are constructed by a bitwise-inclusive OR of open flags. see FatFile::open(FatFile*, const char*, uint8_t). |
|
inline |
|
inline |
Clear all error bits.
|
inline |
Set writeError to zero
bool FatFile::close | ( | ) |
Close a file and force cached data and directory information to be written to the storage device.
bool FatFile::contiguousRange | ( | uint32_t * | bgnBlock, |
uint32_t * | endBlock | ||
) |
Check for contiguous file and return its raw block range.
[out] | bgnBlock | the first block address for the file. |
[out] | endBlock | the last block address for the file. |
bool FatFile::createContiguous | ( | FatFile * | dirFile, |
const char * | path, | ||
uint32_t | size | ||
) |
Create and open a new contiguous file of a specified size.
[in] | dirFile | The directory where the file will be created. |
[in] | path | A path with a validfile name. |
[in] | size | The desired file size. |
|
inline |
Create and open a new contiguous file of a specified size.
[in] | path | A path with a validfile name. |
[in] | size | The desired file size. |
|
inline |
|
inline |
|
inlinestatic |
|
inlinestatic |
Set the date/time callback function
[in] | dateTime | The user's call back function. The callback function is of the form: |
Sets the function that is called when a file is created or when a file's directory entry is modified by sync(). All timestamps, access, creation, and modify, are set when a file is created. sync() maintains the last access date and last modify date/time.
See the timestamp() function.
|
inlinestatic |
Cancel the date/time callback function.
bool FatFile::dirEntry | ( | dir_t * | dir | ) |
Return a file's directory entry.
[out] | dir | Location for return of the file's directory entry. |
|
inline |
|
static |
Format the name field of dir into the 13 byte array name in standard 8.3 short name format.
[in] | dir | The directory structure containing the name. |
[out] | name | A 13 byte char array for the formatted name. |
uint32_t FatFile::dirSize | ( | ) |
void FatFile::dmpFile | ( | print_t * | pr, |
uint32_t | pos, | ||
size_t | n | ||
) |
Dump file in Hex
[in] | pr | Print stream for list. |
[in] | pos | Start position in file. |
[in] | n | number of locations to dump. |
|
inline |
Test for the existence of a file in a directory
[in] | path | Path of the file to be tested for. |
The calling instance must be an open directory file.
dirFile.exists("TOFIND.TXT") searches for "TOFIND.TXT" in the directory dirFile.
int16_t FatFile::fgets | ( | char * | str, |
int16_t | num, | ||
char * | delim = 0 |
||
) |
Get a string from a file.
fgets() reads bytes from a file into the array pointed to by str, until num - 1 bytes are read, or a delimiter is read and transferred to str, or end-of-file is encountered. The string is then terminated with a null byte.
fgets() deletes CR, '\r', from the string. This insures only a '\n' terminates the string for Windows text files which use CRLF for newline.
[out] | str | Pointer to the array where the string is stored. |
[in] | num | Maximum number of characters to be read (including the final null byte). Usually the length of the array str is used. |
[in] | delim | Optional set of delimiters. The default is "\n". |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
bool FatFile::getName | ( | char * | name, |
size_t | size | ||
) |
Get a file's name followed by a zero byte.
[out] | name | An array of characters for the file's name. |
[in] | size | The size of the array in bytes. The array must be at least 13 bytes long. The file's name will be truncated if the file's name is too long. |
void FatFile::getpos | ( | FatPos_t * | pos | ) |
get position for streams
[out] | pos | struct to receive position |
bool FatFile::getSFN | ( | char * | name | ) |
Get a file's Short File Name followed by a zero byte.
[out] | name | An array of characters for the file's name. The array must be at least 13 bytes long. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inlinestatic |
Check for a legal 8.3 character.
[in] | c | Character to be checked. |
|
inline |
List directory contents.
[in] | flags | The inclusive OR of |
LS_DATE - Print file modification date
LS_SIZE - Print file size.
LS_R - Recursive list of subdirectories.
void FatFile::ls | ( | print_t * | pr, |
uint8_t | flags = 0 , |
||
uint8_t | indent = 0 |
||
) |
List directory contents.
[in] | pr | Print stream for list. |
[in] | flags | The inclusive OR of |
LS_DATE - Print file modification date
LS_SIZE - Print file size.
LS_R - Recursive list of subdirectories.
[in] | indent | Amount of space before file name. Used for recursive list to indicate subdirectory level. |
bool FatFile::mkdir | ( | FatFile * | dir, |
const char * | path, | ||
bool | pFlag = true |
||
) |
Make a new directory.
[in] | dir | An open FatFile instance for the directory that will contain the new directory. |
[in] | path | A path with a valid 8.3 DOS name for the new directory. |
[in] | pFlag | Create missing parent directories if true. |
bool FatFile::open | ( | FatFileSystem * | fs, |
const char * | path, | ||
uint8_t | oflag | ||
) |
Open a file in the volume working directory of a FatFileSystem.
[in] | fs | File System where the file is located. |
[in] | path | with a valid 8.3 DOS name for a file to be opened. |
[in] | oflag | bitwise-inclusive OR of open mode flags. See see FatFile::open(FatFile*, const char*, uint8_t). |
bool FatFile::open | ( | FatFile * | dirFile, |
uint16_t | index, | ||
uint8_t | oflag | ||
) |
Open a file by index.
[in] | dirFile | An open FatFile instance for the directory. |
[in] | index | The index of the directory entry for the file to be opened. The value for index is (directory file position)/32. |
[in] | oflag | bitwise-inclusive OR of open mode flags. See see FatFile::open(FatFile*, const char*, uint8_t). |
See open() by path for definition of flags.
bool FatFile::open | ( | FatFile * | dirFile, |
const char * | path, | ||
uint8_t | oflag | ||
) |
Open a file or directory by name.
[in] | dirFile | An open FatFile instance for the directory containing the file to be opened. |
[in] | path | A path with a valid 8.3 DOS name for a file to be opened. |
[in] | oflag | Values for oflag are constructed by a bitwise-inclusive OR of flags from the following list |
O_READ - Open for reading.
O_RDONLY - Same as O_READ.
O_WRITE - Open for writing.
O_WRONLY - Same as O_WRITE.
O_RDWR - Open for reading and writing.
O_APPEND - If set, the file offset shall be set to the end of the file prior to each write.
O_AT_END - Set the initial position at the end of the file.
O_CREAT - If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, the file shall be created
O_EXCL - If O_CREAT and O_EXCL are set, open() shall fail if the file exists.
O_SYNC - Call sync() after each write. This flag should not be used with write(uint8_t) or any functions do character at a time writes since sync() will be called after each byte.
O_TRUNC - If the file exists and is a regular file, and the file is successfully opened and is not read only, its length shall be truncated to 0.
WARNING: A given file must not be opened by more than one FatFile object or file corruption may occur.
|
inline |
Open a file in the current working directory.
[in] | path | A path with a valid 8.3 DOS name for a file to be opened. |
[in] | oflag | bitwise-inclusive OR of open mode flags. See see FatFile::open(FatFile*, const char*, uint8_t). |
bool FatFile::openNext | ( | FatFile * | dirFile, |
uint8_t | oflag = O_READ |
||
) |
Open the next file or subdirectory in a directory.
[in] | dirFile | An open FatFile instance for the directory containing the file to be opened. |
[in] | oflag | bitwise-inclusive OR of open mode flags. See see FatFile::open(FatFile*, const char*, uint8_t). |
bool FatFile::openRoot | ( | FatVolume * | vol | ) |
Open a volume's root directory.
[in] | vol | The FAT volume containing the root directory to be opened. |
int FatFile::peek | ( | ) |
Return the next available byte without consuming it.
bool FatFile::printCreateDateTime | ( | print_t * | pr | ) |
Print a file's creation date and time
[in] | pr | Print stream for output. |
|
inlinestatic |
Print a directory date field.
Format is yyyy-mm-dd.
[in] | fatDate | The date field from a directory entry. |
|
static |
Print a directory date field.
Format is yyyy-mm-dd.
[in] | pr | Print stream for output. |
[in] | fatDate | The date field from a directory entry. |
|
inlinestatic |
Print a directory time field.
Format is hh:mm:ss.
[in] | fatTime | The time field from a directory entry. |
|
static |
Print a directory time field.
Format is hh:mm:ss.
[in] | pr | Print stream for output. |
[in] | fatTime | The time field from a directory entry. |
int FatFile::printField | ( | float | value, |
char | term, | ||
uint8_t | prec = 2 |
||
) |
Print a number followed by a field terminator.
[in] | value | The number to be printed. |
[in] | term | The field terminator. Use '\n' for CR LF. |
[in] | prec | Number of digits after decimal point. |
int FatFile::printField | ( | int16_t | value, |
char | term | ||
) |
Print a number followed by a field terminator.
[in] | value | The number to be printed. |
[in] | term | The field terminator. Use '\n' for CR LF. |
int FatFile::printField | ( | uint16_t | value, |
char | term | ||
) |
Print a number followed by a field terminator.
[in] | value | The number to be printed. |
[in] | term | The field terminator. Use '\n' for CR LF. |
int FatFile::printField | ( | int32_t | value, |
char | term | ||
) |
Print a number followed by a field terminator.
[in] | value | The number to be printed. |
[in] | term | The field terminator. Use '\n' for CR LF. |
int FatFile::printField | ( | uint32_t | value, |
char | term | ||
) |
Print a number followed by a field terminator.
[in] | value | The number to be printed. |
[in] | term | The field terminator. Use '\n' for CR LF. |
size_t FatFile::printFileSize | ( | print_t * | pr | ) |
Print a file's size.
[in] | pr | Print stream for output. |
bool FatFile::printModifyDateTime | ( | print_t * | pr | ) |
Print a file's modify date and time
[in] | pr | Print stream for output. |
|
inline |
Print a file's name.
size_t FatFile::printName | ( | print_t * | pr | ) |
Print a file's name
[in] | pr | Print stream for output. |
size_t FatFile::printSFN | ( | print_t * | pr | ) |
Print a file's Short File Name.
[in] | pr | Print stream for output. |
|
inline |
Read the next byte from a file.
int FatFile::read | ( | void * | buf, |
size_t | nbyte | ||
) |
Read data from a file starting at the current position.
[out] | buf | Pointer to the location that will receive the data. |
[in] | nbyte | Maximum number of bytes to read. |
int8_t FatFile::readDir | ( | dir_t * | dir | ) |
Read the next directory entry from a directory file.
[out] | dir | The dir_t struct that will receive the data. |
bool FatFile::remove | ( | ) |
Remove a file.
The directory entry and all data for the file are deleted.
|
static |
Remove a file.
The directory entry and all data for the file are deleted.
[in] | dirFile | The directory that contains the file. |
[in] | path | Path for the file to be removed. |
bool FatFile::rename | ( | FatFile * | dirFile, |
const char * | newPath | ||
) |
Rename a file or subdirectory.
[in] | dirFile | Directory for the new path. |
[in] | newPath | New path name for the file/directory. |
|
inline |
Set the file's current position to zero.
bool FatFile::rmdir | ( | ) |
Remove a directory file.
The directory file will be removed only if it is empty and is not the root directory. rmdir() follows DOS and Windows and ignores the read-only attribute for the directory.
bool FatFile::rmRfStar | ( | ) |
Recursively delete a directory and all contained files.
This is like the Unix/Linux 'rm -rf *' if called with the root directory hence the name.
Warning - This will remove all contents of the directory including subdirectories. The directory will then be removed if it is not root. The read-only attribute for files will be ignored.
|
inline |
Set the files position to current position + pos. See seekSet().
[in] | offset | The new position in bytes from the current position. |
|
inline |
Set the files position to end-of-file + offset. See seekSet(). Can't be used for directory files since file size is not defined.
[in] | offset | The new position in bytes from end-of-file. |
bool FatFile::seekSet | ( | uint32_t | pos | ) |
Sets a file's position.
[in] | pos | The new position in bytes from the beginning of the file. |
|
inlinestatic |
Set the current working directory.
[in] | dir | New current working directory. |
void FatFile::setpos | ( | FatPos_t * | pos | ) |
set position for streams
[out] | pos | struct with value for new position |
bool FatFile::sync | ( | ) |
The sync() call causes all modified data and directory fields to be written to the storage device.
bool FatFile::timestamp | ( | FatFile * | file | ) |
Copy a file's timestamps
[in] | file | File to copy timestamps from. |
bool FatFile::timestamp | ( | uint8_t | flags, |
uint16_t | year, | ||
uint8_t | month, | ||
uint8_t | day, | ||
uint8_t | hour, | ||
uint8_t | minute, | ||
uint8_t | second | ||
) |
Set a file's timestamps in its directory entry.
[in] | flags | Values for flags are constructed by a bitwise-inclusive OR of flags from the following list |
T_ACCESS - Set the file's last access date.
T_CREATE - Set the file's creation date and time.
T_WRITE - Set the file's last write/modification date and time.
[in] | year | Valid range 1980 - 2107 inclusive. |
[in] | month | Valid range 1 - 12 inclusive. |
[in] | day | Valid range 1 - 31 inclusive. |
[in] | hour | Valid range 0 - 23 inclusive. |
[in] | minute | Valid range 0 - 59 inclusive. |
[in] | second | Valid range 0 - 59 inclusive |
bool FatFile::truncate | ( | uint32_t | length | ) |
Truncate a file to a specified length. The current file position will be maintained if it is less than or equal to length otherwise it will be set to end of file.
[in] | length | The desired length for the file. |
|
inline |
Write a string to a file. Used by the Arduino Print class.
[in] | str | Pointer to the string. Use getWriteError to check for errors. |
|
inline |
Write a single byte.
[in] | b | The byte to be written. |
int FatFile::write | ( | const void * | buf, |
size_t | nbyte | ||
) |
Write data to an open file.
[in] | buf | Pointer to the location of the data to be written. |
[in] | nbyte | Number of bytes to write. |