diff options
author | ridiculousfish <corydoras@ridiculousfish.com> | 2012-02-17 15:55:54 -0800 |
---|---|---|
committer | ridiculousfish <corydoras@ridiculousfish.com> | 2012-02-17 15:55:54 -0800 |
commit | 8f1423946fd3ab9d5bca168de7fb12b9c397b034 (patch) | |
tree | 68031b5db0c159b8a4223f8c89b0d86fb2bf61d6 /wutil.cpp | |
parent | 51da4856e2ff124d8d5ad7a4a6ca844c81825133 (diff) |
Fix a crash when using quotes due to wgettext thread safety issues.
Diffstat (limited to 'wutil.cpp')
-rw-r--r-- | wutil.cpp | 45 |
1 files changed, 25 insertions, 20 deletions
@@ -18,8 +18,10 @@ #include <stdarg.h> #include <limits.h> #include <libgen.h> +#include <pthread.h> #include <string> + #if HAVE_LIBINTL_H #include <libintl.h> #endif @@ -71,13 +73,6 @@ static char *wcs2str_buff=0; */ static size_t wcs2str_buff_count=0; -/** - For wgettext: Flag to tell whether the translation library has been initialized -*/ -static int wgettext_is_init = 0; - - - void wutil_init() { @@ -282,24 +277,25 @@ wcstring wbasename( const wcstring &path ) return result; } -/** - For wgettext: Internal init function. Automatically called when a translation is first requested. -*/ -static void wgettext_init() -{ - int i; - - wgettext_is_init = 1; - - for( i=0; i<BUFF_COUNT; i++ ) +/* Really init wgettext */ +static void wgettext_really_init() { + for( size_t i=0; i<BUFF_COUNT; i++ ) { sb_init( &buff[i] ); } - bindtextdomain( PACKAGE_NAME, LOCALEDIR ); textdomain( PACKAGE_NAME ); } +/** + For wgettext: Internal init function. Automatically called when a translation is first requested. +*/ +static void wgettext_init_if_necessary() +{ + static pthread_once_t once = PTHREAD_ONCE_INIT; + pthread_once(&once, wgettext_really_init); +} + /** For wgettext: Wide to narrow character conversion. Internal implementation that @@ -322,11 +318,12 @@ static char *wgettext_wcs2str( const wchar_t *in ) const wchar_t *wgettext( const wchar_t *in ) { + ASSERT_IS_MAIN_THREAD(); + if( !in ) return in; - if( !wgettext_is_init ) - wgettext_init(); + wgettext_init_if_necessary(); char *mbs_in = wgettext_wcs2str( in ); char *out = gettext( mbs_in ); @@ -341,6 +338,14 @@ const wchar_t *wgettext( const wchar_t *in ) return wres; } +wcstring wgettext2(const wcstring &in) { + wgettext_init_if_necessary(); + std::string mbs_in = wcs2string(in); + char *out = gettext( mbs_in.c_str() ); + wcstring result = format_string(L"%s", out); + return result; +} + const wchar_t *wgetenv( const wchar_t *name ) { ASSERT_IS_MAIN_THREAD(); |