Supporters update.
[dcpomatic.git] / src / lib / rough_duration.cc
1 /*
2     Copyright (C) 2022 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "dcpomatic_assert.h"
23 #include "rough_duration.h"
24 #include <dcp/raw_convert.h>
25 #include <dcp/warnings.h>
26 LIBDCP_DISABLE_WARNINGS
27 #include <libxml++/libxml++.h>
28 LIBDCP_ENABLE_WARNINGS
29
30
31 using std::string;
32
33
34 RoughDuration::RoughDuration (cxml::ConstNodePtr node)
35         : duration(dcp::raw_convert<int>(node->content()))
36 {
37         auto const unit_name = node->string_attribute("unit");
38         if (unit_name == "days") {
39                 unit = Unit::DAYS;
40         } else if (unit_name == "weeks") {
41                 unit = Unit::WEEKS;
42         } else if (unit_name == "months") {
43                 unit = Unit::MONTHS;
44         } else if (unit_name == "years") {
45                 unit = Unit::YEARS;
46         } else {
47                 DCPOMATIC_ASSERT (false);
48         }
49 }
50
51
52 void
53 RoughDuration::as_xml (xmlpp::Element* node) const
54 {
55         node->add_child_text(dcp::raw_convert<string>(duration));
56
57         switch (unit) {
58         case Unit::DAYS:
59                 node->set_attribute("unit", "days");
60                 break;
61         case Unit::WEEKS:
62                 node->set_attribute("unit", "weeks");
63                 break;
64         case Unit::MONTHS:
65                 node->set_attribute("unit", "months");
66                 break;
67         case Unit::YEARS:
68                 node->set_attribute("unit", "years");
69                 break;
70         }
71 }
72
73
74 bool
75 operator== (RoughDuration const& a, RoughDuration const& b)
76 {
77         return a.duration == b.duration && a.unit == b.unit;
78 }
79