STYLE: two-parameter Foam::name replaced by word::printf (issue #724)

- reduces some ambiguity and clarifies the expected output and
  behaviour.

STYLE: reduce some automatic conversions of char to string
This commit is contained in:
Mark Olesen
2018-02-08 12:00:54 +01:00
parent fa8d972796
commit 806b668418
36 changed files with 233 additions and 251 deletions

View File

@ -67,7 +67,7 @@ int main(int argc, char *argv[])
{ {
entry* e = dict1.add entry* e = dict1.add
( (
Foam::name("entry%d", i), word::printf("entry%d", i),
string("entry" + Foam::name(i)) string("entry" + Foam::name(i))
); );
entryInfo(e); entryInfo(e);
@ -76,7 +76,7 @@ int main(int argc, char *argv[])
{ {
entry* e = tmpdict.add entry* e = tmpdict.add
( (
Foam::name("subentry%d", i), word::printf("subentry%d", i),
string("subentry" + Foam::name(i)) string("subentry" + Foam::name(i))
); );
entryInfo(e); entryInfo(e);
@ -85,7 +85,7 @@ int main(int argc, char *argv[])
{ {
entry* e = dict1.add entry* e = dict1.add
( (
Foam::name("dict%d", i), word::printf("dict%d", i),
tmpdict tmpdict
); );
entryInfo(e); entryInfo(e);

View File

@ -64,14 +64,14 @@ int main(int argc, char *argv[])
// Test Foam::name with formatting string // Test Foam::name with formatting string
{ {
word formatted = Foam::name("formatted=<%X>", 0xdeadbeef); word formatted = word::printf("formatted=<%X>", 0xdeadbeef);
Info<<"formatted: " << formatted << nl; Info<<"formatted: " << formatted << nl;
} }
Info<<"formatted: " Info<<"formatted: "
<< Foam::name("formatted not checked for validity=<%X>", 0xdeadbeef) << word::printf("formatted not checked for validity=<%X>", 0xdeadbeef)
<< nl << nl
<< endl << endl;
Info<< "string:" << test << nl << "hash:" Info<< "string:" << test << nl << "hash:"
@ -227,7 +227,7 @@ int main(int argc, char *argv[])
cout<< "\ntest Foam::name()\n"; cout<< "\ntest Foam::name()\n";
Info<< "hash: = " << Foam::name("0x%012X", string::hash()(s2)) << endl; Info<< "hash: = " << word::printf("0x%012X", string::hash()(s2)) << endl;
// test formatting on int // test formatting on int
{ {
@ -236,7 +236,7 @@ int main(int argc, char *argv[])
Info<< "int " << val << " as word >" Info<< "int " << val << " as word >"
<< Foam::name(val) << "< or " << Foam::name(val) << "< or "
<< Foam::name("formatted >%08d<", val) << "\n"; << word::printf("formatted >%08d<", val) << "\n";
} }
// test formatting on scalar // test formatting on scalar
@ -244,7 +244,7 @@ int main(int argc, char *argv[])
scalar val = 3.1415926535897931; scalar val = 3.1415926535897931;
Info<< "scalar " << val << " as word >" Info<< "scalar " << val << " as word >"
<< Foam::name(val) << "< or " << Foam::name(val) << "< or "
<< Foam::name("formatted >%.9f<", val) << "\n"; << word::printf("formatted >%.9f<", val) << "\n";
} }
// test formatting on uint // test formatting on uint
@ -254,7 +254,7 @@ int main(int argc, char *argv[])
Info<< "uint64 " << val << " as word >" Info<< "uint64 " << val << " as word >"
<< Foam::name(val) << "< or " << Foam::name(val) << "< or "
<< Foam::name("formatted >%08d<", val) << "\n"; << word::printf("formatted >%08d<", val) << "\n";
} }
// test startsWith, endsWith methods // test startsWith, endsWith methods

View File

@ -155,7 +155,7 @@ int main(int argc, char *argv[])
if (withVTK && Pstream::master()) if (withVTK && Pstream::master())
{ {
const word outputName = const word outputName =
Foam::name("forces_%06d.vtp", runTime.timeIndex()); word::printf("forces_%06d.vtp", runTime.timeIndex());
Info<<" " << outputName << endl; Info<<" " << outputName << endl;

View File

@ -226,14 +226,16 @@ int main(int argc, char *argv[])
// State/response = what comes back from FEM // State/response = what comes back from FEM
{ {
const word outputName = Foam::name("state_%06d.vtp", index); const word outputName = word::printf("state_%06d.vtp", index);
Info<<" " << outputName << endl; Info<<" " << outputName << endl;
state.writeVTP(outputName, movement().axis()); state.writeVTP(outputName, movement().axis());
} }
{ {
const word outputName = Foam::name("geom_%06d.vtp", index); const word outputName = word::printf("geom_%06d.vtp", index);
Info<<" " << outputName << endl; Info<<" " << outputName << endl;
movement().writeVTP(outputName, state, mesh, patchLst, points0); movement().writeVTP(outputName, state, mesh, patchLst, points0);

View File

@ -133,7 +133,7 @@ void printSourceFileAndLine
else else
{ {
string cwdLine(line.replaceAll(cwd() + '/', "")); string cwdLine(line.replaceAll(cwd() + '/', ""));
string homeLine(cwdLine.replaceAll(home(), '~')); string homeLine(cwdLine.replaceAll(home(), "~"));
os << " at " << homeLine.c_str(); os << " at " << homeLine.c_str();
} }

View File

@ -33,6 +33,14 @@ License
namespace Foam namespace Foam
{ {
// Convert a single character to a word with length 1
inline static Foam::word charToWord(char c)
{
return Foam::word(std::string(1, c), false);
}
// Adjust stream format based on the flagMask // Adjust stream format based on the flagMask
inline static void processFlags(Istream& is, int flagMask) inline static void processFlags(Istream& is, int flagMask)
{ {
@ -45,7 +53,8 @@ inline static void processFlags(Istream& is, int flagMask)
is.format(IOstream::BINARY); is.format(IOstream::BINARY);
} }
} }
}
} // End anonymous namespace
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
@ -315,7 +324,7 @@ Foam::Istream& Foam::UIPstream::read(token& t)
{ {
if (isalpha(c)) if (isalpha(c))
{ {
t = word(c); t = charToWord(c);
return *this; return *this;
} }

View File

@ -33,6 +33,19 @@ License
// Truncate error message for readability // Truncate error message for readability
static const unsigned errLen = 80; static const unsigned errLen = 80;
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
namespace
{
// Convert a single character to a word with length 1
inline static Foam::word charToWord(char c)
{
return Foam::word(std::string(1, c), false);
}
} // End anonymous namespace
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
@ -210,7 +223,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
if (read(nextC).bad()) if (read(nextC).bad())
{ {
// Return lone '#' as word // Return lone '#' as word
t = token(word(c)); t = charToWord(c);
} }
else if (nextC == token::BEGIN_BLOCK) else if (nextC == token::BEGIN_BLOCK)
{ {
@ -246,7 +259,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
if (read(nextC).bad()) if (read(nextC).bad())
{ {
// Return lone '$' as word // Return lone '$' as word
t = token(word(c)); t = charToWord(c);
} }
else if (nextC == token::BEGIN_BLOCK) else if (nextC == token::BEGIN_BLOCK)
{ {

View File

@ -191,7 +191,7 @@ Foam::error::operator Foam::dictionary() const
dictionary errDict; dictionary errDict;
string oneLineMessage(message()); string oneLineMessage(message());
oneLineMessage.replaceAll('\n', ' '); oneLineMessage.replaceAll("\n", " ");
errDict.add("type", word("Foam::error")); errDict.add("type", word("Foam::error"));
errDict.add("message", oneLineMessage); errDict.add("message", oneLineMessage);

View File

@ -45,9 +45,9 @@ namespace Foam
Foam::word Foam::graph::wordify(const Foam::string& sname) Foam::word Foam::graph::wordify(const Foam::string& sname)
{ {
string wname = sname; string wname = sname;
wname.replace(' ', '_'); wname.replace(" ", "_");
wname.replace('(', '_'); wname.replace("(", "_");
wname.replace(')', ""); wname.replace(")", "");
return word(wname); return word(wname);
} }

View File

@ -23,8 +23,6 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "stringOps.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam namespace Foam
@ -64,18 +62,6 @@ word name(const Scalar val)
} }
word name(const char* fmt, const Scalar val)
{
return stringOps::name(fmt, val);
}
word name(const std::string& fmt, const Scalar val)
{
return stringOps::name(fmt, val);
}
Scalar ScalarRead(const char* buf) Scalar ScalarRead(const char* buf)
{ {
char* endptr = nullptr; char* endptr = nullptr;

View File

@ -105,17 +105,10 @@ public:
// * * * * * * * * * * * * * * * IO/Conversion * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IO/Conversion * * * * * * * * * * * * * * * //
//- Return a string representation of a Scalar //- Return a word representation of a Scalar.
// Uses stringstream instead of std::to_string for more consistent formatting.
word name(const Scalar val); word name(const Scalar val);
//- Return a word representation of a Scalar, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const char* fmt, const Scalar val);
//- Return a word representation of a Scalar, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const std::string& fmt, const Scalar val);
//- Parse entire buffer as a float/double, skipping leading/trailing whitespace. //- Parse entire buffer as a float/double, skipping leading/trailing whitespace.
// \return Parsed value or FatalIOError on any problem // \return Parsed value or FatalIOError on any problem
Scalar ScalarRead(const char* buf); Scalar ScalarRead(const char* buf);

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -24,21 +24,6 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "int32.H" #include "int32.H"
#include "stringOps.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::word Foam::name(const char* fmt, const int32_t val)
{
return stringOps::name(fmt, val);
}
Foam::word Foam::name(const std::string& fmt, const int32_t val)
{
return stringOps::name(fmt, val);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -62,16 +62,6 @@ inline word name(const int32_t val)
} }
//- Return a word representation of an int32, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const char* fmt, const int32_t val);
//- Return a word representation of an int32, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const std::string& fmt, const int32_t val);
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Read int32_t from stream //- Read int32_t from stream

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -24,21 +24,6 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "int64.H" #include "int64.H"
#include "stringOps.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::word Foam::name(const char* fmt, const int64_t val)
{
return stringOps::name(fmt, val);
}
Foam::word Foam::name(const std::string& fmt, const int64_t val)
{
return stringOps::name(fmt, val);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -63,16 +63,6 @@ inline word name(const int64_t val)
} }
//- Return a word representation of an int64, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const char* fmt, const int64_t);
//- Return a word representation of an int64, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const std::string& fmt, const int64_t);
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Read int64_t from stream //- Read int64_t from stream

View File

@ -24,21 +24,6 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "uint32.H" #include "uint32.H"
#include "stringOps.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::word Foam::name(const char* fmt, const uint32_t val)
{
return stringOps::name(fmt, val);
}
Foam::word Foam::name(const std::string& fmt, const uint32_t val)
{
return stringOps::name(fmt, val);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -62,16 +62,6 @@ inline word name(const uint32_t val)
} }
//- Return a word representation of a uint32, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const char* fmt, const uint32_t);
//- Return a word representation of a uint32, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const std::string& fmt, const uint32_t);
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Read uint32_t from stream //- Read uint32_t from stream

View File

@ -24,21 +24,6 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "uint64.H" #include "uint64.H"
#include "stringOps.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::word Foam::name(const char* fmt, const uint64_t val)
{
return stringOps::name(fmt, val);
}
Foam::word Foam::name(const std::string& fmt, const uint64_t val)
{
return stringOps::name(fmt, val);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -62,16 +62,6 @@ inline word name(const uint64_t val)
} }
//- Return a word representation of a uint64_t, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const char* fmt, const uint64_t);
//- Return a word representation of a uint64_t, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const std::string& fmt, const uint64_t);
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Read uint64_t from stream. //- Read uint64_t from stream.

View File

@ -42,8 +42,7 @@ inline void Foam::hashedWordList::rehash(const bool unique)
inline Foam::hashedWordList::hashedWordList() inline Foam::hashedWordList::hashedWordList()
: :
List<word>(), List<word>()
indices_()
{} {}

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -39,6 +39,7 @@ See also
SourceFiles SourceFiles
string.C string.C
stringIO.C stringIO.C
stringTemplates.C
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -90,6 +91,24 @@ protected:
// A wrapped version of find_last_of("./") with additional logic. // A wrapped version of find_last_of("./") with additional logic.
inline std::string::size_type find_ext() const; inline std::string::size_type find_ext() const;
//- A printf-style formatter for a primitive.
template<class PrimitiveType>
static std::string::size_type string_printf
(
std::string& output,
const char* fmt,
const PrimitiveType& val
);
//- A printf-style formatter for a primitive.
template<class PrimitiveType>
static std::string::size_type string_printf
(
std::string& output,
const std::string& fmt,
const PrimitiveType& val
);
//- Return file name extension (part after last .) //- Return file name extension (part after last .)
word ext() const; word ext() const;
@ -128,8 +147,7 @@ public:
class hash class hash
{ {
public: public:
hash() hash() = default;
{}
//- Hash for string. //- Hash for string.
// Uses Foam::string instead of std::string for automatic conversions. // Uses Foam::string instead of std::string for automatic conversions.
@ -152,9 +170,9 @@ public:
inline string(const char* str, const size_type len); inline string(const char* str, const size_type len);
//- Construct from a single character //- Construct from a single character
inline string(const char c); inline explicit string(const char c);
//- Construct from copies of a single character //- Construct fill copies of a single character
inline string(const size_type len, const char c); inline string(const size_type len, const char c);
//- Move construct from std::string //- Move construct from std::string
@ -292,6 +310,10 @@ public:
#include "stringI.H" #include "stringI.H"
#ifdef NoRepository
#include "stringTemplates.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif #endif

View File

@ -23,7 +23,6 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
inline std::string::size_type Foam::string::find_ext(const std::string& str) inline std::string::size_type Foam::string::find_ext(const std::string& str)
@ -68,6 +67,8 @@ inline bool Foam::string::removeExt()
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::string::string() inline Foam::string::string()
:
std::string()
{} {}

View File

@ -0,0 +1,72 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include <cstdio>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Could also consider generalizing with C++11 variadic templates
template<class PrimitiveType>
std::string::size_type Foam::string::string_printf
(
std::string& output,
const char* fmt,
const PrimitiveType& val
)
{
// Use snprintf with zero to establish the size (without '\0') required
int n = ::snprintf(nullptr, 0, fmt, val);
if (n > 0)
{
output.resize(n+1);
char* buf = &(output[0]);
// Print directly into buffer, no stripping desired
n = ::snprintf(buf, n+1, fmt, val);
output.resize(n);
}
else
{
output.clear();
}
return output.size();
}
template<class PrimitiveType>
std::string::size_type Foam::string::string_printf
(
std::string& output,
const std::string& fmt,
const PrimitiveType& val
)
{
return string_printf(output, fmt.c_str(), val);
}
// ************************************************************************* //

View File

@ -457,7 +457,7 @@ Foam::string Foam::stringOps::expand
} }
else else
{ {
out.append(string(s[index])); out.append(1, s[index]); // append char
} }
++index; ++index;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -321,19 +321,6 @@ namespace stringOps
void inplaceUpper(std::string& s); void inplaceUpper(std::string& s);
//- Using printf-formatter for a word representation of the primitive.
// The representation is not checked for valid word characters -
// it is assumed that the caller knows what they are doing
template<class PrimitiveType>
Foam::word name(const char* fmt, const PrimitiveType& val);
//- Using printf-formatter for a word representation of the primitive.
// The representation is not checked for valid word characters -
// it is assumed that the caller knows what they are doing
template<class PrimitiveType>
Foam::word name(const std::string& fmt, const PrimitiveType& val);
//- Split string into sub-strings at the delimiter character. //- Split string into sub-strings at the delimiter character.
// Empty sub-strings are normally suppressed. // Empty sub-strings are normally suppressed.
// Behaviour is ill-defined if delim is a NUL character. // Behaviour is ill-defined if delim is a NUL character.

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2016-2017 OpenCFD Ltd. \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -23,49 +23,8 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include <cstdio>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// NOTE: with C++11 could consider variadic templates for a more general
// sprintf implementation
template<class PrimitiveType>
Foam::word Foam::stringOps::name
(
const char* fmt,
const PrimitiveType& val
)
{
word output;
// snprintf with zero to find size (without '\0') required
int n = ::snprintf(nullptr, 0, fmt, val);
if (n > 0)
{
output.resize(n+1);
char* buf = &(output[0]);
// Print directly into buffer, no stripping desired
n = ::snprintf(buf, n+1, fmt, val);
output.resize(n);
}
return output;
}
template<class PrimitiveType>
Foam::word Foam::stringOps::name
(
const std::string& fmt,
const PrimitiveType& val
)
{
return stringOps::name(fmt.c_str(), val);
}
template<class StringType> template<class StringType>
Foam::SubStrings<StringType> Foam::stringOps::split Foam::SubStrings<StringType> Foam::stringOps::split
( (

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -117,7 +117,28 @@ public:
word(Istream& is); word(Istream& is);
// Member functions // Member Functions
//- Use a printf-style formatter for a primitive.
// The representation is not checked for valid characters -
// it is assumed that the caller knows what they are doing
template<class PrimitiveType>
inline static word printf
(
const char* fmt,
const PrimitiveType& val
);
//- Use a printf-style formatter for a primitive.
// The representation is not checked for valid characters -
// it is assumed that the caller knows what they are doing
template<class PrimitiveType>
inline static word printf
(
const std::string& fmt,
const PrimitiveType& val
);
//- Is this character valid for a word? //- Is this character valid for a word?
inline static bool valid(char c); inline static bool valid(char c);
@ -128,7 +149,7 @@ public:
static word validate(const std::string& s, const bool prefix=false); static word validate(const std::string& s, const bool prefix=false);
// File-like functions // File-like Functions
//- Return word without extension (part before last .) //- Return word without extension (part before last .)
word lessExt() const; word lessExt() const;
@ -156,7 +177,7 @@ public:
// Member operators // Member operators
// Assignment // Assignment
//- Copy assignment, no character validation required //- Copy assignment, no character validation required
inline void operator=(const word& w); inline void operator=(const word& w);

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -26,6 +26,34 @@ License
#include <cctype> #include <cctype>
#include <iostream> // for std::cerr #include <iostream> // for std::cerr
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class PrimitiveType>
inline Foam::word Foam::word::printf
(
const char* fmt,
const PrimitiveType& val
)
{
word output;
string_printf(output, fmt, val);
return output;
}
template<class PrimitiveType>
inline Foam::word Foam::word::printf
(
const std::string& fmt,
const PrimitiveType& val
)
{
word output;
string_printf(output, fmt, val);
return output;
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
inline void Foam::word::stripInvalid() inline void Foam::word::stripInvalid()

View File

@ -60,7 +60,7 @@ diffusionMulticomponent<CombThermoType, ThermoType>::init()
( (
IOobject IOobject
( (
"Rijk" + name(k), "Rijk" + Foam::name(k),
this->mesh_.time().timeName(), this->mesh_.time().timeName(),
this->mesh_, this->mesh_,
IOobject::NO_READ, IOobject::NO_READ,
@ -160,14 +160,6 @@ diffusionMulticomponent
} }
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class CombThermoType, class ThermoType>
Foam::combustionModels::diffusionMulticomponent<CombThermoType, ThermoType>::
~diffusionMulticomponent()
{}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
template<class CombThermoType, class ThermoType> template<class CombThermoType, class ThermoType>
@ -200,7 +192,7 @@ diffusionMulticomponent<CombThermoType, ThermoType>::correct()
( (
IOobject IOobject
( (
"Rijl" + word(k), "Rijl" + Foam::name(k),
this->mesh_.time().timeName(), this->mesh_.time().timeName(),
this->mesh_, this->mesh_,
IOobject::NO_READ, IOobject::NO_READ,
@ -258,7 +250,7 @@ diffusionMulticomponent<CombThermoType, ThermoType>::correct()
const volScalarField ft const volScalarField ft
( (
"ft" + name(k), "ft" + Foam::name(k),
( (
s_[k]*Yfuel - (Yox - YoxStream_[k]) s_[k]*Yfuel - (Yox - YoxStream_[k])
) )
@ -279,7 +271,7 @@ diffusionMulticomponent<CombThermoType, ThermoType>::correct()
const volScalarField preExp const volScalarField preExp
( (
"preExp" + name(k), "preExp" + Foam::name(k),
1.0 + sqr(OAvailScaled) 1.0 + sqr(OAvailScaled)
); );
@ -291,7 +283,7 @@ diffusionMulticomponent<CombThermoType, ThermoType>::correct()
const volScalarField topHatFilter(pos(filter - 1e-3)); const volScalarField topHatFilter(pos(filter - 1e-3));
const volScalarField prob("prob" + name(k), preExp*filter); const volScalarField prob("prob" + Foam::name(k), preExp*filter);
const volScalarField RijkDiff const volScalarField RijkDiff
( (
@ -438,10 +430,8 @@ diffusionMulticomponent<CombThermoType, ThermoType>::read()
this->coeffs().readIfPresent("laminarIgn", laminarIgn_); this->coeffs().readIfPresent("laminarIgn", laminarIgn_);
return true; return true;
} }
else
{ return false;
return false;
}
} }

View File

@ -177,7 +177,7 @@ public:
//- Destructor //- Destructor
virtual ~diffusionMulticomponent(); virtual ~diffusionMulticomponent() = default;
// Member Functions // Member Functions

View File

@ -535,7 +535,7 @@ void Foam::ensightCase::write() const
{ {
// moving // moving
*os_ *os_
<< Foam::name("model: %-9d", tsGeom) // width 16 << word::printf("model: %-9d", tsGeom) // width 16 (no quotes)
<< (dataMask/geometryName).c_str() << (dataMask/geometryName).c_str()
<< nl; << nl;
} }
@ -554,7 +554,7 @@ void Foam::ensightCase::write() const
); );
*os_ *os_
<< Foam::name("measured: %-6d", tsCloud) // width 16 << word::printf("measured: %-6d", tsCloud) // width 16 (no quotes)
<< (masked/"positions").c_str() << (masked/"positions").c_str()
<< nl; << nl;
} }
@ -617,7 +617,7 @@ void Foam::ensightCase::write() const
// prefix variables with 'c' (cloud) and cloud index // prefix variables with 'c' (cloud) and cloud index
*os_ *os_
<< ensType.c_str() << " per " << ensType.c_str() << " per "
<< Foam::name("measured node: %-5d", tsCloud) // width 20 << word::printf("measured node: %-5d", tsCloud) // width 20
<< setw(15) << setw(15)
<< ("c" + Foam::name(cloudNo) + varName).c_str() << ' ' << ("c" + Foam::name(cloudNo) + varName).c_str() << ' '
<< (masked/varName).c_str() << (masked/varName).c_str()

View File

@ -48,6 +48,7 @@ Foam::IOstream::streamFormat Foam::ensightCase::options::format() const
return format_; return format_;
} }
const Foam::word& Foam::ensightCase::options::mask() const const Foam::word& Foam::ensightCase::options::mask() const
{ {
return mask_; return mask_;
@ -56,7 +57,7 @@ const Foam::word& Foam::ensightCase::options::mask() const
Foam::word Foam::ensightCase::options::padded(const label i) const Foam::word Foam::ensightCase::options::padded(const label i) const
{ {
// As per Foam::name, but with fixed length // As per word::printf(), but with fixed length
char buf[32]; char buf[32];
::snprintf(buf, 32, printf_.c_str(), static_cast<int>(i)); ::snprintf(buf, 32, printf_.c_str(), static_cast<int>(i));
@ -85,7 +86,7 @@ void Foam::ensightCase::options::width(const label n)
mask_.resize(n, '*'); mask_.resize(n, '*');
// appropriate printf format // appropriate printf format
printf_ = "%0" + Foam::name(n) + "d"; printf_ = "%0" + std::to_string(n) + "d";
} }

View File

@ -1100,9 +1100,9 @@ void Foam::isoAdvection::writeIsoFaces
mesh_.time().path()/".."/"isoFaces" mesh_.time().path()/".."/"isoFaces"
: mesh_.time().path()/"isoFaces" : mesh_.time().path()/"isoFaces"
); );
const string fName const word fName
( (
"isoFaces_" + Foam::name("%012d", mesh_.time().timeIndex()) word::printf("isoFaces_%012d", mesh_.time().timeIndex())
); );
if (Pstream::parRun()) if (Pstream::parRun())

View File

@ -312,7 +312,7 @@ bool Foam::sampledTriSurfaceMesh::update(const meshSearch& meshSearcher)
patchi, patchi,
( (
patches[patchi].name().empty() patches[patchi].name().empty()
? Foam::name("patch%d", patchi) ? word::printf("patch%d", patchi)
: patches[patchi].name() : patches[patchi].name()
) )
); );
@ -345,7 +345,7 @@ bool Foam::sampledTriSurfaceMesh::update(const meshSearch& meshSearcher)
zoneNames.set zoneNames.set
( (
regionid, regionid,
Foam::name("patch%d", regionid) word::printf("patch%d", regionid)
); );
} }
@ -393,7 +393,7 @@ bool Foam::sampledTriSurfaceMesh::update(const meshSearch& meshSearcher)
} }
if (name.empty()) if (name.empty())
{ {
name = ::Foam::name("patch%d", regionid); name = word::printf("patch%d", regionid);
} }
zoneLst[zoneI] = surfZone zoneLst[zoneI] = surfZone

View File

@ -306,7 +306,7 @@ bool Foam::discreteSurface::update(const meshSearch& meshSearcher)
patchi, patchi,
( (
patches[patchi].name().empty() patches[patchi].name().empty()
? Foam::name("patch%d", patchi) ? word::printf("patch%d", patchi)
: patches[patchi].name() : patches[patchi].name()
) )
); );
@ -339,7 +339,7 @@ bool Foam::discreteSurface::update(const meshSearch& meshSearcher)
zoneNames.set zoneNames.set
( (
regionid, regionid,
Foam::name("patch%d", regionid) word::printf("patch%d", regionid)
); );
} }
@ -387,7 +387,7 @@ bool Foam::discreteSurface::update(const meshSearch& meshSearcher)
} }
if (name.empty()) if (name.empty())
{ {
name = ::Foam::name("patch%d", regionid); name = word::printf("patch%d", regionid);
} }
zoneLst[zoneI] = surfZone zoneLst[zoneI] = surfZone

View File

@ -220,7 +220,7 @@ private:
} }
else if (elementName[0] == 'E') else if (elementName[0] == 'E')
{ {
elementName = 'e'; elementName = "e";
} }
} }
@ -314,8 +314,7 @@ public:
//- Destructor //- Destructor
virtual ~chemkinReader() virtual ~chemkinReader() = default;
{}
// Member functions // Member functions