Uniot Core
0.8.1
Loading...
Searching...
No Matches
uniot::NVSFile Class Reference

#include <NVSFS.h>

Public Member Functions

 NVSFile (const String &path, const String &mode, Preferences &prefs)
 Constructs an NVSFile object for a specific path and mode.
 
 operator bool () const
 Check if the file is valid and ready for operations.
 
size_t write (const uint8_t *data, size_t size)
 Write data to the file buffer.
 
Bytes getBytes ()
 Get the entire file content as Bytes object.
 
void close ()
 Close the file and commit any pending write operations.
 

Static Public Member Functions

static String normalizePath (const String &path)
 Normalize a file path to a valid NVS key.
 

Constructor & Destructor Documentation

◆ NVSFile()

uniot::NVSFile::NVSFile ( const String & path,
const String & mode,
Preferences & prefs )
inline

Constructs an NVSFile object for a specific path and mode.

Creates an NVSFile instance for reading from or writing to NVS storage. The file path is automatically normalized to a valid NVS key by removing leading slashes and truncating to 15 characters.

Parameters
pathThe file path (will be converted to NVS key)
modeFile access mode ("r" for read, "w" for write)
prefsReference to the Preferences object for NVS operations
Note
For read mode, data is immediately loaded from NVS into memory. For write mode, data is buffered and written to NVS only when close() is called.

Member Function Documentation

◆ close()

void uniot::NVSFile::close ( )
inline

Close the file and commit any pending write operations.

For write mode files, this method commits the buffered data to NVS. If the buffer is empty, the corresponding NVS key is removed. For read mode files, this simply marks the file as invalid.

After calling close(), the file object becomes invalid and cannot be used for further operations.

Note
This method should always be called when finished with a file to ensure data is properly saved to NVS.

◆ getBytes()

Bytes uniot::NVSFile::getBytes ( )
inline

Get the entire file content as Bytes object.

Returns a copy of the file's content. In read mode, this is the data loaded from NVS. In write mode, this would be the buffered data.

Return values
BytesFile content as Bytes object
EmptyBytes object if file is invalid or not in read mode
Note
This method is specific to the NVS implementation and not part of the standard file system interface.

◆ normalizePath()

static String uniot::NVSFile::normalizePath ( const String & path)
inlinestatic

Normalize a file path to a valid NVS key.

Converts a file system path to a valid NVS key by:

  • Removing leading forward slash "/"
  • Truncating to 15 characters maximum (NVS limitation)
  • Logging a warning if truncation occurs
Parameters
pathThe original file path
Returns
Normalized NVS key string
Note
This is a static utility method that can be used independently of any NVSFile instance.

◆ operator bool()

uniot::NVSFile::operator bool ( ) const
inline

Check if the file is valid and ready for operations.

Return values
trueFile is valid and can be used for read/write operations
falseFile is invalid (wrong mode, NVS error, etc.)

◆ write()

size_t uniot::NVSFile::write ( const uint8_t * data,
size_t size )
inline

Write data to the file buffer.

In write mode, stores the provided data in an internal buffer. The data is not actually written to NVS until close() is called. Each call to write() replaces the entire buffer content.

Parameters
dataPointer to the data to write
sizeNumber of bytes to write
Return values
sizeNumber of bytes written (same as input size on success)
0Failed to write (invalid file or not in write mode)
Warning
Current implementation replaces the entire buffer on each write. Append functionality is not yet implemented.

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