Skip to content

Commit

Permalink
Floating point output stream formatter added to serialize double/floa…
Browse files Browse the repository at this point in the history
…t values nice & tidy.
  • Loading branch information
schnepe2 committed Jan 11, 2024
1 parent c31ea84 commit 6188e64
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Src/Orbiter/OrbiterAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions Src/Orbiter/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "Util.h"
#include <shlobj.h>
#include <sstream>
#include <iomanip>

LONGLONG NameToId (const char *name)
{
Expand Down Expand Up @@ -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);
}
16 changes: 16 additions & 0 deletions Src/Orbiter/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6188e64

Please sign in to comment.