diff options
| author | Carl Hetherington <cth@carlh.net> | 2022-03-01 17:06:26 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2022-03-03 21:58:31 +0100 |
| commit | e375fdddf7cc608eefdc054d74e90bb95d573172 (patch) | |
| tree | eca3e5b384efdafa184610fae0f30f1898076f98 /src/rating.cc | |
| parent | e61267217a8ca7e65ad42bf34b6488b92eca6389 (diff) | |
Add lists of ratings.v1.8.9
Diffstat (limited to 'src/rating.cc')
| -rw-r--r-- | src/rating.cc | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/rating.cc b/src/rating.cc index 2064c644..0ba9f2b8 100644 --- a/src/rating.cc +++ b/src/rating.cc @@ -32,13 +32,23 @@ */ +#include "exceptions.h" #include "rating.h" +#include "util.h" #include <libcxml/cxml.h> +#include <boost/algorithm/string.hpp> +using std::string; +using std::vector; +using boost::algorithm::trim; +using boost::optional; using namespace dcp; +static vector<RatingSystem> rating_systems_list; + + Rating::Rating (cxml::ConstNodePtr node) : agency(node->string_child("Agency")) , label(node->string_child("Label")) @@ -62,3 +72,66 @@ dcp::operator== (Rating const & a, Rating const & b) } +vector<RatingSystem> +dcp::rating_systems() +{ + return rating_systems_list; +} + + +void +dcp::load_rating_list(boost::filesystem::path ratings_file) +{ + auto f = fopen_boost (ratings_file, "r"); + if (!f) { + throw FileError ("Could not open ratings file", ratings_file, errno); + } + + auto get_line_no_throw = [f, ratings_file]() -> optional<string> { + char buffer[512]; + char* r = fgets(buffer, sizeof(buffer), f); + if (r == 0) { + return {}; + } + string a = buffer; + trim(a); + return a; + }; + + auto get_line = [f, ratings_file, &get_line_no_throw]() { + auto line = get_line_no_throw(); + if (!line) { + throw FileError("Bad ratings file", ratings_file, -1); + } + return *line; + }; + + optional<string> agency; + + while (!feof(f)) { + if (!agency) { + agency = get_line(); + } + auto name = get_line(); + auto country_and_region_names = get_line(); + auto country_code = get_line(); + + RatingSystem system(*agency, name, country_and_region_names, country_code); + while (!feof(f)) { + auto rating = get_line_no_throw(); + if (!rating) { + /* End of the file */ + break; + } + if (rating->substr(0, 4) == "http") { + /* End of the system */ + agency = rating; + break; + } + system.ratings.push_back(dcp::Rating(*agency, *rating)); + } + + rating_systems_list.push_back(system); + } +} + |
