From 6188e64fc981593bdeba15e62c04008f2d754015 Mon Sep 17 00:00:00 2001 From: schnepe2 Date: Thu, 11 Jan 2024 21:41:54 +0100 Subject: [PATCH] Floating point output stream formatter added to serialize double/float values nice & tidy. --- Src/Orbiter/OrbiterAPI.cpp | 6 ++++-- Src/Orbiter/Util.cpp | 34 ++++++++++++++++++++++++++++++++++ Src/Orbiter/Util.h | 16 ++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/Src/Orbiter/OrbiterAPI.cpp b/Src/Orbiter/OrbiterAPI.cpp index 9ee9d4f9f..79565f080 100644 --- a/Src/Orbiter/OrbiterAPI.cpp +++ b/Src/Orbiter/OrbiterAPI.cpp @@ -2348,13 +2348,15 @@ DLLEXPORT void oapiWriteScenario_int (FILEHANDLE file, char *item, int i) DLLEXPORT void oapiWriteScenario_float (FILEHANDLE file, char *item, double d) { ofstream &ofs = *(ofstream*)file; - ofs << " " << item << ' ' << d << endl; + FltFormat f{ 6 }; // default precision: 6 + ofs << " " << item << ' ' << f(d) << endl; } DLLEXPORT void oapiWriteScenario_vec (FILEHANDLE file, char *item, const VECTOR3 &vec) { ofstream &ofs = *(ofstream*)file; - ofs << " " << item << ' ' << vec.x << ' ' << vec.y << ' ' << vec.z << endl; + FltFormat f{ 6 }; // default precision: 6 + ofs << " " << item << ' ' << f(vec.x) << ' ' << f(vec.y) << ' ' << f(vec.z) << endl; } DLLEXPORT bool oapiReadScenario_nextline (FILEHANDLE file, char *&line) diff --git a/Src/Orbiter/Util.cpp b/Src/Orbiter/Util.cpp index f251383c3..f7f139d8a 100644 --- a/Src/Orbiter/Util.cpp +++ b/Src/Orbiter/Util.cpp @@ -3,6 +3,8 @@ #include "Util.h" #include +#include +#include LONGLONG NameToId (const char *name) { @@ -97,3 +99,35 @@ void SetClientPos (HWND hWnd, HWND hChild, RECT &r) MoveWindow (hChild, r.left, r.top, r.right-r.left, r.bottom-r.top, true); } + +// ------------------------------------------------------------------------------ +// Floating point output stream formatter +// ------------------------------------------------------------------------------ + +FltFormatter::FltFormatter (int precision, double value) + : precision(precision) + , value(value) +{ +} + +std::ostream& operator<< (std::ostream& os, const FltFormatter& v) +{ + std::stringstream ss; + ss << std::setprecision(v.precision) << std::fixed << v.value; + std::string str; + ss.str().swap(str); + str.resize(str.find_last_not_of("0") + 1); +// if (str[str.length() - 1] == '.') { str.resize(str.length() - 1); } // results in "1" instead of "1." + if (str[str.length() - 1] == '.') { str.push_back('0'); } // results in "1.0" instead of "1." + os << str; + return os; +} + +FltFormat::FltFormat (int precision /* = 6 */) + : precision(precision) +{ +} + +FltFormatter FltFormat::operator() (double value) const { + return FltFormatter(precision, value); +} \ No newline at end of file diff --git a/Src/Orbiter/Util.h b/Src/Orbiter/Util.h index 3cd4ae2c5..4d31c4840 100644 --- a/Src/Orbiter/Util.h +++ b/Src/Orbiter/Util.h @@ -72,4 +72,20 @@ double toc(); // stop clock and return value RECT GetClientPos (HWND hWnd, HWND hChild); void SetClientPos (HWND hWnd, HWND hChild, RECT &r); +// Floating point output stream formatter +struct FltFormatter +{ + int precision; + double value; + FltFormatter (int precision, double value); + friend std::ostream& operator<< (std::ostream& os, const FltFormatter& v); +}; + +struct FltFormat +{ + int precision; + FltFormat (int precision = 6); + FltFormatter operator() (double value) const; +}; + #endif //!__UTIL_H \ No newline at end of file