SdFs
Public Member Functions | List of all members
FsFile Class Reference

FsFile class. More...

#include <FsFile.h>

Inheritance diagram for FsFile:
Inheritance graph
[legend]
Collaboration diagram for FsFile:
Collaboration graph
[legend]

Public Member Functions

int available ()
 
bool close ()
 
uint64_t curPosition ()
 
bool exists (const char *path)
 
int fgets (char *str, int num, char *delim=nullptr)
 
uint64_t fileSize ()
 
void flush ()
 
 FsFile (const FsFile &from)
 
size_t getName (char *name, size_t len)
 
bool isDir ()
 
bool isDirectory ()
 
bool isHidden ()
 
bool isOpen ()
 
void ls (uint8_t flags)
 
void ls ()
 
void ls (print_t *pr)
 
void ls (print_t *pr, uint8_t flags)
 
bool mkdir (FsFile *dir, const char *path, bool pFlag=true)
 
const char * name () const
 
bool open (FsFile *dir, const char *path, uint8_t oflag=O_READ)
 
bool open (FsVolume *vol, const char *path, uint8_t oflag)
 
bool open (const char *path, uint8_t oflag=O_READ)
 
bool openNext (FsFile *dir, uint8_t oflag=O_READ)
 
 operator bool ()
 
FsFileoperator= (const FsFile &from)
 
int peek ()
 
uint64_t position ()
 
bool preAllocate (uint64_t length)
 
size_t printFileSize (print_t *pr)
 
size_t printModifyDateTime (print_t *pr)
 
size_t printName (print_t *pr)
 
int read ()
 
int read (void *buf, size_t count)
 
bool remove ()
 
bool remove (const char *path)
 
bool rename (const char *newPath)
 
bool rename (FsFile *dirFile, const char *newPath)
 
void rewind ()
 
void rewindDirectory ()
 
bool rmdir ()
 
bool seek (uint64_t pos)
 
bool seekCur (int64_t offset)
 
bool seekEnd (int64_t offset=0)
 
bool seekSet (uint64_t pos)
 
uint64_t size ()
 
bool sync ()
 
bool truncate ()
 
bool truncate (uint64_t length)
 
size_t write (uint8_t b)
 
size_t write (const void *buf, size_t count)
 

Detailed Description

FsFile class.

Constructor & Destructor Documentation

FsFile::FsFile ( const FsFile from)

Copy constructor.

Parameters
[in]fromObject used to initialize this instance.

Copyright (c) 20011-2017 Bill Greiman This file is part of the SdFs library for SD memory cards.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Member Function Documentation

int FsFile::available ( )
inline
Returns
number of bytes available from the current position to EOF or INT_MAX if more than INT_MAX bytes are available.
bool FsFile::close ( )

Close a file and force cached data and directory information to be written to the storage device.

Returns
The value true is returned for success and the value false is returned for failure.
uint64_t FsFile::curPosition ( )
inline
Returns
The current position for a file or directory.
bool FsFile::exists ( const char *  path)
inline

Test for the existence of a file in a directory

Parameters
[in]pathPath 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.

Returns
true if the file exists else false.
int FsFile::fgets ( char *  str,
int  num,
char *  delim = nullptr 
)
inline

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.

Parameters
[out]strPointer to the array where the string is stored.
[in]numMaximum number of characters to be read (including the final null byte). Usually the length of the array str is used.
[in]delimOptional set of delimiters. The default is "\n".
Returns
For success fgets() returns the length of the string in str. If no data is read, fgets() returns zero for EOF or -1 if an error occurred.
uint64_t FsFile::fileSize ( )
inline
Returns
The total number of bytes in a file.
void FsFile::flush ( )
inline

Ensure that any bytes written to the file are saved to the SD card.

size_t FsFile::getName ( char *  name,
size_t  len 
)
inline

Get a file's name followed by a zero byte.

Parameters
[out]nameAn array of characters for the file's name.
[in]lenThe 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.
Returns
The length of the returned string.
bool FsFile::isDir ( )
inline
Returns
True if this is a directory else false.
bool FsFile::isDirectory ( )
inline

This function reports if the current file is a directory or not.

Returns
true if the file is a directory.
bool FsFile::isHidden ( )
inline
Returns
True if this is a hidden file else false.
bool FsFile::isOpen ( )
inline
Returns
True if this is an open file/directory else false.
void FsFile::ls ( uint8_t  flags)
inline

List directory contents.

Parameters
[in]flagsThe inclusive OR of

LS_DATE - Print file modification date

LS_SIZE - Print file size.

LS_R - Recursive list of subdirectories.

void FsFile::ls ( )
inline

List directory contents.

void FsFile::ls ( print_t *  pr)
inline

List directory contents.

Parameters
[in]prPrint object.
void FsFile::ls ( print_t *  pr,
uint8_t  flags 
)
inline

List directory contents.

Parameters
[in]prPrint object.
[in]flagsThe inclusive OR of

LS_DATE - Print file modification date

LS_SIZE - Print file size.

LS_R - Recursive list of subdirectories.

bool FsFile::mkdir ( FsFile dir,
const char *  path,
bool  pFlag = true 
)

Make a new directory.

Parameters
[in]dirAn open FatFile instance for the directory that will contain the new directory.
[in]pathA path with a valid 8.3 DOS name for the new directory.
[in]pFlagCreate missing parent directories if true.
Returns
The value true is returned for success and the value false is returned for failure.
const char* FsFile::name ( ) const
inline

No longer implemented due to Long File Names.

Use getName(char* name, size_t size).

Returns
a pointer to replacement suggestion.
bool FsFile::open ( FsFile dir,
const char *  path,
uint8_t  oflag = O_READ 
)

Open a file or directory by name.

Parameters
[in]dirAn open file instance for the directory containing the file to be opened.
[in]pathA path with a valid 8.3 DOS name for a file to be opened.
[in]oflagValues 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_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 file object or file corruption may occur.

Note
Directory files must be opened read only. Write and truncation is not allowed for directory files.
Returns
The value true is returned for success and the value false is returned for failure.
bool FsFile::open ( FsVolume vol,
const char *  path,
uint8_t  oflag 
)

Open a file or directory by name.

Parameters
[in]volVolume where the file is located.
[in]pathA path for a file to be opened.
[in]oflagValues for oflag are constructed by a bitwise-inclusive OR of open flags.
Returns
The value true is returned for success and the value false is returned for failure.
bool FsFile::open ( const char *  path,
uint8_t  oflag = O_READ 
)
inline

Open a file or directory by name.

Parameters
[in]pathA path for a file to be opened.
[in]oflagValues for oflag are constructed by a bitwise-inclusive OR of open flags.
Returns
The value true is returned for success and the value false is returned for failure.
bool FsFile::openNext ( FsFile dir,
uint8_t  oflag = O_READ 
)

Opens the next file or folder in a directory.

Parameters
[in]dirdirectory containing files.
[in]oflagopen flags.
Returns
a file object.
FsFile::operator bool ( )
inline

The parenthesis operator.

Returns
true if a file is open.
FsFile & FsFile::operator= ( const FsFile from)

Copy assignment operator

Parameters
[in]fromObject used to initialize this instance.
Returns
assigned object.
int FsFile::peek ( )
inline

Return the next available byte without consuming it.

Returns
The byte if no error and not at eof else -1;
uint64_t FsFile::position ( )
inline
Returns
the current file position.
bool FsFile::preAllocate ( uint64_t  length)
inline

Allocate contiguous clusters to an empty file.

The file must be empty with no clusters allocated.

The file will contain uninitialized data for FAT16/FAT32 files. exFAT files will have zero validLength and dataLength will equal the requested length.

Parameters
[in]lengthsize of the file in bytes.
Returns
true for success else false.
size_t FsFile::printFileSize ( print_t *  pr)
inline

Print a file's size.

Parameters
[in]prPrint stream for output.
Returns
The number of characters printed is returned for success and zero is returned for failure.
size_t FsFile::printModifyDateTime ( print_t *  pr)
inline

Print a file's modify date and time

Parameters
[in]prPrint stream for output.
Returns
The value true is returned for success and the value false is returned for failure.
size_t FsFile::printName ( print_t *  pr)
inline

Print a file's name

Parameters
[in]prPrint stream for output.
Returns
The value true is returned for success and the value false is returned for failure.
int FsFile::read ( )
inline

Read the next byte from a file.

Returns
For success return the next byte in the file as an int. If an error occurs or end of file is reached return -1.
int FsFile::read ( void *  buf,
size_t  count 
)
inline

Read data from a file starting at the current position.

Parameters
[out]bufPointer to the location that will receive the data.
[in]countMaximum number of bytes to read.
Returns
For success read() returns the number of bytes read. A value less than count, including zero, will be returned if end of file is reached. If an error occurs, read() returns -1. Possible errors include read() called before a file has been opened, corrupt file system or an I/O error occurred.
bool FsFile::remove ( )

Remove a file.

The directory entry and all data for the file are deleted.

Note
This function should not be used to delete the 8.3 version of a file that has a long name. For example if a file has the long name "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
Returns
The value true is returned for success and the value false is returned for failure.
bool FsFile::remove ( const char *  path)
inline

Remove a file.

The directory entry and all data for the file are deleted.

Parameters
[in]pathPath for the file to be removed.

Example use: dirFile.remove(filenameToRemove);

Note
This function should not be used to delete the 8.3 version of a file that has a long name. For example if a file has the long name "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
Returns
The value true is returned for success and the value false is returned for failure.
bool FsFile::rename ( const char *  newPath)
inline

Rename a file or subdirectory.

Parameters
[in]newPathNew path name for the file/directory.
Returns
The value true is returned for success and the value false is returned for failure.
bool FsFile::rename ( FsFile dirFile,
const char *  newPath 
)
inline

Rename a file or subdirectory.

Parameters
[in]dirFileDirectory for the new path.
[in]newPathNew path name for the file/directory.
Returns
The value true is returned for success and the value false is returned for failure.
void FsFile::rewind ( )
inline

Set the file's current position to zero.

void FsFile::rewindDirectory ( )
inline

Rewind a file if it is a directory

bool FsFile::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.

Note
This function should not be used to delete the 8.3 version of a directory that has a long name. For example if a directory has the long name "New folder" you should not delete the 8.3 name "NEWFOL~1".
Returns
The value true is returned for success and the value false is returned for failure.
bool FsFile::seek ( uint64_t  pos)
inline

Seek to a new position in the file, which must be between 0 and the size of the file (inclusive).

Parameters
[in]posthe new file position.
Returns
true for success else false.
bool FsFile::seekCur ( int64_t  offset)
inline

Set the files position to current position + pos. See seekSet().

Parameters
[in]offsetThe new position in bytes from the current position.
Returns
true for success or false for failure.
bool FsFile::seekEnd ( int64_t  offset = 0)
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.

Parameters
[in]offsetThe new position in bytes from end-of-file.
Returns
true for success or false for failure.
bool FsFile::seekSet ( uint64_t  pos)
inline

Sets a file's position.

Parameters
[in]posThe new position in bytes from the beginning of the file.
Returns
The value true is returned for success and the value false is returned for failure.
uint64_t FsFile::size ( )
inline
Returns
the file's size.
bool FsFile::sync ( )
inline

The sync() call causes all modified data and directory fields to be written to the storage device.

Returns
The value true is returned for success and the value false is returned for failure.
bool FsFile::truncate ( )
inline

Truncate a file to the current position.

Returns
The value true is returned for success and the value false is returned for failure.
bool FsFile::truncate ( uint64_t  length)
inline

Truncate a file to a specified length. The current file position will be set to end of file.

Parameters
[in]lengthThe desired length for the file.
Returns
The value true is returned for success and the value false is returned for failure.
size_t FsFile::write ( uint8_t  b)
inline

Write a byte to a file. Required by the Arduino Print class.

Parameters
[in]bthe byte to be written. Use getWriteError to check for errors.
Returns
1 for success and 0 for failure.
size_t FsFile::write ( const void *  buf,
size_t  count 
)
inline

Write data to an open file.

Note
Data is moved to the cache but may not be written to the storage device until sync() is called.
Parameters
[in]bufPointer to the location of the data to be written.
[in]countNumber of bytes to write.
Returns
For success write() returns the number of bytes written, always nbyte. If an error occurs, write() returns -1. Possible errors include write() is called before a file has been opened, write is called for a read-only file, device is full, a corrupt file system or an I/O error.

The documentation for this class was generated from the following files: