static void AddFiltersToCommandArgs(char** commandArgs, int commandArgsLen, const char *filterList ) { char typebuf[NFD_MAX_STRLEN] = {0}; const char *p_filterList = filterList; char *p_typebuf = typebuf; char filterName[NFD_MAX_STRLEN] = {0}; int i; if ( !filterList || strlen(filterList) == 0 ) return; while ( 1 ) { if ( NFDi_IsFilterSegmentChar(*p_filterList) ) { char typebufWildcard[NFD_MAX_STRLEN]; /* add another type to the filter */ assert( strlen(typebuf) > 0 ); assert( strlen(typebuf) < NFD_MAX_STRLEN-1 ); snprintf( typebufWildcard, NFD_MAX_STRLEN, "*.%s", typebuf ); AddTypeToFilterName( typebuf, filterName, NFD_MAX_STRLEN ); p_typebuf = typebuf; memset( typebuf, 0, sizeof(char) * NFD_MAX_STRLEN ); } if ( *p_filterList == ';' || *p_filterList == '\0' ) { /* end of filter -- add it to the dialog */ for(i = 0; commandArgs[i] != NULL && i < commandArgsLen; i++); commandArgs[i] = strdup(filterName); filterName[0] = '\0'; if ( *p_filterList == '\0' ) break; } if ( !NFDi_IsFilterSegmentChar( *p_filterList ) ) { *p_typebuf = *p_filterList; p_typebuf++; } p_filterList++; } /* always append a wildcard option to the end*/ for(i = 0; commandArgs[i] != NULL && i < commandArgsLen; i++); commandArgs[i] = strdup("--file-filter=*.*"); }
static nfdresult_t AddFiltersToDialog( ::IFileDialog *fileOpenDialog, const char *filterList ) { const wchar_t EMPTY_WSTR[] = L""; const wchar_t WILDCARD[] = L"*.*"; if ( !filterList || strlen(filterList) == 0 ) return NFD_OKAY; // 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; } assert(filterCount); if ( !filterCount ) { NFDi_SetError("Error parsing filters."); return NFD_ERROR; } /* filterCount plus 1 because we hardcode the *.* wildcard after the while loop */ COMDLG_FILTERSPEC *specList = (COMDLG_FILTERSPEC*)NFDi_Malloc( sizeof(COMDLG_FILTERSPEC) * (filterCount + 1) ); if ( !specList ) { return NFD_ERROR; } for (size_t i = 0; i < filterCount+1; ++i ) { specList[i].pszName = NULL; specList[i].pszSpec = NULL; } size_t specIdx = 0; p_filterList = filterList; char typebuf[NFD_MAX_STRLEN] = {0}; /* one per comma or semicolon */ char *p_typebuf = typebuf; char filterName[NFD_MAX_STRLEN] = {0}; 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 ); } if ( *p_filterList == ';' || *p_filterList == '\0' ) { /* end of filter -- add it to specList */ // Empty filter name -- Windows describes them by extension. specList[specIdx].pszName = EMPTY_WSTR; CopyNFDCharToWChar( specbuf, (wchar_t**)&specList[specIdx].pszSpec ); memset( specbuf, 0, sizeof(char)*NFD_MAX_STRLEN ); ++specIdx; if ( specIdx == filterCount ) break; } if ( !NFDi_IsFilterSegmentChar( *p_filterList )) { *p_typebuf = *p_filterList; ++p_typebuf; } ++p_filterList; } /* Add wildcard */ specList[specIdx].pszSpec = WILDCARD; specList[specIdx].pszName = EMPTY_WSTR; fileOpenDialog->SetFileTypes( filterCount+1, specList ); /* free speclist */ for ( size_t i = 0; i < filterCount; ++i ) { NFDi_Free( (void*)specList[i].pszSpec ); } NFDi_Free( specList ); return NFD_OKAY; }
static void AddFiltersToDialog( GtkWidget *dialog, const char *filterList ) { GtkFileFilter *filter; char typebuf[NFD_MAX_STRLEN] = {0}; const char *p_filterList = filterList; char *p_typebuf = typebuf; char filterName[NFD_MAX_STRLEN] = {0}; if ( !filterList || strlen(filterList) == 0 ) return; filter = gtk_file_filter_new(); while ( 1 ) { if ( NFDi_IsFilterSegmentChar(*p_filterList) ) { char typebufWildcard[NFD_MAX_STRLEN]; /* add another type to the filter */ if (strlen(typebuf) <= 0 || strlen(typebuf) > NFD_MAX_STRLEN-1) { p_filterList++; continue; } snprintf( typebufWildcard, NFD_MAX_STRLEN, "*.%s", typebuf ); AddTypeToFilterName( typebuf, filterName, NFD_MAX_STRLEN ); gtk_file_filter_add_pattern( filter, typebufWildcard ); p_typebuf = typebuf; memset( typebuf, 0, sizeof(char) * NFD_MAX_STRLEN ); } if ( *p_filterList == ';' || *p_filterList == '\0' ) { /* end of filter -- add it to the dialog */ gtk_file_filter_set_name( filter, filterName ); gtk_file_chooser_add_filter( GTK_FILE_CHOOSER(dialog), filter ); filterName[0] = '\0'; if ( *p_filterList == '\0' ) break; filter = gtk_file_filter_new(); } if ( !NFDi_IsFilterSegmentChar( *p_filterList ) ) { *p_typebuf = *p_filterList; p_typebuf++; } p_filterList++; } /* always append a wildcard option to the end*/ filter = gtk_file_filter_new(); gtk_file_filter_set_name( filter, "*.*" ); gtk_file_filter_add_pattern( filter, "*" ); gtk_file_chooser_add_filter( GTK_FILE_CHOOSER(dialog), filter ); }