write coordinates in C locale

Since a6276cd2 we use g_snprintf for coordinate string formatting. This
function uses number formatting dependant on the current locale and
therefore might produce invalid files. This is fixed now.

Fixes: #1393
presentation
veecue 7 years ago committed by Justus Rossmeier
parent 45906e75f0
commit 984a38bf40
  1. 4
      src/control/xml/XmlPointNode.cpp
  2. 7
      src/control/xml/XmlStrokeNode.cpp
  3. 12
      src/util/Util.cpp
  4. 12
      src/util/Util.h

@ -49,9 +49,7 @@ void XmlPointNode::writeOut(OutputStream* out)
out->write(" ");
}
gchar coordinateStr[Util::PRECISION_FORMAT_BUFF_LEN];
Util::getCoordinateString(coordinateStr, Util::PRECISION_FORMAT_BUFF_LEN, p->x, p->y);
out->write(coordinateStr);
Util::writeCoordinateString(out, p->x, p->y);
}
out->write("</");

@ -89,15 +89,12 @@ void XmlStrokeNode::writeOut(OutputStream* out)
{
out->write(">");
gchar coordinateStr[Util::PRECISION_FORMAT_BUFF_LEN];
Util::getCoordinateString(coordinateStr, Util::PRECISION_FORMAT_BUFF_LEN, points[0].x, points[0].y);
out->write(coordinateStr);
Util::writeCoordinateString(out, points[0].x, points[0].y);
for (int i = 1; i < this->pointsLength; i++)
{
Util::getCoordinateString(coordinateStr, Util::PRECISION_FORMAT_BUFF_LEN, points[i].x, points[i].y);
out->write(" ");
out->write(coordinateStr);
Util::writeCoordinateString(out, points[i].x, points[i].y);
}
out->write("</");

@ -170,8 +170,12 @@ gboolean Util::paintBackgroundWhite(GtkWidget* widget, cairo_t* cr, void*)
return false;
}
gchar* Util::getCoordinateString(gchar* buff, gulong buffLen, double xVal, double yVal)
void Util::writeCoordinateString(OutputStream* out, double xVal, double yVal)
{
g_snprintf(buff, buffLen, Util::PRECISION_FORMAT_STRING_XY, xVal, yVal);
return buff;
}
char coordString[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_formatd(coordString, G_ASCII_DTOSTR_BUF_SIZE, Util::PRECISION_FORMAT_STRING, xVal);
out->write(coordString);
out->write(" ");
g_ascii_formatd(coordString, G_ASCII_DTOSTR_BUF_SIZE, Util::PRECISION_FORMAT_STRING, yVal);
out->write(coordString);
}

@ -11,6 +11,7 @@
#pragma once
#include "OutputStream.h"
#include "Path.h"
#include <gtk/gtk.h>
@ -55,17 +56,12 @@ gboolean paintBackgroundWhite(GtkWidget* widget, cairo_t* cr, void* unused);
/**
* Format coordinates to use 8 digits of precision https://m.xkcd.com/2170/
* This function works like g_ascii_formatd in that it stores the result in buff, and
* also returns the result.
* This function directy writes to the given OutputStream.
*/
extern gchar* getCoordinateString(gchar* buff, gulong buffLen, double xVal, double yVal);
extern void writeCoordinateString(OutputStream* out, double xVal, double yVal);
constexpr const gchar* PRECISION_FORMAT_STRING = "%.8f";
constexpr const gchar* PRECISION_FORMAT_STRING_XY = "%.8f %.8f"; // note the space delimiter
constexpr const int PRECISION_FORMAT_BUFF_LEN = G_ASCII_DTOSTR_BUF_SIZE * 2 + 1;
} // namespace Util
static const size_t npos = std::numeric_limits<size_t>::max();
static const size_t npos = std::numeric_limits<size_t>::max();

Loading…
Cancel
Save