From d8c0ea806aaff228b2cdfcd86d498e676edaf397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=B6fferlein?= Date: Fri, 5 Feb 2021 23:28:22 +0100 Subject: [PATCH] Issue 723 fixed. (#724) * Fixed a segfault with simple 'klayout -v' * Fixed #723 --- src/tl/tl/tlFileUtils.cc | 6 +++- src/tl/unit_tests/tlFileUtilsTests.cc | 41 +++++++++++++++++++++++++++ src/tl/unit_tests/tlStreamTests.cc | 8 +++--- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/tl/tl/tlFileUtils.cc b/src/tl/tl/tlFileUtils.cc index d4028e093..93fa77860 100644 --- a/src/tl/tl/tlFileUtils.cc +++ b/src/tl/tl/tlFileUtils.cc @@ -287,7 +287,11 @@ std::string dirname (const std::string &s) parts.pop_back (); } - return tl::join (parts, ""); + if (parts.empty ()) { + return is_part_with_separator (s) ? "" : "."; + } else { + return tl::join (parts, ""); + } } std::string filename (const std::string &s) diff --git a/src/tl/unit_tests/tlFileUtilsTests.cc b/src/tl/unit_tests/tlFileUtilsTests.cc index edf0ae01f..b77b7968e 100644 --- a/src/tl/unit_tests/tlFileUtilsTests.cc +++ b/src/tl/unit_tests/tlFileUtilsTests.cc @@ -351,6 +351,9 @@ TEST (10) EXPECT_EQ (tl::normalize_path ("d:/"), "D:\\"); EXPECT_EQ (tl::normalize_path ("d://"), "D:\\"); + EXPECT_EQ (tl::dirname ("hello"), "."); + EXPECT_EQ (tl::dirname (".\\hello"), "."); + EXPECT_EQ (tl::dirname ("/hello"), ""); EXPECT_EQ (tl::dirname ("/hello/world"), "\\hello"); EXPECT_EQ (tl::dirname ("\\hello\\world"), "\\hello"); EXPECT_EQ (tl::dirname ("/hello//world/"), "\\hello\\world"); @@ -406,6 +409,33 @@ TEST (10) EXPECT_EQ (tl::combine_path ("hello", "world"), "hello\\world"); EXPECT_EQ (tl::combine_path ("hello", ""), "hello"); EXPECT_EQ (tl::combine_path ("hello", "", true), "hello\\"); + EXPECT_EQ (tl::combine_path ("", "hello", true), "\\hello"); + EXPECT_EQ (tl::combine_path (".", "hello", true), ".\\hello"); + + EXPECT_EQ (tl::combine_path (tl::dirname ("hello"), tl::filename ("hello")), ".\\hello"); + EXPECT_EQ (tl::combine_path (tl::dirname ("\\hello"), tl::filename ("\\hello")), "\\hello"); + EXPECT_EQ (tl::combine_path (tl::dirname ("c:\\hello"), tl::filename ("c:\\hello")), "C:\\hello"); + EXPECT_EQ (tl::combine_path (tl::dirname ("\\c:\\hello"), tl::filename ("\\c:\\hello")), "C:\\hello"); + EXPECT_EQ (tl::combine_path (tl::dirname ("\\\\hello"), tl::filename ("\\\\hello")), "\\hello"); + EXPECT_EQ (tl::combine_path (tl::dirname ("\\\\server:\\hello"), tl::filename ("\\\\server:\\hello")), "\\\\server:\\hello"); + EXPECT_EQ (tl::combine_path (tl::dirname ("\\hello\\x"), tl::filename ("\\hello\\x")), "\\hello\\x"); + EXPECT_EQ (tl::combine_path (tl::dirname ("c:\\hello\\x"), tl::filename ("c:\\hello\\x")), "C:\\hello\\x"); + EXPECT_EQ (tl::combine_path (tl::dirname ("\\c:\\hello\\x"), tl::filename ("\\c:\\hello\\x")), "C:\\hello\\x"); + EXPECT_EQ (tl::combine_path (tl::dirname ("\\\\hello\\x"), tl::filename ("\\\\hello\\x")), "\\\\hello\\x"); + EXPECT_EQ (tl::combine_path (tl::dirname ("\\hello\\x\\y"), tl::filename ("\\hello\\x\\y")), "\\hello\\x\\y"); + + EXPECT_EQ (tl::combine_path (tl::dirname ("hello/x"), tl::filename ("hello/x")), "hello\\x"); + EXPECT_EQ (tl::combine_path (tl::dirname ("/hello"), tl::filename ("/hello")), "\\hello"); + EXPECT_EQ (tl::combine_path (tl::dirname ("c:/hello"), tl::filename ("c:/hello")), "C:\\hello"); + EXPECT_EQ (tl::combine_path (tl::dirname ("/c:/hello"), tl::filename ("/c:/hello")), "C:\\hello"); + EXPECT_EQ (tl::combine_path (tl::dirname ("//hello"), tl::filename ("//hello")), "\\hello"); + EXPECT_EQ (tl::combine_path (tl::dirname ("//server:/hello"), tl::filename ("//server:/hello")), "\\\\server:\\hello"); + EXPECT_EQ (tl::combine_path (tl::dirname ("/hello/x"), tl::filename ("/hello/x")), "\\hello\\x"); + EXPECT_EQ (tl::combine_path (tl::dirname ("c:/hello/x"), tl::filename ("c:/hello/x")), "C:\\hello\\x"); + EXPECT_EQ (tl::combine_path (tl::dirname ("/c:/hello/x"), tl::filename ("/c:/hello/x")), "C:\\hello\\x"); + EXPECT_EQ (tl::combine_path (tl::dirname ("//hello/x"), tl::filename ("//hello/x")), "\\\\hello\\x"); + EXPECT_EQ (tl::combine_path (tl::dirname ("/hello/x/y"), tl::filename ("/hello/x/y")), "\\hello\\x\\y"); + EXPECT_EQ (tl::combine_path (tl::dirname ("hello/x"), tl::filename ("hello/x")), "hello\\x"); tl::file_utils_force_reset (); @@ -436,6 +466,9 @@ TEST (11) EXPECT_EQ (tl::join (tl::split_path ("/"), "+"), "/"); EXPECT_EQ (tl::join (tl::split_path ("//"), "+"), "/"); + EXPECT_EQ (tl::dirname ("hello"), "."); + EXPECT_EQ (tl::dirname ("./hello"), "."); + EXPECT_EQ (tl::dirname ("/hello"), ""); EXPECT_EQ (tl::dirname ("/hello/world"), "/hello"); EXPECT_EQ (tl::dirname ("/hello//world/"), "/hello/world"); EXPECT_EQ (tl::dirname ("hello//world/"), "hello/world"); @@ -461,6 +494,14 @@ TEST (11) EXPECT_EQ (tl::combine_path ("hello", "world"), "hello/world"); EXPECT_EQ (tl::combine_path ("hello", ""), "hello"); EXPECT_EQ (tl::combine_path ("hello", "", true), "hello/"); + EXPECT_EQ (tl::combine_path ("", "hello", true), "/hello"); + EXPECT_EQ (tl::combine_path (".", "hello", true), "./hello"); + + EXPECT_EQ (tl::combine_path (tl::dirname ("hello"), tl::filename ("hello")), "./hello"); + EXPECT_EQ (tl::combine_path (tl::dirname ("/hello"), tl::filename ("/hello")), "/hello"); + EXPECT_EQ (tl::combine_path (tl::dirname ("/hello/x"), tl::filename ("/hello/x")), "/hello/x"); + EXPECT_EQ (tl::combine_path (tl::dirname ("/hello/x/y"), tl::filename ("/hello/x/y")), "/hello/x/y"); + EXPECT_EQ (tl::combine_path (tl::dirname ("hello/x"), tl::filename ("hello/x")), "hello/x"); tl::file_utils_force_reset (); diff --git a/src/tl/unit_tests/tlStreamTests.cc b/src/tl/unit_tests/tlStreamTests.cc index a684f14fa..daad877f7 100644 --- a/src/tl/unit_tests/tlStreamTests.cc +++ b/src/tl/unit_tests/tlStreamTests.cc @@ -138,15 +138,15 @@ TEST(TextInputStream) tl::InputStream is (fn); tl::TextInputStream tis (is); EXPECT_EQ (tis.get_line (), "Hello, world!"); - EXPECT_EQ (tis.line_number (), 1); + EXPECT_EQ (tis.line_number (), size_t (1)); EXPECT_EQ (tis.get_line (), "With another line"); - EXPECT_EQ (tis.line_number (), 2); + EXPECT_EQ (tis.line_number (), size_t (2)); EXPECT_EQ (tis.peek_char (), '\n'); EXPECT_EQ (tis.get_line (), ""); - EXPECT_EQ (tis.line_number (), 3); + EXPECT_EQ (tis.line_number (), size_t (3)); EXPECT_EQ (tis.peek_char (), 's'); EXPECT_EQ (tis.get_line (), "separated by a LFCR and CRLF."); - EXPECT_EQ (tis.line_number (), 4); + EXPECT_EQ (tis.line_number (), size_t (4)); EXPECT_EQ (tis.at_end (), true); }