Leosac  0.8.0
Open Source Access Control
version.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
26 #include "version.hpp"
27 #include "tools/GitSHA1.hpp"
28 #include <algorithm>
29 #include <boost/regex.hpp>
30 #include <spdlog/fmt/fmt.h>
31 
32 using namespace Leosac::Tools;
33 
34 std::string Version::buildVersionString(int major, int minor, int patch,
35  std::string git_sha1)
36 {
37  std::ostringstream oss;
38 
39  oss << major << '.' << minor << '.' << patch;
40  if (!git_sha1.empty())
41  oss << "-" << git_sha1;
42  return (oss.str());
43 }
44 
45 static void cleanVersionString(std::string &v)
46 {
47  v = v.substr(0, v.find_first_of('-'));
48  std::replace(v.begin(), v.end(), '.', ' ');
49 }
50 
51 int Version::versionCompare(std::string a, std::string b)
52 {
53  std::istringstream issa;
54  std::istringstream issb;
55  int va;
56  int vb;
57 
58  cleanVersionString(a);
59  cleanVersionString(b);
60  issa.str(a);
61  issb.str(b);
62  for (int i = 0; i < 3; ++i)
63  {
64  issa >> va;
65  issb >> vb;
66  if (va > vb)
67  return (1);
68  else if (va < vb)
69  return (-1);
70  }
71  return (0);
72 }
73 
74 bool Version::isVersionValid(const std::string &v)
75 {
76  boost::regex r("[0-9]+\\.[0-9]+\\.[0-9]+($|\\-[a-f0-9]{40}$)");
77  return boost::regex_match(v, r);
78 }
79 
81 {
83 }
84 
86 {
88 }
Leosac::Tools::Version::get_short_version
static std::string get_short_version()
Retrieve the short (X.Y.Z) version of Leosac.
Definition: version.cpp:85
Leosac::Tools::get_git_sha1
const std::string & get_git_sha1()
Retrieve the SHA1 of the HEAD commit when Leosac was built.
Leosac::Tools::Version::get_full_version
static std::string get_full_version()
Returns the complete version string.
Definition: version.cpp:80
Leosac::Tools::Version::buildVersionString
static std::string buildVersionString(int major, int minor, int patch, std::string git_sha1="")
return semver compatible version string formatted this way: MAJOR.MINOR.PATCH
Definition: version.cpp:34
version.hpp
version handling
Leosac::Tools::Version::Patch
static const int Patch
Definition: version.hpp:39
GitSHA1.hpp
Leosac::Tools::Version::Major
static const int Major
Definition: version.hpp:37
Leosac::Tools::Version::versionCompare
static int versionCompare(std::string a, std::string b)
compare two version strings using semver v2.0.0
Definition: version.cpp:51
Leosac::Tools::Version::Minor
static const int Minor
Definition: version.hpp:38
Leosac::Tools
Definition: DatabaseLogSink.hpp:27
Leosac::Tools::Version::isVersionValid
static bool isVersionValid(const std::string &v)
check validity of a semver version string
Definition: version.cpp:74