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