1 // ***************************************************************************
2 // SamHeaderVersion.h (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 24 February 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides functionality for comparing SAM header versions
8 // *************************************************************************
10 #ifndef SAM_HEADERVERSION_P_H
11 #define SAM_HEADERVERSION_P_H
17 // This file is not part of the BamTools API. It exists purely as an
18 // implementation detail. This header file may change from version to version
19 // without notice, or even be removed.
23 #include <api/SamConstants.h>
30 class SamHeaderVersion {
34 SamHeaderVersion(void)
39 explicit SamHeaderVersion(const std::string& version)
46 SamHeaderVersion(const unsigned int& major, const unsigned int& minor)
47 : m_majorVersion(major)
48 , m_minorVersion(minor)
51 ~SamHeaderVersion(void) {
58 unsigned int MajorVersion(void) const { return m_majorVersion; }
59 unsigned int MinorVersion(void) const { return m_minorVersion; }
61 void SetVersion(const std::string& version);
62 std::string ToString(void) const;
66 unsigned int m_majorVersion;
67 unsigned int m_minorVersion;
71 void SamHeaderVersion::SetVersion(const std::string& version) {
73 // do nothing if version is empty
74 if ( !version.empty() ) {
76 std::stringstream versionStream("");
78 // do nothing if period not found
79 const size_t periodFound = version.find(Constants::SAM_PERIOD);
80 if ( periodFound != std::string::npos ) {
82 // store major version if non-empty and contains only digits
83 const std::string& majorVersion = version.substr(0, periodFound);
84 versionStream.str(majorVersion);
85 if ( !majorVersion.empty() ) {
86 const size_t nonDigitFound = majorVersion.find_first_not_of(Constants::SAM_DIGITS);
87 if ( nonDigitFound == std::string::npos )
88 versionStream >> m_majorVersion;
91 // store minor version if non-empty and contains only digits
92 const std::string& minorVersion = version.substr(periodFound + 1);
93 versionStream.str(minorVersion);
94 if ( !minorVersion.empty() ) {
95 const size_t nonDigitFound = minorVersion.find_first_not_of(Constants::SAM_DIGITS);
96 if ( nonDigitFound == std::string::npos )
97 versionStream >> m_minorVersion;
103 // -----------------------------------------------------
106 inline std::string SamHeaderVersion::ToString(void) const {
107 std::stringstream version;
108 version << m_majorVersion << Constants::SAM_PERIOD << m_minorVersion;
109 return version.str();
112 // -----------------------------------------------------
113 // comparison operators
115 inline bool operator==(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) {
116 return (lhs.MajorVersion() == rhs.MajorVersion()) &&
117 (lhs.MinorVersion() == rhs.MinorVersion());
120 inline bool operator<(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) {
121 if ( lhs.MajorVersion() == rhs.MajorVersion() )
122 return lhs.MinorVersion() < rhs.MinorVersion();
124 return lhs.MajorVersion() < rhs.MajorVersion();
127 inline bool operator> (const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) { return rhs < lhs; }
128 inline bool operator<=(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) { return !(lhs>rhs); }
129 inline bool operator>=(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) { return !(lhs<rhs); }
131 } // namespace Internal
132 } // namespace BamTools
134 #endif // SAM_HEADERVERSION_P_H