From cae7ce37f522b4a5aa9b8f5b217f0f6038ea697c Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 1 Jul 2016 08:23:13 +0200 Subject: [PATCH] ENH: provide formatting version of Foam::name() (issue #253) - there are some cases in which the C-style sprintf is much more convenient, albeit problematic for buffer overwrites. Provide a formatting version of Foam::name() for language primitives that is buffer-safe. Returns a Foam::word, so that further output will be unquoted, but without any checking that the characters are indeed entirely valid word characters. Example use, i = 1234; s = Foam::name("%08d", i); produces '00001234' Alternative using string streams: std::ostringstream buf; buf.fill('0'); buf << setw(8) << i; s = buf.str(); Note that the format specification can also be slightly more complex: Foam::name("output%08d.vtk", i); Foam::name("timing=%.2fs", time); It remains the caller's responsibility to ensure that the format mask is valid. --- applications/test/string/Test-string.C | 42 ++++++++++- src/OpenFOAM/primitives/Scalar/Scalar.C | 16 ++++- src/OpenFOAM/primitives/Scalar/Scalar.H | 12 +++- src/OpenFOAM/primitives/ints/int32/int32.H | 19 ++++- src/OpenFOAM/primitives/ints/int32/int32IO.C | 15 ++-- src/OpenFOAM/primitives/ints/int64/int64.H | 19 ++++- src/OpenFOAM/primitives/ints/int64/int64IO.C | 13 ++-- src/OpenFOAM/primitives/ints/uint32/uint32.H | 21 +++++- .../primitives/ints/uint32/uint32IO.C | 13 ++-- src/OpenFOAM/primitives/ints/uint64/uint64.H | 21 +++++- .../primitives/ints/uint64/uint64IO.C | 13 ++-- .../primitives/strings/stringOps/stringOps.H | 25 ++++++- .../strings/stringOps/stringOpsTemplates.C | 69 +++++++++++++++++++ 13 files changed, 266 insertions(+), 32 deletions(-) create mode 100644 src/OpenFOAM/primitives/strings/stringOps/stringOpsTemplates.C diff --git a/applications/test/string/Test-string.C b/applications/test/string/Test-string.C index f39be8c206..a8276c2926 100644 --- a/applications/test/string/Test-string.C +++ b/applications/test/string/Test-string.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -31,6 +31,10 @@ Description #include "dictionary.H" #include "IOstreams.H" +#include "int.H" +#include "uint.H" +#include "scalar.H" + using namespace Foam; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -118,6 +122,8 @@ int main(int argc, char *argv[]) Info<< "after replace: " << test2 << endl; } + cout<< "\nEnter some string to test:\n"; + string s; Sin.getLine(s); @@ -126,7 +132,39 @@ int main(int argc, char *argv[]) cout<< "output string with " << s2.length() << " characters\n"; cout<< "ostream<< >" << s2 << "<\n"; Info<< "Ostream<< >" << s2 << "<\n"; - Info<< "hash:" << hex << string::hash()(s2) << endl; + Info<< "hash:" << hex << string::hash()(s2) << dec << endl; + + cout<< "\ntest Foam::name()\n"; + + Info<< "hash: = " << Foam::name("0x%012X", string::hash()(s2)) << endl; + + // test formatting on int + { + label val = 25; + Info<<"val: " << val << "\n"; + + Info<< "int " << val << " as word >" + << Foam::name(val) << "< or " + << Foam::name("formatted >%08d<", val) << "\n"; + } + + // test formatting on scalar + { + scalar val = 3.1415926535897931; + Info<< "scalar " << val << " as word >" + << Foam::name(val) << "< or " + << Foam::name("formatted >%.9f<", val) << "\n"; + } + + // test formatting on uint + { + uint64_t val = 25000000ul; + Info<<"val: " << val << "\n"; + + Info<< "uint64 " << val << " as word >" + << Foam::name(val) << "< or " + << Foam::name("formatted >%08d<", val) << "\n"; + } Info<< "\nEnd\n" << endl; return 0; diff --git a/src/OpenFOAM/primitives/Scalar/Scalar.C b/src/OpenFOAM/primitives/Scalar/Scalar.C index 0a3e09b0b1..a801378b21 100644 --- a/src/OpenFOAM/primitives/Scalar/Scalar.C +++ b/src/OpenFOAM/primitives/Scalar/Scalar.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -23,6 +23,8 @@ License \*---------------------------------------------------------------------------*/ +#include "stringOps.H" + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -62,6 +64,18 @@ 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); +} + + // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // Scalar readScalar(Istream& is) diff --git a/src/OpenFOAM/primitives/Scalar/Scalar.H b/src/OpenFOAM/primitives/Scalar/Scalar.H index 4d8f29ff54..6b2213fa6a 100644 --- a/src/OpenFOAM/primitives/Scalar/Scalar.H +++ b/src/OpenFOAM/primitives/Scalar/Scalar.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -109,6 +109,16 @@ public: word name(const Scalar); +//- 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); + + +//- 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); + + // Standard C++ transcendental functions transFunc(sqrt) diff --git a/src/OpenFOAM/primitives/ints/int32/int32.H b/src/OpenFOAM/primitives/ints/int32/int32.H index de8baa2109..96c748b052 100644 --- a/src/OpenFOAM/primitives/ints/int32/int32.H +++ b/src/OpenFOAM/primitives/ints/int32/int32.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -56,7 +56,22 @@ class Ostream; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // //- Return a word representation of an int32 -word name(const int32_t); +inline word name(const int32_t val) +{ + // no stripping required + return word(std::to_string(val), false); +} + + +//- 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); + + +//- 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&, const int32_t); + // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/ints/int32/int32IO.C b/src/OpenFOAM/primitives/ints/int32/int32IO.C index a8c972377d..c5023315f0 100644 --- a/src/OpenFOAM/primitives/ints/int32/int32IO.C +++ b/src/OpenFOAM/primitives/ints/int32/int32IO.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -24,6 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "int32.H" +#include "stringOps.H" #include "IOstreams.H" #include @@ -32,11 +33,15 @@ License // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -Foam::word Foam::name(const int32_t val) +Foam::word Foam::name(const char* fmt, const int32_t val) { - std::ostringstream buf; - buf << val; - return buf.str(); + return stringOps::name(fmt, val); +} + + +Foam::word Foam::name(const std::string& fmt, const int32_t val) +{ + return stringOps::name(fmt, val); } diff --git a/src/OpenFOAM/primitives/ints/int64/int64.H b/src/OpenFOAM/primitives/ints/int64/int64.H index 2bb5a05804..bcbcea38f7 100644 --- a/src/OpenFOAM/primitives/ints/int64/int64.H +++ b/src/OpenFOAM/primitives/ints/int64/int64.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -60,7 +60,22 @@ class Ostream; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // //- Return a word representation of an int64 -word name(const int64_t); +inline word name(const int64_t val) +{ + // no stripping required + return word(std::to_string(val), false); +} + + +//- 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 * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/ints/int64/int64IO.C b/src/OpenFOAM/primitives/ints/int64/int64IO.C index 89f14163a0..0523bb94cb 100644 --- a/src/OpenFOAM/primitives/ints/int64/int64IO.C +++ b/src/OpenFOAM/primitives/ints/int64/int64IO.C @@ -24,6 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "int64.H" +#include "stringOps.H" #include "IOstreams.H" #include @@ -32,11 +33,15 @@ License // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -Foam::word Foam::name(const int64_t val) +Foam::word Foam::name(const char* fmt, const int64_t val) { - std::ostringstream buf; - buf << val; - return buf.str(); + return stringOps::name(fmt, val); +} + + +Foam::word Foam::name(const std::string& fmt, const int64_t val) +{ + return stringOps::name(fmt, val); } diff --git a/src/OpenFOAM/primitives/ints/uint32/uint32.H b/src/OpenFOAM/primitives/ints/uint32/uint32.H index 34803c218f..469189e976 100644 --- a/src/OpenFOAM/primitives/ints/uint32/uint32.H +++ b/src/OpenFOAM/primitives/ints/uint32/uint32.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -55,8 +55,23 @@ class Ostream; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -//- Return a word representation of an uint32 -word name(const uint32_t); +//- Return a word representation of a uint32 +inline word name(const uint32_t val) +{ + // no stripping required + return word(std::to_string(val), false); +} + + +//- 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 * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/ints/uint32/uint32IO.C b/src/OpenFOAM/primitives/ints/uint32/uint32IO.C index 2746938d63..f88f5b1ad8 100644 --- a/src/OpenFOAM/primitives/ints/uint32/uint32IO.C +++ b/src/OpenFOAM/primitives/ints/uint32/uint32IO.C @@ -24,17 +24,22 @@ License \*---------------------------------------------------------------------------*/ #include "uint32.H" +#include "stringOps.H" #include "IOstreams.H" #include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -Foam::word Foam::name(const uint32_t val) +Foam::word Foam::name(const char* fmt, const uint32_t val) { - std::ostringstream buf; - buf << val; - return buf.str(); + return stringOps::name(fmt, val); +} + + +Foam::word Foam::name(const std::string& fmt, const uint32_t val) +{ + return stringOps::name(fmt, val); } diff --git a/src/OpenFOAM/primitives/ints/uint64/uint64.H b/src/OpenFOAM/primitives/ints/uint64/uint64.H index e1a742b6c2..2cf102e057 100644 --- a/src/OpenFOAM/primitives/ints/uint64/uint64.H +++ b/src/OpenFOAM/primitives/ints/uint64/uint64.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -59,8 +59,23 @@ class Ostream; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -//- Return a word representation of an uint64 -word name(const uint64_t); +//- Return a word representation of a uint64 +inline word name(const uint64_t val) +{ + // no stripping required + return word(std::to_string(val), false); +} + + +//- 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 * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/ints/uint64/uint64IO.C b/src/OpenFOAM/primitives/ints/uint64/uint64IO.C index be10f83b01..3e110d933a 100644 --- a/src/OpenFOAM/primitives/ints/uint64/uint64IO.C +++ b/src/OpenFOAM/primitives/ints/uint64/uint64IO.C @@ -24,17 +24,22 @@ License \*---------------------------------------------------------------------------*/ #include "uint64.H" +#include "stringOps.H" #include "IOstreams.H" #include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -Foam::word Foam::name(const uint64_t val) +Foam::word Foam::name(const char* fmt, const uint64_t val) { - std::ostringstream buf; - buf << val; - return buf.str(); + return stringOps::name(fmt, val); +} + + +Foam::word Foam::name(const std::string& fmt, const uint64_t val) +{ + return stringOps::name(fmt, val); } diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.H b/src/OpenFOAM/primitives/strings/stringOps/stringOps.H index 3a72467bc9..fb408455f6 100644 --- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.H +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -36,6 +36,7 @@ SourceFiles #define stringOps_H #include "string.H" +#include "word.H" #include "dictionary.H" #include "HashTable.H" @@ -292,6 +293,21 @@ namespace stringOps string& inplaceTrim(string&); + //- Return a word representation of the primitive, + // using printf-style formatter. + // The representation is not checked for valid word characters - + // it is assumed that the caller knows what they are doing + template + Foam::word name(const char* fmt, const PrimitiveType& val); + + //- Return a word representation of the primitive, + // using printf-style formatter. + // The representation is not checked for valid word characters - + // it is assumed that the caller knows what they are doing + template + Foam::word name(const std::string& fmt, const PrimitiveType& val); + + } // End namespace stringOps @@ -299,6 +315,13 @@ namespace stringOps } // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef NoRepository + #include "stringOpsTemplates.C" +#endif + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #endif diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOpsTemplates.C b/src/OpenFOAM/primitives/strings/stringOps/stringOpsTemplates.C new file mode 100644 index 0000000000..a3874aa88e --- /dev/null +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOpsTemplates.C @@ -0,0 +1,69 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 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 . + +\*---------------------------------------------------------------------------*/ + +#include + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// NOTE: with C++11 could consider variadic templates for a more general +// sprintf implementation + +template +Foam::word Foam::stringOps::name +( + const char* fmt, + const PrimitiveType& val +) +{ + // same concept as GNU/BSD asprintf() + // use snprintf with zero to determine the number of characters required + + int n = ::snprintf(0, 0, fmt, val); + if (n > 0) + { + char buf[n+1]; + ::snprintf(buf, n+1, fmt, val); + buf[n] = 0; + + // no stripping desired + return word(buf, false); + } + + return word::null; +} + + +template +Foam::word Foam::stringOps::name +( + const std::string& fmt, + const PrimitiveType& val +) +{ + return stringOps::name(fmt.c_str(), val); +} + + +// ************************************************************************* //