Page MenuHomePhabricator

Tiny xml is not locale independent
Closed, ResolvedPublic

Description

Tiny xml does not save/read floats and doubles locale independent, but uses the system locale decimal separator.

Third party bug already entered in their bug tracker

http://sourceforge.net/tracker/index.php?func=detail&aid=2939808&group_id=13559&atid=113559

Event Timeline

I disagree with the resolution in this case since it does not look like the tinyxml bug gets fixes soon. Two options:

  1. (preferred) we fix it in our tinyxml version and submit a patch to them
  2. we change the locale before any tinyxml calls

Made it party bug and adjusted importance.

I agree, that the first method is preferrable. As a hint on how to solve it, especially the printf/scanf part:

Floats are written to a stream or gotten from a stream without
specifying the used locale. This can lead to problems if a file is written on a
system with e.g. german locale and read on a system with an english locale or
vice versa. To prevent this kind of problematic behaviour it is suggested to
specify the locale used.

This can be done similar to the following code snippet:
[code - streams]
// ... create stream here

// define standard locale
std::locale C("C");

get previous locale of the stream and save it
(especially important in case of cout and cin)
std::locale originalLocale = stream.getloc();

// switch the stream to standard locale
stream.imbue(C);

// ... write your data to file here

// switch back to the previous locale
stream.imbue( originalLocale );

[/code - streams]

Remember to take care of possible exceptions, by switching back to the original
locale [ stream.imbue( originalLocale ) ] within the exception handling.

[code - printf]
#include <stdio.h>
#include <locale.h>

int main( void )
{

//... other code

//save old locale
char * oldLocale;
oldLocale = setlocale( LC_ALL, 0 );

//set new locale
setlocale( LC_ALL, "C" );

//... other code using printf or sprintf to print

//restore locale
setlocale( LC_ALL, oldLocale );

//... other code

}
[/code - printf]

[092876]: Merge branch 'bug-6683-tiny-xml-is-not-locale-independant'

Merged commits:

2011-05-28 15:48:54 Anja Groch [839b97]
added the proposed code of metaT2474 to tiny xml in order to make it locale independent

Fixed, but we left the bug open until Marco posts the patch to the tiny xml forum.

Merging "Utilities" component into "Other"

Whats the status here?
@Marco: did you post the patch to tiny xml?

It's done, but Marco, you wanted to post it in the tiny xml forum.

I added a link to the patch to the tinyxml bugtracker.