* @brief Parse resolution value from the string. * @param input String to be parsed. * @param output Reference to output variable to fill in. * @returns True on successful parsing (empty string allowed), false otherwise. * * @examples * std::optional resolution; * if (parse_resolution_string("1920x1080", resolution)) { * if (resolution) {
| 148 | * @examples_end |
| 149 | */ |
| 150 | bool parse_resolution_string(const std::string &input, std::optional<Resolution> &output) { |
| 151 | const std::string trimmed_input {boost::algorithm::trim_copy(input)}; |
| 152 | const std::regex resolution_regex {R"(^(\d+)x(\d+)$)"}; |
| 153 | |
| 154 | if (std::smatch match; std::regex_match(trimmed_input, match, resolution_regex)) { |
| 155 | try { |
| 156 | output = Resolution { |
| 157 | stou(match[1].str()), |
| 158 | stou(match[2].str()) |
| 159 | }; |
| 160 | return true; |
| 161 | } catch (const std::out_of_range &) { |
| 162 | BOOST_LOG(error) << "Failed to parse resolution string " << trimmed_input << " (number out of range)."; |
| 163 | } catch (const std::exception &err) { |
| 164 | BOOST_LOG(error) << "Failed to parse resolution string " << trimmed_input << ":\n" |
| 165 | << err.what(); |
| 166 | } |
| 167 | } else { |
| 168 | if (trimmed_input.empty()) { |
| 169 | output = std::nullopt; |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | BOOST_LOG(error) << "Failed to parse resolution string " << trimmed_input << R"(. It must match a "1920x1080" pattern!)"; |
| 174 | } |
| 175 | |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @brief Parse refresh rate value from the string. |
no test coverage detected