LLFS
add llfs_get_filename_ext
This commit is contained in:
24
docs/llfs.md
24
docs/llfs.md
@@ -23,6 +23,7 @@ restricting operations solely to read functions.
|
||||
- [Iterator function (not recommended)](#iterator-function-not-recommended)
|
||||
- [Reading a file](#reading-a-file)
|
||||
- [Getting the number of files](#getting-the-number-of-files)
|
||||
- [Getting a file extension](#getting-a-file-extension)
|
||||
- [Using the POSIX file functions](#using-the-posix-file-functions)
|
||||
- [Enabling the external loader in STM32CubeIDE](#enabling-the-external-loader-in-stm32cubeide)
|
||||
|
||||
@@ -144,6 +145,29 @@ void main(void) {
|
||||
}
|
||||
```
|
||||
|
||||
### Getting a file extension
|
||||
```c
|
||||
#include "llfs.h"
|
||||
void main(void) {
|
||||
llfs_init();
|
||||
|
||||
// Get a file by name
|
||||
llfs_file_t* file = llfs_file_open("filename with a space.txt");
|
||||
|
||||
if (file != NULL) {
|
||||
// Print the file name, size and data
|
||||
LOG_INFO(TAG, "File found: %s, size: %d", file->name, file->len);
|
||||
LOG_INFO(TAG, "File data: %s", file->data);
|
||||
|
||||
// Get the file extension
|
||||
const char* ext = llfs_file_ext(file->name);
|
||||
LOG_INFO(TAG, "File extension: %s", ext);
|
||||
} else {
|
||||
LOG_WARN(TAG, "File not found");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Using the POSIX file functions
|
||||
The llfs library also supports the POSIX file functions.
|
||||
As the file system is read-only, write functions are not implemented.
|
||||
|
||||
@@ -83,4 +83,11 @@ llfs_file_t* llfs_next_file(void** mem, char* filter);
|
||||
*/
|
||||
size_t llfs_file_count(void);
|
||||
|
||||
/**
|
||||
* @brief This function returns the extension of a file
|
||||
* @param[in] filename The filename to get the extension from
|
||||
* @return char* The extension of the file
|
||||
*/
|
||||
char* llfs_get_filename_ext(char* filename);
|
||||
|
||||
#endif // LLFS_H
|
||||
|
||||
@@ -141,6 +141,13 @@ size_t llfs_file_count(void) {
|
||||
return file_count;
|
||||
}
|
||||
|
||||
char* llfs_get_filename_ext(char* filename) {
|
||||
char* dot = strrchr(filename, '.');
|
||||
if (dot == NULL || dot == filename)
|
||||
return NULL;
|
||||
return dot + 1;
|
||||
}
|
||||
|
||||
#ifndef TESTING
|
||||
/**
|
||||
* @brief Newlib open implementation
|
||||
|
||||
Reference in New Issue
Block a user