16namespace fs = std::filesystem;
25 : result_(std::move(result)) {
26 std::lock_guard<std::mutex> lock(save_dialog_test_mutex);
27 if (save_dialog_test_scope !=
nullptr) {
28 throw std::logic_error(
"Save dialog test scope is already active");
30 save_dialog_test_scope =
this;
34 std::lock_guard<std::mutex> lock(save_dialog_test_mutex);
35 save_dialog_test_scope =
nullptr;
39 std::lock_guard<std::mutex> lock(save_dialog_test_mutex);
43std::optional<std::string>
45 std::lock_guard<std::mutex> lock(save_dialog_test_mutex);
46 if (!save_dialog_test_scope || !save_dialog_test_scope->
result_) {
49 auto result = std::move(save_dialog_test_scope->
result_);
50 save_dialog_test_scope->
result_.reset();
56 return fs::path(filename).extension().string();
60 return fs::path(filename).filename().string();
63std::string
LoadFile(
const std::string& filename) {
65 std::ifstream file(filename);
67 std::stringstream buffer;
68 buffer << file.rdbuf();
69 contents = buffer.str();
73 throw std::runtime_error(
"Could not open file: " + filename);
80 if (!config_dir.ok()) {
84 fs::path filepath = *config_dir / filename;
86 std::ifstream file(filepath);
88 std::stringstream buffer;
89 buffer << file.rdbuf();
90 contents = buffer.str();
96void SaveFile(
const std::string& filename,
const std::string& contents) {
98 if (!config_dir.ok()) {
102 fs::path filepath = *config_dir / filename;
103 std::ofstream file(filepath);
104 if (file.is_open()) {
112#if TARGET_OS_IOS == 1
114 if (resource_path.rfind(
"assets/", 0) == 0) {
115 return kBundlePath + resource_path;
117 return kBundlePath +
"assets/" + resource_path;
122 return resource_path;
130 {
"ROM/Project Files",
"sfc,smc,yaze,yazeproj,zsproj"});
131 options.
filters.push_back({
"ROM Files",
"sfc,smc"});
132 options.
filters.push_back({
"Project Files",
"yaze,yazeproj,zsproj"});
133 if (include_all_files) {
134 options.
filters.push_back({
"All Files",
"*"});
static std::optional< std::string > ConsumeSaveFileDialogResultForTesting()
~ScopedSaveFileDialogResultForTesting()
ScopedSaveFileDialogResultForTesting(std::string result)
std::optional< std::string > result_
ScopedSaveFileDialogResultForTesting * save_dialog_test_scope
std::mutex save_dialog_test_mutex
void SaveFile(const std::string &filename, const std::string &contents)
std::string GetFileName(const std::string &filename)
Gets the filename from a full path.
std::string GetResourcePath(const std::string &resource_path)
std::string LoadFileFromConfigDir(const std::string &filename)
Loads a file from the user's config directory.
FileDialogOptions MakeRomFileDialogOptions(bool include_all_files)
std::string GetFileExtension(const std::string &filename)
Gets the file extension from a filename.
std::string GetBundleResourcePath()
GetBundleResourcePath returns the path to the bundle resource directory. Specific to MacOS.
std::string LoadFile(const std::string &filename)
Loads the entire contents of a file into a string.
std::vector< FileDialogFilter > filters