fix bool settings which don't use "off,on" for their config values. Neatens up the read/write code a bit also

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@12141 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Jonathan Gordon 2007-01-29 12:32:56 +00:00
parent 12f3769adb
commit bc60af1daf

View file

@ -259,6 +259,29 @@ static int hex_to_rgb(const char* hex)
return 0; return 0;
} }
#endif #endif
static bool cfg_int_to_string(int setting_id, int val, char* buf)
{
const char* start = settings[setting_id].cfg_vals;
char* end = NULL;
int count = 0;
while (count < val)
{
start = strchr(start,',');
if (!start)
return false;
count++;
start++;
}
end = strchr(start,',');
if (end == NULL)
strcpy(buf,start);
else
{
strncpy(buf, start, end-start);
buf[end-start] = '\0';
}
return true;
}
bool settings_write_config(char* filename) bool settings_write_config(char* filename)
{ {
@ -274,6 +297,7 @@ bool settings_write_config(char* filename)
{ {
if (settings[i].cfg_name == NULL) if (settings[i].cfg_name == NULL)
continue; continue;
value[0] = '\0';
switch (settings[i].flags&F_T_MASK) switch (settings[i].flags&F_T_MASK)
{ {
case F_T_INT: case F_T_INT:
@ -295,36 +319,12 @@ bool settings_write_config(char* filename)
} }
else else
{ {
const char *s; cfg_int_to_string(i,*(int*)settings[i].setting,value);
const char *end;
int val = 0;
end = s = settings[i].cfg_vals;
do
{
while (*end != 0 && *end != ',')
{
end++;
}
if (val == *(int*)settings[i].setting)
{
strncpy(value, s, end - s);
value[end - s] = 0;
break;
}
else
{
s = end + 1;
val++;
}
}
while (*end++);
} }
break; break;
case F_T_BOOL: case F_T_BOOL:
strcpy(value,*(bool*)settings[i].setting == true?"on":"off"); cfg_int_to_string(i,
*(bool*)settings[i].setting==false?0:1,value);
break; break;
case F_T_CHARPTR: case F_T_CHARPTR:
case F_T_UCHARPTR: case F_T_UCHARPTR:
@ -343,7 +343,6 @@ bool settings_write_config(char* filename)
} /* switch () */ } /* switch () */
if (value[0]) if (value[0])
fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value); fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
value[0] = '\0';
} /* for(...) */ } /* for(...) */
close(fd); close(fd);
return true; return true;
@ -717,6 +716,36 @@ void set_file(char* filename, char* setting, int maxlen)
settings_save(); settings_save();
} }
static bool cfg_string_to_int(int setting_id, int* out, char* str)
{
const char* start = settings[setting_id].cfg_vals;
char* end = NULL;
char temp[MAX_PATH];
int count = 0;
while (1)
{
end = strchr(start, ',');
if (!end)
{
if (!strcmp(str, start))
{
*out = count;
return true;
}
else return false;
}
strncpy(temp, start, end-start);
temp[end-start] = '\0';
if (!strcmp(str, temp))
{
*out = count;
return true;
}
start = end +1;
count++;
}
return false;
}
bool settings_load_config(const char* file, bool apply) bool settings_load_config(const char* file, bool apply)
{ {
@ -754,27 +783,16 @@ bool settings_load_config(const char* file, bool apply)
} }
else else
{ {
char *s,*end; cfg_string_to_int(i,(int*)settings[i].setting,value);
char vals[MAX_PATH];
int val = 0;
strncpy(vals,settings[i].cfg_vals,MAX_PATH);
s = strtok_r(vals,",",&end);
while (s)
{
if (!strcmp(value,s))
{
*(int*)settings[i].setting = val;
break;
}
val++;
s = strtok_r(NULL,",",&end);
}
} }
break; break;
case F_T_BOOL: case F_T_BOOL:
*(bool*)settings[i].setting = {
!strncmp(value,"off",3)?false:true; int temp;
if (cfg_string_to_int(i,&temp,value))
*(bool*)settings[i].setting = (temp==0?false:true);
break; break;
}
case F_T_CHARPTR: case F_T_CHARPTR:
case F_T_UCHARPTR: case F_T_UCHARPTR:
{ {