FileHelperRK
Loading...
Searching...
No Matches
Data Structures | Static Public Member Functions | Static Public Attributes
FileHelperRK Class Reference

Class to perform common file operations. More...

#include <FileHelperRK.h>

Data Structures

class  FileStreamBase
 Class for providing Stream and Print object for reading or writing a file. More...
 
class  FileStreamRead
 Class for reading from a file as a Stream. More...
 
class  FileStreamWrite
 Class for writing to a file as a Print. More...
 
class  ParsedPath
 Container for a parsed pathname (Unix-style, with slashes) More...
 
class  Usage
 Measure file system usage. More...
 
struct  WalkParameters
 Structure to hold the parameters to the walk method. More...
 

Static Public Member Functions

static int mkdirs (const char *path)
 Create all of the directories in path.
 
static int deleteRecursive (const char *path, bool contentsOfPathOnly=false)
 Delete a directory and all of the subdirectories and files.
 
static int storeBytes (const char *fileName, const uint8_t *dataPtr, size_t dataLen)
 Store bytes to a file.
 
static int storeString (const char *fileName, const String &data)
 Store a String object to a file.
 
static int storeString (const char *fileName, const char *str)
 Store a c-string to a file.
 
template<typename T >
static const T & storeStruct (const char *fileName, const T &t)
 Store a struct in a file.
 
static int readBytes (const char *fileName, uint8_t *&dataPtr, size_t &dataLen, bool nullTerminate=false)
 Read bytes from a file.
 
static int readBytesNoAlloc (const char *fileName, uint8_t *dataPtr, size_t &dataLen)
 Read bytes from a file into a buffer instead of allocating one.
 
static int readString (const char *fileName, String &result)
 Read file contents to a String object.
 
template<typename T >
static T & readStruct (const char *fileName, T &t)
 Read file contents to a struct.
 
static int errnoToSystemError ()
 Internal function to convert the value of errno into a Particle system error code.
 
static String pathJoin (const char *a, const char *b)
 Join two pathnames or filenames, adding a / delimeter in between if necessary.
 
static int walk (const char *path, std::function< void(const WalkParameters &walkParameters)> cb)
 Walk the file system calling the callback function or lambda.
 

Static Public Attributes

static const char * pathDelim = "/"
 Path delimeter ("/")
 

Detailed Description

Class to perform common file operations.

License: MIT (can use in free or commercial open or closed source products) Repository: https://github.com/rickkas7/FileHelperRK

Member Function Documentation

◆ deleteRecursive()

int FileHelperRK::deleteRecursive ( const char * path,
bool contentsOfPathOnly = false )
static

Delete a directory and all of the subdirectories and files.

Parameters
pathThe directory (or contents of the directory) to delete
contentsOfPathOnlyIf true, only delete the contents of path, leaving the top directory.
Returns
int SYSTEM_ERROR_NONE (0) on success or a system error code (non-zero)

◆ errnoToSystemError()

int FileHelperRK::errnoToSystemError ( )
static

Internal function to convert the value of errno into a Particle system error code.

Returns
int A system_error_t error code (0 = success, negative value on error)

File system errors are only defined in Device OS 5.5.0 and later. If targeting an earlier version of Device OS, returns SYSTEM_ERROR_UNKNOWN.

◆ mkdirs()

int FileHelperRK::mkdirs ( const char * path)
static

Create all of the directories in path.

Parameters
path
Returns
int SYSTEM_ERROR_NONE (0) on success or a system error code (non-zero)

The standard mkdir() function only will create the last component of the path.

◆ pathJoin()

String FileHelperRK::pathJoin ( const char * a,
const char * b )
static

Join two pathnames or filenames, adding a / delimeter in between if necessary.

Parameters
aPath component(s). Can be slash separated parts, a filename, an empty string, or NULL.
bPath component(s). Can be slash separated parts, a filename, an empty string, or NULL.
Returns
String The combined pathname

◆ readBytes()

int FileHelperRK::readBytes ( const char * fileName,
uint8_t *& dataPtr,
size_t & dataLen,
bool nullTerminate = false )
static

Read bytes from a file.

Parameters
fileNameFilename to read from
dataPtrFilled in with an allocated pointer containing the data
dataLenOn return, the length of the data in bytes
nullTerminateSet to true to null-terminate the buffer. Default is false. If null terminated, the dataLen is the actual string length, not including the null terminator.
Returns
int SYSTEM_ERROR_NONE (0) on success or a system error code (non-zero)

If the returned value in dataPtr is non-zero, you must delete it using delete[] dataPtr.

◆ readBytesNoAlloc()

int FileHelperRK::readBytesNoAlloc ( const char * fileName,
uint8_t * dataPtr,
size_t & dataLen )
static

Read bytes from a file into a buffer instead of allocating one.

Parameters
fileNameFilename to read from
dataPtrBuffer filled in with up to dataLen bytes
dataLenOn entry, size of the buffer in dataPtr. On exit, number of bytes copied to dataPtr
Returns
int SYSTEM_ERROR_NONE (0) on success or a system error code (non-zero)

◆ readString()

int FileHelperRK::readString ( const char * fileName,
String & result )
static

Read file contents to a String object.

Parameters
fileNameFilename to read from
resultString object filled in with the data
Returns
int SYSTEM_ERROR_NONE (0) on success or a system error code (non-zero)

◆ readStruct()

template<typename T >
static T & FileHelperRK::readStruct ( const char * fileName,
T & t )
inlinestatic

Read file contents to a struct.

Template Parameters
TThe struct/class to read
Parameters
fileNameFilename to read from
tThe variable to read into
Returns
T& Returns t.

If the struct is larger than the file, the extra space is padded with 0 bytes. If the struct is smaller than the file, the extra bytes rae ignored. If an error occurs, the struct is filled with 0 bytes.

◆ storeBytes()

int FileHelperRK::storeBytes ( const char * fileName,
const uint8_t * dataPtr,
size_t dataLen )
static

Store bytes to a file.

Parameters
fileNameFilename to write to. File will be created and truncated.
dataPtrPointer to binary data to write
dataLenLength of data (0 or more bytes)
Returns
int SYSTEM_ERROR_NONE (0) on success or a system error code (non-zero)

See also storeString()

◆ storeString() [1/2]

int FileHelperRK::storeString ( const char * fileName,
const char * str )
static

Store a c-string to a file.

Parameters
fileNameFilename to write to. File will be created and truncated.
strc-string to write. Can be an empty string or NULL to save an empty file.
Returns
int SYSTEM_ERROR_NONE (0) on success or a system error code (non-zero)

◆ storeString() [2/2]

int FileHelperRK::storeString ( const char * fileName,
const String & data )
static

Store a String object to a file.

Parameters
fileNameFilename to write to. File will be created and truncated.
dataString object to write. It only needs to remain valid until this method returns.
Returns
int SYSTEM_ERROR_NONE (0) on success or a system error code (non-zero)

◆ storeStruct()

template<typename T >
static const T & FileHelperRK::storeStruct ( const char * fileName,
const T & t )
inlinestatic

Store a struct in a file.

Template Parameters
TThe struct/class to read
Parameters
fileNameFilename to read from
tThe variable to write
Returns
const T& returns t that was passed in

The struct must be flat; embedded objects including String are not serialized and will not be saved and restored properly.

◆ walk()

int FileHelperRK::walk ( const char * path,
std::function< void(const WalkParameters &walkParameters)> cb )
static

Walk the file system calling the callback function or lambda.

Parameters
pathPath to start checking (can be file or directory)
cbCallback function or lambda to call
Returns
int SYSTEM_ERROR_NONE (0) on success or a system error code (non-zero)

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