Skip to content

Commit

Permalink
Possibly fix crash on file save on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
rj45 committed May 18, 2024
1 parent 2baa20d commit c67f950
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 60 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ target_link_libraries(digilogic PRIVATE Freetype::Freetype)

target_compile_definitions(digilogic PRIVATE "NVD_STATIC_LINKAGE")

# some virtual machines and old hardware don't support MSAA
if (MSAA_SAMPLE_COUNT)
target_compile_definitions(digilogic PRIVATE "MSAA_SAMPLE_COUNT=${MSAA_SAMPLE_COUNT}")
endif()
Expand Down
125 changes: 65 additions & 60 deletions thirdparty/nvdialog/src/backend/win32/nvdialog_file_dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,80 +31,85 @@
#include <windows.h>
#include <winuser.h>

static const char* nvd_append_bytes(char* dest, const char* src) {
size_t dest_len = strlen(dest);
size_t src_len = strlen(src);
char* new_str = (char*)malloc(dest_len + src_len + 1);
if (!new_str) {
return NULL;
}
memcpy(new_str, dest, dest_len);
memcpy(new_str + dest_len, src, src_len + 1);
static const char *nvd_append_bytes(char *dest, const char *src) {
size_t dest_len = strlen(dest);
size_t src_len = strlen(src);

char *new_str = (char *)malloc(dest_len + src_len + 1);
if (!new_str) {
return NULL;
}
memcpy(new_str, dest, dest_len);
memcpy(new_str + dest_len, src, src_len + 1);

return new_str;
return new_str;
}

NvdFileDialog *nvd_open_file_dialog_win32(const char *title,
const char *file_extensions) {
NvdFileDialog *dialog = malloc(sizeof(struct _NvdFileDialog));
NVD_RETURN_IF_NULL(dialog);
NvdFileDialog *
nvd_open_file_dialog_win32(const char *title, const char *file_extensions) {
NvdFileDialog *dialog = malloc(sizeof(struct _NvdFileDialog));
NVD_RETURN_IF_NULL(dialog);

dialog->is_save_dialog = false;
dialog->file_extensions = file_extensions;
dialog->title = title;
dialog->is_save_dialog = false;
dialog->file_extensions = file_extensions;
dialog->title = title;

return dialog;
return dialog;
}

/* TODO: Add default filename support. */
NvdFileDialog *nvd_save_file_dialog_win32(const char *title,
const char *default_filename) {
NvdFileDialog *dialog = calloc(1, sizeof(struct _NvdFileDialog));
NVD_RETURN_IF_NULL(dialog);
NvdFileDialog *
nvd_save_file_dialog_win32(const char *title, const char *default_filename) {
NvdFileDialog *dialog = malloc(sizeof(struct _NvdFileDialog));
NVD_RETURN_IF_NULL(dialog);

dialog->is_save_dialog = true;
dialog->file_extensions = NULL; /* Technically, we could add filters here, but how? */
dialog->title = title;
return dialog;
dialog->is_save_dialog = true;
dialog->file_extensions =
NULL; /* Technically, we could add filters here, but how? */
dialog->title = title;
return dialog;
}

void nvd_get_file_location_win32(NvdFileDialog *dialog,
const char **savebuf) {
OPENFILENAME ofn;
char file[NVDIALOG_MAXBUF];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = nvd_get_parent();
ofn.lpstrFile = file;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(file) - 1; /* NULL byte manually set */
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
void nvd_get_file_location_win32(NvdFileDialog *dialog, const char **savebuf) {
OPENFILENAME ofn;
char file[NVDIALOG_MAXBUF];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = nvd_get_parent();
ofn.lpstrFile = file;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(file) - 1; /* NULL byte manually set */
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

if (dialog->file_extensions) {
char** words = nvd_seperate_args(dialog->file_extensions);
size_t i = 0;
const char buffer[NVDIALOG_MAXBUF];
if (dialog->file_extensions) {
char **words = nvd_seperate_args(dialog->file_extensions);
size_t i = 0;
const char buffer[NVDIALOG_MAXBUF];

while (words[i] != NULL) {
const char tmp_buffer[128];
snprintf(buffer, sizeof(buffer), ".%s files (*.%s)", words[i], words[i]);
nvd_append_bytes(buffer, tmp_buffer);
i++;
}
while (words[i] != NULL) {
const char tmp_buffer[128];
snprintf(buffer, sizeof(buffer), ".%s files (*.%s)", words[i], words[i]);
nvd_append_bytes(buffer, tmp_buffer);
i++;
}

ofn.lpstrFilter = buffer;
} else ofn.lpstrFilter = NULL;
ofn.lpstrFilter = buffer;
} else
ofn.lpstrFilter = NULL;

file[NVDIALOG_MAXBUF - 1] = '\0';
if (dialog->is_save_dialog) GetSaveFileName(&ofn);
else GetOpenFileName(&ofn);
file[NVDIALOG_MAXBUF - 1] = '\0';
if (dialog->is_save_dialog)
dialog->location_was_chosen = GetSaveFileName(&ofn);
else
dialog->location_was_chosen = GetOpenFileName(&ofn);

dialog->filename = ofn.lpstrFile;
dialog->location_was_chosen = true;
*savebuf = dialog->filename;
if (dialog->location_was_chosen) {
dialog->filename = strdup(ofn.lpstrFile);
dialog->location_was_chosen = true;
*savebuf = dialog->filename;
}
}

0 comments on commit c67f950

Please sign in to comment.