pastebin - collaborative debugging tool
rovema.kpaste.net RSS


RDS: Windows locale fixes
Posted by Anonymous on Fri 23rd Jun 2023 16:29
raw | new post
view followups (newest first): RDS: Windows locale fixes by Anonymous

  1. diff --git a/Base/CMakeLists.txt b/Base/CMakeLists.txt
  2. index b3922e1..c789604 100644
  3. --- a/Base/CMakeLists.txt
  4. +++ b/Base/CMakeLists.txt
  5. @@ -25,6 +25,7 @@ set(PROJECT_SOURCE_FILES RovDate.cpp
  6.                           MessageBoxWithChangableButtons.cpp
  7.                           RCriticalSection.cpp
  8.                           Thread.cpp
  9. +                        RovLocale.cpp
  10.                           Error.cpp )
  11.  
  12.  if(WIN32)
  13. @@ -34,15 +35,13 @@ else(WIN32)
  14.      if(RDE_BUILD)
  15.          set(PROJECT_SOURCE_FILES ${PROJECT_SOURCE_FILES}
  16.              RecursiveSemaphor.cpp
  17. -            PosixSystem.cpp
  18. -           RovLocale.cpp )
  19. +            PosixSystem.cpp )
  20.      else(RDE_BUILD)
  21.    set(PROJECT_SOURCE_FILES ${PROJECT_SOURCE_FILES}
  22.        TraceBack.cpp
  23.        RovFileSystem.cpp
  24.        RecursiveSemaphor.cpp
  25. -      PosixSystem.cpp
  26. -      RovLocale.cpp )
  27. +      PosixSystem.cpp )
  28.    endif(RDE_BUILD)
  29.  endif(WIN32)
  30.  
  31. diff --git a/Base/PosixSystem.hpp b/Base/PosixSystem.hpp
  32. index cf7e2c7..6e0acc0 100755
  33. --- a/Base/PosixSystem.hpp
  34. +++ b/Base/PosixSystem.hpp
  35. @@ -38,10 +38,10 @@
  36.  #include <stdlib.h>
  37.  #include "RecursiveSemaphor.hpp"
  38.  #include "Error.hpp"
  39. +#include "RovLocale.hpp"
  40.  
  41.  #undef TEST_REALTIME
  42.  
  43. -extern locale_t rov_get_posix_localeobj(void);
  44.  
  45.  extern void dumpBackTrace( ostream & _outStr );
  46.  
  47. diff --git a/Base/RovLocale.cpp b/Base/RovLocale.cpp
  48. index afe7d2d..9b3db6a 100644
  49. --- a/Base/RovLocale.cpp
  50. +++ b/Base/RovLocale.cpp
  51. @@ -1,16 +1,16 @@
  52.  ///////////////////////////////////////////////////////////////////////////////
  53.  // RovLocale.cpp            POSIX i18n/l10n support functions
  54. -// Copyright                acontis technologies GmbH, Weingarten, Germany
  55. +// Copyright                ROVEMA
  56.  // Response                 Roland Mainz <roland.mainz@rovema.de>
  57.  // Description              Support functions for i18n and l10n
  58.  ///////////////////////////////////////////////////////////////////////////////
  59.  
  60. -#include "PosixSystem.hpp"
  61. +#include "RovLocale.hpp"
  62.  
  63. -/*
  64. - * ToDo:
  65. - * - Add implementations of |printf_l()|/|sprintf_l()| etc.
  66. - */
  67. +extern "C" {
  68. +
  69. +#include <stdlib.h>
  70. +#include <pthread.h>
  71.  
  72.  static pthread_once_t  posix_localeobj_init = PTHREAD_ONCE_INIT;
  73.  static locale_t                posix_localeobj = 0;
  74. @@ -23,7 +23,19 @@ static void init_posix_localeobj(void)
  75.          * resources at shutdown, and only writes data to NVRAM
  76.          * before |exit()|.
  77.          */
  78. -       posix_localeobj = newlocale(LC_ALL_MASK, "POSIX", (locale_t)0);
  79. +       posix_localeobj = newlocale(LC_ALL_MASK,
  80. +#if EMULATE_POSIX_NEWLOCALE_ON_WINDOWS
  81. +               "C",
  82. +#else
  83. +               "POSIX",
  84. +#endif
  85. +               (locale_t)0);
  86. +
  87. +#if 1
  88. +       (void)fprintf(stderr,
  89. +               "init_posix_localeobj: POSIX locale object %llx\n",
  90. +               (long long)posix_localeobj);
  91. +#endif
  92.  }
  93.  
  94.  locale_t rov_get_posix_localeobj(void)
  95. @@ -32,3 +44,90 @@ locale_t rov_get_posix_localeobj(void)
  96.        
  97.         return posix_localeobj;
  98.  }
  99. +
  100. +/*
  101. + * POSIX newlocale()&co emulation, if the compiler environment does
  102. + * not provide one
  103. + */
  104. +#if EMULATE_POSIX_NEWLOCALE_ON_WINDOWS
  105. +
  106. +locale_t newlocale(int mask, const char * locale, locale_t base_not_implemented)
  107. +{
  108. +       return _create_locale(mask, locale);
  109. +}
  110. +
  111. +locale_t uselocale(locale_t newlocobj)
  112. +{
  113. +       locale_t old_locale = _get_current_locale();
  114. +
  115. +       /*
  116. +        * uselocale() sets the thread's locale by definition, so
  117. +        * unconditionally use thread-local locale
  118. +        */
  119. +       (void)_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
  120. +
  121. +#define ElementsInArray(x)   (sizeof(x) / sizeof((x)[0]))
  122. +       /*
  123. +        * sets all categories - right now our poor man's emulation
  124. +        * can't do it better
  125. +        */
  126. +       for(int i=0 ; i < ElementsInArray(newlocobj->locinfo->lc_category) ; i++) {
  127. +               const char *curr_ln = newlocobj->locinfo->lc_category[1].locale;
  128. +#if 1
  129. +               (void)fprintf(stderr,
  130. +                       "uselocale: switching to locale %d/|%s|\n",
  131. +                       i, curr_ln);
  132. +#endif
  133. +               (void)setlocale(i, curr_ln);
  134. +       }
  135. +
  136. +       /* return the old locale_t */
  137. +       return old_locale;
  138. +}
  139. +
  140. +void freelocale(locale_t locobj)
  141. +{
  142. +       _free_locale(locobj);
  143. +}
  144. +#endif /* EMULATE_POSIX_NEWLOCALE_ON_WINDOWS */
  145. +
  146. +
  147. +
  148. +#ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  149. +#include <stdarg.h>
  150. +#include <errno.h>
  151. +#endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
  152. +
  153. +#ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  154. +/* cursed glibc does not provide |printf_l()| */
  155. +int vprintf_l(locale_t loc, const char *format, va_list ap)
  156. +{
  157. +       locale_t prev_loc;
  158. +       int ret;
  159. +       int saved_errno;
  160. +
  161. +       prev_loc = uselocale(loc);
  162. +
  163. +       ret = vprintf(format, ap);
  164. +       saved_errno = errno;
  165. +
  166. +       (void)uselocale(prev_loc);
  167. +
  168. +       errno = saved_errno;
  169. +       return ret;
  170. +}
  171. +
  172. +int printf_l(locale_t loc, const char *format, ...)
  173. +{
  174. +       va_list args;
  175. +       int res;
  176. +
  177. +       va_start(args, format);
  178. +       res = vprintf_l(loc, format, args);
  179. +       va_end(args);
  180. +
  181. +       return res;
  182. +}
  183. +#endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
  184. +
  185. +} /* extern "C" */
  186. diff --git a/Base/Win32System.hpp b/Base/Win32System.hpp
  187. index 17dac42..ec6b835 100755
  188. --- a/Base/Win32System.hpp
  189. +++ b/Base/Win32System.hpp
  190. @@ -22,6 +22,7 @@
  191.  #include "RovTypes.hpp"
  192.  #include "time.h"
  193.  #include "Error.hpp"
  194. +#include "RovLocale.hpp"
  195.  
  196.  //-DEFINES/MACROS--------------------------------------------------------------
  197.  //#ifdef WIN32

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at