File: versionstring.h

package info (click to toggle)
aoflagger 3.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,960 kB
  • sloc: cpp: 83,076; python: 10,187; sh: 260; makefile: 178
file content (64 lines) | stat: -rw-r--r-- 1,886 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef VERSION_STRING_H
#define VERSION_STRING_H

#include <stdexcept>
#include <string>

class VersionString {
 public:
  VersionString()
      : _major(0), _minor(0), _subminor(0), _hasMinor(0), _hasSubminor(0) {}

  explicit VersionString(const std::string& str) {
    if (str.empty()) throw std::runtime_error("Empty version string specified");
    for (size_t i = 0; i != str.size(); ++i)
      if (str[i] != '.' && (str[i] < '0' || str[i] > '9'))
        throw std::runtime_error("Invalid version specified: '" + str + "'");
    const size_t pos1 = str.find('.');
    if (pos1 == std::string::npos) {
      _major = std::atoi(str.c_str());
      _minor = 0;
      _subminor = 0;
      _hasMinor = false;
      _hasSubminor = false;
    } else {
      _major = std::atoi(str.substr(0, pos1).c_str());
      _hasMinor = true;
      const size_t pos2 = str.find('.', pos1 + 1);
      if (pos2 == std::string::npos) {
        _minor = std::atoi(str.substr(pos1 + 1).c_str());
        _subminor = 0;
        _hasSubminor = false;
      } else {
        _minor = std::atoi(str.substr(pos1 + 1, pos2 - pos1).c_str());
        _subminor = std::atoi(str.substr(pos2 + 1).c_str());
        _hasSubminor = true;
      }
    }
  }

  int Major() const { return _major; }
  int Minor() const { return _minor; }
  int Subminor() const { return _subminor; }
  bool HasMinor() const { return _hasMinor; }
  bool HasSubminor() const { return _hasSubminor; }

  std::string String() const {
    if (_hasMinor) {
      if (_hasSubminor) {
        return std::to_string(_major) + '.' + std::to_string(_minor) + '.' +
               std::to_string(_subminor);
      } else {
        return std::to_string(_major) + '.' + std::to_string(_minor);
      }
    } else {
      return std::to_string(_major);
    }
  }

 private:
  int _major, _minor, _subminor;
  bool _hasMinor, _hasSubminor;
};

#endif