| 166 | } |
| 167 | |
| 168 | std::string WktParser::WktFormat(bool flat, const short indent, const short indent_offset) { |
| 169 | if (flat) { |
| 170 | // optional format as single line |
| 171 | return ReplaceString(ReplaceString(ReplaceString(wktraw, "\n", ""), "\r", ""), ", ", ","); |
| 172 | } |
| 173 | // default: format with linebreaks and indent |
| 174 | std::string work = wktraw; |
| 175 | std::string token = ""; |
| 176 | std::string result = ""; |
| 177 | bool inStr = false; |
| 178 | bool inEscape = false; |
| 179 | int level = 0; |
| 180 | // parse wkt |
| 181 | for (char& c : work) { |
| 182 | if (inStr) { |
| 183 | // get string till trailing " |
| 184 | if (c == '\\') { |
| 185 | inEscape = true; |
| 186 | } else if (inEscape || c != '"') { |
| 187 | inEscape = false; |
| 188 | token += c; |
| 189 | } else { |
| 190 | // string close |
| 191 | inEscape = false; |
| 192 | inStr = false; |
| 193 | } |
| 194 | } else if (c == '[') { |
| 195 | // output last key in new line with indent |
| 196 | if (!result.empty()) result += '\n'; |
| 197 | result += std::string(indent_offset, ' ') + std::string(static_cast<size_t>(level) * indent, ' ') + BOOST_PRE trim(token) + c; |
| 198 | level++; |
| 199 | token = ""; |
| 200 | } else if (c == ']') { |
| 201 | // close values |
| 202 | result += BOOST_PRE trim(token) + c; |
| 203 | token = ""; |
| 204 | level--; |
| 205 | } else if (c == ',') { |
| 206 | // add this value |
| 207 | result += BOOST_PRE trim(token) + c; |
| 208 | token = ""; |
| 209 | } else if (c == '"') { |
| 210 | // start string parser |
| 211 | inStr = true; |
| 212 | inEscape = false; |
| 213 | } else { |
| 214 | token += c; |
| 215 | } |
| 216 | } |
| 217 | return result; |
| 218 | } |
| 219 | |
| 220 | void WktParser::Debug() { |
| 221 | for (auto const& [key, val] : map) { |
no test coverage detected