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:
Aidan MacDonald 2022-11-30 14:09:53 +00:00
parent fc18235323
commit 90d1ac0448

View file

@ -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;
}
start = end +1;
count++;
}
return false; return false;
ptr++;
}
ptr++;
index++;
}
} }
/** /**
@ -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); *buf++ = '\0';
else
{
int len = MIN(buf_len, (end-start) + 1);
strmemccpy(buf, start, len);
}
return true; return true;
} }
count++;
if (end) while (*ptr != ',')
start = end+1; {
else if (!*ptr)
break;
}
return false; return false;
ptr++;
} }
while (count < val) ptr++;
{ index++;
start = strchr(start,',');
if (!start)
return false;
count++;
start++;
} }
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)