diff --git a/applications/test/fileName/Test-fileName.C b/applications/test/fileName/Test-fileName.C index c6bdd486df..64888aa109 100644 --- a/applications/test/fileName/Test-fileName.C +++ b/applications/test/fileName/Test-fileName.C @@ -224,7 +224,8 @@ void testDirname(const fileName& input) << " path:" << input.path() << " name:\"" << input.name() << '"' << " ext:\"" << input.ext() << '"' - << " components: " << flatOutput(input.components()) << nl; + << " components: " << flatOutput(input.components()) + << " last: " << input.component(string::npos) << nl; } diff --git a/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOps.H b/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOps.H index 421c4fd590..8bd574602d 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOps.H +++ b/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOps.H @@ -43,6 +43,7 @@ SourceFiles #ifndef PtrListOps_H #define PtrListOps_H +#include "predicates.H" #include "PtrList.H" #include "ListOps.H" @@ -169,6 +170,12 @@ List names ); +//- List of names generated by calling \c name() for each list item +//- no filtering (ie, predicates::always) +template +List names(const UPtrList& list); + + //- Find first list item with 'name()' that matches, -1 on failure template label firstMatching diff --git a/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOpsTemplates.C b/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOpsTemplates.C index a4182de016..4019fa0cb2 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOpsTemplates.C +++ b/src/OpenFOAM/containers/PtrLists/PtrListOps/PtrListOpsTemplates.C @@ -166,6 +166,16 @@ Foam::List Foam::PtrListOps::names } +template +Foam::List Foam::PtrListOps::names +( + const UPtrList& list +) +{ + return PtrListOps::names(list, predicates::always()); +} + + template Foam::label Foam::PtrListOps::firstMatching ( diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index 09ebe26f3a..ef7b72c31e 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -485,9 +485,16 @@ Foam::word Foam::fileName::component { const auto parsed = stringOps::split(*this, delim); - if (cmpt < parsed.size()) + if (parsed.size()) { - return parsed[cmpt].str(); + if (cmpt == std::string::npos) + { + return parsed.last().str(); + } + else if (cmpt < parsed.size()) + { + return parsed[cmpt].str(); + } } return word(); diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.H b/src/OpenFOAM/primitives/strings/fileName/fileName.H index b00c78e804..cc1cd4cacf 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.H @@ -358,7 +358,8 @@ public: // \endverbatim wordList components(const char delim = '/') const; - //- Return a single component of the path + //- Return a single component of the path or empty if out of range + // The location \c npos returns the last component word component(const size_type cmpt, const char delim = '/') const; diff --git a/src/meshTools/coordinate/systems/coordinateSystems.C b/src/meshTools/coordinate/systems/coordinateSystems.C index 94cab825d4..ce9eb81152 100644 --- a/src/meshTools/coordinate/systems/coordinateSystems.C +++ b/src/meshTools/coordinate/systems/coordinateSystems.C @@ -269,7 +269,7 @@ Foam::coordinateSystems::lookup(const word& name) const Foam::wordList Foam::coordinateSystems::names() const { - return PtrListOps::names(*this, predicates::always{}); + return PtrListOps::names(*this); // match any/all }