Bv2.1 7.2.3: Check that subtitle <StartTime> exists and is 0.
[libdcp.git] / src / language_tag.h
1 /*
2     Copyright (C) 2020 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 #ifndef LIBDCP_LANGUAGE_TAG_H
35 #define LIBDCP_LANGUAGE_TAG_H
36
37
38 #include <boost/filesystem.hpp>
39 #include <boost/optional.hpp>
40 #include <string>
41 #include <vector>
42
43
44 namespace dcp {
45
46
47 class LanguageTag
48 {
49 public:
50         std::string to_string () const;
51         std::string description () const;
52
53         struct SubtagData
54         {
55                 SubtagData () {}
56
57                 SubtagData (std::string subtag_, std::string description_)
58                         : subtag(subtag_)
59                         , description(description_)
60                 {}
61
62                 std::string subtag;
63                 std::string description;
64
65                 bool operator== (SubtagData const& other) {
66                         return subtag == other.subtag && description == other.description;
67                 }
68         };
69
70         enum SubtagType
71         {
72                 LANGUAGE,
73                 SCRIPT,
74                 REGION,
75                 VARIANT,
76                 EXTLANG,
77         };
78
79         class Subtag
80         {
81         public:
82                 virtual ~Subtag () {}
83
84                 std::string subtag () const {
85                         return _subtag;
86                 }
87
88                 virtual SubtagType type () const = 0;
89
90                 bool operator== (Subtag const& other) {
91                         return _subtag == other._subtag;
92                 }
93
94         protected:
95                 Subtag (std::string subtag, SubtagType type);
96
97         private:
98                 std::string _subtag;
99         };
100
101         class LanguageSubtag : public Subtag
102         {
103         public:
104                 LanguageSubtag (std::string subtag)
105                         : Subtag(subtag, LANGUAGE) {}
106                 LanguageSubtag (char const* subtag)
107                         : Subtag(subtag, LANGUAGE) {}
108
109                 SubtagType type () const {
110                         return LANGUAGE;
111                 }
112         };
113
114         class ScriptSubtag : public Subtag
115         {
116         public:
117                 ScriptSubtag (std::string subtag)
118                         : Subtag(subtag, SCRIPT) {}
119                 ScriptSubtag (char const* subtag)
120                         : Subtag(subtag, SCRIPT) {}
121
122                 SubtagType type () const {
123                         return SCRIPT;
124                 }
125         };
126
127         class RegionSubtag : public Subtag
128         {
129         public:
130                 RegionSubtag (std::string subtag)
131                         : Subtag(subtag, REGION) {}
132                 RegionSubtag (char const* subtag)
133                         : Subtag(subtag, REGION) {}
134
135                 SubtagType type () const {
136                         return REGION;
137                 }
138         };
139
140         class VariantSubtag : public Subtag
141         {
142         public:
143                 VariantSubtag (std::string subtag)
144                         : Subtag(subtag, VARIANT) {}
145                 VariantSubtag (char const* subtag)
146                         : Subtag(subtag, VARIANT) {}
147
148                 SubtagType type () const {
149                         return VARIANT;
150                 }
151
152                 bool operator== (VariantSubtag const& other) const;
153                 bool operator< (VariantSubtag const& other) const;
154         };
155
156
157         class ExtlangSubtag : public Subtag
158         {
159         public:
160                 ExtlangSubtag (std::string subtag)
161                         : Subtag(subtag, EXTLANG) {}
162                 ExtlangSubtag (char const* subtag)
163                         : Subtag(subtag, EXTLANG) {}
164
165                 SubtagType type () const {
166                         return EXTLANG;
167                 }
168
169                 bool operator== (ExtlangSubtag const& other) const;
170                 bool operator< (ExtlangSubtag const& other) const;
171         };
172
173         LanguageTag () {}
174         explicit LanguageTag (std::string tag);
175
176         boost::optional<LanguageSubtag> language() const {
177                 return _language;
178         }
179
180         void set_language (LanguageSubtag language);
181
182         boost::optional<ScriptSubtag> script() const {
183                 return _script;
184         }
185
186         void set_script (ScriptSubtag script);
187
188         boost::optional<RegionSubtag> region() const {
189                 return _region;
190         }
191
192         void set_region (RegionSubtag region);
193
194         std::vector<VariantSubtag> variants() const {
195                 return _variants;
196         }
197
198         void set_variants (std::vector<VariantSubtag> variants);
199         void add_variant (VariantSubtag variant);
200
201         std::vector<ExtlangSubtag> extlangs() const {
202                 return _extlangs;
203         }
204
205         void set_extlangs (std::vector<ExtlangSubtag> extlangs);
206         void add_extlang (ExtlangSubtag extlang);
207
208         std::vector<std::pair<SubtagType, SubtagData> > subtags () const;
209
210         static std::vector<SubtagData> const& get_all (SubtagType type);
211         static std::string subtag_type_name (SubtagType type);
212
213         static boost::optional<std::string> get_subtag_description (SubtagType, std::string subtag);
214         static boost::optional<SubtagData > get_subtag_data (SubtagType, std::string subtag);
215
216         template <class T>
217         static boost::optional<std::string> get_subtag_description (T s) {
218                 return get_subtag_description (s.type(), s.subtag());
219         }
220
221         template <class T>
222         static boost::optional<SubtagData> get_subtag_data (T s) {
223                 return get_subtag_data (s.type(), s.subtag());
224         }
225
226 private:
227
228         boost::optional<LanguageSubtag> _language;
229         boost::optional<ScriptSubtag> _script;
230         boost::optional<RegionSubtag> _region;
231         std::vector<VariantSubtag> _variants;
232         std::vector<ExtlangSubtag> _extlangs;
233 };
234
235 extern bool operator==(dcp::LanguageTag const& a, dcp::LanguageTag const& b);
236 extern std::ostream& operator<<(std::ostream& os, dcp::LanguageTag const& tag);
237
238
239 extern void load_language_tag_lists (boost::filesystem::path tags_directory);
240
241 }
242
243 #endif