Missing error checking on writing KDMs as XML.
[libdcp.git] / src / util.cc
index df79fb26e1a3dc52dba989c3718889b0fc48715f..211f03f8ccda87742a7532c1ad03368026bcb555 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
 
     This file is part of libdcp.
 
@@ -36,6 +36,7 @@
  */
 
 #include "util.h"
+#include "language_tag.h"
 #include "exceptions.h"
 #include "types.h"
 #include "certificate.h"
@@ -55,6 +56,7 @@
 #include <openssl/sha.h>
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
+#include <boost/foreach.hpp>
 #include <stdexcept>
 #include <iostream>
 #include <iomanip>
@@ -75,6 +77,13 @@ using boost::function;
 using boost::algorithm::trim;
 using namespace dcp;
 
+
+/* Some ASDCP objects store this as a *&, for reasons which are not
+ * at all clear, so we have to keep this around forever.
+ */
+ASDCP::Dictionary const* dcp::asdcp_smpte_dict = 0;
+
+
 /** Create a UUID.
  *  @return UUID.
  */
@@ -163,19 +172,23 @@ dcp::empty_or_white_space (string s)
        return true;
 }
 
-/** Set up various bits that the library needs.  Should be called one
+/** Set up various bits that the library needs.  Should be called once
  *  by client applications.
+ *
+ *  @param tags_directory Path to a copy of the tags directory from the source code;
+ *  if none is specified libdcp will look for a tags directory inside the environment
+ *  variable LIBDCP_SHARE_PREFIX or the LIBDCP_SHARE_PREFIX #defined during the build.
  */
 void
-dcp::init ()
+dcp::init (optional<boost::filesystem::path> tags_directory)
 {
        if (xmlSecInit() < 0) {
                throw MiscError ("could not initialise xmlsec");
        }
 
 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
-       if (xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 0) {
-               throw MiscError ("unable to load default xmlsec-crypto library");
+       if (xmlSecCryptoDLLoadLibrary(BAD_CAST "openssl") < 0) {
+               throw MiscError ("unable to load openssl xmlsec-crypto library");
        }
 #endif
 
@@ -188,6 +201,19 @@ dcp::init ()
        }
 
        OpenSSL_add_all_algorithms();
+
+       asdcp_smpte_dict = &ASDCP::DefaultSMPTEDict();
+
+       if (!tags_directory) {
+               char* prefix = getenv("LIBDCP_SHARE_PREFIX");
+               if (prefix) {
+                       tags_directory = boost::filesystem::path(prefix) / "tags";
+               } else {
+                       tags_directory = LIBDCP_SHARE_PREFIX "/tags";
+               }
+       }
+
+       load_language_tag_lists (*tags_directory);
 }
 
 /** Decode a base64 string.  The base64 decode routine in KM_util.cpp
@@ -348,3 +374,97 @@ dcp::openjpeg_version ()
 {
        return opj_version ();
 }
+
+string
+dcp::spaces (int n)
+{
+       string s = "";
+       for (int i = 0; i < n; ++i) {
+               s += " ";
+       }
+       return s;
+}
+
+void
+dcp::indent (xmlpp::Element* element, int initial)
+{
+       xmlpp::Node* last = 0;
+       BOOST_FOREACH (xmlpp::Node * n, element->get_children()) {
+               xmlpp::Element* e = dynamic_cast<xmlpp::Element*>(n);
+               if (e) {
+                       element->add_child_text_before (e, "\n" + spaces(initial + 2));
+                       indent (e, initial + 2);
+                       last = n;
+               }
+       }
+       if (last) {
+               element->add_child_text (last, "\n" + spaces(initial));
+       }
+}
+
+/** @return true if the day represented by \ref a is less than or
+ *  equal to the one represented by \ref b, ignoring the time parts.
+ */
+bool
+dcp::day_less_than_or_equal (LocalTime a, LocalTime b)
+{
+       if (a.year() != b.year()) {
+               return a.year() < b.year();
+       }
+
+       if (a.month() != b.month()) {
+               return a.month() < b.month();
+       }
+
+       return a.day() <= b.day();
+}
+
+/** @return true if the day represented by \ref a is greater than or
+ *  equal to the one represented by \ref b, ignoring the time parts.
+ */
+bool
+dcp::day_greater_than_or_equal (LocalTime a, LocalTime b)
+{
+       if (a.year() != b.year()) {
+               return a.year() > b.year();
+       }
+
+       if (a.month() != b.month()) {
+               return a.month() > b.month();
+       }
+
+       return a.day() >= b.day();
+}
+
+/** Try quite hard to find a string which starts with \ref base and is
+ *  not in \ref existing.
+ */
+string
+dcp::unique_string (list<string> existing, string base)
+{
+       int const max_tries = existing.size() + 1;
+       for (int i = 0; i < max_tries; ++i) {
+               string trial = String::compose("%1%2", base, i);
+               if (find(existing.begin(), existing.end(), trial) == existing.end()) {
+                       return trial;
+               }
+       }
+
+       DCP_ASSERT (false);
+}
+
+
+ASDCPErrorSuspender::ASDCPErrorSuspender ()
+       : _old (Kumu::DefaultLogSink())
+{
+       _sink = new Kumu::EntryListLogSink(_log);
+       Kumu::SetDefaultLogSink (_sink);
+}
+
+
+ASDCPErrorSuspender::~ASDCPErrorSuspender ()
+{
+       Kumu::SetDefaultLogSink (&_old);
+       delete _sink;
+}
+