Skip to content

Commit 53de8bd

Browse files
authored
Add versioned API (#128)
1 parent 17b6e8c commit 53de8bd

13 files changed

+846
-67
lines changed

src/include/nfd.h

Lines changed: 176 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
#ifdef __cplusplus
3131
extern "C" {
32+
#define NFD_INLINE inline
33+
#else
34+
#define NFD_INLINE static inline
3235
#endif // __cplusplus
3336

3437
#include <stddef.h>
@@ -93,6 +96,59 @@ typedef struct {
9396
typedef nfdu8filteritem_t nfdnfilteritem_t;
9497
#endif // _WIN32
9598

99+
typedef size_t nfdversion_t;
100+
101+
typedef struct {
102+
const nfdu8filteritem_t* filterList;
103+
nfdfiltersize_t filterCount;
104+
const nfdu8char_t* defaultPath;
105+
} nfdopendialogu8args_t;
106+
107+
#ifdef _WIN32
108+
typedef struct {
109+
const nfdnfilteritem_t* filterList;
110+
nfdfiltersize_t filterCount;
111+
const nfdnchar_t* defaultPath;
112+
} nfdopendialognargs_t;
113+
#else
114+
typedef nfdopendialogu8args_t nfdopendialognargs_t;
115+
#endif // _WIN32
116+
117+
typedef struct {
118+
const nfdu8filteritem_t* filterList;
119+
nfdfiltersize_t filterCount;
120+
const nfdu8char_t* defaultPath;
121+
const nfdu8char_t* defaultName;
122+
} nfdsavedialogu8args_t;
123+
124+
#ifdef _WIN32
125+
typedef struct {
126+
const nfdnfilteritem_t* filterList;
127+
nfdfiltersize_t filterCount;
128+
const nfdnchar_t* defaultPath;
129+
const nfdnchar_t* defaultName;
130+
} nfdsavedialognargs_t;
131+
#else
132+
typedef nfdsavedialogu8args_t nfdsavedialognargs_t;
133+
#endif // _WIN32
134+
135+
typedef struct {
136+
const nfdu8char_t* defaultPath;
137+
} nfdpickfolderu8args_t;
138+
139+
#ifdef _WIN32
140+
typedef struct {
141+
const nfdnchar_t* defaultPath;
142+
} nfdpickfoldernargs_t;
143+
#else
144+
typedef nfdpickfolderu8args_t nfdpickfoldernargs_t;
145+
#endif // _WIN32
146+
147+
// This is a unique identifier tagged to all the NFD_*With() function calls, for backward
148+
// compatibility purposes. There is usually no need to use this directly, unless you want to use
149+
// NFD differently depending on the version you're building with.
150+
#define NFD_INTERFACE_VERSION 1
151+
96152
/** Free a file path that was returned by the dialogs.
97153
*
98154
* Note: use NFD_PathSet_FreePathN() to free path from pathset instead of this function. */
@@ -134,6 +190,35 @@ NFD_API nfdresult_t NFD_OpenDialogU8(nfdu8char_t** outPath,
134190
nfdfiltersize_t filterCount,
135191
const nfdu8char_t* defaultPath);
136192

193+
/** This function is a library implementation detail. Please use NFD_OpenDialogN_With() instead. */
194+
NFD_API nfdresult_t NFD_OpenDialogN_With_Impl(nfdversion_t version,
195+
nfdnchar_t** outPath,
196+
const nfdopendialognargs_t* args);
197+
198+
/** Single file open dialog, with additional parameters.
199+
*
200+
* It is the caller's responsibility to free `outPath` via NFD_FreePathN() if this function
201+
* returns NFD_OKAY. See documentation of nfdopendialognargs_t for details. */
202+
NFD_INLINE nfdresult_t NFD_OpenDialogN_With(nfdnchar_t** outPath,
203+
const nfdopendialognargs_t* args) {
204+
return NFD_OpenDialogN_With_Impl(NFD_INTERFACE_VERSION, outPath, args);
205+
}
206+
207+
/** This function is a library implementation detail. Please use NFD_OpenDialogU8_With() instead.
208+
*/
209+
NFD_API nfdresult_t NFD_OpenDialogU8_With_Impl(nfdversion_t version,
210+
nfdu8char_t** outPath,
211+
const nfdopendialogu8args_t* args);
212+
213+
/** Single file open dialog, with additional parameters.
214+
*
215+
* It is the caller's responsibility to free `outPath` via NFD_FreePathU8() if this function
216+
* returns NFD_OKAY. See documentation of nfdopendialogu8args_t for details. */
217+
NFD_INLINE nfdresult_t NFD_OpenDialogU8_With(nfdu8char_t** outPath,
218+
const nfdopendialogu8args_t* args) {
219+
return NFD_OpenDialogU8_With_Impl(NFD_INTERFACE_VERSION, outPath, args);
220+
}
221+
137222
/** Multiple file open dialog
138223
*
139224
* It is the caller's responsibility to free `outPaths` via NFD_PathSet_FreeN() if this function
@@ -158,6 +243,36 @@ NFD_API nfdresult_t NFD_OpenDialogMultipleU8(const nfdpathset_t** outPaths,
158243
nfdfiltersize_t filterCount,
159244
const nfdu8char_t* defaultPath);
160245

246+
/** This function is a library implementation detail. Please use NFD_OpenDialogMultipleN_With()
247+
* instead. */
248+
NFD_API nfdresult_t NFD_OpenDialogMultipleN_With_Impl(nfdversion_t version,
249+
const nfdpathset_t** outPaths,
250+
const nfdopendialognargs_t* args);
251+
252+
/** Multiple file open dialog, with additional parameters.
253+
*
254+
* It is the caller's responsibility to free `outPaths` via NFD_PathSet_FreeN() if this function
255+
* returns NFD_OKAY. See documentation of nfdopendialognargs_t for details. */
256+
NFD_INLINE nfdresult_t NFD_OpenDialogMultipleN_With(const nfdpathset_t** outPaths,
257+
const nfdopendialognargs_t* args) {
258+
return NFD_OpenDialogMultipleN_With_Impl(NFD_INTERFACE_VERSION, outPaths, args);
259+
}
260+
261+
/** This function is a library implementation detail. Please use NFD_OpenDialogU8_With() instead.
262+
*/
263+
NFD_API nfdresult_t NFD_OpenDialogMultipleU8_With_Impl(nfdversion_t version,
264+
const nfdpathset_t** outPaths,
265+
const nfdopendialogu8args_t* args);
266+
267+
/** Multiple file open dialog, with additional parameters.
268+
*
269+
* It is the caller's responsibility to free `outPaths` via NFD_PathSet_FreeU8() if this function
270+
* returns NFD_OKAY. See documentation of nfdopendialogu8args_t for details. */
271+
NFD_INLINE nfdresult_t NFD_OpenDialogMultipleU8_With(const nfdpathset_t** outPaths,
272+
const nfdopendialogu8args_t* args) {
273+
return NFD_OpenDialogMultipleU8_With_Impl(NFD_INTERFACE_VERSION, outPaths, args);
274+
}
275+
161276
/** Save dialog
162277
*
163278
* It is the caller's responsibility to free `outPath` via NFD_FreePathN() if this function returns
@@ -184,22 +299,80 @@ NFD_API nfdresult_t NFD_SaveDialogU8(nfdu8char_t** outPath,
184299
const nfdu8char_t* defaultPath,
185300
const nfdu8char_t* defaultName);
186301

187-
/** Select folder dialog
302+
/** This function is a library implementation detail. Please use NFD_SaveDialogN_With() instead. */
303+
NFD_API nfdresult_t NFD_SaveDialogN_With_Impl(nfdversion_t version,
304+
nfdnchar_t** outPath,
305+
const nfdsavedialognargs_t* args);
306+
307+
/** Single file save dialog, with additional parameters.
308+
*
309+
* It is the caller's responsibility to free `outPath` via NFD_FreePathN() if this function
310+
* returns NFD_OKAY. See documentation of nfdsavedialognargs_t for details. */
311+
NFD_INLINE nfdresult_t NFD_SaveDialogN_With(nfdnchar_t** outPath,
312+
const nfdsavedialognargs_t* args) {
313+
return NFD_SaveDialogN_With_Impl(NFD_INTERFACE_VERSION, outPath, args);
314+
}
315+
316+
/** This function is a library implementation detail. Please use NFD_SaveDialogU8_With() instead.
317+
*/
318+
NFD_API nfdresult_t NFD_SaveDialogU8_With_Impl(nfdversion_t version,
319+
nfdu8char_t** outPath,
320+
const nfdsavedialogu8args_t* args);
321+
322+
/** Single file save dialog, with additional parameters.
323+
*
324+
* It is the caller's responsibility to free `outPath` via NFD_FreePathU8() if this function
325+
* returns NFD_OKAY. See documentation of nfdsavedialogu8args_t for details. */
326+
NFD_INLINE nfdresult_t NFD_SaveDialogU8_With(nfdu8char_t** outPath,
327+
const nfdsavedialogu8args_t* args) {
328+
return NFD_SaveDialogU8_With_Impl(NFD_INTERFACE_VERSION, outPath, args);
329+
}
330+
331+
/** Select single folder dialog
188332
*
189333
* It is the caller's responsibility to free `outPath` via NFD_FreePathN() if this function returns
190334
* NFD_OKAY.
191335
* @param[out] outPath
192336
* @param defaultPath If null, the operating system will decide. */
193337
NFD_API nfdresult_t NFD_PickFolderN(nfdnchar_t** outPath, const nfdnchar_t* defaultPath);
194338

195-
/** Select folder dialog
339+
/** Select single folder dialog
196340
*
197341
* It is the caller's responsibility to free `outPath` via NFD_FreePathU8() if this function
198342
* returns NFD_OKAY.
199343
* @param[out] outPath
200344
* @param defaultPath If null, the operating system will decide. */
201345
NFD_API nfdresult_t NFD_PickFolderU8(nfdu8char_t** outPath, const nfdu8char_t* defaultPath);
202346

347+
/** This function is a library implementation detail. Please use NFD_PickFolderN_With() instead. */
348+
NFD_API nfdresult_t NFD_PickFolderN_With_Impl(nfdversion_t version,
349+
nfdnchar_t** outPath,
350+
const nfdpickfoldernargs_t* args);
351+
352+
/** Select single folder dialog, with additional parameters.
353+
*
354+
* It is the caller's responsibility to free `outPath` via NFD_FreePathN() if this function
355+
* returns NFD_OKAY. See documentation of nfdpickfoldernargs_t for details. */
356+
NFD_INLINE nfdresult_t NFD_PickFolderN_With(nfdnchar_t** outPath,
357+
const nfdpickfoldernargs_t* args) {
358+
return NFD_PickFolderN_With_Impl(NFD_INTERFACE_VERSION, outPath, args);
359+
}
360+
361+
/** This function is a library implementation detail. Please use NFD_PickFolderU8_With() instead.
362+
*/
363+
NFD_API nfdresult_t NFD_PickFolderU8_With_Impl(nfdversion_t version,
364+
nfdu8char_t** outPath,
365+
const nfdpickfolderu8args_t* args);
366+
367+
/** Select single folder dialog, with additional parameters.
368+
*
369+
* It is the caller's responsibility to free `outPath` via NFD_FreePathU8() if this function
370+
* returns NFD_OKAY. See documentation of nfdpickfolderu8args_t for details. */
371+
NFD_INLINE nfdresult_t NFD_PickFolderU8_With(nfdu8char_t** outPath,
372+
const nfdpickfolderu8args_t* args) {
373+
return NFD_PickFolderU8_With_Impl(NFD_INTERFACE_VERSION, outPath, args);
374+
}
375+
203376
/** Get the last error
204377
*
205378
* This is set when a function returns NFD_ERROR.
@@ -308,6 +481,7 @@ typedef nfdu8filteritem_t nfdfilteritem_t;
308481
#define NFD_PathSet_EnumNext NFD_PathSet_EnumNextU8
309482
#endif // NFD_NATIVE
310483

484+
#undef NFD_INLINE
311485
#ifdef __cplusplus
312486
}
313487
#endif // __cplusplus

src/include/nfd.hpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,31 @@ inline nfdresult_t OpenDialog(nfdnchar_t*& outPath,
3838
const nfdnfilteritem_t* filterList = nullptr,
3939
nfdfiltersize_t filterCount = 0,
4040
const nfdnchar_t* defaultPath = nullptr) noexcept {
41-
return ::NFD_OpenDialogN(&outPath, filterList, filterCount, defaultPath);
41+
const nfdopendialognargs_t args{filterList, filterCount, defaultPath};
42+
return ::NFD_OpenDialogN_With(&outPath, &args);
4243
}
4344

4445
inline nfdresult_t OpenDialogMultiple(const nfdpathset_t*& outPaths,
4546
const nfdnfilteritem_t* filterList = nullptr,
4647
nfdfiltersize_t filterCount = 0,
4748
const nfdnchar_t* defaultPath = nullptr) noexcept {
48-
return ::NFD_OpenDialogMultipleN(&outPaths, filterList, filterCount, defaultPath);
49+
const nfdopendialognargs_t args{filterList, filterCount, defaultPath};
50+
return ::NFD_OpenDialogMultipleN_With(&outPaths, &args);
4951
}
5052

5153
inline nfdresult_t SaveDialog(nfdnchar_t*& outPath,
5254
const nfdnfilteritem_t* filterList = nullptr,
5355
nfdfiltersize_t filterCount = 0,
5456
const nfdnchar_t* defaultPath = nullptr,
5557
const nfdnchar_t* defaultName = nullptr) noexcept {
56-
return ::NFD_SaveDialogN(&outPath, filterList, filterCount, defaultPath, defaultName);
58+
const nfdsavedialognargs_t args{filterList, filterCount, defaultPath, defaultName};
59+
return ::NFD_SaveDialogN_With(&outPath, &args);
5760
}
5861

5962
inline nfdresult_t PickFolder(nfdnchar_t*& outPath,
6063
const nfdnchar_t* defaultPath = nullptr) noexcept {
61-
return ::NFD_PickFolderN(&outPath, defaultPath);
64+
const nfdpickfoldernargs_t args{defaultPath};
65+
return ::NFD_PickFolderN_With(&outPath, &args);
6266
}
6367

6468
inline const char* GetError() noexcept {
@@ -99,29 +103,33 @@ inline void FreePath(nfdu8char_t* outPath) noexcept {
99103

100104
inline nfdresult_t OpenDialog(nfdu8char_t*& outPath,
101105
const nfdu8filteritem_t* filterList = nullptr,
102-
nfdfiltersize_t count = 0,
106+
nfdfiltersize_t filterCount = 0,
103107
const nfdu8char_t* defaultPath = nullptr) noexcept {
104-
return ::NFD_OpenDialogU8(&outPath, filterList, count, defaultPath);
108+
const nfdopendialogu8args_t args{filterList, filterCount, defaultPath};
109+
return ::NFD_OpenDialogU8_With(&outPath, &args);
105110
}
106111

107112
inline nfdresult_t OpenDialogMultiple(const nfdpathset_t*& outPaths,
108113
const nfdu8filteritem_t* filterList = nullptr,
109-
nfdfiltersize_t count = 0,
114+
nfdfiltersize_t filterCount = 0,
110115
const nfdu8char_t* defaultPath = nullptr) noexcept {
111-
return ::NFD_OpenDialogMultipleU8(&outPaths, filterList, count, defaultPath);
116+
const nfdopendialogu8args_t args{filterList, filterCount, defaultPath};
117+
return ::NFD_OpenDialogMultipleU8_With(&outPaths, &args);
112118
}
113119

114120
inline nfdresult_t SaveDialog(nfdu8char_t*& outPath,
115121
const nfdu8filteritem_t* filterList = nullptr,
116-
nfdfiltersize_t count = 0,
122+
nfdfiltersize_t filterCount = 0,
117123
const nfdu8char_t* defaultPath = nullptr,
118124
const nfdu8char_t* defaultName = nullptr) noexcept {
119-
return ::NFD_SaveDialogU8(&outPath, filterList, count, defaultPath, defaultName);
125+
const nfdsavedialogu8args_t args{filterList, filterCount, defaultPath, defaultName};
126+
return ::NFD_SaveDialogU8_With(&outPath, &args);
120127
}
121128

122129
inline nfdresult_t PickFolder(nfdu8char_t*& outPath,
123130
const nfdu8char_t* defaultPath = nullptr) noexcept {
124-
return ::NFD_PickFolderU8(&outPath, defaultPath);
131+
const nfdpickfolderu8args_t args{defaultPath};
132+
return ::NFD_PickFolderU8_With(&outPath, &args);
125133
}
126134

127135
namespace PathSet {

0 commit comments

Comments
 (0)