| 390 | |
| 391 | using ini_handler = void (*)(void*, estring_view, estring_view, estring_view); |
| 392 | static int do_parse_ini(estring_view text, ini_handler h, void* user) { |
| 393 | int err_cnt = 0; |
| 394 | estring_view section; |
| 395 | for (auto line: text.split_lines()) { |
| 396 | auto comment = line.rfind(" ;"); |
| 397 | if (comment < line.size()) |
| 398 | line = line.substr(0, comment); |
| 399 | line = line.trim(); |
| 400 | if (line.empty() || line[0] == '#' || line[0] == ';') continue; |
| 401 | if (line[0] == '[') { |
| 402 | if (line.back() == ']') { |
| 403 | section = line.substr(1, line.size() - 2).trim(); |
| 404 | } else { |
| 405 | err_cnt++; |
| 406 | LOG_DEBUG("section with no ending: ", line); |
| 407 | } |
| 408 | } else { |
| 409 | auto eq = line.find_first_of(charset("=:")); |
| 410 | if (eq > 0 && eq < line.size() - 1) { |
| 411 | auto key = line.substr(0, eq).trim(); |
| 412 | auto val = line.substr(eq + 1).trim(); |
| 413 | h(user, section, key, val); |
| 414 | } else { |
| 415 | err_cnt++; |
| 416 | LOG_DEBUG("ill formed kv: ", line); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | return -err_cnt; |
| 421 | } |
| 422 | |
| 423 | static NodeImpl* parse_ini(char* text, size_t size, int flags) { |
| 424 | struct Item { |
no test coverage detected