diff --git a/applications/test/OCharStream1/Test-OCharStream1.cxx b/applications/test/OCharStream1/Test-OCharStream1.cxx index b5def1b8fa..2474cfd5df 100644 --- a/applications/test/OCharStream1/Test-OCharStream1.cxx +++ b/applications/test/OCharStream1/Test-OCharStream1.cxx @@ -183,12 +183,25 @@ int main(int argc, char *argv[]) printInfo(obuf); // Overwrite at some position - obuf.stdStream().rdbuf()->pubseekpos(0.60 * obuf.size()); - obuf << "<" << nl << "OVERWRITE" << nl; + if (auto i = obuf.view().find("item5"); i != std::string::npos) + { + // obuf.seek(0.60 * obuf.size()); + obuf.seek(i); + obuf << "" << nl; + } Info<<"after overwrite" << nl; printInfo(obuf); + // Truncate + { + constexpr float fraction = 0.90; + Info<<"truncated at " << (100*fraction) << "% [" + << int(fraction*obuf.size()) << " chars]" << nl; + obuf.seek(fraction*obuf.size()); + printInfo(obuf); + } + Info<< "transfer contents to a List or ICharStream" << nl; // Reclaim data storage from OCharStream -> ICharStream diff --git a/applications/test/OCharStream2/Make/files b/applications/test/OCharStream2/Make/files new file mode 100644 index 0000000000..e2ffcd6203 --- /dev/null +++ b/applications/test/OCharStream2/Make/files @@ -0,0 +1,3 @@ +Test-OCharStream2.cxx + +EXE = $(FOAM_USER_APPBIN)/Test-OCharStream2 diff --git a/applications/test/OCharStream2/Make/options b/applications/test/OCharStream2/Make/options new file mode 100644 index 0000000000..18e6fe47af --- /dev/null +++ b/applications/test/OCharStream2/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = */ +/* EXE_LIBS = */ diff --git a/applications/test/OCharStream2/Test-OCharStream2.cxx b/applications/test/OCharStream2/Test-OCharStream2.cxx new file mode 100644 index 0000000000..5c66990c41 --- /dev/null +++ b/applications/test/OCharStream2/Test-OCharStream2.cxx @@ -0,0 +1,473 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2025 OpenCFD Ltd. +------------------------------------------------------------------------------- +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 . + +Description + +\*---------------------------------------------------------------------------*/ + +#include "SpanStream.H" +#include "wordList.H" +#include "IOstreams.H" +#include "argList.H" + +#include +#include +#include +#include +#include + +using namespace Foam; + +Ostream& printString(Ostream& os, const char* first, const char* last) +{ + os << '"'; + for (; first != last; (void)++first) + { + os << *first; + } + os << '"'; + + return os; +} + + +Ostream& printView(Ostream& os, const char* first, const char* last) +{ + char buf[4]; + os << label(last-first) << '('; + + for (; first != last; (void)++first) + { + const char c = *first; + + if (isprint(c)) + { + os << c; + } + else if (c == '\t') + { + os << "\\t"; + } + else if (c == '\n') + { + os << "\\n"; + } + else + { + ::snprintf(buf, 4, "%02X", c); + os << "\\x" << buf; + } + } + os << ')'; + + return os; +} + + +Ostream& printView(Ostream& os, std::string_view s) +{ + return printView(os, s.begin(), s.end()); +} + + +Ostream& printView(Ostream& os, const UList& list) +{ + return printView(os, list.begin(), list.end()); +} + + +Ostream& writeList(Ostream& os, const UList& list) +{ + return printView(os, list); +} + + +Ostream& toString(Ostream& os, const UList& list) +{ + return printString(os, list.begin(), list.end()); +} + + +Ostream& toString(Ostream& os, std::string_view s) +{ + return printString(os, s.begin(), s.end()); +} + + +template +void printInfo(const BufType& buf) +{ + Info<< nl << "=========================" << endl; + buf.print(Info); + Info<< "addr: " << Foam::name(buf.view().data()) << nl; + toString(Info, buf.view()); + Info<< nl << "=========================" << endl; +} + + +// Return a left-padded integer as "word" +template +std::string leftpadded(IntType val, char fillch = ' ') +{ + std::string buf; + buf.resize((std::numeric_limits::digits10+1), fillch); + + auto first = (buf.data()); + auto last = (buf.data() + buf.size()); + + auto result = std::to_chars(first, last, val); + + if (result.ec == std::errc{}) + { + auto* iter = result.ptr; + int count = std::distance(iter, last); + + std::cout << "did not fill: " << count << " chars\n"; + + // With two spaces before comments + if (count > 0) { *iter++ = ' '; --count; } + if (count > 0) { *iter++ = ' '; --count; } + for (char c = (count >= 2 ? '/' : ' '); count > 0; --count) + { + *iter++ = c; + } + } + + return buf; +} + + +template +void leftpad(std::ostream& os, IntType val, char fillch = ' ') +{ + // set fill char and width + os.setf(std::ios_base::left, std::ios_base::adjustfield); + fillch = os.fill(fillch); + os.width(std::numeric_limits::digits10+1); + os << val; + // restore fill char + os.fill(fillch); +} + + +template +void rightpad(std::ostream& os, IntType val, char fillch = ' ') +{ + // set fill char and width + os.setf(std::ios_base::right, std::ios_base::adjustfield); + fillch = os.fill(fillch); + os.width(std::numeric_limits::digits10+1); + os << val; + // restore fill char + os.fill(fillch); +} + + +// Left-padded value with trailing comment slashes +template +void leftpad(ocharstream& os, IntType val) +{ + const auto beg = os.tellp(); + os << val; + int count = (std::numeric_limits::digits10+1) - (os.tellp() - beg); + + // With two spaces before comments + if (count > 0) { os << ' '; --count; } + if (count > 0) { os << ' '; --count; } + for (const char c = (count >= 2 ? '/' : ' '); count > 0; --count) + { + os << c; + } +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + argList::noBanner(); + argList::noParallel(); + argList::addBoolOption("fake-zerosize", "Fake overwriting with zero data"); + argList::addBoolOption("dict-format", "Format as dictionary entry"); + + #include "setRootCase.H" + + const bool optFakeZerosize = args.found("fake-zerosize"); + const bool isDictFormat = args.found("dict-format"); + + // const constexpr int width = (std::numeric_limits