Ardour  9.0-pre0-350-gf17a656217
gstdio_compat.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 /* glib mingw64/win32 compatibility
20  *
21  * see http://pidgin.im/pipermail/devel/2014-April/023475.html
22  * and https://pidgin.im/pipermail/commits/2014-April/025031.html
23  * https://tracker.ardour.org/view.php?id=6575
24  */
25 
26 #pragma once
27 
28 #include <glib/gstdio.h>
29 
30 /* glib's definition of g_stat+GStatBuf is broken for mingw64-w32
31  * (and possibly other 32-bit windows)
32  */
33 #if defined(_WIN32) && !defined(_MSC_VER) && !defined(_WIN64)
34 typedef struct _stat GStatBufW32;
35 static inline int
36 pbd_g_stat(const gchar *filename, GStatBufW32 *buf)
37 {
38  return g_stat(filename, (GStatBuf*)buf);
39 }
40 # define GStatBuf GStatBufW32
41 # define g_stat pbd_g_stat
42 # define g_lstat pbd_g_stat
43 #endif
44 
45 /* 64bit mingw -- use _mingw_stat64.h
46  *
47  * glib-2.42.0 wrongly uses _wstat() with 'struct stat' (only MSVC is special cased),
48  * while the windows API is
49  * int _wstat(const wchar_t*, struct _stat*)
50  * note that struct _stat != struct stat;
51  *
52  * This has been fixed with sometime between 2.42.0 and 2.64.1
53  */
54 #if defined(_WIN32) && !defined(_MSC_VER) && defined(_WIN64) && !GLIB_CHECK_VERSION (2,64,1)
55 #include <windows.h>
56 #include <errno.h>
57 #include <wchar.h>
58 
59 typedef struct _stat GStatBufW64;
60 static inline int
61 pbd_g_stat(const gchar* filename, GStatBufW64* buf)
62 {
63  gunichar2* wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
64  if (wfilename == NULL) {
65  errno = EINVAL;
66  return -1;
67  }
68 
69  int len = wcslen ((wchar_t*)wfilename);
70  while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1])) {
71  --len;
72  }
73  if (len > 0 && (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename)) {
74  wfilename[len] = '\0';
75  }
76 
77  int retval = _wstat ((wchar_t*)wfilename, buf);
78  int save_errno = errno;
79  g_free (wfilename);
80  errno = save_errno;
81  return retval;
82 }
83 # define GStatBuf GStatBufW64
84 # define g_stat pbd_g_stat
85 # define g_lstat pbd_g_stat
86 #endif
87