From 07515cd716c65a1f97225407e19c788929bba484 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Wed, 15 Dec 2021 23:04:58 +0100 Subject: [PATCH 1/2] Fixed issue #946 for the case of file paths with blanks --- src/tl/tl/tlInclude.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/tl/tl/tlInclude.cc b/src/tl/tl/tlInclude.cc index 53a5a6dc1..6f52a438a 100644 --- a/src/tl/tl/tlInclude.cc +++ b/src/tl/tl/tlInclude.cc @@ -134,7 +134,11 @@ IncludeExpander::to_string () const tl_assert (m_sections.begin ()->second.second == 0); std::string fn = m_sections.begin ()->second.first; - return tl::to_word_or_quoted_string (fn, valid_fn_chars); + if (! fn.empty () && fn.front () == '@') { + return tl::to_quoted_string (fn); + } else { + return fn; + } } else { @@ -166,7 +170,7 @@ IncludeExpander::from_string (const std::string &s) ex.read_quoted (ie.m_sections [1].first); - } else if (ex.test ("@")) { + } else if (*ex == '@') { while (! ex.at_end ()) { From 533a72b7bd6b27e523e4ffdb293931cd17b616fc Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Wed, 15 Dec 2021 23:18:42 +0100 Subject: [PATCH 2/2] Added tests, fixed implementation --- src/tl/tl/tlInclude.cc | 2 ++ src/tl/unit_tests/tlIncludeTests.cc | 28 ++++++++++++++++++++++++++++ testdata/tl/inc 4.txt | 1 + testdata/tl/x_inc4.txt | 3 +++ 4 files changed, 34 insertions(+) create mode 100644 testdata/tl/inc 4.txt create mode 100644 testdata/tl/x_inc4.txt diff --git a/src/tl/tl/tlInclude.cc b/src/tl/tl/tlInclude.cc index 6f52a438a..e247ab9f3 100644 --- a/src/tl/tl/tlInclude.cc +++ b/src/tl/tl/tlInclude.cc @@ -172,6 +172,8 @@ IncludeExpander::from_string (const std::string &s) } else if (*ex == '@') { + ++ex; + while (! ex.at_end ()) { int ln = 0; diff --git a/src/tl/unit_tests/tlIncludeTests.cc b/src/tl/unit_tests/tlIncludeTests.cc index da681c43e..8ed1651fd 100644 --- a/src/tl/unit_tests/tlIncludeTests.cc +++ b/src/tl/unit_tests/tlIncludeTests.cc @@ -108,3 +108,31 @@ TEST(4_multi_include_interpolate) EXPECT_EQ (ie.translate_to_original (6).second, 3); } +TEST(5_issue946) +{ + std::string fn = tl::testdata () + "/tl/x_inc4.txt"; + + std::string et; + tl::IncludeExpander ie = tl::IncludeExpander::expand (fn, tl::InputStream (fn).read_all (), et); + EXPECT_EQ (et, "A line\nincluded.4\nAnother line\n"); + + EXPECT_EQ (np (ie.to_string ()), np ("@1*" + tl::testdata () + "/tl/x_inc4.txt*0;2*'" + tl::testdata () + "/tl/inc 4.txt'*-1;3*" + tl::testdata () + "/tl/x_inc4.txt*0;")); + EXPECT_EQ (tl::IncludeExpander::from_string (ie.to_string ()).to_string (), ie.to_string ()); + + EXPECT_EQ (ie.translate_to_original (1).first, fn); + EXPECT_EQ (ie.translate_to_original (1).second, 1); + EXPECT_EQ (np (ie.translate_to_original (2).first), np (tl::testdata () + "/tl/inc 4.txt")); + EXPECT_EQ (ie.translate_to_original (2).second, 1); + EXPECT_EQ (ie.translate_to_original (3).first, fn); + EXPECT_EQ (ie.translate_to_original (3).second, 3); + + fn = tl::testdata () + "/tl/inc 4.txt"; + + et.clear (); + ie = tl::IncludeExpander::expand (fn, tl::InputStream (fn).read_all (), et); + EXPECT_EQ (et, "included.4\n"); + + // no quotes here so this string can be used as the original file name if there is no include + EXPECT_EQ (np (ie.to_string ()), np (tl::testdata () + "/tl/inc 4.txt")); +} + diff --git a/testdata/tl/inc 4.txt b/testdata/tl/inc 4.txt new file mode 100644 index 000000000..710f3fd3d --- /dev/null +++ b/testdata/tl/inc 4.txt @@ -0,0 +1 @@ +included.4 diff --git a/testdata/tl/x_inc4.txt b/testdata/tl/x_inc4.txt new file mode 100644 index 000000000..954d8ff6f --- /dev/null +++ b/testdata/tl/x_inc4.txt @@ -0,0 +1,3 @@ +A line +# %include 'inc 4.txt' +Another line