From ba42133458d82a6d9f83105b78f8337069b3ac91 Mon Sep 17 00:00:00 2001 From: liamHowatt <30486941+liamHowatt@users.noreply.github.com> Date: Mon, 25 Mar 2024 02:37:51 -0400 Subject: [PATCH] feat(stdlib): add lv_strncat and refactor strcat uses (#5927) --- .../file_explorer/lv_example_file_explorer_1.c | 13 +------------ .../file_explorer/lv_example_file_explorer_2.c | 12 +----------- .../file_explorer/lv_example_file_explorer_3.c | 12 +----------- src/stdlib/builtin/lv_string_builtin.c | 14 ++++++++++++++ src/stdlib/clib/lv_string_clib.c | 5 +++++ src/stdlib/lv_string.h | 12 ++++++++++++ src/stdlib/rtthread/lv_string_rtthread.c | 12 ++++++++++++ 7 files changed, 46 insertions(+), 34 deletions(-) diff --git a/examples/others/file_explorer/lv_example_file_explorer_1.c b/examples/others/file_explorer/lv_example_file_explorer_1.c index ea92b030f..9145cd361 100644 --- a/examples/others/file_explorer/lv_example_file_explorer_1.c +++ b/examples/others/file_explorer/lv_example_file_explorer_1.c @@ -14,18 +14,7 @@ static void file_explorer_event_handler(lv_event_t * e) if(code == LV_EVENT_VALUE_CHANGED) { const char * cur_path = lv_file_explorer_get_current_path(obj); const char * sel_fn = lv_file_explorer_get_selected_file_name(obj); - uint16_t path_len = strlen(cur_path); - uint16_t fn_len = strlen(sel_fn); - - if((path_len + fn_len) <= LV_FILE_EXPLORER_PATH_MAX_LEN) { - char file_info[LV_FILE_EXPLORER_PATH_MAX_LEN]; - - strcpy(file_info, cur_path); - strcat(file_info, sel_fn); - - LV_LOG_USER("%s", file_info); - } - else LV_LOG_USER("%s%s", cur_path, sel_fn); + LV_LOG_USER("%s%s", cur_path, sel_fn); } } diff --git a/examples/others/file_explorer/lv_example_file_explorer_2.c b/examples/others/file_explorer/lv_example_file_explorer_2.c index 0bcd01716..082635a6d 100644 --- a/examples/others/file_explorer/lv_example_file_explorer_2.c +++ b/examples/others/file_explorer/lv_example_file_explorer_2.c @@ -14,18 +14,8 @@ static void file_explorer_event_handler(lv_event_t * e) if(code == LV_EVENT_VALUE_CHANGED) { const char * cur_path = lv_file_explorer_get_current_path(obj); const char * sel_fn = lv_file_explorer_get_selected_file_name(obj); - uint16_t path_len = strlen(cur_path); - uint16_t fn_len = strlen(sel_fn); - if((path_len + fn_len) <= LV_FILE_EXPLORER_PATH_MAX_LEN) { - char file_info[LV_FILE_EXPLORER_PATH_MAX_LEN]; - - strcpy(file_info, cur_path); - strcat(file_info, sel_fn); - - LV_LOG_USER("%s", file_info); - } - else LV_LOG_USER("%s%s", cur_path, sel_fn); + LV_LOG_USER("%s%s", cur_path, sel_fn); } } diff --git a/examples/others/file_explorer/lv_example_file_explorer_3.c b/examples/others/file_explorer/lv_example_file_explorer_3.c index b475d8a74..7dea32c04 100644 --- a/examples/others/file_explorer/lv_example_file_explorer_3.c +++ b/examples/others/file_explorer/lv_example_file_explorer_3.c @@ -50,18 +50,8 @@ static void file_explorer_event_handler(lv_event_t * e) if(code == LV_EVENT_VALUE_CHANGED) { const char * cur_path = lv_file_explorer_get_current_path(obj); const char * sel_fn = lv_file_explorer_get_selected_file_name(obj); - uint16_t path_len = strlen(cur_path); - uint16_t fn_len = strlen(sel_fn); - if((path_len + fn_len) <= LV_FILE_EXPLORER_PATH_MAX_LEN) { - char file_info[LV_FILE_EXPLORER_PATH_MAX_LEN]; - - strcpy(file_info, cur_path); - strcat(file_info, sel_fn); - - LV_LOG_USER("%s", file_info); - } - else LV_LOG_USER("%s%s", cur_path, sel_fn); + LV_LOG_USER("%s%s", cur_path, sel_fn); } else if(code == LV_EVENT_READY) { lv_obj_t * tb = lv_file_explorer_get_file_table(obj); diff --git a/src/stdlib/builtin/lv_string_builtin.c b/src/stdlib/builtin/lv_string_builtin.c index 5cb4f987c..62742a39f 100644 --- a/src/stdlib/builtin/lv_string_builtin.c +++ b/src/stdlib/builtin/lv_string_builtin.c @@ -237,6 +237,20 @@ char * lv_strcat(char * dst, const char * src) return tmp; } +char * lv_strncat(char * dst, const char * src, size_t src_len) +{ + char * tmp = dst; + while(*dst != '\0') { + dst++; + } + while(src_len != 0 && *src != '\0') { + src_len--; + *dst++ = *src++; + } + *dst = '\0'; + return tmp; +} + /********************** * STATIC FUNCTIONS **********************/ diff --git a/src/stdlib/clib/lv_string_clib.c b/src/stdlib/clib/lv_string_clib.c index d003b6b7a..bec286e7a 100644 --- a/src/stdlib/clib/lv_string_clib.c +++ b/src/stdlib/clib/lv_string_clib.c @@ -96,6 +96,11 @@ char * lv_strcat(char * dst, const char * src) return strcat(dst, src); } +char * lv_strncat(char * dst, const char * src, size_t src_len) +{ + return strncat(dst, src, src_len); +} + /********************** * STATIC FUNCTIONS **********************/ diff --git a/src/stdlib/lv_string.h b/src/stdlib/lv_string.h index e8d4b45fd..a7dcc7c30 100644 --- a/src/stdlib/lv_string.h +++ b/src/stdlib/lv_string.h @@ -124,6 +124,18 @@ char * lv_strdup(const char * src); */ char * lv_strcat(char * dst, const char * src); +/** + * @brief Copies up to src_len characters from the string pointed to by src + * to the end of the string pointed to by dst. + * A terminating null character is appended to dst even if no null character + * was encountered in src after src_len characters were copied. + * @param dst Pointer to the destination string where the content is to be appended. + * @param src Pointer to the source of data to be copied. + * @param src_len Maximum number of characters from src to be copied to the end of dst. + * @return A pointer to the destination string, which is dst. + */ +char * lv_strncat(char * dst, const char * src, size_t src_len); + /********************** * MACROS **********************/ diff --git a/src/stdlib/rtthread/lv_string_rtthread.c b/src/stdlib/rtthread/lv_string_rtthread.c index 14f4b4f74..68ea8d9bc 100644 --- a/src/stdlib/rtthread/lv_string_rtthread.c +++ b/src/stdlib/rtthread/lv_string_rtthread.c @@ -90,6 +90,18 @@ char * lv_strcat(char * dst, const char * src) return strcat(dst, src); } +char * lv_strncat(char * dst, const char * src, size_t src_len) +{ + char * tmp = dst; + dst += lv_strlen(dst); + while(src_len != 0 && *src != '\0') { + src_len--; + *dst++ = *src++; + } + *dst = '\0'; + return tmp; +} + /********************** * STATIC FUNCTIONS **********************/