libdcp
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 "rating.h"
37 #include "util.h"
38 #include <libcxml/cxml.h>
39 #include <boost/algorithm/string.hpp>
40 
41 
42 using std::string;
43 using std::vector;
44 using boost::algorithm::trim;
45 using boost::optional;
46 using namespace dcp;
47 
48 
49 static vector<RatingSystem> rating_systems_list;
50 
51 
52 Rating::Rating (cxml::ConstNodePtr node)
53  : agency(node->string_child("Agency"))
54  , label(node->string_child("Label"))
55 {
56  node->done ();
57 }
58 
59 
60 void
61 Rating::as_xml (xmlpp::Element* parent) const
62 {
63  parent->add_child("Agency")->add_child_text(agency);
64  parent->add_child("Label")->add_child_text(label);
65 }
66 
67 
68 bool
69 dcp::operator== (Rating const & a, Rating const & b)
70 {
71  return a.agency == b.agency && a.label == b.label;
72 }
73 
74 
75 vector<RatingSystem>
76 dcp::rating_systems()
77 {
78  return rating_systems_list;
79 }
80 
81 
82 void
83 dcp::load_rating_list(boost::filesystem::path ratings_file)
84 {
85  auto f = fopen_boost (ratings_file, "r");
86  if (!f) {
87  throw FileError ("Could not open ratings file", ratings_file, errno);
88  }
89 
90  auto get_line_no_throw = [f, ratings_file]() -> optional<string> {
91  char buffer[512];
92  char* r = fgets(buffer, sizeof(buffer), f);
93  if (r == 0) {
94  return {};
95  }
96  string a = buffer;
97  trim(a);
98  return a;
99  };
100 
101  auto get_line = [ratings_file, &get_line_no_throw]() {
102  auto line = get_line_no_throw();
103  if (!line) {
104  throw FileError("Bad ratings file", ratings_file, -1);
105  }
106  return *line;
107  };
108 
109  optional<string> agency;
110 
111  while (!feof(f)) {
112  if (!agency) {
113  agency = get_line();
114  }
115  auto name = get_line();
116  auto country_and_region_names = get_line();
117  auto country_code = get_line();
118 
119  RatingSystem system(*agency, name, country_and_region_names, country_code);
120  while (!feof(f)) {
121  auto rating = get_line_no_throw();
122  if (!rating) {
123  /* End of the file */
124  break;
125  }
126  if (rating->substr(0, 4) == "http") {
127  /* End of the system */
128  agency = rating;
129  break;
130  }
131  system.ratings.push_back(dcp::Rating(*agency, *rating));
132  }
133 
134  rating_systems_list.push_back(system);
135  }
136 }
137 
An exception related to a file.
Definition: exceptions.h:56
std::string label
Definition: rating.h:65
std::string agency
Definition: rating.h:63
Exceptions thrown by libdcp.
Namespace for everything in libdcp.
Definition: array_data.h:50
FILE * fopen_boost(boost::filesystem::path, std::string)
Definition: util.cc:232
Utility methods and classes.