pass filter to NFD - DOES NOT BUILD

I am so lazy to adapt the macOS part
This commit is contained in:
tildearrow 2022-06-24 03:11:16 -05:00
parent 1b21d618b6
commit deffd99503
4 changed files with 31 additions and 80 deletions

View file

@ -12,6 +12,8 @@
#include <stddef.h>
#include <functional>
#include <string>
#include <vector>
/* denotes UTF-8 char */
typedef char nfdchar_t;
@ -35,19 +37,19 @@ typedef enum {
/* nfd_<targetplatform>.c */
/* single file open dialog */
nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList,
nfdresult_t NFD_OpenDialog( const std::vector<std::string>& filterList,
const nfdchar_t *defaultPath,
nfdchar_t **outPath,
nfdselcallback_t selCallback = NULL );
/* multiple file open dialog */
nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
nfdresult_t NFD_OpenDialogMultiple( const std::vector<std::string>& filterList,
const nfdchar_t *defaultPath,
nfdpathset_t *outPaths,
nfdselcallback_t selCallback = NULL );
/* save dialog */
nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
nfdresult_t NFD_SaveDialog( const std::vector<std::string>& filterList,
const nfdchar_t *defaultPath,
nfdchar_t **outPath,
nfdselcallback_t selCallback = NULL );

View file

@ -14,7 +14,7 @@
//
// might as well make Objective-Ruswift++...
static NSArray *BuildAllowedFileTypes( const char *filterList )
static NSArray *BuildAllowedFileTypes( const std::vector<std::string>& filterList )
{
// Commas and semicolons are the same thing on this platform
@ -56,7 +56,7 @@ static NSArray *BuildAllowedFileTypes( const char *filterList )
return returnArray;
}
static void AddFilterListToDialog( NSSavePanel *dialog, const char *filterList )
static void AddFilterListToDialog( NSSavePanel *dialog, const std::vector<std::string>& filterList )
{
if ( !filterList || strlen(filterList) == 0 )
return;
@ -130,7 +130,7 @@ static nfdresult_t AllocPathSet( NSArray *urls, nfdpathset_t *pathset )
/* public */
nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList,
nfdresult_t NFD_OpenDialog( const std::vector<std::string>& filterList,
const nfdchar_t *defaultPath,
nfdchar_t **outPath,
nfdselcallback_t selCallback )
@ -173,7 +173,7 @@ nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList,
}
nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
nfdresult_t NFD_OpenDialogMultiple( const std::vector<std::string>& filterList,
const nfdchar_t *defaultPath,
nfdpathset_t *outPaths,
nfdselcallback_t selCallback )
@ -218,7 +218,7 @@ nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
}
nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
nfdresult_t NFD_SaveDialog( const std::vector<std::string>& filterList,
const nfdchar_t *defaultPath,
nfdchar_t **outPath,
nfdselcallback_t selCallback )

View file

@ -197,44 +197,19 @@ static void CopyNFDCharToWChar( const nfdchar_t *inStr, wchar_t **outStr )
#endif
}
/* ext is in format "jpg", no wildcards or separators */
static int AppendExtensionToSpecBuf( const char *ext, char *specBuf, size_t specBufLen )
{
const char SEP[] = ";";
assert( specBufLen > strlen(ext)+3 );
if ( strlen(specBuf) > 0 )
{
strncat( specBuf, SEP, specBufLen - strlen(specBuf) - 1 );
specBufLen += strlen(SEP);
}
char extWildcard[NFD_MAX_STRLEN];
int bytesWritten = sprintf_s( extWildcard, NFD_MAX_STRLEN, "*.%s", ext );
assert( bytesWritten == (int)(strlen(ext)+2) );
_NFD_UNUSED(bytesWritten);
strncat( specBuf, extWildcard, specBufLen - strlen(specBuf) - 1 );
return NFD_OKAY;
}
static nfdresult_t AddFiltersToDialog( ::IFileDialog *fileOpenDialog, const char *filterList )
static nfdresult_t AddFiltersToDialog( ::IFileDialog *fileOpenDialog, const std::vector<std::string>& filterList )
{
const wchar_t WILDCARD[] = L"*.*";
if ( !filterList || strlen(filterList) == 0 )
if (filterList.empty())
return NFD_OKAY;
// list size has to be an even number (name/filter)
if (filterList.size()&1)
return NFD_ERROR;
// Count rows to alloc
UINT filterCount = 1; /* guaranteed to have one filter on a correct, non-empty parse */
const char *p_filterList;
for ( p_filterList = filterList; *p_filterList; ++p_filterList )
{
if ( *p_filterList == ';' )
++filterCount;
}
UINT filterCount = filterList.size()>>1; /* guaranteed to have one filter on a correct, non-empty parse */
assert(filterCount);
if ( !filterCount )
@ -256,43 +231,17 @@ static nfdresult_t AddFiltersToDialog( ::IFileDialog *fileOpenDialog, const char
}
size_t specIdx = 0;
p_filterList = filterList;
char typebuf[NFD_MAX_STRLEN] = {0}; /* one per comma or semicolon */
char *p_typebuf = typebuf;
char specbuf[NFD_MAX_STRLEN] = {0}; /* one per semicolon */
while ( 1 )
{
if ( NFDi_IsFilterSegmentChar(*p_filterList) )
{
/* append a type to the specbuf (pending filter) */
AppendExtensionToSpecBuf( typebuf, specbuf, NFD_MAX_STRLEN );
p_typebuf = typebuf;
memset( typebuf, 0, sizeof(char)*NFD_MAX_STRLEN );
for (size_t i=0; i<filterList.size(); i+=2) {
String name=filterList[i];
String spec=filterList[i+1];
for (char& i: spec) {
if (i==' ') i=',';
}
if ( *p_filterList == ';' || *p_filterList == '\0' )
{
/* end of filter -- add it to specList */
CopyNFDCharToWChar( specbuf, (wchar_t**)&specList[specIdx].pszName );
CopyNFDCharToWChar( specbuf, (wchar_t**)&specList[specIdx].pszSpec );
memset( specbuf, 0, sizeof(char)*NFD_MAX_STRLEN );
CopyNFDCharToWChar( name.c_str(), (wchar_t**)&specList[specIdx].pszName );
CopyNFDCharToWChar( spec.c_str(), (wchar_t**)&specList[specIdx].pszSpec );
++specIdx;
if ( specIdx == filterCount )
break;
}
if ( !NFDi_IsFilterSegmentChar( *p_filterList ))
{
*p_typebuf = *p_filterList;
++p_typebuf;
}
++p_filterList;
}
/* Add wildcard */
@ -450,7 +399,7 @@ static nfdresult_t SetDefaultPath( IFileDialog *dialog, const char *defaultPath
/* public */
nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList,
nfdresult_t NFD_OpenDialog( const std::vector<std::string>& filterList,
const nfdchar_t *defaultPath,
nfdchar_t **outPath,
nfdselcallback_t selCallback )
@ -558,7 +507,7 @@ end:
return nfdResult;
}
nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
nfdresult_t NFD_OpenDialogMultiple( const std::vector<std::string>& filterList,
const nfdchar_t *defaultPath,
nfdpathset_t *outPaths,
nfdselcallback_t selCallback )
@ -653,7 +602,7 @@ end:
return nfdResult;
}
nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
nfdresult_t NFD_SaveDialog( const std::vector<std::string>& filterList,
const nfdchar_t *defaultPath,
nfdchar_t **outPath,
nfdselcallback_t selCallback )

View file

@ -30,9 +30,9 @@ void _nfdThread(const NFDState state, std::atomic<bool>* ok, String* result) {
nfdresult_t ret=NFD_CANCEL;
if (state.isSave) {
ret=NFD_SaveDialog(NULL,state.path.c_str(),&out,state.clickCallback);
ret=NFD_SaveDialog(state.filter,state.path.c_str(),&out,state.clickCallback);
} else {
ret=NFD_OpenDialog(NULL,state.path.c_str(),&out,state.clickCallback);
ret=NFD_OpenDialog(state.filter,state.path.c_str(),&out,state.clickCallback);
}
switch (ret) {