Page MenuHomePhabricator

Make mitk/Core locale independent
Closed, ResolvedPublic

Description

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

mitk/Core/Code/Algorithms/mitkUIDGenerator.cpp
mitk/Core/Code/DataManagement/mitkColorProperty.cpp
mitk/Core/Code/IO/mitkSurfaceVtkWriter.txx
mitk/Core/Code/IO/mitkPicHelper.cpp
mitk/Core/Code/Controllers/mitkSliceNavigationController.h
mitk/CoreUI/Qmitk/QmitkMemoryUsageIndicatorView.cpp

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

Could you please open a Wiki-Page? (http://mitk.org/wiki/ChangeRequests/6682)

  • you mention 6 different files, some of which don't look input/ouput related to me without digging into the code. Please describe how these are affected by locale issues
  • how are you going to fix the problems?
  • will you implement unit tests that verify the fix?

(In reply to comment #2)

Could you please open a Wiki-Page? (http://mitk.org/wiki/ChangeRequests/6682)

  • you mention 6 different files, some of which don't look input/ouput related to me without digging into the code. Please describe how these are affected by locale issues
  • how are you going to fix the problems?
  • will you implement unit tests that verify the fix?

You're right, not all of the files listed in the first post are affected by locale issues. Affected files are:

mitk/Corde/Code/Controllers/mitkSliceNavigationController.h
mitk/Corde/Code/Controllers/mitkSliceNavigationController.cpp
mitk/Core/Code/DataManagement/mitkColorProperty.cpp
mitk/Corde/Code/IO/mitkSurfaceVtkWriter.txx
mitk/CoreUI/Qmitk/QmitkMemoryUsageIndicatorView.cpp

Fixing is pretty straight forward here since all of the locale issues are related to std::ostringstreams and itk::OStringStreams in very local scopes. Hence, when there are floating point values to be written to the streams, all that have to be done is a previous call like the following:

stringstream.imbue(std::locale::classic());

For further information on MITK locale issues have a look at T2474 and related ones.

[3adcc4]: Merge branch 'bug-6682-MitkCoreLocaleIndependent'

Merged commits:

2011-04-06 17:51:40 Stefan Kislinskiy [fbe65a]
Made MITK core locale independent.