Mbed OS Reference
Loading...
Searching...
No Matches
File Class Reference

File class. More...

#include <File.h>

Inheritance diagram for File:
FileHandle NonCopyable< FileHandle >

Public Member Functions

 File ()
 Create an uninitialized file. More...
 
 File (FileSystem *fs, const char *path, int flags=O_RDONLY)
 Create a file on a filesystem. More...
 
virtual ~File ()
 Destroy a file. More...
 
virtual int open (FileSystem *fs, const char *path, int flags=O_RDONLY)
 Open a file on the filesystem. More...
 
virtual int close ()
 Close a file. More...
 
virtual ssize_t read (void *buffer, size_t size)
 Read the contents of a file into a buffer. More...
 
virtual ssize_t write (const void *buffer, size_t size)
 Write the contents of a buffer to a file. More...
 
virtual int sync ()
 Flush any buffers associated with the file. More...
 
virtual int isatty ()
 Check if the file in an interactive terminal device. More...
 
virtual off_t seek (off_t offset, int whence=SEEK_SET)
 Move the file position to a given offset from from a given location. More...
 
virtual off_t tell ()
 Get the file position of the file. More...
 
virtual void rewind ()
 Rewind the file position to the beginning of the file. More...
 
virtual off_t size ()
 Get the size of the file. More...
 
virtual int truncate (off_t length)
 Truncate or extend a file. More...
 
virtual int set_blocking (bool blocking)
 Set blocking or nonblocking mode of the file operation like read/write. More...
 
virtual bool is_blocking () const
 Check current blocking or nonblocking mode for file operations. More...
 
virtual int enable_input (bool enabled)
 Enable or disable input. More...
 
virtual int enable_output (bool enabled)
 Enable or disable output. More...
 
virtual short poll (short events) const
 Check for poll event flags You can use or ignore the input parameter. More...
 
bool writable () const
 Definition depends on the subclass implementing FileHandle. More...
 
bool readable () const
 Definition depends on the subclass implementing FileHandle. More...
 
virtual void sigio (Callback< void()> func)
 Register a callback on state change of the file. More...
 

Detailed Description

File class.

Definition at line 31 of file File.h.

Constructor & Destructor Documentation

◆ File() [1/2]

File ( )

Create an uninitialized file.

Must call open to initialize the file on a file system

◆ File() [2/2]

File ( FileSystem fs,
const char *  path,
int  flags = O_RDONLY 
)

Create a file on a filesystem.

Creates and opens a file on a filesystem

Parameters
fsFilesystem as target for the file
pathThe name of the file to open
flagsThe flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR, bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND

◆ ~File()

virtual ~File ( )
virtual

Destroy a file.

Closes file if the file is still open

Member Function Documentation

◆ open()

virtual int open ( FileSystem fs,
const char *  path,
int  flags = O_RDONLY 
)
virtual

Open a file on the filesystem.

Parameters
fsFilesystem as target for the file
pathThe name of the file to open
flagsThe flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR, bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
Returns
0 on success, negative error code on failure

◆ close()

virtual int close ( )
virtual

Close a file.

Returns
0 on success, negative error code on failure

Implements FileHandle.

◆ read()

virtual ssize_t read ( void *  buffer,
size_t  size 
)
virtual

Read the contents of a file into a buffer.

Parameters
bufferThe buffer to read in to
sizeThe number of bytes to read
Returns
The number of bytes read, 0 at end of file, negative error on failure

Implements FileHandle.

◆ write()

virtual ssize_t write ( const void *  buffer,
size_t  size 
)
virtual

Write the contents of a buffer to a file.

Parameters
bufferThe buffer to write from
sizeThe number of bytes to write
Returns
The number of bytes written, negative error on failure

Implements FileHandle.

◆ sync()

virtual int sync ( )
virtual

Flush any buffers associated with the file.

Returns
0 on success, negative error code on failure

Reimplemented from FileHandle.

◆ isatty()

virtual int isatty ( )
virtual

Check if the file in an interactive terminal device.

Returns
True if the file is a terminal

Reimplemented from FileHandle.

◆ seek()

virtual off_t seek ( off_t  offset,
int  whence = SEEK_SET 
)
virtual

Move the file position to a given offset from from a given location.

Parameters
offsetThe offset from whence to move to
whenceThe start of where to seek SEEK_SET to start from beginning of file, SEEK_CUR to start from current position in file, SEEK_END to start from end of file
Returns
The new offset of the file

Implements FileHandle.

◆ tell()

virtual off_t tell ( )
virtual

Get the file position of the file.

Returns
The current offset in the file

Reimplemented from FileHandle.

◆ rewind()

virtual void rewind ( )
virtual

Rewind the file position to the beginning of the file.

Note
This is equivalent to file_seek(file, 0, FS_SEEK_SET)

Reimplemented from FileHandle.

◆ size()

virtual off_t size ( )
virtual

Get the size of the file.

Returns
Size of the file in bytes

Reimplemented from FileHandle.

◆ truncate()

virtual int truncate ( off_t  length)
virtual

Truncate or extend a file.

The file's length is set to the specified value. The seek pointer is not changed. If the file is extended, the extended area appears as if it were zero-filled.

Parameters
lengthThe requested new length for the file
Returns
Zero on success, negative error code on failure

Reimplemented from FileHandle.

◆ set_blocking()

virtual int set_blocking ( bool  blocking)
virtualinherited

Set blocking or nonblocking mode of the file operation like read/write.

Definition depends on the subclass implementing FileHandle. The default is blocking.

Parameters
blockingtrue for blocking mode, false for nonblocking mode.
Returns
0 on success
Negative error code on failure

Reimplemented in BufferedSerial.

Definition at line 165 of file FileHandle.h.

◆ is_blocking()

virtual bool is_blocking ( ) const
virtualinherited

Check current blocking or nonblocking mode for file operations.

Returns
true for blocking mode, false for nonblocking mode.

Reimplemented in BufferedSerial.

Definition at line 174 of file FileHandle.h.

◆ enable_input()

virtual int enable_input ( bool  enabled)
virtualinherited

Enable or disable input.

Control enabling of device for input. This is primarily intended for temporary power-saving; the overall ability of the device to operate for input and/or output may be fixed at creation time, but this call can allow input to be temporarily disabled to permit power saving without losing device state.

Parameters
enabledtrue to enable input, false to disable.
Returns
0 on success
Negative error code on failure

Reimplemented in BufferedSerial, and UnbufferedSerial.

Definition at line 192 of file FileHandle.h.

◆ enable_output()

virtual int enable_output ( bool  enabled)
virtualinherited

Enable or disable output.

Control enabling of device for output. This is primarily intended for temporary power-saving; the overall ability of the device to operate for input and/or output may be fixed at creation time, but this call can allow output to be temporarily disabled to permit power saving without losing device state.

Parameters
enabledtrue to enable output, false to disable.
Returns
0 on success
Negative error code on failure

Reimplemented in BufferedSerial, and UnbufferedSerial.

Definition at line 210 of file FileHandle.h.

◆ poll()

virtual short poll ( short  events) const
virtualinherited

Check for poll event flags You can use or ignore the input parameter.

You can return all events or check just the events listed in events. Call is nonblocking - returns instantaneous state of events. Whenever an event occurs, the derived class should call the sigio() callback).

Parameters
eventsbitmask of poll events we're interested in - POLLIN/POLLOUT etc.
Returns
bitmask of poll events that have occurred.

Reimplemented in BufferedSerial, and UnbufferedSerial.

Definition at line 225 of file FileHandle.h.

◆ writable()

bool writable ( ) const
inherited

Definition depends on the subclass implementing FileHandle.

For example, if the FileHandle is of type Stream, writable() could return true when there is ample buffer space available for write() calls.

Returns
true if the FileHandle is writable.

Definition at line 237 of file FileHandle.h.

◆ readable()

bool readable ( ) const
inherited

Definition depends on the subclass implementing FileHandle.

For example, if the FileHandle is of type Stream, readable() could return true when there is something available to read.

Returns
true when there is something available to read.

Definition at line 248 of file FileHandle.h.

◆ sigio()

virtual void sigio ( Callback< void()>  func)
virtualinherited

Register a callback on state change of the file.

The specified callback will be called on state changes such as when the file can be written to or read from.

The callback may be called in an interrupt context and should not perform expensive operations.

Note! This is not intended as an attach-like asynchronous API, but rather as a building block for constructing such functionality.

The exact timing of when the registered function is called is not guaranteed and is susceptible to change. It should be used as a cue to make read/write/poll calls to find the current state.

Parameters
funcFunction to call on state change

Reimplemented in BufferedSerial.

Definition at line 270 of file FileHandle.h.