Skip to content

Commit 590ae53

Browse files
committed
Avoid conflicts of embedded vfs with Qt.
1 parent d267253 commit 590ae53

File tree

1 file changed

+70
-2
lines changed

1 file changed

+70
-2
lines changed

Include/np_embed.h

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#ifndef __ASSEMBLER__
1010
#ifdef __cplusplus
11+
#if __has_include (<QtCore>)
12+
# include <QtCore>
13+
#endif
14+
1115
extern "C" {
1216
#endif
1317

@@ -26,6 +30,7 @@ extern "C" {
2630

2731
#ifdef _WIN32
2832
#include <windows.h>
33+
#include <io.h>
2934
#define PATH_MAX MAX_PATH
3035
typedef SSIZE_T ssize_t;
3136
#else
@@ -129,13 +134,76 @@ NP_DECL(void) np_flockfile(void *e);
129134
NP_DECL(void) np_funlockfile(void *e);
130135
NP_DECL(int) np_ftrylockfile(void *e);
131136

132-
#if !defined(NUITKAPYTHON_EMBED_BUILD) && !defined(__cplusplus)
137+
#ifndef NUITKAPYTHON_EMBED_BUILD
138+
139+
/* Using a bunch of macros from
140+
* https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms
141+
*/
142+
143+
#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
144+
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
145+
146+
#define IIF(c) PRIMITIVE_CAT(IIF_, c)
147+
#define IIF_0(t, ...) __VA_ARGS__
148+
#define IIF_1(t, ...) t
149+
150+
#define COMPL(b) PRIMITIVE_CAT(COMPL_, b)
151+
#define COMPL_0 1
152+
#define COMPL_1 0
153+
154+
#define DEC(x) PRIMITIVE_CAT(DEC_, x)
155+
#define DEC_0 0
156+
#define DEC_1 0
157+
#define DEC_2 1
158+
#define DEC_3 2
159+
#define DEC_4 3
160+
#define DEC_5 4
161+
#define DEC_6 5
162+
#define DEC_7 6
163+
#define DEC_8 7
164+
#define DEC_9 8
165+
#define DEC_10 9
166+
#define DEC_11 10
167+
#define DEC_12 11
168+
#define DEC_13 12
169+
#define DEC_14 13
170+
#define DEC_15 14
171+
#define DEC_16 15
172+
#define DEC_17 16
173+
#define DEC_18 17
174+
#define DEC_19 18
175+
#define DEC_20 19
176+
177+
#define CHECK_N(x, n, ...) n
178+
#define CHECK(...) CHECK_N(__VA_ARGS__, 0,)
179+
#define PROBE(x) x, 1,
180+
181+
#define IS_PAREN(x) CHECK(IS_PAREN_PROBE x)
182+
#define IS_PAREN_PROBE(...) PROBE(~)
183+
184+
#define NOT(x) CHECK(PRIMITIVE_CAT(NOT_, x))
185+
#define NOT_0 PROBE(~)
186+
187+
#define BOOL(x) COMPL(NOT(x))
188+
#define IF(c) IIF(BOOL(c))
189+
190+
#define EAT(...)
191+
#define EXPAND(...) __VA_ARGS__
192+
#define WHEN(c) IF(c)(EXPAND, EAT)
193+
194+
#define EXPAND(...) __VA_ARGS__
195+
196+
#define NUM_ARGS1(_20,_19,_18,_17,_16,_15,_14,_13,_12,_11,_10,_9,_8,_7,_6,_5,_4,_3,_2,_1, n, ...) n
197+
#define NUM_ARGS0(...) NUM_ARGS1(__VA_ARGS__,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
198+
#define NUM_ARGS(...) IF(DEC(NUM_ARGS0(__VA_ARGS__)))(NUM_ARGS0(__VA_ARGS__),IF(IS_PAREN(__VA_ARGS__ ()))(0,1))
199+
133200
// Preprocessor Translation
134201
#define FILE EFILE
135202
/* File Opening and Closing */
136203
#define fopen np_fopen
137204
#define _fopen np_fopen
138-
#define open np_open
205+
// Basically this very complicated macro overrides only calls to open() functions that have 2 or more arguments to avoid compatibility issues.
206+
#define open(...) WHEN(DEC(NUM_ARGS( __VA_ARGS__ )))(np_open(__VA_ARGS__))WHEN(NOT(DEC(NUM_ARGS( __VA_ARGS__ ))))(open(__VA_ARGS__))
139207
#define _open np_open
140208
#define fdopen np_fdopen
141209
#define _fdopen np_fdopen

0 commit comments

Comments
 (0)