new function: config_get_tristate()

This commit is contained in:
marschap
2007-05-12 16:08:40 +00:00
parent 4f81466fc1
commit 3e84564e6a
2 changed files with 62 additions and 6 deletions
+49 -6
View File
@@ -185,20 +185,63 @@ short config_get_bool(const char *sectionname, const char *keyname,
if (k == NULL)
return default_value;
if (strcasecmp(k->value, "0") == 0 || strcasecmp(k->value, "false") == 0
|| strcasecmp(k->value, "n") == 0 || strcasecmp(k->value, "no") == 0
|| strcasecmp(k->value, "off") == 0) {
if ((strcasecmp(k->value, "0") == 0) || (strcasecmp(k->value, "false") == 0) ||
(strcasecmp(k->value, "n") == 0) || (strcasecmp(k->value, "no") == 0) ||
(strcasecmp(k->value, "off") == 0)) {
return 0;
}
if (strcasecmp(k->value, "1") == 0 || strcasecmp(k->value, "true") == 0
|| strcasecmp(k->value, "y") == 0 || strcasecmp(k->value, "yes") == 0
|| strcasecmp(k->value, "on") == 0) {
if ((strcasecmp(k->value, "1") == 0) || (strcasecmp(k->value, "true") == 0) ||
(strcasecmp(k->value, "y") == 0) || (strcasecmp(k->value, "yes") == 0) ||
(strcasecmp(k->value, "on") == 0)) {
return 1;
}
return default_value;
}
/** Get tristate value from configuration in memory.
*
* Legal tristate values are:
* \li \c 0 , \c false , \c off , \c no or \c n for 0.
* \li \c 1 , \c true , \c on , \c yes or \c y for 1
* \li \c 2 or the given name of the third state for 2
*
* \param sectionname Name of the section where the key is sought.
* \param keyname Name of the key to look for.
* \param skip Number of values to skip/ignore before returning the value.
* This is used to iterate through the values of a multi-valued key:
* \c 0 for the first value, \c 1 for the 2nd, ... and \c -1 for the last.
* \param name3rd Name of the 3rd state
* \param default_value Default value if section/key is not found, value is no legal boolean,
* or \c skip exceeds the number of values of the key.
* \return Value found / \c default_value
*/
short config_get_tristate(const char *sectionname, const char *keyname,
int skip, const char *name3rd, short default_value)
{
key *k = find_key(find_section(sectionname), keyname, skip);
if (k == NULL)
return default_value;
if ((strcasecmp(k->value, "0") == 0) || (strcasecmp(k->value, "false") == 0) ||
(strcasecmp(k->value, "n") == 0) || (strcasecmp(k->value, "no") == 0) ||
(strcasecmp(k->value, "off") == 0)) {
return 0;
}
if ((strcasecmp(k->value, "1") == 0) || (strcasecmp(k->value, "true") == 0) ||
(strcasecmp(k->value, "y") == 0) || (strcasecmp(k->value, "yes") == 0) ||
(strcasecmp(k->value, "on") == 0)) {
return 1;
}
if ((strcasecmp(k->value, "2") == 0) ||
(name3rd != NULL) && (strcasecmp(k->value, name3rd) == 0)) {
return 2;
}
return default_value;
}
/** Get integer from configuration in memory.
* \param sectionname Name of the section where the key is sought.
* \param keyname Name of the key to look for.
+13
View File
@@ -45,6 +45,19 @@ int config_read_string(const char *sectionname, const char *str);
short config_get_bool(const char *sectionname, const char *keyname,
int skip, short default_value);
/* Tries to interpret a value in the config file as a boolean.
* 0, false, off, no, n = 0
* 1, true, on, yes, y = 1
* 2, or the given 3rd name = 2
* If the key is not found or cannot be interpreted, the given default value is
* returned.
* The skip value can be used to iterate over multiple values with the same
* key. Should be 0 to get the first one, 1 for the second etc. and -1 for the
* last.
*/
short config_get_tristate(const char *sectionname, const char *keyname,
int skip, const char *name3rd, short default_value);
/* Tries to interpret a value in the config file as an integer.*/
long int config_get_int(const char *sectionname, const char *keyname,
int skip, long int default_value);