From 69228f92dbddc9940166c0d7af2b4c79d55f85e7 Mon Sep 17 00:00:00 2001 From: Jonathan Gordon Date: Tue, 12 Feb 2013 20:35:11 +1100 Subject: simplelist: Make better use of the static buffer and simplify API Change-Id: I1327fcd01d6f817be6c7018d30d33446c9b57287 --- apps/gui/list.c | 30 ++++++++++++++++-------------- apps/gui/list.h | 6 ++---- 2 files changed, 18 insertions(+), 18 deletions(-) (limited to 'apps/gui') diff --git a/apps/gui/list.c b/apps/gui/list.c index 26b6ae30e6..cc43843e46 100644 --- a/apps/gui/list.c +++ b/apps/gui/list.c @@ -831,17 +831,20 @@ bool list_do_action(int context, int timeout, } /* Simple use list implementation */ -static int simplelist_line_count = 0; -static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH]; +static int simplelist_line_count = 0, simplelist_line_remaining; +static int simplelist_line_pos; +static char simplelist_buffer[SIMPLELIST_MAX_LINES * SIMPLELIST_MAX_LINELENGTH]; +static char *simplelist_text[SIMPLELIST_MAX_LINES]; /* set the amount of lines shown in the list */ void simplelist_set_line_count(int lines) { - if (lines < 0) + if (lines <= 0) { + simplelist_line_pos = 0; + simplelist_line_remaining = sizeof(simplelist_buffer); simplelist_line_count = 0; + } else if (lines >= SIMPLELIST_MAX_LINES) simplelist_line_count = SIMPLELIST_MAX_LINES; - else - simplelist_line_count = lines; } /* get the current amount of lines shown */ int simplelist_get_line_count(void) @@ -851,20 +854,19 @@ int simplelist_get_line_count(void) /* add/edit a line in the list. if line_number > number of lines shown it adds the line, else it edits the line */ -void simplelist_addline(int line_number, const char *fmt, ...) +void simplelist_addline(const char *fmt, ...) { va_list ap; + size_t len = simplelist_line_remaining; + int line_number = simplelist_line_count++; - if (line_number > simplelist_line_count) - { - if (simplelist_line_count < SIMPLELIST_MAX_LINES) - line_number = simplelist_line_count++; - else - return; - } + simplelist_text[line_number] = &simplelist_buffer[simplelist_line_pos]; va_start(ap, fmt); - vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap); + len = vsnprintf(simplelist_text[line_number], simplelist_line_remaining, fmt, ap); va_end(ap); + len++; + simplelist_line_remaining -= len; + simplelist_line_pos += len; } static const char* simplelist_static_getname(int item, diff --git a/apps/gui/list.h b/apps/gui/list.h index 162bb38e1a..8980573aa3 100644 --- a/apps/gui/list.h +++ b/apps/gui/list.h @@ -265,10 +265,8 @@ struct simplelist_info { void simplelist_set_line_count(int lines); /* get the current amount of lines shown */ int simplelist_get_line_count(void); -/* add/edit a line in the list. - if line_number > number of lines shown it adds the line, else it edits the line */ -#define SIMPLELIST_ADD_LINE (SIMPLELIST_MAX_LINES+1) -void simplelist_addline(int line_number, const char *fmt, ...); +/* add a line in the list. */ +void simplelist_addline(const char *fmt, ...); /* setup the info struct. members not setup in this function need to be assigned manually members set in this function: -- cgit v1.2.3