* Try to interpret "value" as a boolean value, and if successful, * store it in *result. Otherwise don't clobber *result. * * Valid values are: true, false, yes, no, on, off, 1, 0; as well as unique * prefixes thereof. * * "name" is the name of the variable we're assigning to, to use in error * report if any. Pass name == NULL to suppress the error report. * * Return true when "value" i
| 104 | * Return true when "value" is syntactically valid, false otherwise. |
| 105 | */ |
| 106 | bool |
| 107 | ParseVariableBool(const char *value, const char *name, bool *result) |
| 108 | { |
| 109 | size_t len; |
| 110 | bool valid = true; |
| 111 | |
| 112 | /* Treat "unset" as an empty string, which will lead to error below */ |
| 113 | if (value == NULL) |
| 114 | value = ""; |
| 115 | |
| 116 | len = strlen(value); |
| 117 | |
| 118 | if (len > 0 && pg_strncasecmp(value, "true", len) == 0) |
| 119 | *result = true; |
| 120 | else if (len > 0 && pg_strncasecmp(value, "false", len) == 0) |
| 121 | *result = false; |
| 122 | else if (len > 0 && pg_strncasecmp(value, "yes", len) == 0) |
| 123 | *result = true; |
| 124 | else if (len > 0 && pg_strncasecmp(value, "no", len) == 0) |
| 125 | *result = false; |
| 126 | /* 'o' is not unique enough */ |
| 127 | else if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0) |
| 128 | *result = true; |
| 129 | else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0) |
| 130 | *result = false; |
| 131 | else if (pg_strcasecmp(value, "1") == 0) |
| 132 | *result = true; |
| 133 | else if (pg_strcasecmp(value, "0") == 0) |
| 134 | *result = false; |
| 135 | else |
| 136 | { |
| 137 | /* string is not recognized; don't clobber *result */ |
| 138 | if (name) |
| 139 | pg_log_error("unrecognized value \"%s\" for \"%s\": Boolean expected", |
| 140 | value, name); |
| 141 | valid = false; |
| 142 | } |
| 143 | return valid; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Try to interpret "value" as an integer value, and if successful, |
no test coverage detected