In-line run of subs_in_out so that it gets the environment more easily.
[libdcp.git] / src / rating.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 #include "exceptions.h"
36 #include "file.h"
37 #include "rating.h"
38 #include "util.h"
39 #include <libcxml/cxml.h>
40 #include <boost/algorithm/string.hpp>
41
42
43 using std::string;
44 using std::vector;
45 using boost::algorithm::trim;
46 using boost::optional;
47 using namespace dcp;
48
49
50 static vector<RatingSystem> rating_systems_list;
51
52
53 Rating::Rating (cxml::ConstNodePtr node)
54         : agency(node->string_child("Agency"))
55         , label(node->string_child("Label"))
56 {
57         node->done ();
58 }
59
60
61 void
62 Rating::as_xml (xmlpp::Element* parent) const
63 {
64         parent->add_child("Agency")->add_child_text(agency);
65         parent->add_child("Label")->add_child_text(label);
66 }
67
68
69 bool
70 dcp::operator== (Rating const & a, Rating const & b)
71 {
72         return a.agency == b.agency && a.label == b.label;
73 }
74
75
76 vector<RatingSystem>
77 dcp::rating_systems()
78 {
79         return rating_systems_list;
80 }
81
82
83 void
84 dcp::load_rating_list(boost::filesystem::path ratings_file)
85 {
86         File f(ratings_file, "r");
87         if (!f) {
88                 throw FileError ("Could not open ratings file", ratings_file, errno);
89         }
90
91         auto get_line_no_throw = [&f, ratings_file]() -> optional<string> {
92                 char buffer[512];
93                 char* r = f.gets(buffer, sizeof(buffer));
94                 if (r == 0) {
95                         return {};
96                 }
97                 string a = buffer;
98                 trim(a);
99                 return a;
100         };
101
102         auto get_line = [ratings_file, &get_line_no_throw]() {
103                 auto line = get_line_no_throw();
104                 if (!line) {
105                         throw FileError("Bad ratings file", ratings_file, -1);
106                 }
107                 return *line;
108         };
109
110         optional<string> agency;
111
112         while (!f.eof()) {
113                 if (!agency) {
114                         agency = get_line();
115                 }
116                 auto name = get_line();
117                 auto country_and_region_names = get_line();
118                 auto country_code = get_line();
119
120                 RatingSystem system(*agency, name, country_and_region_names, country_code);
121                 while (!f.eof()) {
122                         auto rating = get_line_no_throw();
123                         if (!rating) {
124                                 /* End of the file */
125                                 break;
126                         }
127                         if (rating->substr(0, 4) == "http") {
128                                 /* End of the system */
129                                 agency = rating;
130                                 break;
131                         }
132                         system.ratings.push_back(dcp::Rating(*agency, *rating));
133                 }
134
135                 rating_systems_list.push_back(system);
136         }
137 }
138