1 // ***************************************************************************
2 // SamHeaderVersion.h (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 24 February 2011 (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 void SetVersion(const std::string& version);
63 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 std::stringstream versionStream("");
79 // do nothing if period not found
80 const size_t periodFound = version.find(Constants::SAM_PERIOD);
81 if ( periodFound != std::string::npos ) {
83 // store major version if non-empty and contains only digits
84 const std::string& majorVersion = version.substr(0, periodFound);
85 versionStream.str(majorVersion);
86 if ( !majorVersion.empty() ) {
87 const size_t nonDigitFound = majorVersion.find_first_not_of(Constants::SAM_DIGITS);
88 if ( nonDigitFound == std::string::npos )
89 versionStream >> m_majorVersion;
92 // store minor version if non-empty and contains only digits
93 const std::string& minorVersion = version.substr(periodFound + 1);
94 versionStream.str(minorVersion);
95 if ( !minorVersion.empty() ) {
96 const size_t nonDigitFound = minorVersion.find_first_not_of(Constants::SAM_DIGITS);
97 if ( nonDigitFound == std::string::npos )
98 versionStream >> 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