add function string_option to misc.c use in skin_parser.c

function string_option allows a string to be found in a
supplied list of options

Change-Id: If9134090406b74ab11f4ef9ed6517a4b99b9d73e
This commit is contained in:
William Wilgus 2022-03-13 03:35:18 -04:00
parent a9c62f2c39
commit 74df3ba2d8
3 changed files with 97 additions and 29 deletions

View file

@ -601,11 +601,16 @@ static int parse_viewporttextstyle(struct skin_element *element,
*line = (struct line_desc)LINE_DESC_DEFINIT; *line = (struct line_desc)LINE_DESC_DEFINIT;
unsigned colour; unsigned colour;
if (!strcmp(mode, "invert")) const char *vp_options[] = { "invert", "color", "colour",
"clear", "gradient", NULL};
int vp_op = string_option(mode, vp_options, false);
if (vp_op == 0) /*invert*/
{ {
line->style = STYLE_INVERT; line->style = STYLE_INVERT;
} }
else if (!strcmp(mode, "colour") || !strcmp(mode, "color")) else if (vp_op == 1 || vp_op == 2) /*color/colour*/
{ {
if (element->params_count < 2 || if (element->params_count < 2 ||
!parse_color(curr_screen, get_param_text(element, 1), &colour)) !parse_color(curr_screen, get_param_text(element, 1), &colour))
@ -614,7 +619,7 @@ static int parse_viewporttextstyle(struct skin_element *element,
line->text_color = colour; line->text_color = colour;
} }
#ifdef HAVE_LCD_COLOR #ifdef HAVE_LCD_COLOR
else if (!strcmp(mode, "gradient")) else if (vp_op == 4) /*gradient*/
{ {
int num_lines; int num_lines;
if (element->params_count < 2) if (element->params_count < 2)
@ -627,7 +632,7 @@ static int parse_viewporttextstyle(struct skin_element *element,
line->nlines = num_lines; line->nlines = num_lines;
} }
#endif #endif
else if (!strcmp(mode, "clear")) else if (vp_op == 3) /*clear*/
{ {
line->style = STYLE_DEFAULT; line->style = STYLE_DEFAULT;
} }
@ -1042,20 +1047,38 @@ static int parse_progressbar_tag(struct skin_element* element,
} }
pb->horizontal = pb->width > pb->height; pb->horizontal = pb->width > pb->height;
enum
{
eINVERT = 0, eNOFILL, eNOBORDER, eNOBAR, eSLIDER, eIMAGE,
eBACKDROP, eVERTICAL, eHORIZONTAL, eNOTOUCH, eSETTING
};
const char *pb_options[] = {"invert", "nofill", "noborder, nobar", "slider",
"image", "backdrop", "vertical", "horizontal",
"notouch", "setting", NULL};
int pb_op;
while (curr_param < element->params_count) while (curr_param < element->params_count)
{ {
char* text; char* text;
param++; param++;
text = SKINOFFSETTOPTR(skin_buffer, param->data.text); text = SKINOFFSETTOPTR(skin_buffer, param->data.text);
if (!strcmp(text, "invert"))
pb_op = string_option(text, pb_options, false);
if (pb_op == eINVERT)
pb->invert_fill_direction = true; pb->invert_fill_direction = true;
else if (!strcmp(text, "nofill")) else if (pb_op == eNOFILL)
pb->nofill = true; pb->nofill = true;
else if (!strcmp(text, "noborder")) else if (pb_op == eNOBORDER)
pb->noborder = true; pb->noborder = true;
else if (!strcmp(text, "nobar")) else if (pb_op == eNOBAR)
pb->nobar = true; pb->nobar = true;
else if (!strcmp(text, "slider")) else if (pb_op == eSLIDER)
{ {
if (curr_param+1 < element->params_count) if (curr_param+1 < element->params_count)
{ {
@ -1068,7 +1091,7 @@ static int parse_progressbar_tag(struct skin_element* element,
else /* option needs the next param */ else /* option needs the next param */
return -1; return -1;
} }
else if (!strcmp(text, "image")) else if (pb_op == eIMAGE)
{ {
if (curr_param+1 < element->params_count) if (curr_param+1 < element->params_count)
{ {
@ -1079,7 +1102,7 @@ static int parse_progressbar_tag(struct skin_element* element,
else /* option needs the next param */ else /* option needs the next param */
return -1; return -1;
} }
else if (!strcmp(text, "backdrop")) else if (pb_op == eBACKDROP)
{ {
if (curr_param+1 < element->params_count) if (curr_param+1 < element->params_count)
{ {
@ -1093,31 +1116,31 @@ static int parse_progressbar_tag(struct skin_element* element,
else /* option needs the next param */ else /* option needs the next param */
return -1; return -1;
} }
else if (!strcmp(text, "vertical")) else if (pb_op == eVERTICAL)
{ {
pb->horizontal = false; pb->horizontal = false;
if (isdefault(get_param(element, 3))) if (isdefault(get_param(element, 3)))
pb->height = vp->height - pb->y; pb->height = vp->height - pb->y;
} }
else if (!strcmp(text, "horizontal")) else if (pb_op == eHORIZONTAL)
pb->horizontal = true; pb->horizontal = true;
#ifdef HAVE_TOUCHSCREEN #ifdef HAVE_TOUCHSCREEN
else if (!strcmp(text, "notouch")) else if (pb_op == eNOTOUCH)
suppress_touchregion = true; suppress_touchregion = true;
#endif #endif
else if (token->type == SKIN_TOKEN_SETTING && !strcmp(text, "setting")) else if (token->type == SKIN_TOKEN_SETTING && pb_op == eSETTING)
{ {
if (curr_param+1 < element->params_count) if (curr_param+1 < element->params_count)
{ {
curr_param++; curr_param++;
param++; param++;
text = SKINOFFSETTOPTR(skin_buffer, param->data.text); text = SKINOFFSETTOPTR(skin_buffer, param->data.text);
#ifndef __PCTOOL__ #ifndef __PCTOOL__
if (find_setting_by_cfgname(text, &pb->setting_id) == NULL) if (find_setting_by_cfgname(text, &pb->setting_id) == NULL)
return WPS_ERROR_INVALID_PARAM; return WPS_ERROR_INVALID_PARAM;
#endif #endif
} }
} }
else if (curr_param == 4) else if (curr_param == 4)
image_filename = text; image_filename = text;
@ -1379,28 +1402,32 @@ static int parse_skinvar( struct skin_element *element,
return 0; return 0;
case SKIN_TOKEN_VAR_SET: case SKIN_TOKEN_VAR_SET:
{ {
const char *sv_options[] = {"touch", "set", "inc", "dec", NULL};
struct skin_var_changer *data = skin_buffer_alloc(sizeof(*data)); struct skin_var_changer *data = skin_buffer_alloc(sizeof(*data));
if (!data) if (!data)
return WPS_ERROR_INVALID_PARAM; return WPS_ERROR_INVALID_PARAM;
data->var = PTRTOSKINOFFSET(skin_buffer, var); data->var = PTRTOSKINOFFSET(skin_buffer, var);
char *text_param1 = get_param_text(element, 1); char *text_param1 = get_param_text(element, 1);
int sv_op = string_option(text_param1, sv_options, false);
if (!isdefault(get_param(element, 2))) if (!isdefault(get_param(element, 2)))
data->newval = get_param(element, 2)->data.number; data->newval = get_param(element, 2)->data.number;
else if (strcmp(text_param1, "touch")) else if (sv_op == 0) /*touch*/
return WPS_ERROR_INVALID_PARAM; return WPS_ERROR_INVALID_PARAM;
data->max = 0; data->max = 0;
if (!strcmp(text_param1, "set")) if ((sv_op == 1) /*set*/
data->direct = true; data->direct = true;
else if (!strcmp(text_param1, "inc")) else if (sv_op == 2) /*inc*/
{ {
data->direct = false; data->direct = false;
} }
else if (!strcmp(text_param1, "dec")) (sv_op == 3) /*dec*/
{ {
data->direct = false; data->direct = false;
data->newval *= -1; data->newval *= -1;
} }
else if (!strcmp(text_param1, "touch")) else if (sv_op == 0) /*touch*/
{ {
data->direct = false; data->direct = false;
data->newval = 0; data->newval = 0;
@ -1679,16 +1706,21 @@ static int parse_touchregion(struct skin_element *element,
if (region->action == ACTION_NONE) if (region->action == ACTION_NONE)
return WPS_ERROR_INVALID_PARAM; return WPS_ERROR_INVALID_PARAM;
} }
const char *pm_options[] = {"allow_while_locked", "reverse_bar",
"repeat_press", "long_press", NULL};
int pm_op;
while (p < element->params_count) while (p < element->params_count)
{ {
char* param = get_param_text(element, p++); char* param = get_param_text(element, p++);
if (!strcmp(param, "allow_while_locked")) pm_op = string_option(param, pm_options, false);
if (pm_op == 0)
region->allow_while_locked = true; region->allow_while_locked = true;
else if (!strcmp(param, "reverse_bar")) else if (pm_op == 1)
region->reverse_bar = true; region->reverse_bar = true;
else if (!strcmp(param, "repeat_press")) else if (pm_op == 2)
region->press_length = REPEAT; region->press_length = REPEAT;
else if (!strcmp(param, "long_press")) else if (pm_op == 3)
region->press_length = LONG_PRESS; region->press_length = LONG_PRESS;
} }
struct skin_token_list *item = new_skin_token_list_item(NULL, region); struct skin_token_list *item = new_skin_token_list_item(NULL, region);

View file

@ -1384,6 +1384,41 @@ int split_string(char *str, const char split_char, char *vector[], const int vec
return i; return i;
} }
/* returns match index from option list
* returns -1 if option was not found
* option list is array of char pointers with the final item set to null
* ex - const char *option[] = { "op_a", "op_b", "op_c", NULL}
*/
int string_option(const char *option, const char *const oplist[], bool ignore_case)
{
int i;
int ifound = -1;
const char *op;
if (ignore_case)
{
for (i=0; (op=oplist[i]) != NULL; i++)
{
if (strcasecmp(op, option) == 0)
{
ifound = i;
break;
}
}
}
else
{
for (i=0; (op=oplist[i]) != NULL; i++)
{
if (strcmp(op, option) == 0)
{
ifound = i;
break;
}
}
}
return ifound;
}
/** Open a UTF-8 file and set file descriptor to first byte after BOM. /** Open a UTF-8 file and set file descriptor to first byte after BOM.
* If no BOM is present this behaves like open(). * If no BOM is present this behaves like open().

View file

@ -123,6 +123,7 @@ extern int show_logo(void);
int split_string(char *str, const char needle, char *vector[], int vector_length); int split_string(char *str, const char needle, char *vector[], int vector_length);
int open_utf8(const char* pathname, int flags); int open_utf8(const char* pathname, int flags);
int string_option(const char *option, const char *const oplist[], bool ignore_case);
#ifdef BOOTFILE #ifdef BOOTFILE
#if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) \ #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) \