[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 7fe7f97 2/4: Add and test a function to remov
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 7fe7f97 2/4: Add and test a function to remove an alien msw root-name from a path |
Date: |
Wed, 11 Nov 2020 12:05:46 -0500 (EST) |
branch: master
commit 7fe7f97817f554caed8fdee558600ce02f900835
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Add and test a function to remove an alien msw root-name from a path
---
path_utility.cpp | 25 +++++++++++++++++++++++++
path_utility.hpp | 4 ++++
path_utility_test.cpp | 27 +++++++++++++++++++--------
3 files changed, 48 insertions(+), 8 deletions(-)
diff --git a/path_utility.cpp b/path_utility.cpp
index 833aa13..b815f6b 100644
--- a/path_utility.cpp
+++ b/path_utility.cpp
@@ -193,6 +193,31 @@ std::string orthodox_filename(std::string const&
original_filename)
return s;
}
+/// Remove an msw root /^.*:/ from path iff system is not msw.
+///
+/// Motivation: Prevent the ghastly outcome demonstrated in the unit
+/// test when an msw-native path is used on a posix system.
+///
+/// On an msw system, return the path unaltered: it may contain a
+/// 'root-name', but that 'root-name' is native, not alien.
+
+fs::path remove_alien_msw_root(fs::path const& original_filepath)
+{
+#if defined LMI_POSIX
+ std::string s {original_filepath.string()};
+ std::string::size_type p = s.find_last_of(':');
+ if(std::string::npos != p)
+ {
+ s.erase(0, 1 + p);
+ }
+ return s;
+#elif defined LMI_MSW
+ return original_filepath;
+#else // Unknown platform.
+ throw "Unrecognized platform."
+#endif // Unknown platform.
+}
+
namespace
{
/// Prepend a serial number to a file extension. This is intended to
diff --git a/path_utility.hpp b/path_utility.hpp
index 57da70f..47ac584 100644
--- a/path_utility.hpp
+++ b/path_utility.hpp
@@ -42,6 +42,10 @@ LMI_SO std::string orthodox_filename
(std::string const& original_filename
);
+LMI_SO fs::path remove_alien_msw_root
+ (fs::path const& original_filepath
+ );
+
LMI_SO fs::path serial_file_path
(fs::path const& exemplar
,std::string const& personal_name
diff --git a/path_utility_test.cpp b/path_utility_test.cpp
index 7108e08..85a264f 100644
--- a/path_utility_test.cpp
+++ b/path_utility_test.cpp
@@ -416,18 +416,29 @@ void test_path_validation()
/// does something bizarre, viz.:
/// fs::system_complete(/opt/lmi/data) returns:
/// /opt/lmi/data
-/// fs::system_complete(Z:/opt/lmi/data) returns:
+/// as expected, but
+/// fs::system_complete(Z:/opt/lmi/data) bizarrely returns:
/// /opt/lmi/gcc_x86_64-pc-linux-gnu/build/ship/Z:/opt/lmi/data
+/// or something like that, depending on the build directory.
void test_oddities()
{
- std::cout << "Test fs::system_complete():" << std::endl;
- std::string z0 = "/opt/lmi/data";
- std::cout << "fs::system_complete(" << z0 << ") returns:" << std::endl;
- std::cout << fs::system_complete(z0).string() << std::endl;
- std::string z1 = "Z:/opt/lmi/data";
- std::cout << "fs::system_complete(" << z1 << ") returns:" << std::endl;
- std::cout << fs::system_complete(z1).string() << std::endl;
+ std::string const z0 = "/opt/lmi/data";
+ std::string const z1 = "Z:/opt/lmi/data";
+ std::string const z2 = remove_alien_msw_root(z1).string();
+#if defined LMI_POSIX
+ BOOST_TEST_EQUAL (z0, fs::system_complete(z0).string());
+ BOOST_TEST_UNEQUAL(z0, fs::system_complete(z1).string());
+ BOOST_TEST_EQUAL (z0, z2);
+ BOOST_TEST_EQUAL (z0, fs::system_complete(z2).string());
+#elif defined LMI_MSW
+ BOOST_TEST_EQUAL (z1, fs::system_complete(z0).string());
+ BOOST_TEST_EQUAL (z1, fs::system_complete(z1).string());
+ BOOST_TEST_EQUAL (z1, z2);
+ BOOST_TEST_EQUAL (z1, fs::system_complete(z2).string());
+#else // Unknown platform.
+ throw "Unrecognized platform."
+#endif // Unknown platform.
}
int test_main(int, char*[])