| 555 | } |
| 556 | |
| 557 | mitk::TestDICOMLoading::KeyValueMap |
| 558 | mitk::TestDICOMLoading::ParseDump( const std::string& dump ) |
| 559 | { |
| 560 | KeyValueMap parsedResult; |
| 561 | |
| 562 | std::string shredder(dump); |
| 563 | |
| 564 | std::stack<std::string> surroundingKeys; |
| 565 | |
| 566 | std::stack<std::string::size_type> expectedIndents; |
| 567 | expectedIndents.push(0); |
| 568 | |
| 569 | while (true) |
| 570 | { |
| 571 | std::string::size_type newLinePos = shredder.find( '\n' ); |
| 572 | if (newLinePos == std::string::npos || newLinePos == 0) break; |
| 573 | |
| 574 | std::string line = shredder.substr( 0, newLinePos ); |
| 575 | shredder = shredder.erase( 0, newLinePos+1 ); |
| 576 | |
| 577 | std::string::size_type keyPosition = line.find_first_not_of( ' ' ); |
| 578 | std::string::size_type colonPosition = line.find( ':' ); |
| 579 | |
| 580 | std::string key = line.substr(keyPosition, colonPosition - keyPosition); |
| 581 | std::string::size_type firstSpacePosition = key.find_first_of(" "); |
| 582 | if (firstSpacePosition != std::string::npos) |
| 583 | { |
| 584 | key.erase(firstSpacePosition); |
| 585 | } |
| 586 | |
| 587 | if ( keyPosition > expectedIndents.top() ) |
| 588 | { |
| 589 | // more indent than before |
| 590 | expectedIndents.push(keyPosition); |
| 591 | } |
| 592 | else |
| 593 | { |
| 594 | if (!surroundingKeys.empty()) |
| 595 | { |
| 596 | surroundingKeys.pop(); // last of same length |
| 597 | } |
| 598 | |
| 599 | while (expectedIndents.top() != keyPosition) |
| 600 | { |
| 601 | expectedIndents.pop(); |
| 602 | if (!surroundingKeys.empty()) |
| 603 | { |
| 604 | surroundingKeys.pop(); |
| 605 | } |
| 606 | }; // unwind until current indent is found |
| 607 | } |
| 608 | |
| 609 | if (!surroundingKeys.empty()) |
| 610 | { |
| 611 | key = surroundingKeys.top() + "." + key; // construct current key name |
| 612 | } |
| 613 | |
| 614 | surroundingKeys.push(key); // this is the new embracing key |