[glib: 2/3] gstdio: simplify _g_win32_get_mode_alias



commit 0fda1d46cba40e0e7aaa2f6ec150911286306707
Author: Jeremy Tan <jtanx outlook com>
Date:   Mon Jun 17 19:22:39 2019 +1000

    gstdio: simplify _g_win32_get_mode_alias
    
    Do an in-place update on the wide-character mode string

 glib/gstdio.c | 54 +++++++++++++++++++++++++-----------------------------
 1 file changed, 25 insertions(+), 29 deletions(-)
---
diff --git a/glib/gstdio.c b/glib/gstdio.c
index 16cf1805f..653c8a3a1 100644
--- a/glib/gstdio.c
+++ b/glib/gstdio.c
@@ -139,6 +139,26 @@ w32_error_to_errno (DWORD error_code)
 
 #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
  * 
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.
@@ -778,26 +798,6 @@ g_win32_fstat (int                fd,
   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:
  * @filename: (type filename): a pathname in UTF-8
@@ -1552,7 +1552,6 @@ g_fopen (const gchar *filename,
 #ifdef G_OS_WIN32
   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
   wchar_t *wmode;
-  gchar   *mode2;
   FILE *retval;
   int save_errno;
 
@@ -1562,9 +1561,7 @@ g_fopen (const gchar *filename,
       return NULL;
     }
 
-  mode2 = _g_win32_get_mode_alias (mode);
-  wmode = g_utf8_to_utf16 (mode2, -1, NULL, NULL, NULL);
-  g_free (mode2);
+  wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
 
   if (wmode == NULL)
     {
@@ -1573,6 +1570,7 @@ g_fopen (const gchar *filename,
       return NULL;
     }
 
+  _g_win32_fix_mode (wmode);
   retval = _wfopen (wfilename, wmode);
   save_errno = errno;
 
@@ -1611,7 +1609,6 @@ g_freopen (const gchar *filename,
 #ifdef G_OS_WIN32
   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
   wchar_t *wmode;
-  gchar   *mode2;
   FILE *retval;
   int save_errno;
 
@@ -1621,9 +1618,7 @@ g_freopen (const gchar *filename,
       return NULL;
     }
 
-  mode2 = _g_win32_get_mode_alias (mode);
-  wmode = g_utf8_to_utf16 (mode2, -1, NULL, NULL, NULL);
-  g_free (mode2);
+  wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
 
   if (wmode == NULL)
     {
@@ -1631,7 +1626,8 @@ g_freopen (const gchar *filename,
       errno = EINVAL;
       return NULL;
     }
-  
+
+  _g_win32_fix_mode (wmode);
   retval = _wfreopen (wfilename, wmode, stream);
   save_errno = errno;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]