From: Tim King Date: Sat, 3 Feb 2018 01:03:10 +0000 (-0800) Subject: Restoring ostream format. Resolves a few CIDs 1362780. (#1543) X-Git-Tag: cvc5-1.0.0~5336 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=1b24f3f0fd5fdd4163a46689949fa8a5c60f3322;p=cvc5.git Restoring ostream format. Resolves a few CIDs 1362780. (#1543) --- diff --git a/src/main/command_executor.cpp b/src/main/command_executor.cpp index 7c8ee7827..a7666dfcf 100644 --- a/src/main/command_executor.cpp +++ b/src/main/command_executor.cpp @@ -193,7 +193,10 @@ bool smtEngineInvoke(SmtEngine* smt, Command* cmd, std::ostream *out) return !cmd->fail(); } -void printStatsIncremental(std::ostream& out, const std::string& prvsStatsString, const std::string& curStatsString) { +void printStatsIncremental(std::ostream& out, + const std::string& prvsStatsString, + const std::string& curStatsString) +{ if(prvsStatsString == "") { out << curStatsString; return; @@ -229,9 +232,11 @@ void printStatsIncremental(std::ostream& out, const std::string& prvsStatsString (std::istringstream(curStatValue) >> curFloat); if(isFloat) { + const std::streamsize old_precision = out.precision(); out << curStatName << ", " << curStatValue << " " << "(" << std::setprecision(8) << (curFloat-prvsFloat) << ")" << std::endl; + out.precision(old_precision); } else { out << curStatName << ", " << curStatValue << std::endl; } diff --git a/src/theory/arith/cut_log.cpp b/src/theory/arith/cut_log.cpp index ad04cfe22..08fe0bc1e 100644 --- a/src/theory/arith/cut_log.cpp +++ b/src/theory/arith/cut_log.cpp @@ -26,6 +26,7 @@ #include "theory/arith/constraint.h" #include "theory/arith/cut_log.h" #include "theory/arith/normal_form.h" +#include "util/ostream_util.h" using namespace std; @@ -84,8 +85,9 @@ void PrimitiveVec::setup(int l){ } void PrimitiveVec::print(std::ostream& out) const{ Assert(initialized()); - out << len << " "; - out.precision(15); + StreamFormatScope scope(out); + + out << len << " " << std::setprecision(15); for(int i = 1; i <= len; ++i){ out << "["<< inds[i] <<", " << coeffs[i]<<"]"; } diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 33218dbe9..ddee2e72b 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -38,6 +38,8 @@ libutil_la_SOURCES = \ index.h \ maybe.h \ ntuple.h \ + ostream_util.cpp \ + ostream_util.h \ proof.h \ regexp.cpp \ regexp.h \ diff --git a/src/util/ostream_util.cpp b/src/util/ostream_util.cpp new file mode 100644 index 000000000..3d6eeea01 --- /dev/null +++ b/src/util/ostream_util.cpp @@ -0,0 +1,31 @@ +/********************* */ +/*! \file result.cpp + ** \verbatim + ** Top contributors (to current version): + ** Tim King, Morgan Deters, Andrew Reynolds + ** This file is part of the CVC4 project. + ** Copyright (c) 2009-2017 by the authors listed in the file AUTHORS + ** in the top-level source directory) and their institutional affiliations. + ** All rights reserved. See the file COPYING in the top-level source + ** directory for licensing information.\endverbatim + ** + ** \brief Utilities for using ostreams. + ** + ** Utilities for using ostreams. + **/ +#include "util/ostream_util.h" + +namespace CVC4 { + +StreamFormatScope::StreamFormatScope(std::ostream& out) + : d_out(out), d_format_flags(out.flags()), d_precision(out.precision()) +{ +} + +StreamFormatScope::~StreamFormatScope() +{ + d_out.precision(d_precision); + d_out.flags(d_format_flags); +} + +} // namespace CVC4 diff --git a/src/util/ostream_util.h b/src/util/ostream_util.h new file mode 100644 index 000000000..e047caa17 --- /dev/null +++ b/src/util/ostream_util.h @@ -0,0 +1,49 @@ +/********************* */ +/*! \file ostream_util.h + ** \verbatim + ** Top contributors (to current version): + ** Tim King + ** This file is part of the CVC4 project. + ** Copyright (c) 2009-2017 by the authors listed in the file AUTHORS + ** in the top-level source directory) and their institutional affiliations. + ** All rights reserved. See the file COPYING in the top-level source + ** directory for licensing information.\endverbatim + ** + ** \brief Utilities for using ostreams. + ** + ** Utilities for using ostreams. + **/ + +#include "cvc4_private.h" + +#ifndef __CVC4__UTIL__OSTREAM_UTIL_H +#define __CVC4__UTIL__OSTREAM_UTIL_H + +#include +#include + +namespace CVC4 { + +// Saves the formatting of an ostream and restores the previous settings on +// destruction. Example usage: +// void Foo::Print(std::ostream& out) { +// StreamFormatScope format_scope(out); +// out << std::setprecision(6) << bar(); +// } +class StreamFormatScope +{ + public: + // `out` must outlive StreamFormatScope. + StreamFormatScope(std::ostream& out); + ~StreamFormatScope(); + + private: + // Does not own the memory of d_out + std::ostream& d_out; + std::ios_base::fmtflags d_format_flags; + std::streamsize d_precision; +}; + +} // namespace CVC4 + +#endif /* __CVC4__UTIL__OSTREAM_UTIL_H */ diff --git a/src/util/sexpr.cpp b/src/util/sexpr.cpp index 61dbccbee..504d58b0e 100644 --- a/src/util/sexpr.cpp +++ b/src/util/sexpr.cpp @@ -30,6 +30,7 @@ #include "base/cvc4_assert.h" #include "options/set_language.h" +#include "util/ostream_util.h" #include "util/smt2_quote_string.h" namespace CVC4 { @@ -219,6 +220,8 @@ void SExpr::toStream(std::ostream& out, const SExpr& sexpr, void SExpr::toStreamRec(std::ostream& out, const SExpr& sexpr, OutputLanguage language, int indent) { + StreamFormatScope scope(out); + if (sexpr.isInteger()) { out << sexpr.getIntegerValue(); } else if (sexpr.isRational()) { diff --git a/src/util/statistics_registry.cpp b/src/util/statistics_registry.cpp index 11afb99ed..4e2b06d53 100644 --- a/src/util/statistics_registry.cpp +++ b/src/util/statistics_registry.cpp @@ -19,7 +19,7 @@ #include "base/cvc4_assert.h" #include "lib/clock_gettime.h" - +#include "util/ostream_util.h" #ifdef CVC4_STATISTICS_ON # define __CVC4_USE_STATISTICS true @@ -134,6 +134,7 @@ inline bool operator>=(const timespec& a, const timespec& b) { /** Output a timespec on an output stream. */ std::ostream& operator<<(std::ostream& os, const timespec& t) { // assumes t.tv_nsec is in range + StreamFormatScope format_scope(os); return os << t.tv_sec << "." << std::setfill('0') << std::setw(9) << std::right << t.tv_nsec; }