]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/SamHeaderVersion_p.h
Added SAM header-handling classes for read/write/validate.
[bamtools.git] / src / api / internal / SamHeaderVersion_p.h
1 #ifndef SAM_HEADERVERSION_P_H
2 #define SAM_HEADERVERSION_P_H
3
4 #include <api/SamConstants.h>
5 #include <sstream>
6 #include <string>
7
8 namespace BamTools {
9 namespace Internal {
10
11 class SamHeaderVersion {
12
13     // ctors & dtor
14     public:
15         SamHeaderVersion(void)
16             : m_majorVersion(0)
17             , m_minorVersion(0)
18         { }
19
20         explicit SamHeaderVersion(const std::string& version)
21             : m_majorVersion(0)
22             , m_minorVersion(0)
23         {
24             SetVersion(version);
25         }
26
27         SamHeaderVersion(const unsigned int& major, const unsigned int& minor)
28             : m_majorVersion(major)
29             , m_minorVersion(minor)
30         { }
31
32         ~SamHeaderVersion(void) {
33             m_majorVersion = 0;
34             m_minorVersion = 0;
35         }
36     
37     // acess data
38     public:
39         unsigned int MajorVersion(void) const { return m_majorVersion; }
40         unsigned int MinorVersion(void) const { return m_minorVersion; }
41
42         inline void SetVersion(const std::string& version);
43         inline std::string ToString(void) const;
44
45     // data members
46     private:
47         unsigned int m_majorVersion;
48         unsigned int m_minorVersion;
49 };
50
51 inline
52 void SamHeaderVersion::SetVersion(const std::string& version) {
53
54     // do nothing if version is empty
55     if ( !version.empty() ) {
56
57         // do nothing if period not found
58         const size_t periodFound = version.find(Constants::SAM_PERIOD);
59         if ( periodFound != std::string::npos ) {
60
61             // store major version if non-empty and contains only digits
62             const std::string& majorVersion = version.substr(0, periodFound);
63             if ( majorVersion.empty() ) {
64                 const size_t nonDigitFound = majorVersion.find_first_not_of(Constants::SAM_DIGITS);
65                 if ( nonDigitFound == std::string::npos ) {
66                     std::stringstream major(majorVersion);
67                     major >> m_majorVersion;
68                 }
69             }
70
71             // store minor version if non-empty and contains only digits
72             const std::string& minorVersion = version.substr(periodFound + 1);
73             if ( minorVersion.empty() ) {
74                 const size_t nonDigitFound = minorVersion.find_first_not_of(Constants::SAM_DIGITS);
75                 if ( nonDigitFound == std::string::npos ) {
76                     std::stringstream minor(minorVersion);
77                     minor >> m_minorVersion;
78                 }
79             }
80         }
81     }
82 }
83
84 // -----------------------------------------------------
85 // printing
86
87 inline std::string SamHeaderVersion::ToString(void) const {
88     std::stringstream version;
89     version << m_majorVersion << Constants::SAM_PERIOD << m_minorVersion;
90     return version.str();
91 }
92
93 // -----------------------------------------------------
94 // comparison operators
95
96 inline bool operator==(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) {
97     return (lhs.MajorVersion() == rhs.MajorVersion()) &&
98            (lhs.MinorVersion() == rhs.MinorVersion());
99 }
100
101 inline bool operator<(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) {
102     if ( lhs.MajorVersion() == rhs.MajorVersion() )
103         return lhs.MinorVersion() < rhs.MinorVersion();
104     else 
105         return lhs.MajorVersion() < rhs.MajorVersion();
106 }
107
108 inline bool operator> (const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) { return rhs < lhs;  }
109 inline bool operator<=(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) { return !(lhs>rhs); }
110 inline bool operator>=(const SamHeaderVersion& lhs, const SamHeaderVersion& rhs) { return !(lhs<rhs); }
111
112 } // namespace Internal 
113 } // namespace BamTools
114
115 #endif // SAM_HEADERVERSION_P_H