settings: Rewrite cfg_int_to_string() and cfg_string_to_int()
It's easier to do the parsing manually. Change-Id: Ief8b71942d3ab9313dd6927bd7f4bb254d0c12db
This commit is contained in:
parent
fc18235323
commit
90d1ac0448
1 changed files with 42 additions and 59 deletions
101
apps/settings.c
101
apps/settings.c
|
@ -240,32 +240,34 @@ void settings_load(int which)
|
||||||
|
|
||||||
bool cfg_string_to_int(const struct settings_list *setting, int* out, const char* str)
|
bool cfg_string_to_int(const struct settings_list *setting, int* out, const char* str)
|
||||||
{
|
{
|
||||||
const char* start = setting->cfg_vals;
|
const char* ptr = setting->cfg_vals;
|
||||||
char* end = NULL;
|
size_t len = strlen(str);
|
||||||
char temp[MAX_PATH];
|
int index = 0;
|
||||||
int count = 0;
|
|
||||||
while (1)
|
while (true)
|
||||||
{
|
{
|
||||||
end = strchr(start, ',');
|
if (!strncmp(ptr, str, len))
|
||||||
if (!end)
|
|
||||||
{
|
{
|
||||||
if (!strcmp(str, start))
|
ptr += len;
|
||||||
|
/* if the next character is not a comma or end of string,
|
||||||
|
* it means the comparison was only a partial match. */
|
||||||
|
if (*ptr == ',' || *ptr == '\0')
|
||||||
{
|
{
|
||||||
*out = count;
|
*out = index;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else return false;
|
|
||||||
}
|
}
|
||||||
strmemccpy(temp, start, end-start+1);
|
|
||||||
if (!strcmp(str, temp))
|
while (*ptr != ',')
|
||||||
{
|
{
|
||||||
*out = count;
|
if (!*ptr)
|
||||||
return true;
|
return false;
|
||||||
|
ptr++;
|
||||||
}
|
}
|
||||||
start = end +1;
|
|
||||||
count++;
|
ptr++;
|
||||||
|
index++;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -409,55 +411,36 @@ bool settings_load_config(const char* file, bool apply)
|
||||||
|
|
||||||
bool cfg_int_to_string(const struct settings_list *setting, int val, char* buf, int buf_len)
|
bool cfg_int_to_string(const struct settings_list *setting, int val, char* buf, int buf_len)
|
||||||
{
|
{
|
||||||
const char* start = setting->cfg_vals;
|
const char* ptr = setting->cfg_vals;
|
||||||
char* end = NULL;
|
const int *values = NULL;
|
||||||
int count = 0;
|
int index = 0;
|
||||||
|
|
||||||
if ((setting->flags & F_T_MASK) == F_T_INT &&
|
if (setting->flags & F_TABLE_SETTING)
|
||||||
(setting->flags & F_TABLE_SETTING))
|
values = setting->table_setting->values;
|
||||||
|
|
||||||
|
while (true)
|
||||||
{
|
{
|
||||||
const int *value = setting->table_setting->values;
|
if ((values && values[index] == val) ||
|
||||||
while (start)
|
(!values && index == val))
|
||||||
{
|
{
|
||||||
end = strchr(start,',');
|
char *buf_end = buf + buf_len - 1;
|
||||||
if (value[count] == val)
|
while (*ptr && *ptr != ',' && buf != buf_end)
|
||||||
{
|
*buf++ = *ptr++;
|
||||||
if (end == NULL)
|
|
||||||
strmemccpy(buf, start, buf_len);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int len = MIN(buf_len, (end-start) + 1);
|
|
||||||
strmemccpy(buf, start, len);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
count++;
|
|
||||||
|
|
||||||
if (end)
|
*buf++ = '\0';
|
||||||
start = end+1;
|
return true;
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (count < val)
|
while (*ptr != ',')
|
||||||
{
|
{
|
||||||
start = strchr(start,',');
|
if (!*ptr)
|
||||||
if (!start)
|
return false;
|
||||||
return false;
|
ptr++;
|
||||||
count++;
|
}
|
||||||
start++;
|
|
||||||
|
ptr++;
|
||||||
|
index++;
|
||||||
}
|
}
|
||||||
end = strchr(start,',');
|
|
||||||
if (end == NULL)
|
|
||||||
strmemccpy(buf, start, buf_len);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int len = MIN(buf_len, (end-start) + 1);
|
|
||||||
strmemccpy(buf, start, len);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void cfg_to_string(const struct settings_list *setting, char* buf, int buf_len)
|
void cfg_to_string(const struct settings_list *setting, char* buf, int buf_len)
|
||||||
|
|
Loading…
Reference in a new issue