mirror of
https://github.com/lvgl/lvgl.git
synced 2026-06-02 17:47:28 +08:00
feat(str): add lv_strdup (#4498)
Signed-off-by: Xu Xingliang <xuxingliang@xiaomi.com>
This commit is contained in:
@@ -85,8 +85,7 @@ LV_ATTRIBUTE_FAST_MEM void lv_draw_label(lv_layer_t * layer, const lv_draw_label
|
||||
/*The text is stored in a local variable so malloc memory for it*/
|
||||
if(dsc->text_local) {
|
||||
lv_draw_label_dsc_t * new_dsc = t->draw_dsc;
|
||||
new_dsc->text = lv_malloc(lv_strlen(new_dsc->text) + 1);
|
||||
lv_strcpy((char *)new_dsc->text, dsc->text);
|
||||
new_dsc->text = lv_strdup(dsc->text);
|
||||
}
|
||||
|
||||
lv_draw_finalize_task_creation(layer, t);
|
||||
|
||||
@@ -118,15 +118,8 @@ void lv_file_explorer_set_quick_access_path(lv_obj_t * obj, lv_file_explorer_dir
|
||||
*dir_str = NULL;
|
||||
}
|
||||
|
||||
/*Get the size of the text*/
|
||||
size_t len = lv_strlen(path) + 1;
|
||||
|
||||
/*Allocate space for the new text*/
|
||||
*dir_str = lv_malloc(len);
|
||||
LV_ASSERT_MALLOC(*dir_str);
|
||||
if(*dir_str == NULL) return;
|
||||
|
||||
lv_strcpy(*dir_str, path);
|
||||
*dir_str = lv_strdup(path);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "../../misc/lv_log.h"
|
||||
#include "../../misc/lv_math.h"
|
||||
#include "../../stdlib/lv_string.h"
|
||||
#include "../../stdlib/lv_mem.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
@@ -170,6 +171,16 @@ char * lv_strcpy(char * dst, const char * src)
|
||||
return tmp;
|
||||
}
|
||||
|
||||
char * lv_strdup(const char * src)
|
||||
{
|
||||
size_t len = lv_strlen(src) + 1;
|
||||
char * dst = lv_malloc(len);
|
||||
if(dst == NULL) return NULL;
|
||||
|
||||
lv_memcpy(dst, src, len); /*do memcpy is faster than strncpy when length is known*/
|
||||
return dst;
|
||||
}
|
||||
|
||||
/**********************
|
||||
* STATIC FUNCTIONS
|
||||
**********************/
|
||||
|
||||
@@ -64,6 +64,11 @@ char * lv_strcpy(char * dst, const char * src)
|
||||
return strcpy(dst, src);
|
||||
}
|
||||
|
||||
char * lv_strdup(const char * src)
|
||||
{
|
||||
return strdup(src);
|
||||
}
|
||||
|
||||
/**********************
|
||||
* STATIC FUNCTIONS
|
||||
**********************/
|
||||
|
||||
@@ -83,6 +83,12 @@ char * lv_strncpy(char * dst, const char * src, size_t dest_size);
|
||||
*/
|
||||
char * lv_strcpy(char * dst, const char * src);
|
||||
|
||||
/**
|
||||
* @brief Duplicate a string by allocating a new one and copying the content.
|
||||
* @param src Pointer to the source of data to be copied.
|
||||
* @return A pointer to the new allocated string. NULL if failed.
|
||||
*/
|
||||
char * lv_strdup(const char * src);
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
|
||||
@@ -198,13 +198,9 @@ void lv_dropdown_add_option(lv_obj_t * obj, const char * option, uint32_t pos)
|
||||
/*Convert static options to dynamic*/
|
||||
if(dropdown->static_txt != 0) {
|
||||
char * static_options = dropdown->options;
|
||||
size_t len = lv_strlen(static_options) + 1;
|
||||
|
||||
dropdown->options = lv_malloc(len);
|
||||
dropdown->options = lv_strdup(static_options);
|
||||
LV_ASSERT_MALLOC(dropdown->options);
|
||||
if(dropdown->options == NULL) return;
|
||||
|
||||
lv_strcpy(dropdown->options, static_options);
|
||||
dropdown->static_txt = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -118,10 +118,9 @@ void lv_img_set_src(lv_obj_t * obj, const void * src)
|
||||
if(img->src_type == LV_IMG_SRC_FILE || img->src_type == LV_IMG_SRC_SYMBOL) {
|
||||
old_src = img->src;
|
||||
}
|
||||
char * new_str = lv_malloc(lv_strlen(src) + 1);
|
||||
char * new_str = lv_strdup(src);
|
||||
LV_ASSERT_MALLOC(new_str);
|
||||
if(new_str == NULL) return;
|
||||
lv_strcpy(new_str, src);
|
||||
img->src = new_str;
|
||||
|
||||
if(old_src) lv_free((void *)old_src);
|
||||
|
||||
@@ -393,15 +393,12 @@ void lv_menu_set_page_title(lv_obj_t * page_obj, char const * const title)
|
||||
}
|
||||
|
||||
if(title) {
|
||||
size_t len = lv_strlen(title) + 1;
|
||||
page->title = lv_malloc(len);
|
||||
page->static_title = false;
|
||||
|
||||
page->title = lv_strdup(title);
|
||||
LV_ASSERT_MALLOC(page->title);
|
||||
if(page->title == NULL) {
|
||||
return;
|
||||
}
|
||||
lv_strcpy(page->title, title);
|
||||
}
|
||||
else {
|
||||
page->title = NULL;
|
||||
|
||||
@@ -178,7 +178,7 @@ void lv_span_set_text(lv_span_t * span, const char * text)
|
||||
if(span->txt == NULL) return;
|
||||
|
||||
span->static_flag = 0;
|
||||
lv_strcpy(span->txt, text);
|
||||
lv_memcpy(span->txt, text, text_alloc_len);
|
||||
|
||||
refresh_self_size(span->spangroup);
|
||||
}
|
||||
|
||||
@@ -89,10 +89,8 @@ lv_obj_t * lv_tabview_add_tab(lv_obj_t * obj, const char * name)
|
||||
if(tabview->tab_pos & LV_DIR_VER) {
|
||||
new_map = lv_malloc((tab_id + 1) * sizeof(const char *));
|
||||
lv_memcpy(new_map, old_map, sizeof(const char *) * (tab_id - 1));
|
||||
size_t len = lv_strlen(name) + 1;
|
||||
new_map[tab_id - 1] = lv_malloc(len);
|
||||
new_map[tab_id - 1] = lv_strdup(name);
|
||||
LV_ASSERT_MALLOC(new_map[tab_id - 1]);
|
||||
lv_strcpy((char *)new_map[tab_id - 1], name);
|
||||
new_map[tab_id] = (char *)"";
|
||||
}
|
||||
/*left or right dir*/
|
||||
@@ -100,18 +98,14 @@ lv_obj_t * lv_tabview_add_tab(lv_obj_t * obj, const char * name)
|
||||
new_map = lv_malloc((tab_id * 2) * sizeof(const char *));
|
||||
lv_memcpy(new_map, old_map, sizeof(const char *) * (tab_id - 1) * 2);
|
||||
if(tabview->tab_cnt == 0) {
|
||||
size_t len = lv_strlen(name) + 1;
|
||||
new_map[0] = lv_malloc(len);
|
||||
new_map[0] = lv_strdup(name);
|
||||
LV_ASSERT_MALLOC(new_map[0]);
|
||||
lv_strcpy((char *)new_map[0], name);
|
||||
new_map[1] = (char *)"";
|
||||
}
|
||||
else {
|
||||
size_t len = lv_strlen(name) + 1;
|
||||
new_map[tab_id * 2 - 3] = (char *)"\n";
|
||||
new_map[tab_id * 2 - 2] = lv_malloc(len);
|
||||
new_map[tab_id * 2 - 2] = lv_strdup(name);
|
||||
new_map[tab_id * 2 - 1] = (char *)"";
|
||||
lv_strcpy((char *)new_map[(tab_id * 2) - 2], name);
|
||||
}
|
||||
}
|
||||
tabview->map = new_map;
|
||||
@@ -140,10 +134,8 @@ void lv_tabview_rename_tab(lv_obj_t * obj, uint32_t id, const char * new_name)
|
||||
if(tabview->tab_pos & LV_DIR_HOR) id *= 2;
|
||||
|
||||
lv_free(tabview->map[id]);
|
||||
size_t len = lv_strlen(new_name) + 1;
|
||||
tabview->map[id] = lv_malloc(len);
|
||||
tabview->map[id] = lv_strdup(new_name);
|
||||
LV_ASSERT_MALLOC(tabview->map[id]);
|
||||
lv_strcpy(tabview->map[id], new_name);
|
||||
lv_obj_invalidate(obj);
|
||||
}
|
||||
|
||||
|
||||
@@ -301,11 +301,9 @@ void lv_textarea_set_text(lv_obj_t * obj, const char * txt)
|
||||
}
|
||||
|
||||
if(ta->pwd_mode) {
|
||||
size_t len = lv_strlen(txt) + 1;
|
||||
ta->pwd_tmp = lv_realloc(ta->pwd_tmp, len);
|
||||
ta->pwd_tmp = lv_strdup(txt);
|
||||
LV_ASSERT_MALLOC(ta->pwd_tmp);
|
||||
if(ta->pwd_tmp == NULL) return;
|
||||
lv_strcpy(ta->pwd_tmp, txt);
|
||||
|
||||
/*Auto hide characters*/
|
||||
auto_hide_characters(obj);
|
||||
@@ -414,14 +412,10 @@ void lv_textarea_set_password_mode(lv_obj_t * obj, bool en)
|
||||
/*Pwd mode is now enabled*/
|
||||
if(en) {
|
||||
char * txt = lv_label_get_text(ta->label);
|
||||
size_t len = lv_strlen(txt);
|
||||
|
||||
ta->pwd_tmp = lv_malloc(len + 1);
|
||||
ta->pwd_tmp = lv_strdup(txt);
|
||||
LV_ASSERT_MALLOC(ta->pwd_tmp);
|
||||
if(ta->pwd_tmp == NULL) return;
|
||||
|
||||
lv_strcpy(ta->pwd_tmp, txt);
|
||||
|
||||
pwd_char_hider(obj);
|
||||
|
||||
lv_textarea_clear_selection(obj);
|
||||
@@ -460,7 +454,7 @@ void lv_textarea_set_password_bullet(lv_obj_t * obj, const char * bullet)
|
||||
return;
|
||||
}
|
||||
|
||||
lv_strcpy(ta->pwd_bullet, bullet);
|
||||
lv_memcpy(ta->pwd_bullet, bullet, txt_len);
|
||||
ta->pwd_bullet[txt_len] = '\0';
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user