Basics of allowing custom filenames for DCP components.
authorCarl Hetherington <cth@carlh.net>
Fri, 29 Jul 2016 14:53:26 +0000 (15:53 +0100)
committerCarl Hetherington <cth@carlh.net>
Fri, 29 Jul 2016 14:53:26 +0000 (15:53 +0100)
src/dcp.cc
src/dcp.h
src/filename_format.cc [new file with mode: 0644]
src/filename_format.h [new file with mode: 0644]
src/name_format.cc [new file with mode: 0644]
src/name_format.h [new file with mode: 0644]
src/wscript

index d2fc974536a29e4dc44ca16895c8691725b0a824..18b06c557fa5b3152cc13118a37b457d503a5dd2 100644 (file)
@@ -303,10 +303,10 @@ DCP::add (DecryptedKDM const & kdm)
 }
 
 boost::filesystem::path
-DCP::write_pkl (Standard standard, string pkl_uuid, XMLMetadata metadata, shared_ptr<const CertificateChain> signer) const
+DCP::write_pkl (string file, Standard standard, string pkl_uuid, XMLMetadata metadata, shared_ptr<const CertificateChain> signer) const
 {
        boost::filesystem::path p = _directory;
-       p /= String::compose ("pkl_%1.xml", pkl_uuid);
+       p /= file;
 
        xmlpp::Document doc;
        xmlpp::Element* pkl;
@@ -458,16 +458,22 @@ void
 DCP::write_xml (
        Standard standard,
        XMLMetadata metadata,
-       shared_ptr<const CertificateChain> signer
+       shared_ptr<const CertificateChain> signer,
+       FilenameFormat filename_format
        )
 {
        BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
-               string const filename = "cpl_" + i->id() + ".xml";
-               i->write_xml (_directory / filename, standard, signer);
+               NameFormat::Map values;
+               values["type"] = "cpl";
+               values["id"] = i->id();
+               i->write_xml (_directory / (filename_format.get(values) + ".xml"), standard, signer);
        }
 
        string const pkl_uuid = make_uuid ();
-       boost::filesystem::path const pkl_path = write_pkl (standard, pkl_uuid, metadata, signer);
+       NameFormat::Map values;
+       values["type"] = "pkl";
+       values["id"] = pkl_uuid;
+       boost::filesystem::path const pkl_path = write_pkl (filename_format.get(values) + ".xml", standard, pkl_uuid, metadata, signer);
 
        write_volindex (standard);
        write_assetmap (standard, pkl_uuid, boost::filesystem::file_size (pkl_path), metadata);
index 7c318622ae2a30478284334618f7b15de951561a..cc87d0d14a31bc7a8d7996630e60d134bfa8720f 100644 (file)
--- a/src/dcp.h
+++ b/src/dcp.h
@@ -41,6 +41,7 @@
 #include "types.h"
 #include "certificate.h"
 #include "metadata.h"
+#include "filename_format.h"
 #include <boost/shared_ptr.hpp>
 #include <boost/signals2.hpp>
 #include <string>
@@ -111,7 +112,8 @@ public:
        void write_xml (
                Standard standard,
                XMLMetadata metadata = XMLMetadata (),
-               boost::shared_ptr<const CertificateChain> signer = boost::shared_ptr<const CertificateChain> ()
+               boost::shared_ptr<const CertificateChain> signer = boost::shared_ptr<const CertificateChain> (),
+               FilenameFormat filename_format = FilenameFormat("%t_%i")
        );
 
        void resolve_refs (std::list<boost::shared_ptr<Asset> > assets);
@@ -127,6 +129,7 @@ private:
         *  @param pkl_uuid UUID to use.
         */
        boost::filesystem::path write_pkl (
+               std::string file,
                Standard standard,
                std::string pkl_uuid,
                XMLMetadata metadata,
diff --git a/src/filename_format.cc b/src/filename_format.cc
new file mode 100644 (file)
index 0000000..37fc12a
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+    Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
+
+    This file is part of libdcp.
+
+    libdcp is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    libdcp is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
+
+    In addition, as a special exception, the copyright holders give
+    permission to link the code of portions of this program with the
+    OpenSSL library under certain conditions as described in each
+    individual source file, and distribute linked combinations
+    including the two.
+
+    You must obey the GNU General Public License in all respects
+    for all of the code used other than OpenSSL.  If you modify
+    file(s) with this exception, you may extend this exception to your
+    version of the file(s), but you are not obligated to do so.  If you
+    do not wish to do so, delete this exception statement from your
+    version.  If you delete this exception statement from all source
+    files in the program, then also delete it here.
+*/
+
+#include "filename_format.h"
+
+using std::string;
+using namespace dcp;
+
+FilenameFormat::FilenameFormat (string specification)
+       : NameFormat (specification)
+{
+       add ("type", 't', "asset type (j2c/pcm/sub/pkl/cpl)");
+       add ("id", 'i', "unique ID");
+}
diff --git a/src/filename_format.h b/src/filename_format.h
new file mode 100644 (file)
index 0000000..836c643
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+    Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
+
+    This file is part of libdcp.
+
+    libdcp is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    libdcp is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
+
+    In addition, as a special exception, the copyright holders give
+    permission to link the code of portions of this program with the
+    OpenSSL library under certain conditions as described in each
+    individual source file, and distribute linked combinations
+    including the two.
+
+    You must obey the GNU General Public License in all respects
+    for all of the code used other than OpenSSL.  If you modify
+    file(s) with this exception, you may extend this exception to your
+    version of the file(s), but you are not obligated to do so.  If you
+    do not wish to do so, delete this exception statement from your
+    version.  If you delete this exception statement from all source
+    files in the program, then also delete it here.
+*/
+
+#ifndef LIBDCP_FILENAME_FORMAT
+#define LIBDCP_FILENAME_FORMAT
+
+#include "name_format.h"
+
+namespace dcp {
+
+class FilenameFormat : public NameFormat
+{
+public:
+       FilenameFormat () {}
+       FilenameFormat (std::string specification);
+};
+
+}
+
+#endif
diff --git a/src/name_format.cc b/src/name_format.cc
new file mode 100644 (file)
index 0000000..1dc1135
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+    Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
+
+    This file is part of libdcp.
+
+    libdcp is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    libdcp is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
+
+    In addition, as a special exception, the copyright holders give
+    permission to link the code of portions of this program with the
+    OpenSSL library under certain conditions as described in each
+    individual source file, and distribute linked combinations
+    including the two.
+
+    You must obey the GNU General Public License in all respects
+    for all of the code used other than OpenSSL.  If you modify
+    file(s) with this exception, you may extend this exception to your
+    version of the file(s), but you are not obligated to do so.  If you
+    do not wish to do so, delete this exception statement from your
+    version.  If you delete this exception statement from all source
+    files in the program, then also delete it here.
+*/
+
+#include "name_format.h"
+#include <boost/optional.hpp>
+#include <boost/foreach.hpp>
+
+using std::string;
+using std::map;
+using boost::optional;
+using namespace dcp;
+
+static char
+filter (char c)
+{
+       if (c == '/' || c == ':') {
+               c = '-';
+       } else if (c == ' ') {
+               c = '_';
+       }
+
+       return c;
+}
+
+static string
+filter (string c)
+{
+       string o;
+
+       for (size_t i = 0; i < c.length(); ++i) {
+               o += filter (c[i]);
+       }
+
+       return o;
+}
+
+void
+NameFormat::add (string name, char placeholder, string title)
+{
+       _components.push_back (Component (name, placeholder, title));
+}
+
+optional<NameFormat::Component>
+NameFormat::component_by_placeholder (char p) const
+{
+       BOOST_FOREACH (Component const & i, _components) {
+               if (i.placeholder == p) {
+                       return i;
+               }
+       }
+
+       return optional<Component> ();
+}
+
+string
+NameFormat::get (Map values) const
+{
+       string result;
+       for (size_t i = 0; i < _specification.length(); ++i) {
+               bool done = false;
+               if (_specification[i] == '%' && (i < _specification.length() - 1)) {
+                       optional<Component> c = component_by_placeholder (_specification[i + 1]);
+                       if (c) {
+                               result += filter (values[c->name]);
+                               ++i;
+                               done = true;
+                       }
+               }
+
+               if (!done) {
+                       result += filter (_specification[i]);
+               }
+       }
+
+       return result;
+}
+
+bool
+dcp::operator== (NameFormat const & a, NameFormat const & b)
+{
+       return a.specification() == b.specification();
+}
diff --git a/src/name_format.h b/src/name_format.h
new file mode 100644 (file)
index 0000000..c2a7128
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+    Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
+
+    This file is part of libdcp.
+
+    libdcp is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    libdcp is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
+
+    In addition, as a special exception, the copyright holders give
+    permission to link the code of portions of this program with the
+    OpenSSL library under certain conditions as described in each
+    individual source file, and distribute linked combinations
+    including the two.
+
+    You must obey the GNU General Public License in all respects
+    for all of the code used other than OpenSSL.  If you modify
+    file(s) with this exception, you may extend this exception to your
+    version of the file(s), but you are not obligated to do so.  If you
+    do not wish to do so, delete this exception statement from your
+    version.  If you delete this exception statement from all source
+    files in the program, then also delete it here.
+*/
+
+#ifndef LIBDCP_NAME_FORMAT
+#define LIBDCP_NAME_FORMAT
+
+#include <boost/optional.hpp>
+#include <map>
+#include <list>
+
+namespace dcp {
+
+class NameFormat
+{
+public:
+       struct Component
+       {
+               Component (std::string name_, char placeholder_, std::string title_)
+                       : name (name_)
+                       , placeholder (placeholder_)
+                       , title (title_)
+               {}
+
+               std::string name;
+               char placeholder;
+               std::string title;
+       };
+
+       std::list<Component> components () const {
+               return _components;
+       }
+
+       std::string specification () const {
+               return _specification;
+       }
+
+       void set_specification (std::string specification) {
+               _specification = specification;
+       }
+
+       typedef std::map<std::string, std::string> Map;
+
+       std::string get (Map) const;
+
+protected:
+       NameFormat () {}
+
+       NameFormat (std::string specification)
+               : _specification (specification)
+       {}
+
+       void add (std::string name, char placeholder, std::string title);
+
+private:
+       boost::optional<NameFormat::Component> component_by_placeholder (char p) const;
+
+       std::list<Component> _components;
+       std::string _specification;
+};
+
+extern bool operator== (NameFormat const & a, NameFormat const & b);
+
+}
+
+#endif
index f9f769db3c8e66159569174e2c53dc4a323d5ef2..43fc0e69b56040ce04d4197d04959be3b1c38ee9 100644 (file)
@@ -51,6 +51,7 @@ def build(bld):
              encrypted_kdm.cc
              exceptions.cc
              file.cc
+             filename_format.cc
              font_asset.cc
              font_node.cc
              gamma_transfer_function.cc
@@ -66,6 +67,7 @@ def build(bld):
              mono_picture_asset_writer.cc
              mono_picture_frame.cc
              mxf.cc
+             name_format.cc
              object.cc
              openjpeg_image.cc
              picture_asset.cc
@@ -119,6 +121,7 @@ def build(bld):
               decrypted_kdm_key.h
               encrypted_kdm.h
               exceptions.h
+              filename_format.h
               font_asset.h
               gamma_transfer_function.h
               interop_load_font_node.h
@@ -133,6 +136,7 @@ def build(bld):
               mono_picture_frame.h
               modified_gamma_transfer_function.h
               mxf.h
+              name_format.h
               object.h
               openjpeg_image.h
               picture_asset.h