]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/SamHeaderVersion_p.h
256401739ece8c3db2713f5b60a1eb212902ac23
[bamtools.git] / src / api / internal / SamHeaderVersion_p.h
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 // *************************************************************************
10
11 #ifndef SAM_HEADERVERSION_P_H
12 #define SAM_HEADERVERSION_P_H
13
14 //  -------------
15 //  W A R N I N G
16 //  -------------
17 //
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.
21 //
22 // We mean it.
23
24 #include <api/SamConstants.h>
25 #include <sstream>
26 #include <string>
27
28 namespace BamTools {
29 namespace Internal {
30
31 class SamHeaderVersion {
32
33     // ctors & dtor
34     public:
35         SamHeaderVersion(void)
36             : m_majorVersion(0)
37             , m_minorVersion(0)
38         { }
39
40         explicit SamHeaderVersion(const std::string& version)
41             : m_majorVersion(0)
42             , m_minorVersion(0)
43         {
44             SetVersion(version);
45         }
46
47         SamHeaderVersion(const unsigned int& major, const unsigned int& minor)
48             : m_majorVersion(major)
49             , m_minorVersion(minor)
50         { }
51
52         ~SamHeaderVersion(void) {
53             m_majorVersion = 0;
54             m_minorVersion = 0;
55         }
56     
57     // acess data
58     public:
59         unsigned int MajorVersion(void) const { return m_majorVersion; }
60         unsigned int MinorVersion(void) const { return m_minorVersion; }
61
62         void SetVersion(const std::string& version);
63         std::string ToString(void) const;
64
65     // data members
66     private:
67         unsigned int m_majorVersion;
68         unsigned int m_minorVersion;
69 };
70
71 inline
72 void SamHeaderVersion::SetVersion(const std::string& version) {
73
74     // do nothing if version is empty
75     if ( !version.empty() ) {
76
77         std::stringstream versionStream("");
78
79         // do nothing if period not found
80         const size_t periodFound = version.find(Constants::SAM_PERIOD);
81         if ( periodFound != std::string::npos ) {
82
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;
90             }
91
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;
99             }
100         }
101     }
102 }
103
104 // -----------------------------------------------------
105 // printing
106
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();
111 }
112
113 // -----------------------------------------------------
114 // comparison operators
115
116 inline bool operator==(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) {
117     return (lhs.MajorVersion() == rhs.MajorVersion()) &&
118            (lhs.MinorVersion() == rhs.MinorVersion());
119 }
120
121 inline bool operator<(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) {
122     if ( lhs.MajorVersion() == rhs.MajorVersion() )
123         return lhs.MinorVersion() < rhs.MinorVersion();
124     else 
125         return lhs.MajorVersion() < rhs.MajorVersion();
126 }
127
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); }
131
132 } // namespace Internal 
133 } // namespace BamTools
134
135 #endif // SAM_HEADERVERSION_P_H