Replace slightly weird add_font_assets() API.
[libdcp.git] / src / util.h
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 /** @file  src/util.h
36  *  @brief Utility methods and classes
37  */
38
39
40 #ifndef LIBDCP_UTIL_H
41 #define LIBDCP_UTIL_H
42
43
44 #include "array_data.h"
45 #include "local_time.h"
46 #include "types.h"
47 LIBDCP_DISABLE_WARNINGS
48 #include <asdcp/KM_log.h>
49 LIBDCP_ENABLE_WARNINGS
50 #include <boost/filesystem.hpp>
51 #include <boost/function.hpp>
52 #include <boost/optional.hpp>
53 #include <memory>
54 #include <string>
55 #include <stdint.h>
56
57
58 #define LIBDCP_UNUSED(x) (void)(x)
59
60
61 namespace xmlpp {
62         class Element;
63         class Node;
64 }
65
66
67 namespace dcp {
68
69
70 class CertificateChain;
71 class GammaLUT;
72 class OpenJPEGImage;
73
74
75 extern std::string make_uuid ();
76
77 /** Create a digest for a file
78  *  @param filename File name
79  *  @param progress Optional progress reporting function.  The function will be called
80  *  with a progress value between 0 and 1
81  *  @return Digest
82  */
83 extern std::string make_digest (boost::filesystem::path filename, boost::function<void (float)>);
84
85 extern std::string make_digest (ArrayData data);
86
87 extern bool ids_equal (std::string a, std::string b);
88 extern std::string remove_urn_uuid (std::string raw);
89
90 /** Set up various bits that the library needs.  Should be called once
91  *  by client applications.
92  *
93  *  @param resources_directory Path to a directory containing the tags and xsd
94  *  directories from the source code; if none is specified libdcp will look
95  *  in the directory given by LIBDCP_RESOURCES or based on where the current
96  *  executable is.
97  */
98 extern void init (boost::optional<boost::filesystem::path> resources_directory = boost::optional<boost::filesystem::path>());
99
100 /** Decode a base64 string.  The base64 decode routine in KM_util.cpp
101  *  gives different values to both this and the command-line base64
102  *  for some inputs.  Not sure why.
103  *
104  *  @param in base64-encoded string
105  *  @param out Output buffer
106  *  @param out_length Length of output buffer
107  *  @return Number of characters written to the output buffer
108  */
109 extern int base64_decode (std::string const & in, unsigned char* out, int out_length);
110
111 extern boost::optional<boost::filesystem::path> relative_to_root (boost::filesystem::path root, boost::filesystem::path file);
112
113 extern std::string file_to_string (boost::filesystem::path, uintmax_t max_length = 1048576);
114
115 /** @param key RSA private key in PEM format (optionally with -----BEGIN... / -----END...)
116  *  @return SHA1 fingerprint of key
117  */
118 extern std::string private_key_fingerprint (std::string key);
119 extern xmlpp::Node* find_child (xmlpp::Node const * node, std::string name);
120 extern std::string openjpeg_version();
121 extern std::string spaces (int n);
122 extern void indent (xmlpp::Element* element, int initial);
123
124 /** @return true if the day represented by \ref a is less than or
125  *  equal to the one represented by \ref b, ignoring the time parts
126  */
127 extern bool day_less_than_or_equal (LocalTime a, LocalTime b);
128
129 /** @return true if the day represented by \ref a is greater than or
130  *  equal to the one represented by \ref b, ignoring the time parts
131  */
132 extern bool day_greater_than_or_equal (LocalTime a, LocalTime b);
133
134 /** Try quite hard to find a string which starts with \ref base and is
135  *  not in \ref existing
136  */
137 extern std::string unique_string (std::vector<std::string> existing, std::string base);
138
139 extern ASDCP::Dictionary const* asdcp_smpte_dict;
140
141 extern boost::filesystem::path directory_containing_executable ();
142 extern boost::filesystem::path resources_directory ();
143
144
145 class ASDCPErrorSuspender
146 {
147 public:
148         ASDCPErrorSuspender();
149         ~ASDCPErrorSuspender();
150
151 private:
152         Kumu::LogEntryList _log;
153         Kumu::ILogSink& _old;
154         Kumu::EntryListLogSink* _sink;
155 };
156
157
158 template <class From, class To>
159 void
160 add_to_container(To& container, From source)
161 {
162         std::copy(source.begin(), source.end(), std::back_inserter(container));
163 }
164
165
166 }
167
168
169 #endif