Commit b060378d authored by Mark Olesen's avatar Mark Olesen Committed by Andrew Heather
Browse files

ENH: improve consistency of fileName handling windows/non-windows (#2057)

- wrap command-line retrieval of fileName with an implicit validate.

  Instead of this:
      fileName input(args[1]);
      fileName other(args["someopt"]);

  Now use this:
      auto input = args.get<fileName>(1);
      auto other = args.get<fileName>("someopt");

  which adds a fileName::validate on the inputs

  Because of how it is implemented, it will automatically also apply
  to argList getOrDefault<fileName>, readIfPresent<fileName> etc.

- adjust fileName::validate and clean to handle backslash conversion.
  This makes it easier to ensure that path names arising from MS-Windows
  are consistently handled internally.

- dictionarySearch: now check for initial '/' directly instead of
  relying on fileName isAbsolute(), which now does more things

BREAKING: remove fileName::clean() const method

- relying on const/non-const to control the behaviour (inplace change
  or return a copy) is too fragile and the const version was
  almost never used.

  Replace:
      fileName sanitized = constPath.clean();

  With:
      fileName sanitized(constPath);
      sanitized.clean());

STYLE: test empty() instead of comparing with fileName::null
parent 96a1b86f
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -101,7 +101,7 @@ int main(int argc, char *argv[])
{
if (true)
{
IFstream is(args[argi]);
IFstream is(args.get<fileName>(argi));
Info<< nl << nl
<< "read from " << is.name() << nl << endl;
......@@ -132,7 +132,7 @@ int main(int argc, char *argv[])
if (true)
{
IFstream is(args[argi]);
IFstream is(args.get<fileName>(argi));
Info<< nl << nl
<< "read from " << is.name() << nl << endl;
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -125,9 +126,9 @@ int main(int argc, char *argv[])
}
for (label argI=1; argI < args.size(); ++argI)
for (label argi=1; argi < args.size(); ++argi)
{
const string& srcFile = args[argI];
const auto srcFile = args.get<fileName>(argi);
Info<< nl << "reading " << srcFile << nl;
IFstream ifs(srcFile);
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2020 OpenCFD Ltd.
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -74,8 +74,7 @@ int main(int argc, char *argv[])
#include "setRootCase.H"
const fileName decompFile = args[1];
const auto decompFile = args.get<fileName>(1);
const bool region = args.found("region");
const bool allRegions = args.found("allRegions");
const bool verbose = args.found("verbose");
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -59,9 +60,9 @@ int main(int argc, char *argv[])
else
{
IOobject::writeDivider(Info);
for (label argI=1; argI < args.size(); ++argI)
for (label argi=1; argi < args.size(); ++argi)
{
const string& dictFile = args[argI];
const auto dictFile = args.get<fileName>(argi);
IFstream is(dictFile);
dictionary dict(is);
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -179,7 +179,7 @@ int main(int argc, char *argv[])
for (label argi=1; argi < args.size(); ++argi)
{
const string& dictFile = args[argi];
const auto dictFile = args.get<fileName>(argi);
IFstream is(dictFile);
dictionary inputDict(is);
......@@ -201,7 +201,7 @@ int main(int argc, char *argv[])
{
for (label argi=1; argi < args.size(); ++argi)
{
const string& dictFile = args[argi];
const auto dictFile = args.get<fileName>(argi);
IFstream is(dictFile);
dictionary inputDict(is);
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -135,8 +135,8 @@ int main(int argc, char *argv[])
args.readIfPresent("maxPath", maxPath);
#endif
const fileName srcFile(fileName::validate(args[1]));
const fileName dstFile(fileName::validate(args[2]));
const auto srcFile = args.get<fileName>(1);
const auto dstFile = args.get<fileName>(2);
const fileName tmpFile(dstFile + Foam::name(pid()));
Info<< "src : " << srcFile << nl
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenFOAM Foundation
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -54,7 +55,7 @@ int main(int argc, char *argv[])
#include "createTime.H"
const fileName file(args[1]);
const auto file = args.get<fileName>(1);
Info<< "Reading " << file << nl << endl;
decomposedBlockData data
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2012 OpenFOAM Foundation
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -123,9 +124,9 @@ int main(int argc, char *argv[])
else
{
IOobject::writeDivider(Info);
for (label argI=1; argI < args.size(); ++argI)
for (label argi=1; argi < args.size(); ++argi)
{
const string& dictFile = args[argI];
const auto dictFile = args.get<fileName>(argi);
IFstream is(dictFile);
dictionary dict(is);
......
......@@ -69,7 +69,7 @@ int main(int argc, char *argv[])
for (label argi=1; argi < args.size(); ++argi)
{
const string& dictFile = args[argi];
const auto dictFile = args.get<fileName>(argi);
IFstream is(dictFile);
dictionary input(is);
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenCFD Ltd.
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -61,7 +61,7 @@ int main(int argc, char *argv[])
for (label argi=1; argi < args.size(); ++argi)
{
IFstream is(args[argi]);
IFstream is(args.get<fileName>(argi));
dictionary dict(is);
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -70,7 +70,7 @@ int main(int argc, char *argv[])
for (int argi = 1; argi < args.size(); ++argi)
{
const fileName libName(fileName::validate(args[argi]));
const auto libName = args.get<fileName>(argi);
if (libName.empty())
{
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -79,7 +79,7 @@ int main(int argc, char *argv[])
{
IOobject::writeDivider(Info);
IFstream is(args[argi]);
IFstream is(args.get<fileName>(argi));
const dictionary dict(is);
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2017 OpenCFD Ltd.
Copyright (C) 2016-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -60,7 +60,7 @@ unsigned testClean(std::initializer_list<Pair<std::string>> tests)
const std::string& expected = test.second();
fileName cleaned(test.first());
cleaned.clean();
cleaned.clean(); // Remove unneeded ".."
if (cleaned == expected)
{
......
......@@ -5,7 +5,8 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -24,11 +25,10 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
fileNameCleanTest
Test-fileNameClean
Description
\*---------------------------------------------------------------------------*/
#include "argList.H"
......@@ -51,7 +51,7 @@ void printCleaning(fileName& pathName)
Info<< "components = " << flatOutput(pathName.components()) << nl;
Info<< "component 2 = " << pathName.component(2) << nl;
pathName.clean();
pathName.clean(); // Remove unneeded ".."
Info<< "cleaned = " << pathName << nl
<< " path() = " << pathName.path() << nl
......@@ -94,9 +94,15 @@ int main(int argc, char *argv[])
printCleaning(pathName);
}
for (label argI=1; argI < args.size(); ++argI)
for (label argi=1; argi < args.size(); ++argi)
{
pathName = args[argI];
fileName fn(args[argi], false); // no strip
Info<< "Input = " << fn << nl;
fn.clean(); // Remove unneeded ".."
Info<< "cleaned = " << fn << nl;
Info<< "get = " << args.get<fileName>(argi) << nl;
pathName = fileName::validate(args[argi]);
printCleaning(pathName);
}
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
......@@ -81,7 +81,7 @@ int main(int argc, char *argv[])
for (label argi = 1; argi < args.size(); ++argi)
{
const fileName inputName(args[argi]);
const auto inputName = args.get<fileName>(argi);
InfoErr<< "input: " << inputName;
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -53,9 +54,9 @@ int main(int argc, char *argv[])
label ok = 0;
for (label argI=1; argI < args.size(); ++argI)
for (label argi=1; argi < args.size(); ++argi)
{
const string& srcFile = args[argI];
const auto srcFile = args.get<fileName>(argi);
if (args.found("ext"))
{
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -73,7 +73,7 @@ int main(int argc, char *argv[])
for (label argi=1; argi < args.size(); ++argi)
{
IFstream is(args[argi]);
IFstream is(args.get<fileName>(argi));
dictionary dict(is);
......
......@@ -375,7 +375,7 @@ int main(int argc, char *argv[])
for (label argi = 1; argi < args.size(); ++argi)
{
IFstream is(args[argi]);
IFstream is(args.get<fileName>(argi));
List<regexTest> tests(is);
Info<< "Test expressions:" << tests << endl;
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2013 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -76,9 +76,9 @@ int main(int argc, char *argv[])
useCatmullRom = true;
}
for (label argI=1; argI < args.size(); ++argI)
for (label argi=1; argi < args.size(); ++argi)
{
const string& srcFile = args[argI];
const auto srcFile = args.get<fileName>(argi);
Info<< nl << "reading " << srcFile << nl;
IFstream ifs(srcFile);
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2020 OpenCFD Ltd.
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -139,13 +139,13 @@ int main(int argc, char *argv[])
const word outputFile(args.executable() + ".obj");
const fileName surf1Name(args[1]);
const auto surf1Name = args.get<fileName>(1);
triSurface surf1 = loadSurface(runTime, surf1Name, scaleFactor)();
Info<< surf1Name << " statistics:" << endl;
surf1.writeStats(Info);
Info<< endl;
const fileName surf2Name(args[2]);
const auto surf2Name = args.get<fileName>(2);
triSurface surf2 = loadSurface(runTime, surf2Name, scaleFactor)();
Info<< surf2Name << " statistics:" << endl;
surf2.writeStats(Info);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment