mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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.
This commit is contained in:
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#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
|
||||
)
|
||||
{
|
||||
// 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<class PrimitiveType>
|
||||
Foam::word Foam::stringOps::name
|
||||
(
|
||||
const std::string& fmt,
|
||||
const PrimitiveType& val
|
||||
)
|
||||
{
|
||||
return stringOps::name(fmt.c_str(), val);
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
Reference in New Issue
Block a user