From 0cc34bfd6b5f21a8c053af2893d729ad9195ed17 Mon Sep 17 00:00:00 2001 From: Sani7 Date: Sat, 2 Dec 2023 23:01:06 +0100 Subject: [PATCH] LLFS add llfs_get_filename_ext --- docs/llfs.md | 24 ++++++++++++++++++++++++ project/Core/Inc/llfs.h | 7 +++++++ project/Core/Src/llfs.c | 7 +++++++ 3 files changed, 38 insertions(+) diff --git a/docs/llfs.md b/docs/llfs.md index 38f4a7b..1311238 100644 --- a/docs/llfs.md +++ b/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. diff --git a/project/Core/Inc/llfs.h b/project/Core/Inc/llfs.h index 780a73b..4707873 100644 --- a/project/Core/Inc/llfs.h +++ b/project/Core/Inc/llfs.h @@ -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 diff --git a/project/Core/Src/llfs.c b/project/Core/Src/llfs.c index 7866be9..155c9bb 100644 --- a/project/Core/Src/llfs.c +++ b/project/Core/Src/llfs.c @@ -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