gstdio: simplify _g_win32_get_mode_alias

Do an in-place update on the wide-character mode string
This commit is contained in:
Jeremy Tan 2019-06-17 19:22:39 +10:00
parent 04dcee1814
commit 0fda1d46cb

View File

@ -139,6 +139,26 @@ w32_error_to_errno (DWORD error_code)
#include "gstdio-private.c" #include "gstdio-private.c"
/* Windows implementation of fopen() does not accept modes such as
* "wb+". The 'b' needs to be appended to "w+", i.e. "w+b". Note
* that otherwise these 2 modes are supposed to be aliases, hence
* swappable at will. TODO: Is this still true?
*/
static void
_g_win32_fix_mode (wchar_t *mode)
{
wchar_t *ptr;
wchar_t temp;
ptr = wcschr (mode, L'+');
if (ptr != NULL && (ptr - mode) > 1)
{
temp = mode[1];
mode[1] = *ptr;
*ptr = temp;
}
}
/* From /* From
* https://support.microsoft.com/en-ca/help/167296/how-to-convert-a-unix-time-t-to-a-win32-filetime-or-systemtime * https://support.microsoft.com/en-ca/help/167296/how-to-convert-a-unix-time-t-to-a-win32-filetime-or-systemtime
* FT = UT * 10000000 + 116444736000000000. * FT = UT * 10000000 + 116444736000000000.
@ -778,26 +798,6 @@ g_win32_fstat (int fd,
return _g_win32_stat_fd (fd, buf); return _g_win32_stat_fd (fd, buf);
} }
static gchar *
_g_win32_get_mode_alias (const gchar *mode)
{
gchar *alias;
alias = g_strdup (mode);
if (strlen (mode) > 2 && mode[2] == '+')
{
/* Windows implementation of fopen() does not accept modes such as
* "wb+". The 'b' needs to be appended to "w+", i.e. "w+b". Note
* that otherwise these 2 modes are supposed to be aliases, hence
* swappable at will.
*/
alias[1] = '+';
alias[2] = mode[1];
}
return alias;
}
/** /**
* g_win32_readlink_utf8: * g_win32_readlink_utf8:
* @filename: (type filename): a pathname in UTF-8 * @filename: (type filename): a pathname in UTF-8
@ -1552,7 +1552,6 @@ g_fopen (const gchar *filename,
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
wchar_t *wmode; wchar_t *wmode;
gchar *mode2;
FILE *retval; FILE *retval;
int save_errno; int save_errno;
@ -1562,9 +1561,7 @@ g_fopen (const gchar *filename,
return NULL; return NULL;
} }
mode2 = _g_win32_get_mode_alias (mode); wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
wmode = g_utf8_to_utf16 (mode2, -1, NULL, NULL, NULL);
g_free (mode2);
if (wmode == NULL) if (wmode == NULL)
{ {
@ -1573,6 +1570,7 @@ g_fopen (const gchar *filename,
return NULL; return NULL;
} }
_g_win32_fix_mode (wmode);
retval = _wfopen (wfilename, wmode); retval = _wfopen (wfilename, wmode);
save_errno = errno; save_errno = errno;
@ -1611,7 +1609,6 @@ g_freopen (const gchar *filename,
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
wchar_t *wmode; wchar_t *wmode;
gchar *mode2;
FILE *retval; FILE *retval;
int save_errno; int save_errno;
@ -1621,9 +1618,7 @@ g_freopen (const gchar *filename,
return NULL; return NULL;
} }
mode2 = _g_win32_get_mode_alias (mode); wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
wmode = g_utf8_to_utf16 (mode2, -1, NULL, NULL, NULL);
g_free (mode2);
if (wmode == NULL) if (wmode == NULL)
{ {
@ -1631,7 +1626,8 @@ g_freopen (const gchar *filename,
errno = EINVAL; errno = EINVAL;
return NULL; return NULL;
} }
_g_win32_fix_mode (wmode);
retval = _wfreopen (wfilename, wmode, stream); retval = _wfreopen (wfilename, wmode, stream);
save_errno = errno; save_errno = errno;