From 137b2b1b440b5594b4d371c10884acd0a90df6bf Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Fri, 22 Mar 2019 00:09:07 +0000 Subject: [PATCH] Make careful_string_filter a little better and use it for KDM filenames. --- src/lib/cinema_kdms.cc | 2 +- src/lib/screen_kdm.cc | 2 +- src/lib/util.cc | 29 +++++++++++++++++++++++++---- test/util_test.cc | 9 +++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/lib/cinema_kdms.cc b/src/lib/cinema_kdms.cc index 2e1d03b40..558aee504 100644 --- a/src/lib/cinema_kdms.cc +++ b/src/lib/cinema_kdms.cc @@ -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"); } diff --git a/src/lib/screen_kdm.cc b/src/lib/screen_kdm.cc index df74cc7ef..5b7692469 100644 --- a/src/lib/screen_kdm.cc +++ b/src/lib/screen_kdm.cc @@ -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; diff --git a/src/lib/util.cc b/src/lib/util.cc index b52655194..c20891464 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -62,6 +62,7 @@ extern "C" { #include #include #include +#include #ifdef DCPOMATIC_WINDOWS #include #include @@ -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(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(out); } /** @param mapped List of mapped audio channels from a Film. diff --git a/test/util_test.cc b/test/util_test.cc index d8cb61fdc..1e13efa82 100644 --- a/test/util_test.cc +++ b/test/util_test.cc @@ -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_à")); +} -- 2.30.2