]> git.donarmstrong.com Git - lilypond.git/blob - lily/lilypond-version.cc
Web-ja: update introduction
[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 Lilypond_version::Lilypond_version (int major, int minor, int patch)
27 {
28   major_ = major;
29   minor_ = minor;
30   patch_ = patch;
31 }
32
33 Lilypond_version::Lilypond_version (const string &str)
34 {
35   major_ = -1;
36   minor_ = 0;
37   patch_ = 0;
38
39   vector<string> version;
40   const char *digits = "0123456789";
41   version = string_split (str, '.');
42
43   switch (version.size ()) {
44   case 4:
45     extra_patch_string_ = version[3];
46     if (version[2].empty ())
47       return;
48   case 3:
49     if (version[2].find_first_not_of (digits) != string::npos
50         || version[1].empty ())
51       return;
52     patch_ = String_convert::dec2int (version[2]);
53   case 2:
54     if (version[1].find_first_not_of (digits) != string::npos
55         || version[1].empty () || version[0].empty ())
56       return;
57     minor_ = String_convert::dec2int (version[1]);
58     if (version[0].find_first_not_of (digits) != string::npos)
59       return;
60     major_ = String_convert::dec2int (version[0]);
61   }
62 }
63
64 string
65 Lilypond_version::to_string () const
66 {
67   if (major_ < 0)
68     return "invalid";
69   return ::to_string (major_)
70          + "." + ::to_string (minor_)
71          + "." + ::to_string (patch_);
72 }
73
74 Lilypond_version::operator bool () const
75 {
76   return !(major_ < 0);
77 }
78
79 int
80 Lilypond_version::compare (const Lilypond_version &a, const Lilypond_version &b)
81 {
82   if (!a || !b)
83     return 0;
84   if (a.major_ != b.major_)
85     return a.major_ > b.major_ ? 1 : -1;
86   if (a.minor_ != b.minor_)
87     return a.minor_ > b.minor_ ? 1 : -1;
88   if (a.patch_ != b.patch_)
89     return a.patch_ > b.patch_ ? 1 : -1;
90   return 0;
91 }