Added a generic string to boolean parser.

This commit is contained in:
Jan Vidar Krey 2010-07-29 08:42:40 +02:00
parent 97feb3635e
commit c75090cdf2
2 changed files with 43 additions and 0 deletions

View File

@ -448,3 +448,38 @@ void strip_off_ini_line_comments(char* line, int line_count)
} }
*out = '\0'; *out = '\0';
} }
int string_to_boolean(const char* str, int* boolean)
{
if (!str || !*str || !boolean)
return 0;
switch (strlen(str))
{
case 1:
if (str[0] == '1') { *boolean = 1; return 1; }
else if (str[0] == '0') { *boolean = 0; return 1; }
return 0;
case 2:
if (!strcasecmp(str, "on")) { *boolean = 1; return 1; }
if (!strcasecmp(str, "no")) { *boolean = 0; return 1; }
return 0;
case 3:
if (!strcasecmp(str, "yes")) { *boolean = 1; return 1; }
if (!strcasecmp(str, "off")) { *boolean = 0; return 1; }
return 0;
case 4:
if (!strcasecmp(str, "true")) { *boolean = 1; return 1; }
return 0;
case 5:
if (!strcasecmp(str, "false")) { *boolean = 0; return 1; }
return 0;
default:
return 0;
}
}

View File

@ -37,6 +37,14 @@ extern void strip_off_ini_line_comments(char* line, int line_count);
extern int file_read_lines(const char* file, void* data, file_line_handler_t handler); extern int file_read_lines(const char* file, void* data, file_line_handler_t handler);
/**
* Convert a string to a boolean (0 or 1).
* Example:
* "yes", "true", "1", "on" sets 1 in boolean, and returns 1.
* "no", "false", "0", "off" sets 0 in boolean, and returns 1.
* All other values return 0, and boolean is unchanged.
*/
extern int string_to_boolean(const char* str, int* boolean);
extern const char* uhub_itoa(int val); extern const char* uhub_itoa(int val);