Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Modules/Core/Common/CMake/itkCheckHasConfigthreadlocale.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

// Check if _configthreadlocale is available (Windows thread-local locale)
#include <locale.h>

int
main()
{
int prev = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
if (prev != -1)
{
_configthreadlocale(prev);
}
return 0;
}
37 changes: 37 additions & 0 deletions Modules/Core/Common/CMake/itkCheckHasNewlocale.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

// Check if newlocale and uselocale are available (POSIX thread-local locale)
#include <locale.h>

#if defined(__APPLE__)
# include <xlocale.h>
#endif

int
main()
{
locale_t loc = newlocale(LC_NUMERIC_MASK, "C", nullptr);
if (loc)
{
locale_t prev = uselocale(loc);
uselocale(prev);
freelocale(loc);
}
return 0;
}
22 changes: 22 additions & 0 deletions Modules/Core/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,28 @@ try_compile(
${CMAKE_CURRENT_SOURCE_DIR}/CMake/itkCheckHasSchedGetAffinity.cxx
)

# Check for thread-local locale functions for NumericLocale
try_compile(
ITK_HAS_NEWLOCALE
${ITK_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/CMake/itkCheckHasNewlocale.cxx
)

try_compile(
ITK_HAS_CONFIGTHREADLOCALE
${ITK_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/CMake/itkCheckHasConfigthreadlocale.cxx
)

# Warn if no thread-safe locale method is available
if(NOT ITK_HAS_NEWLOCALE AND NOT ITK_HAS_CONFIGTHREADLOCALE)
message(WARNING
"Neither POSIX newlocale/uselocale nor Windows _configthreadlocale detected. "
"Thread-safe numeric locale handling is not available. "
"Locale must be managed at the application level if needed."
)
endif()

#-----------------------------------------------------------------------------
# Make default visibility as an option for generated export header

Expand Down
84 changes: 84 additions & 0 deletions Modules/Core/Common/include/itkNumericLocale.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkNumericLocale_h
#define itkNumericLocale_h

#include "itkMacro.h"
#include "ITKCommonExport.h"

#include <memory>

namespace itk
{

/** \class NumericLocale
* \brief RAII class for thread-safe temporary setting of LC_NUMERIC locale to "C".
*
* This class provides a thread-safe mechanism to temporarily set the LC_NUMERIC
* locale to "C" for locale-independent parsing and formatting of floating-point
* numbers. The original locale is automatically restored when the object goes
* out of scope.
*
* This is particularly useful when parsing file formats that use dot as decimal
* separator (like NRRD, VTK, etc.) regardless of the system locale setting.
*
* Thread safety:
* - On POSIX systems (when newlocale/uselocale are available): Uses thread-local locale
* - On Windows (when _configthreadlocale is available): Uses thread-specific locale
* - Fallback (when neither is available): Issues a warning if locale differs from "C",
* but does not change the locale. Applications must manage locale externally.
*
* Example usage:
* \code
* {
* NumericLocale cLocale;
* // Parse file with dot decimal separator
* double value = std::strtod("3.14159", nullptr);
* // Locale automatically restored here
* }
* \endcode
*
* \ingroup ITKCommon
*/
class ITKCommon_EXPORT NumericLocale
{
public:
/** Constructor: Saves current LC_NUMERIC locale and sets it to "C" */
NumericLocale();

/** Destructor: Restores the original LC_NUMERIC locale */
~NumericLocale();

// Delete copy and move operations
NumericLocale(const NumericLocale &) = delete;
NumericLocale &
operator=(const NumericLocale &) = delete;
NumericLocale(NumericLocale &&) = delete;
NumericLocale &
operator=(NumericLocale &&) = delete;

private:
// Forward declaration of implementation structure
struct Impl;
// Pointer to implementation (pImpl idiom)
std::unique_ptr<Impl> m_Impl;
};

} // end namespace itk

#endif // itkNumericLocale_h
1 change: 1 addition & 0 deletions Modules/Core/Common/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ set(
itkLogger.cxx
itkLogOutput.cxx
itkLoggerOutput.cxx
itkNumericLocale.cxx
itkProgressAccumulator.cxx
itkTotalProgressReporter.cxx
itkNumericTraits.cxx
Expand Down
5 changes: 5 additions & 0 deletions Modules/Core/Common/src/itkConfigurePrivate.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@
// defined if fenv_t struct as __cw member
#cmakedefine ITK_HAS_STRUCT_FENV_T_CW

// defined if newlocale/uselocale are available (POSIX thread-local locale)
#cmakedefine ITK_HAS_NEWLOCALE
// defined if _configthreadlocale is available (Windows thread-local locale)
#cmakedefine ITK_HAS_CONFIGTHREADLOCALE

#endif //itkConfigurePrivate_h
150 changes: 150 additions & 0 deletions Modules/Core/Common/src/itkNumericLocale.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

#include "itkNumericLocale.h"
#include "itkConfigurePrivate.h"

#include <locale.h>
#include <cstring>
#include <cstdlib>

// Include platform-specific headers based on detected features
#ifdef ITK_HAS_NEWLOCALE
# if defined(__APPLE__)
# include <xlocale.h>
# endif
#endif

namespace itk
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of assuming the thread-specific locale method are an available based on preprocessor defined add CMake try_compile to detect the method. Follow the existing pattern in Modules/Core/Common/CMakeLists.txt, add the test source code to the same CMake directory, and add a cmakedefine to itkConfigurePrivate.h.in.

If neither method is available produce a CMake warning, thread-safe locales are not supported and that the locale will need to be managed and in the application if needed.

{

// Implementation structure definition
struct NumericLocale::Impl
{
#ifdef ITK_HAS_CONFIGTHREADLOCALE
// Windows: thread-specific locale
int m_PreviousThreadLocaleSetting{ -1 };
char * m_SavedLocale{ nullptr };
#elif defined(ITK_HAS_NEWLOCALE)
// POSIX: thread-local locale
locale_t m_PreviousLocale{ nullptr };
locale_t m_CLocale{ nullptr };
#else
// Fallback: no locale change, only check and warn
bool m_WarningIssued{ false };
#endif
};

#ifdef ITK_HAS_CONFIGTHREADLOCALE

// Windows implementation using thread-specific locale
NumericLocale::NumericLocale()
: m_Impl(new Impl())
{
// Enable thread-specific locale for this thread
m_Impl->m_PreviousThreadLocaleSetting = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);

// Save current LC_NUMERIC locale
const char * currentLocale = setlocale(LC_NUMERIC, nullptr);
if (currentLocale)
{
m_Impl->m_SavedLocale = _strdup(currentLocale);
// If _strdup fails (returns nullptr), m_SavedLocale remains nullptr
// and the locale will not be restored in the destructor
}

// Set to C locale for parsing
setlocale(LC_NUMERIC, "C");
}

NumericLocale::~NumericLocale()
{
// Restore original locale
if (m_Impl->m_SavedLocale)
{
setlocale(LC_NUMERIC, m_Impl->m_SavedLocale);
free(m_Impl->m_SavedLocale);
}

// Restore previous thread-specific locale setting
if (m_Impl->m_PreviousThreadLocaleSetting != -1)
{
_configthreadlocale(m_Impl->m_PreviousThreadLocaleSetting);
}
}

#elif defined(ITK_HAS_NEWLOCALE)

// POSIX implementation using thread-local locale (uselocale/newlocale)
NumericLocale::NumericLocale()
: m_Impl(new Impl())
{
// Create a new C locale
// If newlocale fails (returns nullptr), m_CLocale remains nullptr
// and the locale will not be changed - this is a safe fallback
m_Impl->m_CLocale = newlocale(LC_NUMERIC_MASK, "C", nullptr);

if (m_Impl->m_CLocale)
{
// Set the C locale for this thread and save the previous locale
// uselocale returns the previous locale, which may be LC_GLOBAL_LOCALE
m_Impl->m_PreviousLocale = uselocale(m_Impl->m_CLocale);
}
}

NumericLocale::~NumericLocale()
{
// If we created and installed a C locale for this thread,
// restore the previous locale and free the C locale.
// Always restore if m_CLocale is set, as m_PreviousLocale may be
// LC_GLOBAL_LOCALE which is a valid non-null value.
if (m_Impl->m_CLocale)
{
uselocale(m_Impl->m_PreviousLocale);
freelocale(m_Impl->m_CLocale);
}
}

#else

// Fallback implementation - only check locale and issue warning if not "C"
// Do not modify the locale, application must manage it externally
NumericLocale::NumericLocale()
: m_Impl(new Impl())
{
// Check if current locale is compatible with expected "C" locale
const char * currentLocale = setlocale(LC_NUMERIC, nullptr);
if (currentLocale && std::strcmp(currentLocale, "C") != 0)
{
// Issue warning only once per instance
itkWarningMacro("LC_NUMERIC locale is '" << currentLocale << "' (not 'C'). "
<< "Thread-safe locale functions not available. "
<< "Locale-dependent number parsing may cause issues. "
<< "Please manage locale at application level.");
m_Impl->m_WarningIssued = true;
}
}

NumericLocale::~NumericLocale()
{
// No action needed in fallback - we don't change the locale
}

#endif

} // end namespace itk
1 change: 1 addition & 0 deletions Modules/Core/Common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1890,6 +1890,7 @@ set(
itkMersenneTwisterRandomVariateGeneratorGTest.cxx
itkNeighborhoodAllocatorGTest.cxx
itkNumberToStringGTest.cxx
itkNumericLocaleGTest.cxx
itkObjectFactoryBaseGTest.cxx
itkOffsetGTest.cxx
itkOptimizerParametersGTest.cxx
Expand Down
Loading
Loading