Class to perform common file operations.
More...
#include <FileHelperRK.h>
|
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 const char * | pathDelim = "/" |
| Path delimeter ("/")
|
|
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
◆ deleteRecursive()
int FileHelperRK::deleteRecursive |
( |
const char * | path, |
|
|
bool | contentsOfPathOnly = false ) |
|
static |
Delete a directory and all of the subdirectories and files.
- Parameters
-
path | The directory (or contents of the directory) to delete |
contentsOfPathOnly | If 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
-
- 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
-
a | Path component(s). Can be slash separated parts, a filename, an empty string, or NULL. |
b | Path 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
-
fileName | Filename to read from |
dataPtr | Filled in with an allocated pointer containing the data |
dataLen | On return, the length of the data in bytes |
nullTerminate | Set 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
-
fileName | Filename to read from |
dataPtr | Buffer filled in with up to dataLen bytes |
dataLen | On 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
-
fileName | Filename to read from |
result | String 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
-
T | The struct/class to read |
- Parameters
-
fileName | Filename to read from |
t | The 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
-
fileName | Filename to write to. File will be created and truncated. |
dataPtr | Pointer to binary data to write |
dataLen | Length 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
-
fileName | Filename to write to. File will be created and truncated. |
str | c-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
-
fileName | Filename to write to. File will be created and truncated. |
data | String 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
-
T | The struct/class to read |
- Parameters
-
fileName | Filename to read from |
t | The 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
-
path | Path to start checking (can be file or directory) |
cb | Callback 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: