From f14ffd5b1db1c243d47eccd2a37c71f719c33681 Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 28 Oct 2011 12:57:07 +0100 Subject: [PATCH] Fix a bug affecting TerraGear, and extend unit-tests to cover this. (SGPath::file returned an empty string for paths with no directory separator) --- simgear/misc/path_test.cxx | 8 ++++++++ simgear/misc/sg_path.cxx | 11 ++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/simgear/misc/path_test.cxx b/simgear/misc/path_test.cxx index cda48859..459f064c 100644 --- a/simgear/misc/path_test.cxx +++ b/simgear/misc/path_test.cxx @@ -135,6 +135,14 @@ int main(int argc, char* argv[]) #else COMPARE(d1.str_native(), std::string("/usr/local")); #endif + +// paths with only the file components + SGPath pf("something.txt.gz"); + COMPARE(pf.base(), "something.txt"); + COMPARE(pf.file(), "something.txt.gz"); + COMPARE(pf.dir(), ""); + COMPARE(pf.lower_extension(), "gz"); + COMPARE(pf.complete_lower_extension(), "txt.gz"); test_dir(); diff --git a/simgear/misc/sg_path.cxx b/simgear/misc/sg_path.cxx index ed31e18b..46e80228 100644 --- a/simgear/misc/sg_path.cxx +++ b/simgear/misc/sg_path.cxx @@ -175,12 +175,13 @@ void SGPath::concat( const string& p ) { // Get the file part of the path (everything after the last path sep) -string SGPath::file() const { - int index = path.rfind(sgDirPathSep); - if (index >= 0) { - return path.substr(index + 1); +string SGPath::file() const +{ + string::size_type index = path.rfind(sgDirPathSep); + if (index != string::npos) { + return path.substr(index + 1); } else { - return ""; + return path; } } -- 2.39.5