1 // ***************************************************************************
2 // SamHeaderVersion.h (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 23 December 2010 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides functionality for comparing SAM header versions
9 // *************************************************************************
11 #ifndef SAM_HEADERVERSION_P_H
12 #define SAM_HEADERVERSION_P_H
18 // This file is not part of the BamTools API. It exists purely as an
19 // implementation detail. This header file may change from version to version
20 // without notice, or even be removed.
24 #include <api/SamConstants.h>
31 class SamHeaderVersion {
35 SamHeaderVersion(void)
40 explicit SamHeaderVersion(const std::string& version)
47 SamHeaderVersion(const unsigned int& major, const unsigned int& minor)
48 : m_majorVersion(major)
49 , m_minorVersion(minor)
52 ~SamHeaderVersion(void) {
59 unsigned int MajorVersion(void) const { return m_majorVersion; }
60 unsigned int MinorVersion(void) const { return m_minorVersion; }
62 inline void SetVersion(const std::string& version);
63 inline std::string ToString(void) const;
67 unsigned int m_majorVersion;
68 unsigned int m_minorVersion;
72 void SamHeaderVersion::SetVersion(const std::string& version) {
74 // do nothing if version is empty
75 if ( !version.empty() ) {
77 // do nothing if period not found
78 const size_t periodFound = version.find(Constants::SAM_PERIOD);
79 if ( periodFound != std::string::npos ) {
81 // store major version if non-empty and contains only digits
82 const std::string& majorVersion = version.substr(0, periodFound);
83 if ( majorVersion.empty() ) {
84 const size_t nonDigitFound = majorVersion.find_first_not_of(Constants::SAM_DIGITS);
85 if ( nonDigitFound == std::string::npos ) {
86 std::stringstream major(majorVersion);
87 major >> m_majorVersion;
91 // store minor version if non-empty and contains only digits
92 const std::string& minorVersion = version.substr(periodFound + 1);
93 if ( minorVersion.empty() ) {
94 const size_t nonDigitFound = minorVersion.find_first_not_of(Constants::SAM_DIGITS);
95 if ( nonDigitFound == std::string::npos ) {
96 std::stringstream minor(minorVersion);
97 minor >> m_minorVersion;
104 // -----------------------------------------------------
107 inline std::string SamHeaderVersion::ToString(void) const {
108 std::stringstream version;
109 version << m_majorVersion << Constants::SAM_PERIOD << m_minorVersion;
110 return version.str();
113 // -----------------------------------------------------
114 // comparison operators
116 inline bool operator==(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) {
117 return (lhs.MajorVersion() == rhs.MajorVersion()) &&
118 (lhs.MinorVersion() == rhs.MinorVersion());
121 inline bool operator<(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) {
122 if ( lhs.MajorVersion() == rhs.MajorVersion() )
123 return lhs.MinorVersion() < rhs.MinorVersion();
125 return lhs.MajorVersion() < rhs.MajorVersion();
128 inline bool operator> (const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) { return rhs < lhs; }
129 inline bool operator<=(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) { return !(lhs>rhs); }
130 inline bool operator>=(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) { return !(lhs<rhs); }
132 } // namespace Internal
133 } // namespace BamTools
135 #endif // SAM_HEADERVERSION_P_H