add llfs_get_filename_ext
This commit is contained in:
2023-12-02 23:01:06 +01:00
parent d0fb9c5856
commit 0cc34bfd6b
3 changed files with 38 additions and 0 deletions

View File

@@ -23,6 +23,7 @@ restricting operations solely to read functions.
- [Iterator function (not recommended)](#iterator-function-not-recommended) - [Iterator function (not recommended)](#iterator-function-not-recommended)
- [Reading a file](#reading-a-file) - [Reading a file](#reading-a-file)
- [Getting the number of files](#getting-the-number-of-files) - [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) - [Using the POSIX file functions](#using-the-posix-file-functions)
- [Enabling the external loader in STM32CubeIDE](#enabling-the-external-loader-in-stm32cubeide) - [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 ## Using the POSIX file functions
The llfs library also supports 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. As the file system is read-only, write functions are not implemented.

View File

@@ -83,4 +83,11 @@ llfs_file_t* llfs_next_file(void** mem, char* filter);
*/ */
size_t llfs_file_count(void); 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 #endif // LLFS_H

View File

@@ -141,6 +141,13 @@ size_t llfs_file_count(void) {
return file_count; 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 #ifndef TESTING
/** /**
* @brief Newlib open implementation * @brief Newlib open implementation