Use wide character API when available. Document that string parameters and

2004-11-24  Tor Lillqvist  <tml@iki.fi>

	* glib/gwin32.c (g_win32_error_message,
	get_package_directory_from_module,
	g_win32_get_package_installation_directory,
	g_win32_get_package_installation_subdirectory): Use wide character
	API when available. Document that string parameters and return
	values are in UTF-8.
This commit is contained in:
Tor Lillqvist
2004-11-24 18:07:26 +00:00
committed by Tor Lillqvist
parent 40fb4cff10
commit 567bf3a0b7
6 changed files with 173 additions and 43 deletions

View File

@@ -1,3 +1,12 @@
2004-11-24 Tor Lillqvist <tml@iki.fi>
* glib/gwin32.c (g_win32_error_message,
get_package_directory_from_module,
g_win32_get_package_installation_directory,
g_win32_get_package_installation_subdirectory): Use wide character
API when available. Document that string parameters and return
values are in UTF-8.
2004-11-24 Matthias Clasen <mclasen@redhat.com> 2004-11-24 Matthias Clasen <mclasen@redhat.com>
* glib/gutf8.c: Replace g_utf8_validate() with an * glib/gutf8.c: Replace g_utf8_validate() with an

View File

@@ -1,3 +1,12 @@
2004-11-24 Tor Lillqvist <tml@iki.fi>
* glib/gwin32.c (g_win32_error_message,
get_package_directory_from_module,
g_win32_get_package_installation_directory,
g_win32_get_package_installation_subdirectory): Use wide character
API when available. Document that string parameters and return
values are in UTF-8.
2004-11-24 Matthias Clasen <mclasen@redhat.com> 2004-11-24 Matthias Clasen <mclasen@redhat.com>
* glib/gutf8.c: Replace g_utf8_validate() with an * glib/gutf8.c: Replace g_utf8_validate() with an

View File

@@ -1,3 +1,12 @@
2004-11-24 Tor Lillqvist <tml@iki.fi>
* glib/gwin32.c (g_win32_error_message,
get_package_directory_from_module,
g_win32_get_package_installation_directory,
g_win32_get_package_installation_subdirectory): Use wide character
API when available. Document that string parameters and return
values are in UTF-8.
2004-11-24 Matthias Clasen <mclasen@redhat.com> 2004-11-24 Matthias Clasen <mclasen@redhat.com>
* glib/gutf8.c: Replace g_utf8_validate() with an * glib/gutf8.c: Replace g_utf8_validate() with an

View File

@@ -1,3 +1,12 @@
2004-11-24 Tor Lillqvist <tml@iki.fi>
* glib/gwin32.c (g_win32_error_message,
get_package_directory_from_module,
g_win32_get_package_installation_directory,
g_win32_get_package_installation_subdirectory): Use wide character
API when available. Document that string parameters and return
values are in UTF-8.
2004-11-24 Matthias Clasen <mclasen@redhat.com> 2004-11-24 Matthias Clasen <mclasen@redhat.com>
* glib/gutf8.c: Replace g_utf8_validate() with an * glib/gutf8.c: Replace g_utf8_validate() with an

View File

@@ -1,3 +1,12 @@
2004-11-24 Tor Lillqvist <tml@iki.fi>
* glib/gwin32.c (g_win32_error_message,
get_package_directory_from_module,
g_win32_get_package_installation_directory,
g_win32_get_package_installation_subdirectory): Use wide character
API when available. Document that string parameters and return
values are in UTF-8.
2004-11-24 Matthias Clasen <mclasen@redhat.com> 2004-11-24 Matthias Clasen <mclasen@redhat.com>
* glib/gutf8.c: Replace g_utf8_validate() with an * glib/gutf8.c: Replace g_utf8_validate() with an

View File

@@ -1053,28 +1053,56 @@ g_win32_getlocale (void)
gchar * gchar *
g_win32_error_message (gint error) g_win32_error_message (gint error)
{ {
gchar *msg = NULL;
gchar *retval; gchar *retval;
int nbytes;
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER if (G_WIN32_HAVE_WIDECHAR_API ())
|FORMAT_MESSAGE_IGNORE_INSERTS
|FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error, 0,
(LPTSTR) &msg, 0, NULL);
if (msg != NULL)
{ {
nbytes = strlen (msg); wchar_t *msg = NULL;
int nchars;
if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r') FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
msg[nbytes-2] = '\0'; |FORMAT_MESSAGE_IGNORE_INSERTS
|FORMAT_MESSAGE_FROM_SYSTEM,
retval = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL); NULL, error, 0,
(LPTSTR) &msg, 0, NULL);
if (msg != NULL)
{
nchars = wcslen (msg);
LocalFree (msg); if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r')
msg[nchars-2] = '\0';
retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
LocalFree (msg);
}
else
retval = g_strdup ("");
} }
else else
retval = g_strdup (""); {
gchar *msg = NULL;
int nbytes;
FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_IGNORE_INSERTS
|FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error, 0,
(LPTSTR) &msg, 0, NULL);
if (msg != NULL)
{
nbytes = strlen (msg);
if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
msg[nbytes-2] = '\0';
retval = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
LocalFree (msg);
}
else
retval = g_strdup ("");
}
return retval; return retval;
} }
@@ -1104,17 +1132,41 @@ get_package_directory_from_module (gchar *module_name)
if (module_name) if (module_name)
{ {
hmodule = GetModuleHandle (module_name); if (G_WIN32_HAVE_WIDECHAR_API ())
{
wchar_t *wc_module_name = g_utf8_to_utf16 (module_name, -1, NULL, NULL, NULL);
hmodule = GetModuleHandleW (wc_module_name);
g_free (wc_module_name);
}
else
{
char *cp_module_name = g_locale_from_utf8 (module_name, -1, NULL, NULL, NULL);
hmodule = GetModuleHandleA (cp_module_name);
g_free (cp_module_name);
}
if (!hmodule) if (!hmodule)
return NULL; return NULL;
} }
fn = g_malloc (MAX_PATH); if (G_WIN32_HAVE_WIDECHAR_API ())
if (!GetModuleFileName (hmodule, fn, MAX_PATH))
{ {
G_UNLOCK (module_dirs); wchar_t wc_fn[MAX_PATH];
g_free (fn); if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
return NULL; {
G_UNLOCK (module_dirs);
return NULL;
}
fn = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
}
else
{
gchar cp_fn[MAX_PATH];
if (!GetModuleFileNameA (hmodule, cp_fn, MAX_PATH))
{
G_UNLOCK (module_dirs);
return NULL;
}
fn = g_locale_to_utf8 (cp_fn, -1, NULL, NULL, NULL);
} }
if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL) if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
@@ -1145,8 +1197,8 @@ get_package_directory_from_module (gchar *module_name)
/** /**
* g_win32_get_package_installation_directory: * g_win32_get_package_installation_directory:
* @package: An identifier for a software package, or %NULL * @package: An identifier for a software package, or %NULL, in UTF-8
* @dll_name: The name of a DLL that a package provides, or %NULL. * @dll_name: The name of a DLL that a package provides, or %NULL, in the GLib file name encoding, which is UTF-8 on Windows.
* *
* Try to determine the installation directory for a software package. * Try to determine the installation directory for a software package.
* Typically used by GNU software packages. * Typically used by GNU software packages.
@@ -1173,8 +1225,10 @@ get_package_directory_from_module (gchar *module_name)
* the main executable of the process was loaded is uses instead in * the main executable of the process was loaded is uses instead in
* the same way as above. * the same way as above.
* *
* Returns: a string containing the installation directory for @package. * Returns: a string containing the installation directory for
* The return value should be freed with g_free() when not needed any longer. * @package. The string is in the GLib file name encoding, i.e. UTF-8
* on Windows. The return value should be freed with g_free() when not
* needed any longer.
**/ **/
gchar * gchar *
@@ -1207,21 +1261,51 @@ g_win32_get_package_installation_directory (gchar *package,
key = g_strconcat ("Software\\", package, NULL); key = g_strconcat ("Software\\", package, NULL);
nbytes = 0; nbytes = 0;
if ((RegOpenKeyEx (HKEY_CURRENT_USER, key, 0, if (G_WIN32_HAVE_WIDECHAR_API ())
KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
&& RegQueryValueEx (reg_key, "InstallationDirectory", 0,
&type, NULL, &nbytes) == ERROR_SUCCESS)
||
((RegOpenKeyEx (HKEY_LOCAL_MACHINE, key, 0,
KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
&& RegQueryValueEx (reg_key, "InstallationDirectory", 0,
&type, NULL, &nbytes) == ERROR_SUCCESS)
&& type == REG_SZ))
{ {
result = g_malloc (nbytes + 1); wchar_t *wc_key = g_utf8_to_utf16 (key, -1, NULL, NULL, NULL);
RegQueryValueEx (reg_key, "InstallationDirectory", 0, if (((RegOpenKeyExW (HKEY_CURRENT_USER, wc_key, 0,
&type, result, &nbytes); KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
result[nbytes] = '\0'; && RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
&type, NULL, &nbytes) == ERROR_SUCCESS)
||
(RegOpenKeyExW (HKEY_LOCAL_MACHINE, wc_key, 0,
KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
&& RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
&type, NULL, &nbytes) == ERROR_SUCCESS))
&& type == REG_SZ)
{
wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
&type, (LPBYTE) wc_temp, &nbytes);
wc_temp[nbytes/2] = '\0';
result = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
g_free (wc_temp);
}
g_free (wc_key);
}
else
{
char *cp_key = g_locale_from_utf8 (key, -1, NULL, NULL, NULL);
if (((RegOpenKeyExA (HKEY_CURRENT_USER, cp_key, 0,
KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
&& RegQueryValueExA (reg_key, "InstallationDirectory", 0,
&type, NULL, &nbytes) == ERROR_SUCCESS)
||
(RegOpenKeyExA (HKEY_LOCAL_MACHINE, cp_key, 0,
KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
&& RegQueryValueExA (reg_key, "InstallationDirectory", 0,
&type, NULL, &nbytes) == ERROR_SUCCESS))
&& type == REG_SZ)
{
char *cp_temp = g_malloc (nbytes + 1);
RegQueryValueExA (reg_key, "InstallationDirectory", 0,
&type, cp_temp, &nbytes);
cp_temp[nbytes] = '\0';
result = g_locale_to_utf8 (cp_temp, -1, NULL, NULL, NULL);
g_free (cp_temp);
}
g_free (cp_key);
} }
if (reg_key != NULL) if (reg_key != NULL)
@@ -1233,7 +1317,7 @@ g_win32_get_package_installation_directory (gchar *package,
{ {
g_hash_table_insert (package_dirs, package, result); g_hash_table_insert (package_dirs, package, result);
G_UNLOCK (package_dirs); G_UNLOCK (package_dirs);
return g_strdup (result); return result;
} }
G_UNLOCK (package_dirs); G_UNLOCK (package_dirs);
} }
@@ -1258,9 +1342,10 @@ g_win32_get_package_installation_directory (gchar *package,
* g_win32_get_package_installation_directory() with the @package and * g_win32_get_package_installation_directory() with the @package and
* @dll_name parameters. * @dll_name parameters.
* *
* Returns: a string containing the complete path to @subdir inside the * Returns: a string containing the complete path to @subdir inside
* installation directory of @package. The return value should be freed with * the installation directory of @package. The string is in the GLib
* g_free() when no longer needed. * file name encoding, i.e. UTF-8 on Windows. The return value should
* be freed with g_free() when no longer needed.
**/ **/
gchar * gchar *