Extract set_win32_thread_locale() to a new C file gnucash-windows-locale.c

g++ can't find the Microsoft Locale symbols, gcc can.
This commit is contained in:
John Ralls
2020-05-30 14:21:07 -07:00
parent e810ee1c09
commit 14ed322297
3 changed files with 77 additions and 46 deletions

View File

@@ -41,6 +41,7 @@ set (gnucash_SOURCES
gnucash-commands.cpp
gnucash-core-app.cpp
gnucash-gresources.c
gnucash-windows-locale.c
${GNUCASH_RESOURCE_FILE}
)
@@ -66,6 +67,7 @@ add_executable (gnucash-cli
gnucash-cli.cpp
gnucash-commands.cpp
gnucash-core-app.cpp
gnucash-windows-locale.c
${GNUCASH_RESOURCE_FILE}
${gnucash_noinst_HEADERS}
)

View File

@@ -25,6 +25,7 @@
#include <libguile.h>
#include <guile-mappings.h>
#ifdef __MINGW32__
extern "C" void set_win32_thread_locale(char**);
#include <Windows.h>
#include <fcntl.h>
#endif
@@ -412,51 +413,6 @@ gnc_log_init (std::vector <std::string> &log_flags, std::string &log_to_filename
}
}
#ifdef __MINGW32__
/* If one of the Unix locale variables LC_ALL, LC_MESSAGES, or LANG is
* set in the environment check to see if it's a valid locale and if
* it is set both the Windows and POSIX locales to that. If not
* retrieve the Windows locale and set POSIX to match.
*/
static void
set_win32_thread_locale()
{
WCHAR lpLocaleName[LOCALE_NAME_MAX_LENGTH];
char *locale = NULL;
if (((locale = getenv ("LC_ALL")) != NULL && locale[0] != '\0') ||
((locale = getenv ("LC_MESSAGES")) != NULL && locale[0] != '\0') ||
((locale = getenv ("LANG")) != NULL && locale[0] != '\0'))
{
gunichar2* wlocale = NULL;
int len = 0;
len = strchr(locale, '.') - locale;
locale[2] = '-';
wlocale = g_utf8_to_utf16 (locale, len, NULL, NULL, NULL);
if (IsValidLocaleName(wlocale))
{
LCID lcid = LocaleNameToLCID(wlocale, LOCALE_ALLOW_NEUTRAL_NAMES);
SetThreadLocale(lcid);
locale[2] = '_';
setlocale (LC_ALL, locale);
sys_locale = locale;
g_free(wlocale);
return;
}
g_free(locale);
g_free(wlocale);
}
if (GetUserDefaultLocaleName(lpLocaleName, LOCALE_NAME_MAX_LENGTH))
{
sys_locale = g_utf16_to_utf8((gunichar2*)lpLocaleName,
LOCALE_NAME_MAX_LENGTH,
NULL, NULL, NULL);
sys_locale[2] = '_';
setlocale (LC_ALL, sys_locale);
return;
}
}
#endif
/* Creates a console window on MSWindows to display stdout and stderr
* when __MSWIN_CONSOLE__ is defined at the top of the file.
@@ -529,7 +485,7 @@ Gnucash::CoreApp::CoreApp ()
#ifdef MAC_INTEGRATION
set_mac_locale();
#elif defined __MINGW32__
set_win32_thread_locale();
set_win32_thread_locale(&sys_locale);
#endif
gnc_environment_setup();
#if ! defined MAC_INTEGRATION && ! defined __MINGW32__/* setlocale already done */

View File

@@ -0,0 +1,73 @@
/*
* gnucash-core-app.cpp -- Basic application object for gnucash binaries
*
* Copyright (C) 2020 John Ralls <jralls@ceridwen.us>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, contact:
*
* Free Software Foundation Voice: +1-617-542-5942
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
* Boston, MA 02110-1301, USA gnu@gnu.org
*/
#include <Windows.h>
#include <fcntl.h>
#include <glib/gi18n.h>
//sacrificial prototype
void set_win32_thread_locale(char **sys_locale);
/* If one of the Unix locale variables LC_ALL, LC_MESSAGES, or LANG is
* set in the environment check to see if it's a valid locale and if
* it is set both the Windows and POSIX locales to that. If not
* retrieve the Windows locale and set POSIX to match.
*/
void
set_win32_thread_locale(char **sys_locale)
{
WCHAR lpLocaleName[LOCALE_NAME_MAX_LENGTH];
char *locale = NULL;
if (((locale = getenv ("LC_ALL")) != NULL && locale[0] != '\0') ||
((locale = getenv ("LC_MESSAGES")) != NULL && locale[0] != '\0') ||
((locale = getenv ("LANG")) != NULL && locale[0] != '\0'))
{
gunichar2* wlocale = NULL;
int len = 0;
len = strchr(locale, '.') - locale;
locale[2] = '-';
wlocale = g_utf8_to_utf16 (locale, len, NULL, NULL, NULL);
if (IsValidLocaleName(wlocale))
{
LCID lcid = LocaleNameToLCID(wlocale, LOCALE_ALLOW_NEUTRAL_NAMES);
SetThreadLocale(lcid);
locale[2] = '_';
setlocale (LC_ALL, locale);
*sys_locale = g_strdup (locale);
g_free(wlocale);
return;
}
g_free(locale);
g_free(wlocale);
}
if (GetUserDefaultLocaleName(lpLocaleName, LOCALE_NAME_MAX_LENGTH))
{
*sys_locale = g_utf16_to_utf8((gunichar2*)lpLocaleName,
LOCALE_NAME_MAX_LENGTH,
NULL, NULL, NULL);
(*sys_locale)[2] = '_';
setlocale (LC_ALL, *sys_locale);
return;
}
}