Make careful_string_filter a little better and use it for KDM filenames. v2.13.133
authorCarl Hetherington <cth@carlh.net>
Fri, 22 Mar 2019 00:09:07 +0000 (00:09 +0000)
committerCarl Hetherington <cth@carlh.net>
Fri, 22 Mar 2019 00:09:07 +0000 (00:09 +0000)
src/lib/cinema_kdms.cc
src/lib/screen_kdm.cc
src/lib/util.cc
test/util_test.cc

index 2e1d03b404bc5accc45d35f8ee71e0d487ab4738..558aee504bca2998aee932681b2b04b4cf7047e2 100644 (file)
@@ -67,7 +67,7 @@ CinemaKDMs::make_zip_file (boost::filesystem::path zip_file, dcp::NameFormat nam
 
                name_values['s'] = i.screen->name;
                name_values['i'] = i.kdm.id ();
-               string const name = name_format.get(name_values, ".xml");
+               string const name = careful_string_filter(name_format.get(name_values, ".xml"));
                if (zip_add (zip, name.c_str(), source) == -1) {
                        throw runtime_error ("failed to add KDM to ZIP archive");
                }
index df74cc7eff823b47b760a6594e9c5e3c15c4f002..5b7692469a8d68a3be03929f00e980b7f586d27d 100644 (file)
@@ -65,7 +65,7 @@ ScreenKDM::write_files (
                name_values['c'] = i.screen->cinema ? i.screen->cinema->name : "";
                name_values['s'] = i.screen->name;
                name_values['i'] = i.kdm.id ();
-               boost::filesystem::path out = directory / (name_format.get(name_values, ".xml"));
+               boost::filesystem::path out = directory / careful_string_filter(name_format.get(name_values, ".xml"));
                if (!boost::filesystem::exists (out) || confirm_overwrite (out)) {
                        i.kdm.as_xml (out);
                        ++written;
index b5265519413976328254749d63cb5236caa200cd..c2089146431614bda8957b4b26e74434653f6414 100644 (file)
@@ -62,6 +62,7 @@ extern "C" {
 #include <boost/range/algorithm/replace_if.hpp>
 #include <boost/thread.hpp>
 #include <boost/filesystem.hpp>
+#include <boost/locale.hpp>
 #ifdef DCPOMATIC_WINDOWS
 #include <boost/locale.hpp>
 #include <dbghelp.h>
@@ -80,6 +81,7 @@ extern "C" {
 #include "i18n.h"
 
 using std::string;
+using std::wstring;
 using std::setfill;
 using std::ostream;
 using std::endl;
@@ -743,15 +745,34 @@ careful_string_filter (string s)
           Safety first and all that.
        */
 
+       wstring ws = boost::locale::conv::utf_to_utf<wchar_t>(s);
+
        string out;
        string const allowed = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_%.+";
-       for (size_t i = 0; i < s.size(); ++i) {
-               if (allowed.find (s[i]) != string::npos) {
-                       out += s[i];
+       for (size_t i = 0; i < ws.size(); ++i) {
+
+               wchar_t c = ws[i];
+
+               /* Remove some accents */
+               if (wstring(L"áàâ").find(c) != string::npos) {
+                       c = 'a';
+               }
+               if (wstring(L"éèêë").find(c) != string::npos) {
+                       c = 'e';
+               }
+               if (wstring(L"ö").find(c) != string::npos) {
+                       c = 'o';
+               }
+               if (wstring(L"ü").find(c) != string::npos) {
+                       c = 'u';
+               }
+
+               if (allowed.find(c) != string::npos) {
+                       out += c;
                }
        }
 
-       return out;
+       return boost::locale::conv::utf_to_utf<char>(out);
 }
 
 /** @param mapped List of mapped audio channels from a Film.
index d8cb61fdc30677d2208e52ca232554f60bd1762b..1e13efa82f456959d4cf538c70dd809fbf7c657f 100644 (file)
@@ -119,3 +119,12 @@ BOOST_AUTO_TEST_CASE (swaroop_chain_test)
        BOOST_CHECK (cc->root_to_leaf() == back->root_to_leaf());
 }
 #endif
+
+BOOST_AUTO_TEST_CASE (careful_string_filter_test)
+{
+       BOOST_CHECK_EQUAL ("hello_world", careful_string_filter("hello_world"));
+       BOOST_CHECK_EQUAL ("hello_world", careful_string_filter("héllo_world"));
+       BOOST_CHECK_EQUAL ("hello_world", careful_string_filter("héllo_wörld"));
+       BOOST_CHECK_EQUAL ("hello_world", careful_string_filter("héllo_wörld"));
+       BOOST_CHECK_EQUAL ("hello_world_a", careful_string_filter("héllo_wörld_à"));
+}