Fix improper handling of NULL dialog filter on Cocoa

This commit is contained in:
Semphris 2024-05-02 12:22:23 -04:00 committed by Sam Lantinga
parent 9f842e9b5a
commit 4ac8968f65
1 changed files with 47 additions and 43 deletions

View File

@ -37,12 +37,14 @@ void show_file_dialog(cocoa_FileDialogType type, SDL_DialogFileCallback callback
SDL_SetError("tvOS and iOS don't support path-based file dialogs");
callback(userdata, NULL, -1);
#else
const char *msg = validate_filters(filters);
if (filters) {
const char *msg = validate_filters(filters);
if (msg) {
SDL_SetError("%s", msg);
callback(userdata, NULL, -1);
return;
if (msg) {
SDL_SetError("%s", msg);
callback(userdata, NULL, -1);
return;
}
}
if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) {
@ -73,49 +75,51 @@ void show_file_dialog(cocoa_FileDialogType type, SDL_DialogFileCallback callback
break;
};
int n = -1;
while (filters[++n].name && filters[n].pattern);
// On macOS 11.0 and up, this is an array of UTType. Prior to that, it's an array of NSString
NSMutableArray *types = [[NSMutableArray alloc] initWithCapacity:n ];
if (filters) {
int n = -1;
while (filters[++n].name && filters[n].pattern);
// On macOS 11.0 and up, this is an array of UTType. Prior to that, it's an array of NSString
NSMutableArray *types = [[NSMutableArray alloc] initWithCapacity:n ];
int has_all_files = 0;
for (int i = 0; i < n; i++) {
char *pattern = SDL_strdup(filters[i].pattern);
char *pattern_ptr = pattern;
int has_all_files = 0;
for (int i = 0; i < n; i++) {
char *pattern = SDL_strdup(filters[i].pattern);
char *pattern_ptr = pattern;
if (!pattern_ptr) {
SDL_OutOfMemory();
callback(userdata, NULL, -1);
return;
}
for (char *c = pattern; *c; c++) {
if (*c == ';') {
*c = '\0';
if(@available(macOS 11.0, *)) {
[types addObject: [UTType typeWithFilenameExtension:[NSString stringWithFormat: @"%s", pattern_ptr]]];
} else {
[types addObject: [NSString stringWithFormat: @"%s", pattern_ptr]];
}
pattern_ptr = c + 1;
} else if (*c == '*') {
has_all_files = 1;
if (!pattern_ptr) {
SDL_OutOfMemory();
callback(userdata, NULL, -1);
return;
}
}
if(@available(macOS 11.0, *)) {
[types addObject: [UTType typeWithFilenameExtension:[NSString stringWithFormat: @"%s", pattern_ptr]]];
} else {
[types addObject: [NSString stringWithFormat: @"%s", pattern_ptr]];
for (char *c = pattern; *c; c++) {
if (*c == ';') {
*c = '\0';
if(@available(macOS 11.0, *)) {
[types addObject: [UTType typeWithFilenameExtension:[NSString stringWithFormat: @"%s", pattern_ptr]]];
} else {
[types addObject: [NSString stringWithFormat: @"%s", pattern_ptr]];
}
pattern_ptr = c + 1;
} else if (*c == '*') {
has_all_files = 1;
}
}
if(@available(macOS 11.0, *)) {
[types addObject: [UTType typeWithFilenameExtension:[NSString stringWithFormat: @"%s", pattern_ptr]]];
} else {
[types addObject: [NSString stringWithFormat: @"%s", pattern_ptr]];
}
SDL_free(pattern);
}
SDL_free(pattern);
}
if (!has_all_files) {
if (@available(macOS 11.0, *)) {
[dialog setAllowedContentTypes:types];
} else {
[dialog setAllowedFileTypes:types];
if (!has_all_files) {
if (@available(macOS 11.0, *)) {
[dialog setAllowedContentTypes:types];
} else {
[dialog setAllowedFileTypes:types];
}
}
}