osdialog_file and file extension

I’m using osdialog_file to save a file. In my testing on windows, it does not force the user to enter a path that matches one of the filters, so I need to have logic that adds the file extension if missing.

But now I am supporting multiple file formats and I don’t know how to tell which file type the user has selected.

image


static const char FILE_FILTERS[] = "Wave (.wav):wav,WAV;Aiff (.aiff .aif):aiff,AIFF,aif,AIF";

void saveDialog() {
	osdialog_filters* filters = osdialog_filters_parse(FILE_FILTERS);
	DEFER({osdialog_filters_free(filters);});

	char* pathC = osdialog_file(OSDIALOG_SAVE, fileDir.empty() ? NULL : fileDir.c_str(), NULL, filters);
	if (!pathC) {
		// Fail silently
		return;
	}
	std::string path = pathC;
	std::free(pathC);

	//***
	//This section no longer works since I want to support two different file types
	//***
	if (system::getExtension(path) != ".wav") {
		path += ".wav";
	}
	//***

	fileDir = system::getDirectory(path);

	MyModule* module = dynamic_cast<MyModule*>(this->module);
	module->saveSample(path);
}
1 Like

As far as I can tell from the osdialog code, there is no way to know which filter was selected.

From osdialog.h

/** Linked list of file filters. */
typedef struct osdialog_filters {
	char* name;
	osdialog_filter_patterns* patterns;
	struct osdialog_filters* next;
} osdialog_filters;

osdialog_filters* filters = osdialog_filters_parse(FILE_FILTERS);

Couldn’t you read the name from filters?

Looking at the code it seems like osdialog_filters is not updated, so there would be no way of knowing which on was selected based on it.

I also tested it and the osdialog_filters.name always returns the name for the first file format.

Unfortunately I think @carbon14 is right, that there is no way :frowning:

Can you elaborate why you need the choice for type in your module? The .aiff is mostly used with Logic Users as it’s still the default in that DAW. But Logic opens .wav files totally fine and even let’s you change the default file type to .wav. So I get why you’d want to support „loading“ aiff files (ie. Sample playback) but why save to that format? Knowing you, there’s probably a solid reason that I can’t think of right now :slight_smile:

Eventually I wound like to support other file types too.

1 Like

but doesn’t your file reading library not care what the extension or supposed type is?

The problem is with saving

ah! I get it. I mean, you could just save in wav all the time, but I see.