Commit da217ba5ee548bac04cffff3884f88e829fd2072
1 parent
bec3ff2c
element(std:string) method no longer throws an exception if the element does not…
… exist. Element existence is verified by a NULL pointer.
Showing
2 changed files
with
17 additions
and
1 deletions
src/xml/GXml.cpp
| 1 | /*************************************************************************** | 1 | /*************************************************************************** |
| 2 | * GXml.cpp - XML class * | 2 | * GXml.cpp - XML class * |
| 3 | * ----------------------------------------------------------------------- * | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2010-2013 by Juergen Knoedlseder * | 4 | + * copyright (C) 2010-2015 by Juergen Knoedlseder * |
| 5 | * ----------------------------------------------------------------------- * | 5 | * ----------------------------------------------------------------------- * |
| 6 | * * | 6 | * * |
| 7 | * This program is free software: you can redistribute it and/or modify * | 7 | * This program is free software: you can redistribute it and/or modify * |
src/xml/GXmlNode.cpp
| @@ -564,12 +564,20 @@ GXmlElement* GXmlNode::element(const std::string& name) | @@ -564,12 +564,20 @@ GXmlElement* GXmlNode::element(const std::string& name) | ||
| 564 | std::string tag = gammalib::strip_whitespace(tags[i]); | 564 | std::string tag = gammalib::strip_whitespace(tags[i]); |
| 565 | int index = extract_index(tag); | 565 | int index = extract_index(tag); |
| 566 | 566 | ||
| 567 | + // Break if the requested node does not exist | ||
| 568 | + int n = current->elements(tag); | ||
| 569 | + if (n < 1 || index < 0 || index >= n) { | ||
| 570 | + element = NULL; | ||
| 571 | + break; | ||
| 572 | + } | ||
| 573 | + | ||
| 567 | // Get node | 574 | // Get node |
| 568 | element = current->element(tag, index); | 575 | element = current->element(tag, index); |
| 569 | current = element; | 576 | current = element; |
| 570 | 577 | ||
| 571 | // Break if node has not been found | 578 | // Break if node has not been found |
| 572 | if (current == NULL) { | 579 | if (current == NULL) { |
| 580 | + element = NULL; | ||
| 573 | break; | 581 | break; |
| 574 | } | 582 | } |
| 575 | 583 | ||
| @@ -618,12 +626,20 @@ const GXmlElement* GXmlNode::element(const std::string& name) const | @@ -618,12 +626,20 @@ const GXmlElement* GXmlNode::element(const std::string& name) const | ||
| 618 | std::string tag = gammalib::strip_whitespace(tags[i]); | 626 | std::string tag = gammalib::strip_whitespace(tags[i]); |
| 619 | int index = extract_index(tag); | 627 | int index = extract_index(tag); |
| 620 | 628 | ||
| 629 | + // Break if the requested node does not exist | ||
| 630 | + int n = current->elements(tag); | ||
| 631 | + if (n < 1 || index < 0 || index >= n) { | ||
| 632 | + element = NULL; | ||
| 633 | + break; | ||
| 634 | + } | ||
| 635 | + | ||
| 621 | // Get node | 636 | // Get node |
| 622 | element = current->element(tag, index); | 637 | element = current->element(tag, index); |
| 623 | current = element; | 638 | current = element; |
| 624 | 639 | ||
| 625 | // Break if node has not been found | 640 | // Break if node has not been found |
| 626 | if (current == NULL) { | 641 | if (current == NULL) { |
| 642 | + element = NULL; | ||
| 627 | break; | 643 | break; |
| 628 | } | 644 | } |
| 629 | 645 |