From 4ae4f0928d275665f739f53c6c19ba4d6f0d579c Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 2 Feb 2024 12:27:51 +0100 Subject: [PATCH] ENH: ensightFile writeInt() method to replace two-parameter write - more explicit/transparent handling - avoids compiler warnings about non-virtual methods --- src/fileFormats/ensight/file/ensightFile.C | 71 ++++++++++--------- src/fileFormats/ensight/file/ensightFile.H | 19 +++-- .../conversion/ensight/ensightOutputCloud.C | 4 +- 3 files changed, 53 insertions(+), 41 deletions(-) diff --git a/src/fileFormats/ensight/file/ensightFile.C b/src/fileFormats/ensight/file/ensightFile.C index dca789e4c1..6ee8f464ba 100644 --- a/src/fileFormats/ensight/file/ensightFile.C +++ b/src/fileFormats/ensight/file/ensightFile.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2016-2023 OpenCFD Ltd. + Copyright (C) 2016-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -221,7 +221,7 @@ Foam::Ostream& Foam::ensightFile::write } -Foam::Ostream& Foam::ensightFile::write(const int32_t val) +void Foam::ensightFile::writeInt(const int32_t val, const int fieldWidth) { if (format() == IOstreamOption::BINARY) { @@ -233,24 +233,22 @@ Foam::Ostream& Foam::ensightFile::write(const int32_t val) } else { - stdStream().width(10); + stdStream().width(fieldWidth); stdStream() << val; syncState(); } - - return *this; } -Foam::Ostream& Foam::ensightFile::write(const int64_t val) +void Foam::ensightFile::writeInt(const int64_t val, const int fieldWidth) { - int32_t ivalue(narrowInt32(val)); + int32_t work(narrowInt32(val)); - return write(ivalue); + writeInt(work, fieldWidth); } -Foam::Ostream& Foam::ensightFile::write(const float val) +void Foam::ensightFile::writeFloat(const float val, const int fieldWidth) { if (format() == IOstreamOption::BINARY) { @@ -262,40 +260,45 @@ Foam::Ostream& Foam::ensightFile::write(const float val) } else { - stdStream().width(12); + stdStream().width(fieldWidth); stdStream() << val; syncState(); } +} + +void Foam::ensightFile::writeFloat(const double val, const int fieldWidth) +{ + float work(narrowFloat(val)); + + writeFloat(work, fieldWidth); +} + + +Foam::Ostream& Foam::ensightFile::write(const int32_t val) +{ + writeInt(val, 10); + return *this; +} + + +Foam::Ostream& Foam::ensightFile::write(const int64_t val) +{ + writeInt(val, 10); + return *this; +} + + +Foam::Ostream& Foam::ensightFile::write(const float val) +{ + writeFloat(val, 12); return *this; } Foam::Ostream& Foam::ensightFile::write(const double val) { - float fvalue(narrowFloat(val)); - - return write(fvalue); -} - - -Foam::Ostream& Foam::ensightFile::write -( - const label value, - const label fieldWidth -) -{ - if (format() == IOstreamOption::BINARY) - { - write(value); - } - else - { - stdStream().width(fieldWidth); - stdStream() << value; - syncState(); - } - + writeFloat(val, 12); return *this; } @@ -376,7 +379,7 @@ void Foam::ensightFile::beginParticleCoordinates(const label nparticles) { writeString("particle coordinates"); newline(); - write(nparticles, 8); // unusual width + writeInt(nparticles, 8); // Warning: unusual width newline(); } diff --git a/src/fileFormats/ensight/file/ensightFile.H b/src/fileFormats/ensight/file/ensightFile.H index 2e16f0e95d..79ed2f0a7b 100644 --- a/src/fileFormats/ensight/file/ensightFile.H +++ b/src/fileFormats/ensight/file/ensightFile.H @@ -163,6 +163,18 @@ public: //- Write string as "%79s" or as binary (max 80 chars) void writeString(const std::string& str); + //- Write integer value with specified width or as binary + void writeInt(const int32_t val, const int fieldWidth); + + //- Write (narrowed) integer value with specified width or as binary + void writeInt(const int64_t val, const int fieldWidth); + + //- Write floating-point with specified width or as binary + void writeFloat(const float val, const int fieldWidth); + + //- Write (narrowed) floating-point with specified width or as binary + void writeFloat(const double val, const int fieldWidth); + //- Write undef value void writeUndef(); @@ -197,15 +209,12 @@ public: //- Write string, uses writeString() virtual Ostream& write(const std::string& str) override; - //- Write integer as "%10d" or as binary + //- Write integer value as "%10d" or as binary virtual Ostream& write(const int32_t val) override; - //- Write integer as "%10d" or as binary + //- Write integer value as "%10d" or as binary (narrowed to int32_t) virtual Ostream& write(const int64_t val) override; - //- Write integer with specified width or as binary - Ostream& write(const label value, const label fieldWidth); - //- Write floating-point as "%12.5e" or as binary virtual Ostream& write(const float val) override; diff --git a/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloud.C b/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloud.C index d81b259444..747bb0abeb 100644 --- a/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloud.C +++ b/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloud.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2016-2022 OpenCFD Ltd. + Copyright (C) 2016-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -61,7 +61,7 @@ static inline label writeMeasured_ascii { for (const floatVector& p : points) { - os.write(++pointId, 8); // 1-index and an unusual width + os.writeInt(++pointId, 8); // 1-index and an unusual width os.write(p.x()); os.write(p.y()); os.write(p.z());