Tidying.
[libdcp.git] / src / util.h
1 /*
2     Copyright (C) 2012-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
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 "types.h"
46 #include "local_time.h"
47 #include <asdcp/KM_log.h>
48 #include <memory>
49 #include <boost/function.hpp>
50 #include <boost/filesystem.hpp>
51 #include <boost/optional.hpp>
52 #include <string>
53 #include <stdint.h>
54
55
56 #define LIBDCP_UNUSED(x) (void)(x)
57
58
59 namespace xmlpp {
60         class Element;
61         class Node;
62 }
63
64
65 namespace dcp {
66
67
68 class CertificateChain;
69 class GammaLUT;
70 class OpenJPEGImage;
71
72
73 extern std::string make_uuid ();
74
75 /** Create a digest for a file
76  *  @param filename File name
77  *  @param progress Optional progress reporting function.  The function will be called
78  *  with a progress value between 0 and 1
79  *  @return Digest
80  */
81 extern std::string make_digest (boost::filesystem::path filename, boost::function<void (float)>);
82
83 extern std::string make_digest (ArrayData data);
84
85 /** @param s A string
86  *  @return true if the string contains only space, newline or tab characters, or is empty
87  */
88 extern bool empty_or_white_space (std::string s);
89
90 extern bool ids_equal (std::string a, std::string b);
91 extern std::string remove_urn_uuid (std::string raw);
92
93 /** Set up various bits that the library needs.  Should be called once
94  *  by client applications.
95  *
96  *  @param tags_directory Path to a copy of the tags directory from the source code;
97  *  if none is specified libdcp will look for a tags directory inside the environment
98  *  variable LIBDCP_SHARE_PREFIX or the LIBDCP_SHARE_PREFIX #defined during the build.
99  */
100 extern void init (boost::optional<boost::filesystem::path> tags_directory = boost::optional<boost::filesystem::path>());
101
102 /** Decode a base64 string.  The base64 decode routine in KM_util.cpp
103  *  gives different values to both this and the command-line base64
104  *  for some inputs.  Not sure why.
105  *
106  *  @param in base64-encoded string
107  *  @param out Output buffer
108  *  @param out_length Length of output buffer
109  *  @return Number of characters written to the output buffer
110  */
111 extern int base64_decode (std::string const & in, unsigned char* out, int out_length);
112
113 extern boost::optional<boost::filesystem::path> relative_to_root (boost::filesystem::path root, boost::filesystem::path file);
114
115 /** @param p Path to open
116  *  @param t mode flags, as for fopen(3)
117  *  @return FILE pointer or 0 on error
118  *
119  *  Apparently there is no way to create an ofstream using a UTF-8
120  *  filename under Windows.  We are hence reduced to using fopen
121  *  with this wrapper.
122  */
123 extern FILE * fopen_boost (boost::filesystem::path, std::string);
124
125 extern std::string file_to_string (boost::filesystem::path, uintmax_t max_length = 1048576);
126
127 /** @param key RSA private key in PEM format (optionally with -----BEGIN... / -----END...)
128  *  @return SHA1 fingerprint of key
129  */
130 extern std::string private_key_fingerprint (std::string key);
131 extern xmlpp::Node* find_child (xmlpp::Node const * node, std::string name);
132 extern std::string openjpeg_version();
133 extern std::string spaces (int n);
134 extern void indent (xmlpp::Element* element, int initial);
135
136 /** @return true if the day represented by \ref a is less than or
137  *  equal to the one represented by \ref b, ignoring the time parts
138  */
139 extern bool day_less_than_or_equal (LocalTime a, LocalTime b);
140
141 /** @return true if the day represented by \ref a is greater than or
142  *  equal to the one represented by \ref b, ignoring the time parts
143  */
144 extern bool day_greater_than_or_equal (LocalTime a, LocalTime b);
145
146 /** Try quite hard to find a string which starts with \ref base and is
147  *  not in \ref existing
148  */
149 extern std::string unique_string (std::vector<std::string> existing, std::string base);
150
151 extern ASDCP::Dictionary const* asdcp_smpte_dict;
152
153
154 class ASDCPErrorSuspender
155 {
156 public:
157         ASDCPErrorSuspender();
158         ~ASDCPErrorSuspender();
159
160 private:
161         Kumu::LogEntryList _log;
162         Kumu::ILogSink& _old;
163         Kumu::EntryListLogSink* _sink;
164 };
165
166
167 }
168
169 #endif