]> git.donarmstrong.com Git - lilypond.git/blob - lily/lilypond-version.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / lilypond-version.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU 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   LilyPond 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 General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <ctype.h>
21
22 #include "lilypond-version.hh"
23 #include "string-convert.hh"
24 #include "misc.hh"
25
26 using std::string;
27 using std::vector;
28
29 Lilypond_version::Lilypond_version (int major, int minor, int patch)
30 {
31   major_ = major;
32   minor_ = minor;
33   patch_ = patch;
34 }
35
36 Lilypond_version::Lilypond_version (const string &str)
37 {
38   major_ = -1;
39   minor_ = 0;
40   patch_ = 0;
41
42   vector<string> version;
43   const char *digits = "0123456789";
44   version = string_split (str, '.');
45
46   switch (version.size ()) {
47   case 4:
48     extra_patch_string_ = version[3];
49     if (version[2].empty ())
50       return;
51   case 3:
52     if (version[2].find_first_not_of (digits) != string::npos
53         || version[1].empty ())
54       return;
55     patch_ = String_convert::dec2int (version[2]);
56   case 2:
57     if (version[1].find_first_not_of (digits) != string::npos
58         || version[1].empty () || version[0].empty ())
59       return;
60     minor_ = String_convert::dec2int (version[1]);
61     if (version[0].find_first_not_of (digits) != string::npos)
62       return;
63     major_ = String_convert::dec2int (version[0]);
64   }
65 }
66
67 string
68 Lilypond_version::to_string () const
69 {
70   if (major_ < 0)
71     return "invalid";
72   return ::to_string (major_)
73          + "." + ::to_string (minor_)
74          + "." + ::to_string (patch_);
75 }
76
77 Lilypond_version::operator bool () const
78 {
79   return !(major_ < 0);
80 }
81
82 int
83 Lilypond_version::compare (const Lilypond_version &a, const Lilypond_version &b)
84 {
85   if (!a || !b)
86     return 0;
87   if (a.major_ != b.major_)
88     return a.major_ > b.major_ ? 1 : -1;
89   if (a.minor_ != b.minor_)
90     return a.minor_ > b.minor_ ? 1 : -1;
91   if (a.patch_ != b.patch_)
92     return a.patch_ > b.patch_ ? 1 : -1;
93   return 0;
94 }