]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/api/SamHeader.cpp
Removed STDERR pollution by API
[bamtools.git] / src / api / SamHeader.cpp
index 77e1c6d4b5fee014aadb6e6e702f4cc60f9f8867..363fb5ce18fa1579cee6885351a1eb2d969cad44 100644 (file)
@@ -2,13 +2,14 @@
 // SamHeader.cpp (c) 2010 Derek Barnett
 // Marth Lab, Department of Biology, Boston College
 // ---------------------------------------------------------------------------
-// Last modified: 19 April 2011 (DB)
+// Last modified: 6 October 2011 (DB)
 // ---------------------------------------------------------------------------
 // Provides direct read/write access to the SAM header data fields.
 // ***************************************************************************
 
 #include <api/SamConstants.h>
 #include <api/SamHeader.h>
+#include <api/internal/BamException_p.h>
 #include <api/internal/SamFormatParser_p.h>
 #include <api/internal/SamFormatPrinter_p.h>
 #include <api/internal/SamHeaderValidator_p.h>
@@ -63,8 +64,7 @@ SamHeader::SamHeader(const std::string& headerText)
     , SortOrder(Constants::SAM_HD_SORTORDER_UNKNOWN)
     , GroupOrder("")
 {
-    SamFormatParser parser(*this);
-    parser.Parse(headerText);
+    SetHeaderText(headerText);
 }
 
 /*! \fn SamHeader::SamHeader(const SamHeader& other)
@@ -88,6 +88,8 @@ SamHeader::~SamHeader(void) { }
     \brief Clears all header contents.
 */
 void SamHeader::Clear(void) {
+
+    // clear SAM header components
     Version.clear();
     SortOrder.clear();
     GroupOrder.clear();
@@ -95,6 +97,23 @@ void SamHeader::Clear(void) {
     ReadGroups.Clear();
     Programs.Clear();
     Comments.clear();
+
+    // clear error string
+    m_errorString.clear();
+}
+
+/*! \fn std::string SamHeader::GetErrorString(void) const
+    \brief Returns human-readable description of last error encountered
+*/
+std::string SamHeader::GetErrorString(void) const {
+    return m_errorString;
+}
+
+/*! \fn bool SamHeader::HasError(void) const
+    \brief Returns \c true if header encountered an error
+*/
+bool SamHeader::HasError(void) const {
+    return (!m_errorString.empty());
 }
 
 /*! \fn bool SamHeader::HasVersion(void) const
@@ -149,12 +168,32 @@ bool SamHeader::HasComments(void) const {
 /*! \fn bool SamHeader::IsValid(bool verbose = false) const
     \brief Checks header contents for required data and proper formatting.
     \param verbose If set to true, validation errors & warnings will be printed to stderr.
-                   Otherwise, output is suppressed and only validation check occurs.
+                   Otherwise, messages are available through SamHeader::GetErrorString().
     \return \c true if SAM header is well-formed
 */
 bool SamHeader::IsValid(bool verbose) const {
+
     SamHeaderValidator validator(*this);
-    return validator.Validate(verbose);
+
+    // if SAM header is valid, return success
+    if ( validator.Validate() )
+        return true;
+
+    // otherwiser
+    else {
+
+        // print messages to stderr
+        if ( verbose )
+            validator.PrintMessages(std::cerr);
+
+        // or catch in local error string
+        else {
+            stringstream errorStream("");
+            validator.PrintMessages(errorStream);
+            m_errorString = errorStream.str();
+        }
+        return false;
+    }
 }
 
 /*! \fn void SamHeader::SetHeaderText(const std::string& headerText)
@@ -166,9 +205,18 @@ void SamHeader::SetHeaderText(const std::string& headerText) {
     // clear prior data
     Clear();
 
-    // parse header text into data
-    SamFormatParser parser(*this);
-    parser.Parse(headerText);
+    try {
+        SamFormatParser parser(*this);
+        parser.Parse(headerText);
+    } catch ( BamException& e ) {
+
+        // clear anything parsed so far
+        // no telling what's valid and what's partially parsed
+        Clear();
+
+        // set error string
+        m_errorString = e.what();
+    }
 }
 
 /*! \fn std::string SamHeader::ToString(void) const