diff --git a/applications/test/DirLister/DirLister.C b/applications/test/DirLister/DirLister.C index 651f5158f6..0c5c91ae0d 100644 --- a/applications/test/DirLister/DirLister.C +++ b/applications/test/DirLister/DirLister.C @@ -28,11 +28,6 @@ License #include "DirLister.H" #include -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // - -static const Foam::word extgz("gz"); - - // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // bool Foam::DirLister::const_iterator::open(const fileName& dir) @@ -110,9 +105,9 @@ Foam::word Foam::DirLister::next(DIR* dirPtr) const if (ok) { - if (fType == fileName::FILE && stripgz_ && name.hasExt(extgz)) + if (fType == fileName::FILE && stripgz_ && name.has_ext("gz")) { - name = name.lessExt(); + name.remove_ext(); } if (!name.empty() && accept(name)) diff --git a/applications/test/OCountStream/Test-OCountStream.C b/applications/test/OCountStream/Test-OCountStream.C index 1af8aa4255..ee0ebf19fa 100644 --- a/applications/test/OCountStream/Test-OCountStream.C +++ b/applications/test/OCountStream/Test-OCountStream.C @@ -83,9 +83,9 @@ int main(int argc, char *argv[]) { IOstreamOption streamOpt; - if (outputName.hasExt("gz")) + if (outputName.has_ext("gz")) { - outputName.removeExt(); + outputName.remove_ext(); streamOpt.compression(IOstreamOption::COMPRESSED); } diff --git a/applications/test/fileName/Test-fileName.C b/applications/test/fileName/Test-fileName.C index 4d44bc358e..8715e014fb 100644 --- a/applications/test/fileName/Test-fileName.C +++ b/applications/test/fileName/Test-fileName.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2016-2021 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -260,7 +260,7 @@ int main(int argc, char *argv[]) "hello1", "hello2", "hello3", - "hello4.hmm" + "hello4.ext" }; Info<< file1 << nl; @@ -270,7 +270,7 @@ int main(int argc, char *argv[]) { file1, "some", - "more/things.hmm" + "more/things.ext" }; Info<< file2 << nl; @@ -281,7 +281,7 @@ int main(int argc, char *argv[]) { std::string("ffO"), "some", - "more/things.hmm" + "more/things.ext" }; Info<< file3 << nl; @@ -295,7 +295,7 @@ int main(int argc, char *argv[]) { "some", file3, - "more/things.hmm", + "more/things.ext", file1 }; Info<< "All ==> " << file4 << nl; @@ -328,26 +328,26 @@ int main(int argc, char *argv[]) fileName input1("path.to/media/image.png"); Info<<"File : " << input0 << " ext: " - << Switch(input0.hasExt()) + << Switch(input0.has_ext()) << " = " << input0.ext() << nl; Info<<"File : " << input1 << " ext: " - << Switch(input1.hasExt()) + << Switch(input1.has_ext()) << " = " << input1.ext() << nl; Info<<"File : " << endWithDot << " ext: " - << Switch(endWithDot.hasExt()) + << Switch(endWithDot.has_ext()) << " = " << endWithDot.ext() << " <-- perhaps return false?" << nl; Info<<"File : " << endWithSlash << " ext: " - << Switch(endWithSlash.hasExt()) + << Switch(endWithSlash.has_ext()) << " = " << endWithSlash.ext() << nl; - Info<<"Remove extension " << (input0.removeExt()); + Info<<"Remove extension " << (input0.remove_ext()); Info<< " now: " << input0 << nl; Info<<"Remove extension " << (input1.removeExt()); Info<< " now: " << input1 << nl; - Info<<"Remove extension " << (endWithSlash.removeExt()); + Info<<"Remove extension " << (endWithSlash.remove_ext()); Info<< " now: " << endWithSlash << nl; wordList exts{ "jpg", "png", "txt", word::null }; @@ -359,14 +359,14 @@ int main(int argc, char *argv[]) Info<< nl; - Info<<"Test hasExt(word)" << nl + Info<<"Test has_ext(word)" << nl <<"~~~~~~~~~~~~~~~~~" << nl; Info<<"Has extension(s):" << nl << "input: " << input1 << nl; for (const word& e : exts) { Info<<" '" << e << "' -> " - << Switch(input1.hasExt(e)) << nl; + << Switch(input1.has_ext(e)) << nl; } Info<< nl; @@ -375,12 +375,12 @@ int main(int argc, char *argv[]) for (const word& e : exts) { Info<<" '" << e << "' -> " - << Switch(endWithDot.hasExt(e)) << nl; + << Switch(endWithDot.has_ext(e)) << nl; } Info<< nl; - Info<<"Test hasExt(wordRe)" << nl + Info<<"Test has_ext(wordRe)" << nl <<"~~~~~~~~~~~~~~~~~~~" << nl; // A regex with a zero length matcher doesn't work at all: @@ -393,25 +393,25 @@ int main(int argc, char *argv[]) Info<<"Has extension(s):" << nl << "input: " << endWithDot << nl; Info<<" " << matcher0 << " -> " - << Switch(endWithDot.hasExt(matcher0)) << nl; + << Switch(endWithDot.has_ext(matcher0)) << nl; Info<<" " << matcher1 << " -> " - << Switch(endWithDot.hasExt(matcher1)) << nl; + << Switch(endWithDot.has_ext(matcher1)) << nl; Info<<" " << matcher2 << " -> " - << Switch(endWithDot.hasExt(matcher2)) << nl; + << Switch(endWithDot.has_ext(matcher2)) << nl; Info<< "input: " << input1 << nl; Info<<" " << matcher0 << " -> " - << Switch(input1.hasExt(matcher0)) << nl; + << Switch(input1.has_ext(matcher0)) << nl; Info<<" " << matcher1 << " -> " - << Switch(input1.hasExt(matcher1)) << nl; + << Switch(input1.has_ext(matcher1)) << nl; Info<<" " << matcher2 << " -> " - << Switch(input1.hasExt(matcher2)) << nl; + << Switch(input1.has_ext(matcher2)) << nl; Info<< nl; Info<<"Remove extension(s):" << nl << "input: " << input1 << nl; while (!input1.empty()) { - if (input1.removeExt()) + if (input1.remove_ext()) { Info<< " -> " << input1 << nl; } @@ -708,7 +708,7 @@ int main(int argc, char *argv[]) "hello1", "hello2", "hello3", - "hello4.hmm" + "hello4.ext" }; fileName pathName(wrdList); @@ -718,14 +718,28 @@ int main(int argc, char *argv[]) << "pathName.name() = >" << pathName.name() << "<\n" << "pathName.path() = " << pathName.path() << nl << "pathName.ext() = >" << pathName.ext() << "<\n" - << "pathName.nameLessExt= >" << pathName.nameLessExt() << "<\n"; + << "pathName.stem = >" << pathName.stem() << "<\n"; Info<< "pathName.components() = " << pathName.components() << nl << "pathName.component(2) = " << pathName.component(2) << nl << endl; - Info<< "hasPath = " << Switch(pathName.hasPath()) << nl; - pathName.removePath(); + pathName.replace_name("newName.ext"); + Info<< "new name = " << pathName << nl; + Info<< "has ext = " << Switch::name(pathName.has_ext()) << nl; + Info<< "has ext('') = " << Switch::name(pathName.has_ext("")) << nl; + Info<< "has ext(foo) = " << Switch::name(pathName.has_ext("foo")) << nl; + Info<< "has ext(ext) = " << Switch::name(pathName.has_ext("ext")) << nl; + + pathName.replace_ext("png"); + Info<< "new ext = " << pathName << nl; + + pathName.replace_ext(""); // Same as remove_ext + Info<< "new ext = " << pathName << nl; + + Info<< "has path = " << Switch::name(pathName.has_path()) << nl; + pathName.remove_path(); + pathName.removePath(); // second type should be a no-op Info<< "removed path = " << pathName << nl; Info<< nl << nl; diff --git a/applications/test/fstreamPointer/Test-fstreamPointer.C b/applications/test/fstreamPointer/Test-fstreamPointer.C index 2cad909ace..cca84b544d 100644 --- a/applications/test/fstreamPointer/Test-fstreamPointer.C +++ b/applications/test/fstreamPointer/Test-fstreamPointer.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2020-2021 OpenCFD Ltd. + Copyright (C) 2020-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM, distributed under GPL-3.0-or-later. @@ -61,10 +61,10 @@ int main(int argc, char *argv[]) InfoErr<< "output: " << outputName; IOstreamOption::compressionType comp(IOstreamOption::UNCOMPRESSED); - if (outputName.hasExt("gz")) + if (outputName.has_ext("gz")) { comp = IOstreamOption::COMPRESSED; - outputName.removeExt(); + outputName.remove_ext(); InfoErr<< " [compress]"; } diff --git a/applications/test/surfaceReading/Test-surfaceReading.C b/applications/test/surfaceReading/Test-surfaceReading.C index bd0c39d12b..3e25910704 100644 --- a/applications/test/surfaceReading/Test-surfaceReading.C +++ b/applications/test/surfaceReading/Test-surfaceReading.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2021 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -91,15 +91,15 @@ int main(int argc, char *argv[]) const auto importName = args.get(1); - word ext; - if (!args.readIfPresent("ext", ext)) - { - ext = importName.ext(); - if (ext == "gz") - { - ext = importName.lessExt().ext(); - } - } + word ext = + ( + importName.has_ext("gz") + ? importName.stem().ext() + : importName.ext() + ); + + // Allow override of extension + args.readIfPresent("ext", ext); args.readIfPresent("stl-parser", fileFormats::STLReader::parserType); diff --git a/applications/utilities/mesh/conversion/ccm/ccmToFoam/ccmToFoam.C b/applications/utilities/mesh/conversion/ccm/ccmToFoam/ccmToFoam.C index 4ccbe6f300..a11603861f 100644 --- a/applications/utilities/mesh/conversion/ccm/ccmToFoam/ccmToFoam.C +++ b/applications/utilities/mesh/conversion/ccm/ccmToFoam/ccmToFoam.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2016-2021 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -175,7 +175,7 @@ int main(int argc, char *argv[]) // strip erroneous extension (.ccm, .ccmg, .ccmp) if (ext == "ccm" || ext == "ccmg" || ext == "ccmp") { - exportName = exportName.lessExt(); + exportName.remove_ext(); } } else if (args.found("export")) diff --git a/applications/utilities/mesh/conversion/ccm/foamToCcm/foamToCcm.C b/applications/utilities/mesh/conversion/ccm/foamToCcm/foamToCcm.C index 0d2c7cfbba..59905b162c 100644 --- a/applications/utilities/mesh/conversion/ccm/foamToCcm/foamToCcm.C +++ b/applications/utilities/mesh/conversion/ccm/foamToCcm/foamToCcm.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2016 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -137,7 +137,7 @@ int main(int argc, char *argv[]) // strip erroneous extension (.ccm, .ccmg, .ccmp) if (ext == "ccm" || ext == "ccmg" || ext == "ccmp") { - exportName = exportName.lessExt(); + exportName.remove_ext(); } } else if (args.found("case")) diff --git a/applications/utilities/mesh/conversion/foamToSurface/foamToSurface.C b/applications/utilities/mesh/conversion/foamToSurface/foamToSurface.C index 53b359a2f2..a6cae8727a 100644 --- a/applications/utilities/mesh/conversion/foamToSurface/foamToSurface.C +++ b/applications/utilities/mesh/conversion/foamToSurface/foamToSurface.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2020-2021 OpenCFD Ltd. + Copyright (C) 2020-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -87,8 +87,8 @@ int main(int argc, char *argv[]) const scalar scaleFactor = args.getOrDefault("scale", 0); const bool doTriangulate = args.found("tri"); - fileName exportBase = exportName.lessExt(); - word exportExt = exportName.ext(); + const fileName exportBase = exportName.lessExt(); + const word exportExt = exportName.ext(); if (!meshedSurface::canWriteType(exportExt, true)) { diff --git a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellSizeControlSurfaces/surfaceCellSizeFunction/cellSizeCalculationType/automatic/automatic.C b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellSizeControlSurfaces/surfaceCellSizeFunction/cellSizeCalculationType/automatic/automatic.C index 3b50972776..cade761a35 100644 --- a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellSizeControlSurfaces/surfaceCellSizeFunction/cellSizeCalculationType/automatic/automatic.C +++ b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellSizeControlSurfaces/surfaceCellSizeFunction/cellSizeCalculationType/automatic/automatic.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2012-2017 OpenFOAM Foundation - Copyright (C) 2016-2020 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -298,7 +298,7 @@ Foam::tmp Foam::automatic::load() ( surface_.searchableSurface::time().constant() / "triSurface" - / surfaceName_.nameLessExt() + "_cellSize" + / surfaceName_.stem() + "_cellSize" ) ); diff --git a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C index fddf9b20c2..b0f7f35696 100644 --- a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C +++ b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016-2017 OpenFOAM Foundation - Copyright (C) 2017-2021 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -144,11 +144,7 @@ using namespace Foam; // but leave anything with '/' delimiters untouched bool upgradeScope(word& entryName) { - if - ( - entryName.find('/') == string::npos - && entryName.find(':') != string::npos - ) + if (!entryName.contains('/') && entryName.contains(':')) { const wordList names(fileName(entryName).components(':')); diff --git a/applications/utilities/surface/surfaceBooleanFeatures/surfaceBooleanFeatures.C b/applications/utilities/surface/surfaceBooleanFeatures/surfaceBooleanFeatures.C index f711f2c98e..9d06ae89c2 100644 --- a/applications/utilities/surface/surfaceBooleanFeatures/surfaceBooleanFeatures.C +++ b/applications/utilities/surface/surfaceBooleanFeatures/surfaceBooleanFeatures.C @@ -1718,9 +1718,9 @@ int main(int argc, char *argv[]) const fileName sFeatFileName ( - fileName(surf1Name).nameLessExt() + fileName::stem(surf1Name) + "_" - + fileName(surf2Name).nameLessExt() + + fileName::stem(surf2Name) + "_" + action ); diff --git a/applications/utilities/surface/surfaceCheck/surfaceCheck.C b/applications/utilities/surface/surfaceCheck/surfaceCheck.C index 604853ebed..ec9a5cb6db 100644 --- a/applications/utilities/surface/surfaceCheck/surfaceCheck.C +++ b/applications/utilities/surface/surfaceCheck/surfaceCheck.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2016-2021 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -134,7 +134,7 @@ void writeZoning const labelList& faceZone, const word& fieldName, const fileName& surfFilePath, - const fileName& surfFileNameBase + const word& surfFileStem ) { // Transcribe faces @@ -145,7 +145,7 @@ void writeZoning ( surf.points(), faces, - (surfFilePath / surfFileNameBase), + (surfFilePath / surfFileStem), false // serial - already merged ); @@ -163,7 +163,7 @@ void writeParts const label nFaceZones, const labelList& faceZone, const fileName& surfFilePath, - const fileName& surfFileNameBase + const word& surfFileStem ) { for (label zone = 0; zone < nFaceZones; zone++) @@ -183,7 +183,7 @@ void writeParts fileName subName ( surfFilePath - / surfFileNameBase + "_" + name(zone) + ".obj" + / surfFileStem + "_" + name(zone) + ".obj" ); Info<< "writing part " << zone << " size " << subSurf.size() @@ -334,7 +334,7 @@ int main(int argc, char *argv[]) argList args(argc, argv); - const auto surfFileName = args.get(1); + const auto surfName = args.get(1); const bool checkSelfIntersect = args.found("checkSelfIntersection"); const bool splitNonManifold = args.found("splitNonManifold"); const label outputThreshold = @@ -360,12 +360,13 @@ int main(int argc, char *argv[]) } - Info<< "Reading surface from " << surfFileName << " ..." << nl << endl; + Info<< "Reading surface from " + << args.relativePath(surfName) << " ..." << nl << endl; // Read // ~~~~ - triSurface surf(surfFileName); + triSurface surf(surfName); Info<< "Statistics:" << endl; @@ -373,17 +374,15 @@ int main(int argc, char *argv[]) Info<< endl; - // Determine path and extension - fileName surfFileNameBase(surfFileName.name()); - const word fileType = surfFileNameBase.ext(); - // Strip extension - surfFileNameBase = surfFileNameBase.lessExt(); - // If extension was .gz strip original extension - if (fileType == "gz") + // Split into path and stem (no extension) + const fileName surfFilePath(surfName.path()); + word surfFileStem(surfName.stem()); + + // If originally ".gz", need to strip extension again + if (surfName.has_ext("gz")) { - surfFileNameBase = surfFileNameBase.lessExt(); + surfFileStem.remove_ext(); } - const fileName surfFilePath(surfFileName.path()); // write bounding box corners @@ -484,7 +483,7 @@ int main(int argc, char *argv[]) ( subSurf.points(), faces, - (surfFilePath / surfFileNameBase), + (surfFilePath / surfFileStem), false // serial - already merged ); @@ -607,7 +606,7 @@ int main(int argc, char *argv[]) ( surf.points(), faces, - (surfFilePath / surfFileNameBase), + (surfFilePath / surfFileStem), false // serial - already merged ); @@ -810,26 +809,27 @@ int main(int argc, char *argv[]) if (!edgeFormat.empty() && openEdges.size()) { - const fileName openName + const fileName outputName ( - surfFileName.lessExt() + surfName.lessExt() + "_open." + edgeFormat ); - Info<< "Writing open edges to " << openName << " ..." << endl; - writeEdgeSet(openName, surf, openEdges); + Info<< "Writing open edges to " + << args.relativePath(outputName) << " ..." << endl; + writeEdgeSet(outputName, surf, openEdges); } if (!edgeFormat.empty() && multipleEdges.size()) { - const fileName multName + const fileName outputName ( - surfFileName.lessExt() + surfName.lessExt() + "_multiply." + edgeFormat ); Info<< "Writing multiply connected edges to " - << multName << " ..." << endl; - writeEdgeSet(multName, surf, multipleEdges); + << args.relativePath(outputName) << " ..." << endl; + writeEdgeSet(outputName, surf, multipleEdges); } } else @@ -878,7 +878,7 @@ int main(int argc, char *argv[]) faceZone, "zone", surfFilePath, - surfFileNameBase + surfFileStem ); if (numZones > outputThreshold) @@ -892,7 +892,7 @@ int main(int argc, char *argv[]) min(outputThreshold, numZones), faceZone, surfFilePath, - surfFileNameBase + surfFileStem ); } } @@ -944,7 +944,7 @@ int main(int argc, char *argv[]) normalZone, "normal", surfFilePath, - surfFileNameBase + surfFileStem ); if (numNormalZones > outputThreshold) @@ -958,7 +958,7 @@ int main(int argc, char *argv[]) min(outputThreshold, numNormalZones), normalZone, surfFilePath, - surfFileNameBase + "_normal" + surfFileStem + "_normal" ); } } diff --git a/applications/utilities/surface/surfaceConvert/surfaceConvert.C b/applications/utilities/surface/surfaceConvert/surfaceConvert.C index 3530da1ae1..e9851a432f 100644 --- a/applications/utilities/surface/surfaceConvert/surfaceConvert.C +++ b/applications/utilities/surface/surfaceConvert/surfaceConvert.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2020-2021 OpenCFD Ltd. + Copyright (C) 2020-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -68,13 +68,12 @@ using namespace Foam; static word getExtension(const fileName& name) { - word ext(name.ext()); - if (ext == "gz") - { - ext = name.lessExt().ext(); - } - - return ext; + return + ( + name.has_ext("gz") + ? name.stem().ext() + : name.ext() + ); } diff --git a/applications/utilities/surface/surfaceFeatureConvert/surfaceFeatureConvert.C b/applications/utilities/surface/surfaceFeatureConvert/surfaceFeatureConvert.C index 03c64a2f39..53379fe6f3 100644 --- a/applications/utilities/surface/surfaceFeatureConvert/surfaceFeatureConvert.C +++ b/applications/utilities/surface/surfaceFeatureConvert/surfaceFeatureConvert.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2015-2021 OpenCFD Ltd. + Copyright (C) 2015-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -44,13 +44,12 @@ using namespace Foam; static word getExtension(const fileName& name) { - word ext(name.ext()); - if (ext == "gz") - { - ext = name.lessExt().ext(); - } - - return ext; + return + ( + name.has_ext("gz") + ? name.stem().ext() + : name.ext() + ); } diff --git a/applications/utilities/surface/surfaceFeatureExtract/surfaceFeatureExtract.C b/applications/utilities/surface/surfaceFeatureExtract/surfaceFeatureExtract.C index 211809ea4e..6b70328df7 100644 --- a/applications/utilities/surface/surfaceFeatureExtract/surfaceFeatureExtract.C +++ b/applications/utilities/surface/surfaceFeatureExtract/surfaceFeatureExtract.C @@ -291,7 +291,7 @@ int main(int argc, char *argv[]) // (ie, probably not a surface filename at all). // If it is missing, this will fail nicely with an appropriate error // message. - if (surfaceDict.found("surfaces") || !dictName.hasExt()) + if (surfaceDict.found("surfaces") || !dictName.has_ext()) { loader.select(surfaceDict.get("surfaces")); } diff --git a/applications/utilities/surface/surfaceMeshConvert/surfaceMeshConvert.C b/applications/utilities/surface/surfaceMeshConvert/surfaceMeshConvert.C index 53cfa96511..a76ab2af02 100644 --- a/applications/utilities/surface/surfaceMeshConvert/surfaceMeshConvert.C +++ b/applications/utilities/surface/surfaceMeshConvert/surfaceMeshConvert.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2018-2021 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -81,15 +81,15 @@ using namespace Foam; static word getExtension(const fileName& name) { - word ext(name.ext()); - if (ext == "gz") - { - ext = name.lessExt().ext(); - } - - return ext; + return + ( + name.has_ext("gz") + ? name.stem().ext() + : name.ext() + ); } + // Non-short-circuiting check to get all warnings static bool hasReadWriteTypes(const word& readType, const word& writeType) { diff --git a/applications/utilities/surface/surfaceMeshExport/surfaceMeshExport.C b/applications/utilities/surface/surfaceMeshExport/surfaceMeshExport.C index 8469ed0b33..cc9b33e51e 100644 --- a/applications/utilities/surface/surfaceMeshExport/surfaceMeshExport.C +++ b/applications/utilities/surface/surfaceMeshExport/surfaceMeshExport.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2018-2021 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -79,13 +79,12 @@ using namespace Foam; static word getExtension(const fileName& name) { - word ext(name.ext()); - if (ext == "gz") - { - ext = name.lessExt().ext(); - } - - return ext; + return + ( + name.has_ext("gz") + ? name.stem().ext() + : name.ext() + ); } diff --git a/applications/utilities/surface/surfaceMeshExtract/surfaceMeshExtract.C b/applications/utilities/surface/surfaceMeshExtract/surfaceMeshExtract.C index cd9c96f667..341566fb94 100644 --- a/applications/utilities/surface/surfaceMeshExtract/surfaceMeshExtract.C +++ b/applications/utilities/surface/surfaceMeshExtract/surfaceMeshExtract.C @@ -157,7 +157,7 @@ int main(int argc, char *argv[]) const auto userOutFileName = args.get(1); - if (!userOutFileName.hasExt()) + if (!userOutFileName.has_ext()) { FatalErrorInFunction << "Missing extension on output name " << userOutFileName diff --git a/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C b/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C index ba7ca665d2..702b49b5a4 100644 --- a/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C +++ b/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2018-2021 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -79,13 +79,12 @@ using namespace Foam; static word getExtension(const fileName& name) { - word ext(name.ext()); - if (ext == "gz") - { - ext = name.lessExt().ext(); - } - - return ext; + return + ( + name.has_ext("gz") + ? name.stem().ext() + : name.ext() + ); } diff --git a/applications/utilities/surface/surfacePatch/surfacePatch.C b/applications/utilities/surface/surfacePatch/surfacePatch.C index 5a5cc8b36e..0a2ea07db3 100644 --- a/applications/utilities/surface/surfacePatch/surfacePatch.C +++ b/applications/utilities/surface/surfacePatch/surfacePatch.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2013 OpenFOAM Foundation - Copyright (C) 2015-2020 OpenCFD Ltd. + Copyright (C) 2015-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -156,10 +156,10 @@ int main(int argc, char *argv[]) if (changed) { - const fileName name(surf.name()); - surf.rename(name.lessExt() + "_patched." + name.ext()); + const word oldName(surf.name()); + surf.rename(oldName.lessExt() + "_patched." + oldName.ext()); - Info<< "Writing repatched surface " << name << " to " + Info<< "Writing repatched surface " << oldName << " to " << surf.name() << nl << endl; surf.write(); diff --git a/applications/utilities/surface/surfaceTransformPoints/surfaceTransformPoints.C b/applications/utilities/surface/surfaceTransformPoints/surfaceTransformPoints.C index 4db518c5bf..fa0412d62f 100644 --- a/applications/utilities/surface/surfaceTransformPoints/surfaceTransformPoints.C +++ b/applications/utilities/surface/surfaceTransformPoints/surfaceTransformPoints.C @@ -64,13 +64,12 @@ using namespace Foam::coordinateRotations; static word getExtension(const fileName& name) { - word ext(name.ext()); - if (ext == "gz") - { - ext = name.lessExt().ext(); - } - - return ext; + return + ( + name.has_ext("gz") + ? name.stem().ext() + : name.ext() + ); } diff --git a/src/OSspecific/MSwindows/MSwindows.C b/src/OSspecific/MSwindows/MSwindows.C index 37ef5d356e..ded1de3238 100644 --- a/src/OSspecific/MSwindows/MSwindows.C +++ b/src/OSspecific/MSwindows/MSwindows.C @@ -725,7 +725,6 @@ Foam::fileNameList Foam::readDir // Basic sanity: cannot strip '.gz' from directory names const bool stripgz = filtergz && (type != fileName::DIRECTORY); - const word extgz("gz"); fileNameList dirEntries; @@ -762,7 +761,7 @@ Foam::fileNameList Foam::readDir // Validate filename without quotes, etc in the name. // No duplicate slashes to strip - dirent will not have them anyhow. - const fileName name(fileName::validate(item)); + fileName name(fileName::validate(item)); if (name != item) { ++nFailed; @@ -780,14 +779,13 @@ Foam::fileNameList Foam::readDir dirEntries.resize(dirEntries.size() + maxNnames); } - if (stripgz && name.hasExt(extgz)) + if (stripgz && name.has_ext("gz")) { - dirEntries[nEntries++] = name.lessExt(); - } - else - { - dirEntries[nEntries++] = name; + name.remove_ext(); } + + dirEntries[nEntries] = std::move(name); + ++nEntries; } } } @@ -1238,7 +1236,7 @@ void* Foam::dlOpen(const fileName& libName, const bool check) if ( !handle - && libName.find('/') == std::string::npos + && !libName.has_path() && !libso.starts_with("lib") ) { diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index c4935ff499..9a584d428c 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -901,7 +901,6 @@ Foam::fileNameList Foam::readDir // Basic sanity: cannot strip '.gz' from directory names const bool stripgz = filtergz && (type != fileName::DIRECTORY); - const word extgz("gz"); fileNameList dirEntries; @@ -941,7 +940,7 @@ Foam::fileNameList Foam::readDir // Validate filename without spaces, quotes, etc in the name. // No duplicate slashes to strip - dirent will not have them anyhow. - const fileName name(fileName::validate(item)); + fileName name(fileName::validate(item)); if (name != item) { ++nFailed; @@ -959,14 +958,13 @@ Foam::fileNameList Foam::readDir dirEntries.resize(dirEntries.size() + maxNnames); } - if (stripgz && name.hasExt(extgz)) + if (stripgz && name.has_ext("gz")) { - dirEntries[nEntries++] = name.lessExt(); - } - else - { - dirEntries[nEntries++] = name; + name.remove_ext(); } + + dirEntries[nEntries] = std::move(name); + ++nEntries; } } } @@ -1696,11 +1694,7 @@ void* Foam::dlOpen(const fileName& libName, const bool check) { fileName libso; - if - ( - libName.find('/') == std::string::npos - && !libName.starts_with("lib") - ) + if (!libName.has_path() && !libName.starts_with("lib")) { // Try with 'lib' prefix libso = "lib" + libName; @@ -1720,9 +1714,9 @@ void* Foam::dlOpen(const fileName& libName, const bool check) // With canonical library extension ("so" or "dylib"), which remaps // "libXX" to "libXX.so" as well as "libXX.so" -> "libXX.dylib" - if (!handle && !libso.hasExt(EXT_SO)) + if (!handle && !libso.has_ext(EXT_SO)) { - libso = libso.lessExt().ext(EXT_SO); + libso.replace_ext(EXT_SO); handle = ::dlopen(libso.c_str(), ldflags); if (POSIX::debug) diff --git a/src/OSspecific/POSIX/printStack/printStack.C b/src/OSspecific/POSIX/printStack/printStack.C index 026e71ee57..358ee8e8c0 100644 --- a/src/OSspecific/POSIX/printStack/printStack.C +++ b/src/OSspecific/POSIX/printStack/printStack.C @@ -140,7 +140,7 @@ void printSourceFileAndLine // On other systems (Linux), only use relative addresses for libraries. #ifndef __APPLE__ - if (filename.hasExt("so")) + if (filename.has_ext("so")) #endif { // Convert address into offset into dynamic library diff --git a/src/OpenFOAM/db/dictionary/dictionarySearch.C b/src/OpenFOAM/db/dictionary/dictionarySearch.C index b26e5988d3..b80e29a9c7 100644 --- a/src/OpenFOAM/db/dictionary/dictionarySearch.C +++ b/src/OpenFOAM/db/dictionary/dictionarySearch.C @@ -327,7 +327,7 @@ Foam::dictionary::const_searcher Foam::dictionary::csearchScoped enum keyType::option matchOpt ) const { - if (keyword.find('/') != string::npos) + if (keyword.contains('/')) { return csearchSlashScoped(keyword, matchOpt); } diff --git a/src/OpenFOAM/db/dictionary/entry/entryIO.C b/src/OpenFOAM/db/dictionary/entry/entryIO.C index 886458d77f..1b5d9fab2a 100644 --- a/src/OpenFOAM/db/dictionary/entry/entryIO.C +++ b/src/OpenFOAM/db/dictionary/entry/entryIO.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2018-2021 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -295,7 +295,7 @@ bool Foam::entry::New const bool scoped = ( !disableFunctionEntries - && (keyword.find('/') != string::npos) + && keyword.contains('/') ); // See (using exact match) if entry already present diff --git a/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C index 7756187118..2126d797e3 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011 OpenFOAM Foundation - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -61,7 +61,7 @@ bool Foam::functionEntries::removeEntry::execute for (const wordRe& key : patterns) { - if (key.isLiteral() && key.find('/') != string::npos) + if (key.isLiteral() && key.contains('/')) { // Remove scoped keyword, or keyword in the local scope auto finder(parentDict.searchScoped(key, keyType::LITERAL)); diff --git a/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C b/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C index 9a5ad023d9..a3d690ab94 100644 --- a/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C +++ b/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2018-2020 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -59,7 +59,7 @@ std::unique_ptr Foam::dlLibraryTable::global_(nullptr); Foam::word Foam::dlLibraryTable::basename(const fileName& libPath) { - word libName(libPath.nameLessExt()); + word libName(libPath.stem()); libName.removeStart("lib"); // Remove leading 'lib' from name return libName; } diff --git a/src/OpenFOAM/expressions/exprString/exprStringI.H b/src/OpenFOAM/expressions/exprString/exprStringI.H index 8218d709f6..bcae5d9f19 100644 --- a/src/OpenFOAM/expressions/exprString/exprStringI.H +++ b/src/OpenFOAM/expressions/exprString/exprStringI.H @@ -174,7 +174,7 @@ Foam::expressions::exprString::toExpr inline bool Foam::expressions::exprString::valid() const { - const bool ok = (std::string::npos == find('$')); + const bool ok = !contains('$'); #ifdef FULLDEBUG if (!ok) diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C index d4e53691aa..18fc14f7b3 100644 --- a/src/OpenFOAM/global/argList/argList.C +++ b/src/OpenFOAM/global/argList/argList.C @@ -1924,7 +1924,7 @@ void Foam::argList::displayDoc(bool source) const } // Can use FOAM_DOC_BROWSER='application file://%f' if required - if (docBrowser.find("%f") != std::string::npos) + if (docBrowser.contains("%f")) { docBrowser.replace("%f", url); } diff --git a/src/OpenFOAM/primitives/hashes/Hash/Hasher.H b/src/OpenFOAM/primitives/hashes/Hash/Hasher.H index fb31cdb9f5..17e2236428 100644 --- a/src/OpenFOAM/primitives/hashes/Hash/Hasher.H +++ b/src/OpenFOAM/primitives/hashes/Hash/Hasher.H @@ -43,8 +43,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef Hasher_H -#define Hasher_H +#ifndef Foam_Hasher_H +#define Foam_Hasher_H #include diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C index dd0da5a0ba..d91aa11942 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C @@ -211,25 +211,19 @@ bool Foam::SHA1Digest::operator==(const SHA1Digest& rhs) const bool Foam::SHA1Digest::operator==(const std::string& hexdigits) const { - // Null or empty string is not an error - interpret as '0000..' - if (hexdigits.empty()) - { - return empty(); - } + // Interpret empty string as '0000..' + size_t len = hexdigits.length(); - return isEqual(hexdigits.data(), hexdigits.length()); + return len ? isEqual(hexdigits.data(), len) : empty(); } bool Foam::SHA1Digest::operator==(const char* hexdigits) const { - // Null or empty string is not an error - interpret as '0000..' - if (!hexdigits || !*hexdigits) - { - return empty(); - } + // Interpret nullptr or empty string as '0000..' + size_t len = (hexdigits ? strlen(hexdigits) : 0); - return isEqual(hexdigits, std::char_traits::length(hexdigits)); + return len ? isEqual(hexdigits, len) : empty(); } diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index 1b255af7a3..4b921ba4a9 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -393,7 +393,7 @@ bool Foam::fileName::clean() } -std::string Foam::fileName::nameLessExt(const std::string& str) +std::string Foam::fileName::stem(const std::string& str) { auto beg = str.rfind('/'); auto dot = str.rfind('.'); @@ -421,6 +421,29 @@ std::string Foam::fileName::nameLessExt(const std::string& str) } +Foam::fileName& Foam::fileName::replace_name(const word& newName) +{ + const auto len = newName.length(); + + if (len) + { + auto beg = rfind('/'); + + if (beg == npos) + { + fileName::assign(newName); + } + else + { + ++beg; + replace(beg, length()-beg, newName); + } + } + + return *this; +} + + Foam::fileName Foam::fileName::relative ( const fileName& parent, diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.H b/src/OpenFOAM/primitives/strings/fileName/fileName.H index 32e4cc5f7a..287438df6b 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.H @@ -254,42 +254,6 @@ public: // Decomposition - //- Return basename (part beyond last /), including its extension - // The result normally corresponds to a Foam::word - // - // Behaviour compared to /usr/bin/basename: - // \verbatim - // input name() basename - // ----- ------ -------- - // "" "" "" - // "abc" "abc" "abc" - // "/" "" "/" - // "/abc" "abc" "abc" - // "abc/def" "def" "def" - // "/abc/def" "def" "def" - // "/abc/def/" "" "def" - // "/abc/../def" "def" "def" - // \endverbatim - inline static std::string name(const std::string& str); - - //- Return basename (part beyond last /), including its extension - inline word name() const; - - //- Return basename, without extension - // The result normally corresponds to a Foam::word - static std::string nameLessExt(const std::string& str); - - //- Return basename, without extension - inline word nameLessExt() const; - - //- Deprecated(2017-03) return basename, optionally without extension - // \deprecated(2017-03) - use name() or nameLessExt() methods - // which describe their behaviour explicitly - word name(const bool noExt) const - { - return noExt ? this->nameLessExt() : this->name(); - } - //- Return directory path name (part before last /) // The result normally corresponds to a Foam::fileName // @@ -311,11 +275,65 @@ public: //- Return directory path name (part before last /) inline fileName path() const; - //- Return true if it contains a '/' character - inline bool hasPath() const; + //- Remove leading path, return true if string changed. + using string::remove_path; + + //- True if it contains a '/' character + inline bool has_path() const; + + + //- Return basename (part beyond last /), including its extension + // The result normally corresponds to a Foam::word + // + // Behaviour compared to /usr/bin/basename: + // \verbatim + // input name() basename + // ----- ------ -------- + // "" "" "" + // "abc" "abc" "abc" + // "/" "" "/" + // "/abc" "abc" "abc" + // "abc/def" "def" "def" + // "/abc/def" "def" "def" + // "/abc/def/" "" "def" + // "/abc/../def" "def" "def" + // \endverbatim + inline static std::string name(const std::string& str); + + //- Return basename (part beyond last /), including its extension + inline word name() const; + + //- Replace basename (part beyond last /) with a new name + fileName& replace_name(const word& newName); + + //- Return the basename, without extension + // The result normally corresponds to a Foam::word + static std::string stem(const std::string& str); + + //- Return basename, without extension + inline word stem() const; + + + //- Return file name extension (part after last .) + inline word ext() const; + + //- Append a '.' and the ending, and return the object. + // The '.' and ending will not be added when the ending is empty, + // or when the file name is empty or ended with a '/'. + inline fileName& ext(const word& ending); + + //- Remove extension (if any) and append a new one + inline fileName& replace_ext(const word& ending); + + //- Return file name without extension (part before last .) + inline fileName lessExt() const; + + //- Remove extension, returning true if string changed. + using string::remove_ext; + + //- Various checks for extensions + using string::has_ext; - //- Remove leading path, returning true if string changed. - inline bool removePath(); //- Return a relative name by stripping off the parent directory //- where possible. @@ -330,23 +348,6 @@ public: const bool caseTag = false ) const; - //- Return file name without extension (part before last .) - inline fileName lessExt() const; - - //- Return file name extension (part after last .) - inline word ext() const; - - //- Append a '.' and the ending, and return the object. - // The '.' and ending will not be added when the ending is empty, - // or when the file name is empty or ended with a '/'. - inline fileName& ext(const word& ending); - - //- Various checks for extensions - using string::hasExt; - - //- Remove extension, returning true if string changed. - using string::removeExt; - //- Return path components as wordList // @@ -408,8 +409,44 @@ public: //- Append a path element with '/' separator. // No '/' separator is added if this or the argument are empty. fileName& operator/=(const string& other); + + + // Housekeeping + + //- Same as has_path() + bool hasPath() const { return has_path(); } + + //- Same as remove_path() + bool removePath() { return remove_path(); } + + //- Same as has_ext() + bool hasExt() const { return has_ext(); } + + //- Same as has_ext() + bool hasExt(const std::string& s) const { return has_ext(s); } + + //- Same as remove_ext() + bool removeExt() { return remove_ext(); } + + //- Same as stem() + static std::string nameLessExt(const std::string& s) + { + return fileName::stem(s); + } + + //- Same as stem() + word nameLessExt() const { return stem(); } + + //- Deprecated(2017-03) return basename, optionally without extension + // \deprecated(2017-03) - use name() or stem() methods + // which describe their behaviour explicitly + word name(const bool noExt) const + { + return noExt ? stem() : name(); + } }; +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // IOstream Operators diff --git a/src/OpenFOAM/primitives/strings/fileName/fileNameI.H b/src/OpenFOAM/primitives/strings/fileName/fileNameI.H index ed1e860e45..a83f4bb55d 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileNameI.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileNameI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011 OpenFOAM Foundation - Copyright (C) 2016-2021 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -157,7 +157,7 @@ inline bool Foam::fileName::isAbsolute(const std::string& str) inline bool Foam::fileName::isAbsolute() const { - return isAbsolute(*this); + return fileName::isAbsolute(*this); } @@ -167,9 +167,9 @@ inline bool Foam::fileName::isBackup() const } -inline bool Foam::fileName::hasPath() const +inline bool Foam::fileName::has_path() const { - return string::hasPath(); + return contains('/'); } @@ -177,7 +177,7 @@ inline std::string Foam::fileName::path(const std::string& str) { const auto i = str.rfind('/'); - if (i == npos) + if (i == std::string::npos) { return "."; } @@ -192,7 +192,7 @@ inline std::string Foam::fileName::path(const std::string& str) inline Foam::fileName Foam::fileName::path() const { - return path(*this); + return fileName::path(*this); } @@ -200,7 +200,7 @@ inline std::string Foam::fileName::name(const std::string& str) { const auto i = str.rfind('/'); - if (npos == i) + if (i == std::string::npos) { return str; } @@ -221,28 +221,9 @@ Foam::word Foam::fileName::ext() const } -inline Foam::word Foam::fileName::nameLessExt() const +inline Foam::word Foam::fileName::stem() const { - return nameLessExt(*this); -} - - -inline Foam::fileName Foam::fileName::lessExt() const -{ - const auto i = find_ext(); - - if (i == npos) - { - return *this; - } - - return substr(0, i); -} - - -inline bool Foam::fileName::removePath() -{ - return string::removePath(); + return fileName::stem(*this); } @@ -253,6 +234,27 @@ inline Foam::fileName& Foam::fileName::ext(const word& ending) } +inline Foam::fileName& Foam::fileName::replace_ext(const word& ending) +{ + string::remove_ext(); + string::ext(ending); + return *this; +} + + +inline Foam::fileName Foam::fileName::lessExt() const +{ + const auto i = find_ext(); + + if (i == std::string::npos) + { + return *this; + } + + return substr(0, i); +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // inline Foam::fileName& Foam::fileName::operator=(const fileName& str) diff --git a/src/OpenFOAM/primitives/strings/regex/regExpCxx.H b/src/OpenFOAM/primitives/strings/regex/regExpCxx.H index cb1672dab5..d2f288e1be 100644 --- a/src/OpenFOAM/primitives/strings/regex/regExpCxx.H +++ b/src/OpenFOAM/primitives/strings/regex/regExpCxx.H @@ -235,7 +235,7 @@ public: // Matching/Searching //- Find position within the text. - // \return The index where it begins or string::npos if not found + // \return The index where it begins or std::string::npos if not found // // \note does not properly work with negated regex! inline std::string::size_type find(const std::string& text) const; diff --git a/src/OpenFOAM/primitives/strings/string/string.C b/src/OpenFOAM/primitives/strings/string/string.C index 04f0b91662..6c60810303 100644 --- a/src/OpenFOAM/primitives/strings/string/string.C +++ b/src/OpenFOAM/primitives/strings/string/string.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2016-2021 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -48,7 +48,7 @@ Foam::word Foam::string::ext() const if (i == npos) { - return word::null; + return word(); } return substr(i+1); @@ -78,11 +78,11 @@ bool Foam::string::ext(const word& ending) } -bool Foam::string::hasExt(const wordRe& ending) const +bool Foam::string::has_ext(const wordRe& ending) const { - if (ending.isLiteral() || ending.empty()) + if (ending.empty() || ending.isLiteral()) { - return hasExt(static_cast(ending)); + return has_ext(static_cast(ending)); } const auto i = find_ext(); diff --git a/src/OpenFOAM/primitives/strings/string/string.H b/src/OpenFOAM/primitives/strings/string/string.H index d5646b4b62..f90c9c225a 100644 --- a/src/OpenFOAM/primitives/strings/string/string.H +++ b/src/OpenFOAM/primitives/strings/string/string.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2016-2021 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -47,8 +47,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef string_H -#define string_H +#ifndef Foam_string_H +#define Foam_string_H #include "char.H" #include "Hasher.H" @@ -118,26 +118,24 @@ protected: // \return True if append occurred. bool ext(const word& ending); - //- Return true if it contains a '/' character - inline bool hasPath() const; - //- Return true if it has an extension or simply ends with a '.' - inline bool hasExt() const; + inline bool has_ext() const; //- Return true if the extension is the same as the given ending. - inline bool hasExt(const char* ending) const; + // No proper nullptr protection. + inline bool has_ext(const char* ending) const; //- Return true if the extension is the same as the given ending. - inline bool hasExt(const std::string& ending) const; + inline bool has_ext(const std::string& ending) const; //- Return true if the extension matches the given ending. - bool hasExt(const wordRe& ending) const; + bool has_ext(const wordRe& ending) const; - //- Remove extension, returning true if string changed. - inline bool removeExt(); + //- Remove leading path, return true if string changed. + inline bool remove_path(); - //- Remove leading path, returning true if string changed. - inline bool removePath(); + //- Remove extension, return true if string changed. + inline bool remove_ext(); public: @@ -294,28 +292,60 @@ public: // Housekeeping - //- True if string starts with the given prefix (cf. C++20) + //- True if string contains given character (cf. C++23) + bool contains(char c) const noexcept + { + return (find(c) != std::string::npos); + } + + //- True if string contains given [string view] substring (cf. C++23) + bool contains(const std::string& s) const noexcept + { + return (find(s) != std::string::npos); + } + + //- True if string contains given substring (cf. C++23) + bool contains(const char* s) const + { + return (find(s) != std::string::npos); + } + + //- True if string starts with given character (cf. C++20) + bool starts_with(char c) const + { + return (!empty() && front() == c); + } + + //- True if string starts with given [string view] prefix (C++20) bool starts_with(const std::string& s) const { return (size() >= s.size() && !compare(0, s.size(), s)); } - //- True if string starts with the given character (cf. C++20) - bool starts_with(const char c) const + //- True if string starts with given prefix (C++20) + bool starts_with(const char* s) const { - return (!empty() && front() == c); + const auto len = strlen(s); + return (size() >= len && !compare(0, len, s, len)); } - //- True if string ends with the given suffix (cf. C++20) + //- True if string ends with given character (cf. C++20) + bool ends_with(char c) const + { + return (!empty() && back() == c); + } + + //- True if string ends with given [string view] suffix (cf. C++20) bool ends_with(const std::string& s) const { return (size() >= s.size() && !compare(size()-s.size(), npos, s)); } - //- True if string ends with the given character (cf. C++20) - bool ends_with(const char c) const + //- True if string ends with given suffix (cf. C++20) + bool ends_with(const char* s) const { - return (!empty() && back() == c); + const auto len = strlen(s); + return (size() >= len && !compare(size()-len, npos, s, len)); } //- Count the number of occurrences of the specified character diff --git a/src/OpenFOAM/primitives/strings/string/stringI.H b/src/OpenFOAM/primitives/strings/string/stringI.H index 31a1fbc79a..3f89a95198 100644 --- a/src/OpenFOAM/primitives/strings/string/stringI.H +++ b/src/OpenFOAM/primitives/strings/string/stringI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2017-2021 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -47,39 +47,43 @@ inline std::string::size_type Foam::string::find_ext() const } -inline bool Foam::string::hasPath() const +inline bool Foam::string::has_ext() const { - return (npos != find('/')); + return (std::string::npos != find_ext()); } -inline bool Foam::string::hasExt() const +inline bool Foam::string::has_ext(const char* ending) const { - return (npos != find_ext()); + const auto n1 = size(); + const auto n2 = strlen(ending); + + // Like ends_with with extra check for dot + return + ( + n1 > n2 + && operator[](n1-n2-1) == '.' // Require a dot separator + && !compare(n1-n2, npos, ending, n2) + ); } -inline bool Foam::string::hasExt(const char* ending) const +inline bool Foam::string::has_ext(const std::string& ending) const { - return (ending && string::hasExt(std::string(ending))); + const auto n1 = size(); + const auto n2 = ending.size(); + + // Like ends_with with extra check for dot + return + ( + n1 > n2 + && operator[](n1-n2-1) == '.' // Require a dot separator + && !compare(n1-n2, npos, ending) + ); } -inline bool Foam::string::hasExt(const std::string& ending) const -{ - const auto len = ending.size(); - auto i = find_ext(); - if (i == npos || !len) - { - return false; - } - - ++i; // Compare *after* the dot - return ((size() - i) == len) && !compare(i, npos, ending); -} - - -inline bool Foam::string::removePath() +inline bool Foam::string::remove_path() { const auto i = rfind('/'); @@ -93,7 +97,7 @@ inline bool Foam::string::removePath() } -inline bool Foam::string::removeExt() +inline bool Foam::string::remove_ext() { const auto i = find_ext(); diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C index 408d6d7e17..aa929ae450 100644 --- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C @@ -704,12 +704,8 @@ std::string::size_type Foam::stringOps::count std::string::size_type Foam::stringOps::count(const char* s, const char c) { - return - ( - s == nullptr - ? 0 - : std::count(s, (s + std::char_traits::length(s)), c) - ); + size_t len = (s ? strlen(s) : 0); + return len ? std::count(s, (s + len), c) : 0; } diff --git a/src/OpenFOAM/primitives/strings/word/word.C b/src/OpenFOAM/primitives/strings/word/word.C index bd9f5e0fa8..6465c41962 100644 --- a/src/OpenFOAM/primitives/strings/word/word.C +++ b/src/OpenFOAM/primitives/strings/word/word.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011 OpenFOAM Foundation - Copyright (C) 2017-2021 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -108,34 +108,6 @@ Foam::word Foam::word::validate } -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -Foam::word Foam::word::lessExt() const -{ - const auto i = find_ext(); - - if (i == npos) - { - return *this; - } - - return substr(0, i); -} - - -Foam::word Foam::word::ext() const -{ - return string::ext(); -} - - -Foam::word& Foam::word::ext(const word& ending) -{ - string::ext(ending); - return *this; -} - - // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // Foam::word Foam::operator&(const word& a, const word& b) diff --git a/src/OpenFOAM/primitives/strings/word/word.H b/src/OpenFOAM/primitives/strings/word/word.H index f3fb2eb7ed..642a9073a5 100644 --- a/src/OpenFOAM/primitives/strings/word/word.H +++ b/src/OpenFOAM/primitives/strings/word/word.H @@ -161,22 +161,25 @@ public: // File-like Functions - //- Return word without extension (part before last .) - word lessExt() const; - //- Return file name extension (part after last .) - word ext() const; + inline word ext() const; //- Append a '.' and the ending, and return the object. // The '.' and ending will not be added when the ending is empty, // or when the file name is empty or ended with a '/'. - word& ext(const word& ending); + inline word& ext(const word& ending); + + //- Remove extension (if any) and append a new one + inline word& replace_ext(const word& ending); + + //- Return word without extension (part before last .) + inline word lessExt() const; + + //- Remove extension, return true if string changed. + using string::remove_ext; //- Various checks for extensions - using string::hasExt; - - //- Remove extension, returning true if string changed. - using string::removeExt; + using string::has_ext; // Member Operators @@ -205,6 +208,18 @@ public: //- Copy, stripping invalid characters inline word& operator=(const char* s); + + + // Housekeeping + + //- Same as has_ext() + bool hasExt() const { return has_ext(); } + + //- Same as has_ext() + bool hasExt(const std::string& s) const { return has_ext(s); } + + //- Same as remove_ext() + bool removeExt() { return remove_ext(); } }; diff --git a/src/OpenFOAM/primitives/strings/word/wordI.H b/src/OpenFOAM/primitives/strings/word/wordI.H index bc907c744d..502e77f740 100644 --- a/src/OpenFOAM/primitives/strings/word/wordI.H +++ b/src/OpenFOAM/primitives/strings/word/wordI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2018-2021 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -161,6 +161,54 @@ inline void Foam::word::stripInvalid() } +// FUTURE? +// inline Foam::word Foam::word::stem() const +// { +// const auto i = find_ext(); // Or just rfind('.') +// +// if (i == std::string::npos) +// { +// return *this; +// } +// +// return substr(0, i); +// } + + +inline Foam::word Foam::word::ext() const +{ + return string::ext(); +} + + +inline Foam::word& Foam::word::ext(const word& ending) +{ + string::ext(ending); + return *this; +} + + +inline Foam::word& Foam::word::replace_ext(const word& ending) +{ + string::remove_ext(); + string::ext(ending); + return *this; +} + + +inline Foam::word Foam::word::lessExt() const +{ + const auto i = find_ext(); // Or just rfind('.') + + if (i == std::string::npos) + { + return *this; + } + + return substr(0, i); +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // inline Foam::word& Foam::word::operator=(const word& s) diff --git a/src/conversion/fire/FIREMeshWriter.C b/src/conversion/fire/FIREMeshWriter.C index 4476e6db6f..94eaec9514 100644 --- a/src/conversion/fire/FIREMeshWriter.C +++ b/src/conversion/fire/FIREMeshWriter.C @@ -323,7 +323,7 @@ bool Foam::fileFormats::FIREMeshWriter::write(const fileName& meshName) const } } - baseName = baseName.lessExt(); + baseName.remove_ext(); } diff --git a/src/fileFormats/abaqus/ABAQUSCore.C b/src/fileFormats/abaqus/ABAQUSCore.C index c99f217b03..ac09d7643b 100644 --- a/src/fileFormats/abaqus/ABAQUSCore.C +++ b/src/fileFormats/abaqus/ABAQUSCore.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2020 OpenCFD Ltd. + Copyright (C) 2020-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -201,11 +201,11 @@ Foam::fileFormats::ABAQUSCore::abaqusToFoamFaceAddr() Foam::fileFormats::ABAQUSCore::shapeType -Foam::fileFormats::ABAQUSCore::getElementType(const std::string& elemTypeName) +Foam::fileFormats::ABAQUSCore::getElementType(const string& elemTypeName) { - // Check for element-type - #undef checkElemType - #define checkElemType(test) (elemTypeName.find(test) != std::string::npos) + // Check for element-type. + #undef checkElemType + #define checkElemType(Name) elemTypeName.contains(Name) if ( diff --git a/src/fileFormats/abaqus/ABAQUSCore.H b/src/fileFormats/abaqus/ABAQUSCore.H index 4149d59621..c74924dcaa 100644 --- a/src/fileFormats/abaqus/ABAQUSCore.H +++ b/src/fileFormats/abaqus/ABAQUSCore.H @@ -76,8 +76,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef ABAQUSCore_H -#define ABAQUSCore_H +#ifndef Foam_ABAQUSCore_H +#define Foam_ABAQUSCore_H #include "Fstream.H" #include "Enum.H" @@ -121,7 +121,7 @@ public: //- Classify named element type (eg, S4R) to known/supported //- element types. // The input string must be Uppercase! - static shapeType getElementType(const std::string& elemTypeName); + static shapeType getElementType(const string& elemTypeName); //- The number of points associated with the element type inline static int nPoints(shapeType tag) diff --git a/src/fileFormats/ensight/read/ensightReadFile.C b/src/fileFormats/ensight/read/ensightReadFile.C index 5a6ba00137..e7fd13d504 100644 --- a/src/fileFormats/ensight/read/ensightReadFile.C +++ b/src/fileFormats/ensight/read/ensightReadFile.C @@ -48,7 +48,7 @@ Foam::ensightReadFile::detectBinaryHeader(const fileName& pathname) istream& iss = is.stdStream(); // Binary string is *exactly* 80 characters - std::string buf(size_t(80), '\0'); + string buf(size_t(80), '\0'); iss.read(&buf[0], 80); if (!iss) @@ -64,12 +64,8 @@ Foam::ensightReadFile::detectBinaryHeader(const fileName& pathname) buf.erase(endp); } - // Contains "C Binary" ? - if - ( - (buf.find("Binary") == std::string::npos) - && (buf.find("binary") == std::string::npos) - ) + // ASCII if it does not contain "C Binary" + if (!buf.contains("Binary") && !buf.contains("binary")) { fmt = IOstreamOption::ASCII; } diff --git a/src/fileFormats/gltf/foamGltfScene.C b/src/fileFormats/gltf/foamGltfScene.C index c1ae6c7d9f..d40a5bcb69 100644 --- a/src/fileFormats/gltf/foamGltfScene.C +++ b/src/fileFormats/gltf/foamGltfScene.C @@ -161,8 +161,8 @@ void Foam::glTF::scene::addToAnimation void Foam::glTF::scene::write(const fileName& outputFile) { - fileName jsonFile(outputFile.lessExt()); - jsonFile.ext("gltf"); + fileName jsonFile(outputFile); + jsonFile.replace_ext("gltf"); // Note: called on master only @@ -178,8 +178,8 @@ void Foam::glTF::scene::write(const fileName& outputFile) void Foam::glTF::scene::write(Ostream& os) { - fileName binFile(os.name().lessExt()); - binFile.ext("bin"); + fileName binFile(os.name()); + binFile.replace_ext("bin"); // Write binary file // Note: using stdStream diff --git a/src/fileFormats/gltf/foamGltfSceneWriter.C b/src/fileFormats/gltf/foamGltfSceneWriter.C index 897117b75a..c36161cea3 100644 --- a/src/fileFormats/gltf/foamGltfSceneWriter.C +++ b/src/fileFormats/gltf/foamGltfSceneWriter.C @@ -78,8 +78,8 @@ void Foam::glTF::sceneWriter::open(const fileName& outputFile) { close(); - fileName jsonFile(outputFile.lessExt()); - jsonFile.ext("gltf"); + fileName jsonFile(outputFile); + jsonFile.replace_ext("gltf"); // Note: called on master only if (!isDir(jsonFile.path())) diff --git a/src/fileFormats/stl/STLCore.C b/src/fileFormats/stl/STLCore.C index 9bd3864650..c51b4edbee 100644 --- a/src/fileFormats/stl/STLCore.C +++ b/src/fileFormats/stl/STLCore.C @@ -72,7 +72,7 @@ bool Foam::fileFormats::STLCore::isBinaryName return ( format == STLFormat::UNKNOWN - ? filename.hasExt("stlb") + ? filename.has_ext("stlb") : format == STLFormat::BINARY ); } @@ -236,7 +236,7 @@ void Foam::fileFormats::STLCore::writeBinaryHeader char header[STLHeaderSize]; sprintf(header, "STL binary file %u facets", nTris); - // avoid trailing junk + // Fill trailing with zeroes (to avoid writing junk) for (size_t i = strlen(header); i < STLHeaderSize; ++i) { header[i] = 0; diff --git a/src/fileFormats/vtk/file/foamVtkFileWriter.C b/src/fileFormats/vtk/file/foamVtkFileWriter.C index 119116f9be..a5a4533c3e 100644 --- a/src/fileFormats/vtk/file/foamVtkFileWriter.C +++ b/src/fileFormats/vtk/file/foamVtkFileWriter.C @@ -334,16 +334,15 @@ bool Foam::vtk::fileWriter::open(const fileName& file, bool parallel) if ( legacy() - ? outputFile_.hasExt(vtk::fileExtension[contentType_]) - : outputFile_.hasExt(vtk::legacy::fileExtension) + ? outputFile_.has_ext(vtk::fileExtension[contentType_]) + : outputFile_.has_ext(vtk::legacy::fileExtension) ) { // Inappropriate extension. Legacy instead of xml, or vice versa. - - outputFile_.removeExt(); + outputFile_.remove_ext(); } - if (!outputFile_.hasExt(ext())) + if (!outputFile_.has_ext(ext())) { // Add extension if required outputFile_.ext(ext()); diff --git a/src/fileFormats/vtk/file/foamVtkSeriesWriter.C b/src/fileFormats/vtk/file/foamVtkSeriesWriter.C index 6185cad622..c5539f3957 100644 --- a/src/fileFormats/vtk/file/foamVtkSeriesWriter.C +++ b/src/fileFormats/vtk/file/foamVtkSeriesWriter.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2021 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -162,7 +162,7 @@ Foam::Ostream& Foam::vtk::seriesWriter::print // stem = "file" // ext = ".vtm" - const word stem = base.nameLessExt(); + const word stem = base.stem(); const word ext = "." + base.ext(); // Begin file-series (JSON) @@ -240,7 +240,7 @@ void Foam::vtk::seriesWriter::write autoPtr osPtr = ( - seriesName.hasExt("series") + seriesName.has_ext("series") ? autoPtr::New(seriesName) : autoPtr::New(seriesName + ".series") ); @@ -260,7 +260,7 @@ void Foam::vtk::seriesWriter::write autoPtr osPtr = ( - seriesName.hasExt("series") + seriesName.has_ext("series") ? autoPtr::New(seriesName) : autoPtr::New(seriesName + ".series") ); @@ -372,7 +372,7 @@ Foam::label Foam::vtk::seriesWriter::load clear(); fileName seriesFile(seriesName); - if (!seriesFile.hasExt("series")) + if (!seriesFile.has_ext("series")) { seriesFile.ext("series"); } @@ -597,12 +597,12 @@ Foam::label Foam::vtk::seriesWriter::scan fileName seriesFile(seriesName); - if (seriesName.hasExt("series")) + if (seriesName.has_ext("series")) { - seriesFile.removeExt(); + seriesFile.remove_ext(); } - const word stem = seriesFile.nameLessExt(); + const word stem = seriesFile.stem(); const word ext = seriesFile.ext(); // Accept "fileN.ext", "fileNN.ext", but reject "file.ext" @@ -614,7 +614,7 @@ Foam::label Foam::vtk::seriesWriter::scan return ( minLen < file.length() - && file.hasExt(ext) && file.starts_with(stem) + && file.has_ext(ext) && file.starts_with(stem) ); }; diff --git a/src/fileFormats/vtk/file/foamVtkSeriesWriterI.H b/src/fileFormats/vtk/file/foamVtkSeriesWriterI.H index cd69137f04..87c5c7e9cd 100644 --- a/src/fileFormats/vtk/file/foamVtkSeriesWriterI.H +++ b/src/fileFormats/vtk/file/foamVtkSeriesWriterI.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2019 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -56,7 +56,7 @@ inline bool Foam::vtk::seriesWriter::append(const fileNameInstant& inst) inline bool Foam::vtk::seriesWriter::append(fileNameInstant&& inst) { // Strip out path before saving - inst.name().removePath(); + inst.name().remove_path(); return appendCheck(inst); } @@ -80,7 +80,7 @@ inline bool Foam::vtk::seriesWriter::append ) { // Strip out path before saving - file.removePath(); + file.remove_path(); return appendCheck(fileNameInstant(timeValue, std::move(file))); } diff --git a/src/fileFormats/vtk/file/foamVtmWriter.C b/src/fileFormats/vtk/file/foamVtmWriter.C index fe75496a02..cf5b75160f 100644 --- a/src/fileFormats/vtk/file/foamVtmWriter.C +++ b/src/fileFormats/vtk/file/foamVtmWriter.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2019 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -421,7 +421,7 @@ bool Foam::vtk::vtmWriter::append(const fileName& file) { if (autoName_) { - return append(fileName::nameLessExt(file), file); + return append(fileName::stem(file), file); } return append(word::null, file); @@ -436,7 +436,7 @@ bool Foam::vtk::vtmWriter::append { if (autoName_) { - return append(fileName::nameLessExt(file), file, contentType); + return append(fileName::stem(file), file, contentType); } return append(word::null, file, contentType); @@ -471,7 +471,7 @@ bool Foam::vtk::vtmWriter::append return false; } - if (file.hasExt(vtk::fileExtension[contentType])) + if (file.has_ext(vtk::fileExtension[contentType])) { entries_.append(vtmEntry::entry(name, file)); } @@ -582,7 +582,7 @@ Foam::label Foam::vtk::vtmWriter::write(const fileName& file) mkDir(file.path()); - if (file.hasExt(ext())) + if (file.has_ext(ext())) { os_.open(file); } diff --git a/src/fileFormats/vtk/format/foamVtkFormatter.C b/src/fileFormats/vtk/format/foamVtkFormatter.C index 00242cffce..f5e11ce1cb 100644 --- a/src/fileFormats/vtk/format/foamVtkFormatter.C +++ b/src/fileFormats/vtk/format/foamVtkFormatter.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2016-2018 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -256,7 +256,7 @@ Foam::vtk::formatter& Foam::vtk::formatter::DataSet { if (autoName) { - xmlAttr("name", fileName::nameLessExt(file)); + xmlAttr("name", fileName::stem(file)); } xmlAttr("file", file); } diff --git a/src/finiteVolume/cfdTools/general/solutionControl/solutionControl/solutionControlTemplates.C b/src/finiteVolume/cfdTools/general/solutionControl/solutionControl/solutionControlTemplates.C index 5c2bf0fe4b..42a1cfd674 100644 --- a/src/finiteVolume/cfdTools/general/solutionControl/solutionControl/solutionControlTemplates.C +++ b/src/finiteVolume/cfdTools/general/solutionControl/solutionControl/solutionControlTemplates.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2013 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -45,11 +45,7 @@ void Foam::solutionControl::storePrevIter() const const word& fldName = fld.name(); - if - ( - (fldName.find("PrevIter") == std::string::npos) - && mesh_.relaxField(fldName) - ) + if (!fldName.contains("PrevIter") && mesh_.relaxField(fldName)) { DebugInfo << algorithmName_ << ": storing previous iter for " diff --git a/src/functionObjects/field/ddt2/ddt2.C b/src/functionObjects/field/ddt2/ddt2.C index 7fa82ad074..0cdc325297 100644 --- a/src/functionObjects/field/ddt2/ddt2.C +++ b/src/functionObjects/field/ddt2/ddt2.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2016-2021 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -45,14 +45,16 @@ namespace functionObjects } } -// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -bool Foam::functionObjects::ddt2::checkFormatName -( - const std::string& str -) +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // + +namespace Foam { - if (std::string::npos == str.find("@@")) + +// Check that string contains the appropriate substitution token(s) +static bool checkFormatName(const word& str) +{ + if (!str.contains("@@")) { WarningInFunction << "Bad result naming (no '@@' token found)." @@ -72,6 +74,10 @@ bool Foam::functionObjects::ddt2::checkFormatName return true; } +} // End namespace Foam + + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // bool Foam::functionObjects::ddt2::accept(const word& fieldName) const { diff --git a/src/functionObjects/field/ddt2/ddt2.H b/src/functionObjects/field/ddt2/ddt2.H index 7cb8a734e2..ea494cff4b 100644 --- a/src/functionObjects/field/ddt2/ddt2.H +++ b/src/functionObjects/field/ddt2/ddt2.H @@ -150,10 +150,6 @@ class ddt2 // Private Member Functions - //- Check that string contains the appropriate substitution token(s) - static bool checkFormatName(const std::string& str); - - //- Accept unless field name appears to have already been processed bool accept(const word& fieldName) const; diff --git a/src/functionObjects/field/zeroGradient/zeroGradient.C b/src/functionObjects/field/zeroGradient/zeroGradient.C index cdcb08667f..d0b953252e 100644 --- a/src/functionObjects/field/zeroGradient/zeroGradient.C +++ b/src/functionObjects/field/zeroGradient/zeroGradient.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2016-2020 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -43,14 +43,16 @@ namespace functionObjects } } -// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -bool Foam::functionObjects::zeroGradient::checkFormatName -( - const std::string& str -) +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // + +namespace Foam { - if (std::string::npos == str.find("@@")) + +// Check that string contains the appropriate substitution token(s) +static bool checkFormatName(const word& str) +{ + if (!str.contains("@@")) { WarningInFunction << "Bad result naming (no '@@' token found)." @@ -70,6 +72,10 @@ bool Foam::functionObjects::zeroGradient::checkFormatName return true; } +} // End namespace Foam + + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // int Foam::functionObjects::zeroGradient::process(const word& fieldName) { @@ -95,7 +101,7 @@ Foam::functionObjects::zeroGradient::zeroGradient : fvMeshFunctionObject(name, runTime, dict), selectFields_(), - resultName_(string::null), + resultName_(), results_() { read(dict); diff --git a/src/functionObjects/field/zeroGradient/zeroGradient.H b/src/functionObjects/field/zeroGradient/zeroGradient.H index 236cb61b3b..aaab1c799b 100644 --- a/src/functionObjects/field/zeroGradient/zeroGradient.H +++ b/src/functionObjects/field/zeroGradient/zeroGradient.H @@ -137,10 +137,6 @@ class zeroGradient // Private Member Functions - //- Check that string contains the appropriate substitution token(s) - static bool checkFormatName(const std::string& str); - - //- Accept unless field only has constraint patches // (ie, empty/zero-gradient/processor) // This should also avoid fields that were already processed by diff --git a/src/functionObjects/lagrangian/vtkCloud/vtkCloud.C b/src/functionObjects/lagrangian/vtkCloud/vtkCloud.C index 2b8c973822..3709429dcb 100644 --- a/src/functionObjects/lagrangian/vtkCloud/vtkCloud.C +++ b/src/functionObjects/lagrangian/vtkCloud/vtkCloud.C @@ -156,7 +156,7 @@ bool Foam::functionObjects::vtkCloud::writeCloud std::ofstream os; autoPtr format; - if (!file.hasExt("vtp")) + if (!file.has_ext("vtp")) { FatalErrorInFunction << type() << " File missing .vtp extension!" << nl << endl diff --git a/src/meshTools/coordSet/writers/gnuplot/gnuplotCoordSetWriter.C b/src/meshTools/coordSet/writers/gnuplot/gnuplotCoordSetWriter.C index 9da231f69a..5669120896 100644 --- a/src/meshTools/coordSet/writers/gnuplot/gnuplotCoordSetWriter.C +++ b/src/meshTools/coordSet/writers/gnuplot/gnuplotCoordSetWriter.C @@ -158,7 +158,7 @@ bool Foam::coordSetWriters::gnuplotWriter::writeBuffered() os.precision(precision_); os << "set term pngcairo" << nl - << "set output \"" << outputFile.nameLessExt() << ".png\"" << nl; + << "set output \"" << outputFile.stem() << ".png\"" << nl; label nplots = 0; do diff --git a/src/meshTools/coordSet/writers/gnuplot/gnuplotCoordSetWriterImpl.C b/src/meshTools/coordSet/writers/gnuplot/gnuplotCoordSetWriterImpl.C index 73884ac4d9..56db963aec 100644 --- a/src/meshTools/coordSet/writers/gnuplot/gnuplotCoordSetWriterImpl.C +++ b/src/meshTools/coordSet/writers/gnuplot/gnuplotCoordSetWriterImpl.C @@ -71,7 +71,7 @@ Foam::fileName Foam::coordSetWriters::gnuplotWriter::writeTemplate os.precision(precision_); os << "set term pngcairo" << nl - << "set output \"" << outputFile.nameLessExt() << ".png\"" << nl; + << "set output \"" << outputFile.stem() << ".png\"" << nl; // Header { diff --git a/src/meshTools/coordSet/writers/nastran/nastranCoordSetWriter.C b/src/meshTools/coordSet/writers/nastran/nastranCoordSetWriter.C index 07253c8c7c..14dbfc8cb1 100644 --- a/src/meshTools/coordSet/writers/nastran/nastranCoordSetWriter.C +++ b/src/meshTools/coordSet/writers/nastran/nastranCoordSetWriter.C @@ -258,7 +258,7 @@ Foam::fileName Foam::coordSetWriters::nastranWriter::writeTemplate OFstream os(outputFile); fileFormats::NASCore::setPrecision(os, writeFormat_); - os << "TITLE=OpenFOAM " << outputFile.nameLessExt() + os << "TITLE=OpenFOAM " << outputFile.stem() << " geometry" << nl << "BEGIN BULK" << nl; @@ -301,7 +301,7 @@ Foam::fileName Foam::coordSetWriters::nastranWriter::writeTemplate OFstream os(outputFile); fileFormats::NASCore::setPrecision(os, writeFormat_); - os << "TITLE=OpenFOAM " << outputFile.nameLessExt() + os << "TITLE=OpenFOAM " << outputFile.stem() << " geometry" << nl << "BEGIN BULK" << nl; diff --git a/src/meshTools/coordSet/writers/vtk/vtkCoordSetWriter.C b/src/meshTools/coordSet/writers/vtk/vtkCoordSetWriter.C index 43b479176d..2c877519ae 100644 --- a/src/meshTools/coordSet/writers/vtk/vtkCoordSetWriter.C +++ b/src/meshTools/coordSet/writers/vtk/vtkCoordSetWriter.C @@ -231,7 +231,7 @@ Foam::fileName Foam::coordSetWriters::vtkWriter::write() else { // Set name in title - writer_->beginFile(outputPath_.nameLessExt()); + writer_->beginFile(outputPath_.stem()); } writer_->writeGeometry(); diff --git a/src/meshTools/coupling/externalFileCoupler.C b/src/meshTools/coupling/externalFileCoupler.C index 722322dc14..95c6611f3f 100644 --- a/src/meshTools/coupling/externalFileCoupler.C +++ b/src/meshTools/coupling/externalFileCoupler.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2015-2020 OpenCFD Ltd. + Copyright (C) 2015-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -60,12 +60,12 @@ namespace Foam static enum Time::stopAtControls getStopAction(const std::string& filename) { // Slurp entire input file (must exist) as a single string - std::string fileContent; + string fileContent; std::ifstream is(filename); std::getline(is, fileContent, '\0'); - if (fileContent.find("done") != std::string::npos) + if (fileContent.contains("done")) { return Time::stopAtControls::saEndTime; } diff --git a/src/meshTools/edgeMesh/edgeFormats/obj/OBJedgeFormat.C b/src/meshTools/edgeMesh/edgeFormats/obj/OBJedgeFormat.C index e4af4dbbbd..c349c566cb 100644 --- a/src/meshTools/edgeMesh/edgeFormats/obj/OBJedgeFormat.C +++ b/src/meshTools/edgeMesh/edgeFormats/obj/OBJedgeFormat.C @@ -246,7 +246,7 @@ void Foam::fileFormats::OBJedgeFormat::write os << "# Wavefront OBJ file written " << clock::dateTime().c_str() << nl - << "o " << os.name().nameLessExt() << nl + << "o " << os.name().stem() << nl << nl << "# points : " << pointLst.size() << nl << "# lines : " << edgeLst.size() << nl; diff --git a/src/meshTools/edgeMesh/edgeFormats/starcd/STARCDedgeFormat.C b/src/meshTools/edgeMesh/edgeFormats/starcd/STARCDedgeFormat.C index a1f252bcfc..9dda97df8b 100644 --- a/src/meshTools/edgeMesh/edgeFormats/starcd/STARCDedgeFormat.C +++ b/src/meshTools/edgeMesh/edgeFormats/starcd/STARCDedgeFormat.C @@ -69,7 +69,7 @@ void Foam::fileFormats::STARCDedgeFormat::writeCase const label nEdges ) { - const word caseName = os.name().nameLessExt(); + const word caseName = os.name().stem(); os << "! STARCD file written " << clock::dateTime().c_str() << nl << "! " << pointLst.size() << " points, " << nEdges << " lines" << nl @@ -114,7 +114,7 @@ bool Foam::fileFormats::STARCDedgeFormat::read { clear(); - fileName baseName = filename.lessExt(); + const fileName prefix(filename.lessExt()); // STARCD index of points List