Page MenuHomePhabricator

Poco is not locale independent
Closed, WontfixPublic

Description

The Poco library number parsers are not locale independent.

This is a known bug:
http://sourceforge.net/tracker/index.php?func=detail&aid=2801954&group_id=132964&atid=725709

As it does not seem as if Poco is going to fix this very soon (It is more than 18 months old) we should think about
a) Fixing this ourselves and proposing a patch to Poco or
b) Creating a standard solution to calling Poco code from within MITK that avoids this problem

See also T6683

As a hint here is the standard implementation for fixing this within our own code:

It seems, that 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:

// ... 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 )

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

[reply] [-]
Private
Comment 15 Caspar Jonas Goch 2011-01-25 15:25:15 CET

Expanded default message to encompass hints how to do this using printf and

sprintf

In an effort to make mitk locale independent we have taken a look at the
following files:

It seems, that 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]

Event Timeline

Won't fix because Poco is no longer supported

Merging "Utilities" component into "Other"

kislinsk added a project: Bulk Edit.
kislinsk removed a project: Bulk Edit.