Small bits of pre-release tidying.
[libdcp.git] / src / util.cc
index ba9e6fa227c51276dd638c4aa843e6e4026f1769..2aaeddb57e5501ee9c01769c302a08b96ddf7da3 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,7 +56,6 @@
 #include <openssl/sha.h>
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
-#include <boost/foreach.hpp>
 #include <stdexcept>
 #include <iostream>
 #include <iomanip>
@@ -65,17 +65,24 @@ using std::wstring;
 using std::cout;
 using std::min;
 using std::max;
-using std::list;
 using std::setw;
 using std::setfill;
 using std::ostream;
-using boost::shared_ptr;
+using std::shared_ptr;
+using std::vector;
 using boost::shared_array;
 using boost::optional;
 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.
  */
@@ -90,11 +97,11 @@ dcp::make_uuid ()
 }
 
 string
-dcp::make_digest (Data data)
+dcp::make_digest (ArrayData data)
 {
        SHA_CTX sha;
        SHA1_Init (&sha);
-       SHA1_Update (&sha, data.data().get(), data.size());
+       SHA1_Update (&sha, data.data(), data.size());
        byte_t byte_buffer[SHA_DIGEST_LENGTH];
        SHA1_Final (byte_buffer, &sha);
        char digest[64];
@@ -164,11 +171,15 @@ 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");
@@ -176,7 +187,7 @@ dcp::init ()
 
 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
        if (xmlSecCryptoDLLoadLibrary(BAD_CAST "openssl") < 0) {
-               throw MiscError (String::compose("unable to load openssl xmlsec-crypto library"));
+               throw MiscError ("unable to load openssl xmlsec-crypto library");
        }
 #endif
 
@@ -189,6 +200,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
@@ -364,8 +388,8 @@ 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);
+       for (auto n: element->get_children()) {
+               auto e = dynamic_cast<xmlpp::Element*>(n);
                if (e) {
                        element->add_child_text_before (e, "\n" + spaces(initial + 2));
                        indent (e, initial + 2);
@@ -415,7 +439,7 @@ dcp::day_greater_than_or_equal (LocalTime a, LocalTime b)
  *  not in \ref existing.
  */
 string
-dcp::unique_string (list<string> existing, string base)
+dcp::unique_string (vector<string> existing, string base)
 {
        int const max_tries = existing.size() + 1;
        for (int i = 0; i < max_tries; ++i) {
@@ -427,3 +451,19 @@ dcp::unique_string (list<string> existing, string base)
 
        DCP_ASSERT (false);
 }
+
+
+ASDCPErrorSuspender::ASDCPErrorSuspender ()
+       : _old (Kumu::DefaultLogSink())
+{
+       _sink = new Kumu::EntryListLogSink(_log);
+       Kumu::SetDefaultLogSink (_sink);
+}
+
+
+ASDCPErrorSuspender::~ASDCPErrorSuspender ()
+{
+       Kumu::SetDefaultLogSink (&_old);
+       delete _sink;
+}
+