pastebin - collaborative debugging tool
rovema.kpaste.net RSS


RDS: Windows locale fixes
Posted by Anonymous on Tue 27th Jun 2023 11:31
raw | new post
view followups (newest first): RDS: Windows locale fixes by Anonymous
modification of post by Anonymous (view diff)

  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..85d6aeb 100644
  49. --- a/Base/RovLocale.cpp
  50. +++ b/Base/RovLocale.cpp
  51. @@ -1,16 +1,17 @@
  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 <stdio.h>
  71. +#include <pthread.h>
  72.  
  73.  static pthread_once_t  posix_localeobj_init = PTHREAD_ONCE_INIT;
  74.  static locale_t                posix_localeobj = 0;
  75. @@ -23,7 +24,19 @@ static void init_posix_localeobj(void)
  76.          * resources at shutdown, and only writes data to NVRAM
  77.          * before |exit()|.
  78.          */
  79. -       posix_localeobj = newlocale(LC_ALL_MASK, "POSIX", (locale_t)0);
  80. +       posix_localeobj = newlocale(LC_ALL_MASK,
  81. +#if EMULATE_POSIX_NEWLOCALE_ON_WINDOWS
  82. +               "C",
  83. +#else
  84. +               "POSIX",
  85. +#endif
  86. +               (locale_t)0);
  87. +
  88. +#if 0
  89. +       (void)fprintf(stderr,
  90. +               "init_posix_localeobj: POSIX locale object %llx\n",
  91. +               (long long)posix_localeobj);
  92. +#endif
  93.  }
  94.  
  95.  locale_t rov_get_posix_localeobj(void)
  96. @@ -32,3 +45,85 @@ locale_t rov_get_posix_localeobj(void)
  97.        
  98.         return posix_localeobj;
  99.  }
  100. +
  101. +/*
  102. + * POSIX |newlocale()|&co emulation, if the compiler environment does
  103. + * not provide one
  104. + */
  105. +#if EMULATE_POSIX_NEWLOCALE_ON_WINDOWS
  106. +
  107. +locale_t newlocale(int mask, const char * locale, locale_t base_not_implemented)
  108. +{
  109. +       return _create_locale(mask, locale);
  110. +}
  111. +
  112. +locale_t uselocale(locale_t newlocobj)
  113. +{
  114. +       locale_t old_locale = _get_current_locale();
  115. +
  116. +       /*
  117. +        * uselocale() sets the thread's locale by definition, so
  118. +        * unconditionally use thread-local locale
  119. +        */
  120. +       (void)_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
  121. +
  122. +#define ElementsInArray(x)   (sizeof(x) / sizeof((x)[0]))
  123. +       /*
  124. +        * sets all categories - right now our poor man's emulation
  125. +        * can't do it better
  126. +        */
  127. +       for(int i=0 ; i < ElementsInArray(newlocobj->locinfo->lc_category) ; i++) {
  128. +               const char *curr_ln = newlocobj->locinfo->lc_category[1].locale;
  129. +#if 0
  130. +               (void)fprintf(stderr,
  131. +                       "uselocale: switching to locale %d/|%s|\n",
  132. +                       i, curr_ln);
  133. +#endif
  134. +               (void)setlocale(i, curr_ln);
  135. +       }
  136. +
  137. +       /* return the old locale_t */
  138. +       return old_locale;
  139. +}
  140. +
  141. +void freelocale(locale_t locobj)
  142. +{
  143. +       _free_locale(locobj);
  144. +}
  145. +#endif /* EMULATE_POSIX_NEWLOCALE_ON_WINDOWS */
  146. +
  147. +
  148. +
  149. +#ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  150. +/* cursed glibc does not provide |printf_l()| */
  151. +int vprintf_l(locale_t loc, const char *format, va_list ap)
  152. +{
  153. +       locale_t prev_loc;
  154. +       int ret;
  155. +       int saved_errno;
  156. +
  157. +       prev_loc = uselocale(loc);
  158. +
  159. +       ret = vprintf(format, ap);
  160. +       saved_errno = errno;
  161. +
  162. +       (void)uselocale(prev_loc);
  163. +
  164. +       errno = saved_errno;
  165. +       return ret;
  166. +}
  167. +
  168. +int printf_l(locale_t loc, const char *format, ...)
  169. +{
  170. +       va_list args;
  171. +       int res;
  172. +
  173. +       va_start(args, format);
  174. +       res = vprintf_l(loc, format, args);
  175. +       va_end(args);
  176. +
  177. +       return res;
  178. +}
  179. +#endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
  180. +
  181. +} /* extern "C" */
  182. diff --git a/Base/RovLocale.hpp b/Base/RovLocale.hpp
  183. new file mode 100644
  184. index 0000000..ab4136e
  185. --- /dev/null
  186. +++ b/Base/RovLocale.hpp
  187. @@ -0,0 +1,60 @@
  188. +///////////////////////////////////////////////////////////////////////////////
  189. +// RovLocale.hpp            POSIX i18n/l10n support functions
  190. +// Copyright                ROVEMA
  191. +// Response                 Roland Mainz <roland.mainz@rovema.de>
  192. +// Description              Support functions for i18n and l10n
  193. +///////////////////////////////////////////////////////////////////////////////
  194. +
  195. +#ifndef INC_ROVLOCALE_HPP
  196. +#define INC_ROVLOCALE_HPP 1
  197. +
  198. +extern "C" {
  199. +
  200. +#include <locale.h>
  201. +
  202. +#if defined(WIN32) && !defined(LC_GLOBAL_LOCATE)
  203. +#define EMULATE_POSIX_NEWLOCALE_ON_WINDOWS 1
  204. +#define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L 1
  205. +#endif /* defined(WIN32) && !defined(LC_GLOBAL_LOCATE) */
  206. +
  207. +#ifdef __GLIBC__
  208. +/* { BSD, MacOSX, Illumos, ... } do not need this! */
  209. +#define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L 1
  210. +#endif
  211. +
  212. +
  213. +/*
  214. + * POSIX |()*_l| functions, for platforms which do not implement them
  215. + */
  216. +#if CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  217. +#include <stdarg.h>
  218. +#include <errno.h>
  219. +
  220. +int vprintf_l(locale_t loc, const char *format, va_list ap);
  221. +int printf_l(locale_t loc, const char *format, ...);
  222. +#endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
  223. +
  224. +
  225. +/*
  226. + * POSIX |newlocale()|&co emulation for Windows
  227. + */
  228. +#if EMULATE_POSIX_NEWLOCALE_ON_WINDOWS
  229. +typedef _locale_t locale_t;
  230. +#define LC_ALL_MASK    (LC_ALL)
  231. +
  232. +#define strtod_l(a, b, c) _strtod_l((a), (b), (c))
  233. +
  234. +locale_t       newlocale(int mask, const char * locale, locale_t base_not_implemented);
  235. +locale_t       uselocale(locale_t newlocobj);
  236. +void           freelocale(locale_t locobj);
  237. +#endif /* EMULATE_POSIX_NEWLOCALE_ON_WINDOWS */
  238. +
  239. +
  240. +/*
  241. + * ROVEMA prototypes
  242. + */
  243. +extern locale_t rov_get_posix_localeobj(void);
  244. +
  245. +} /* extern "C" */
  246. +
  247. +#endif /* !INC_ROVLOCALE_HPP */
  248. diff --git a/Base/Win32System.hpp b/Base/Win32System.hpp
  249. index 17dac42..ec6b835 100755
  250. --- a/Base/Win32System.hpp
  251. +++ b/Base/Win32System.hpp
  252. @@ -22,6 +22,7 @@
  253.  #include "RovTypes.hpp"
  254.  #include "time.h"
  255.  #include "Error.hpp"
  256. +#include "RovLocale.hpp"
  257.  
  258.  //-DEFINES/MACROS--------------------------------------------------------------
  259.  //#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