yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
file_util.cc
Go to the documentation of this file.
1#include "util/file_util.h"
2
3#include <filesystem>
4#include <fstream>
5#include <mutex>
6#include <sstream>
7#include <stdexcept>
8#include <utility>
9
10#include "core/features.h"
11#include "util/platform_paths.h"
12
13namespace yaze {
14namespace util {
15
16namespace fs = std::filesystem;
17
22
24 std::string result)
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");
29 }
30 save_dialog_test_scope = this;
31}
32
34 std::lock_guard<std::mutex> lock(save_dialog_test_mutex);
35 save_dialog_test_scope = nullptr;
36}
37
39 std::lock_guard<std::mutex> lock(save_dialog_test_mutex);
40 return consumed_;
41}
42
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_) {
47 return std::nullopt;
48 }
49 auto result = std::move(save_dialog_test_scope->result_);
50 save_dialog_test_scope->result_.reset();
51 save_dialog_test_scope->consumed_ = true;
52 return result;
53}
54
55std::string GetFileExtension(const std::string& filename) {
56 return fs::path(filename).extension().string();
57}
58
59std::string GetFileName(const std::string& filename) {
60 return fs::path(filename).filename().string();
61}
62
63std::string LoadFile(const std::string& filename) {
64 std::string contents;
65 std::ifstream file(filename);
66 if (file.is_open()) {
67 std::stringstream buffer;
68 buffer << file.rdbuf();
69 contents = buffer.str();
70 file.close();
71 } else {
72 // Throw an exception
73 throw std::runtime_error("Could not open file: " + filename);
74 }
75 return contents;
76}
77
78std::string LoadFileFromConfigDir(const std::string& filename) {
79 auto config_dir = PlatformPaths::GetConfigDirectory();
80 if (!config_dir.ok()) {
81 return ""; // Or handle error appropriately
82 }
83
84 fs::path filepath = *config_dir / filename;
85 std::string contents;
86 std::ifstream file(filepath);
87 if (file.is_open()) {
88 std::stringstream buffer;
89 buffer << file.rdbuf();
90 contents = buffer.str();
91 file.close();
92 }
93 return contents;
94}
95
96void SaveFile(const std::string& filename, const std::string& contents) {
97 auto config_dir = PlatformPaths::GetConfigDirectory();
98 if (!config_dir.ok()) {
99 // Or handle error appropriately
100 return;
101 }
102 fs::path filepath = *config_dir / filename;
103 std::ofstream file(filepath);
104 if (file.is_open()) {
105 file << contents;
106 file.close();
107 }
108}
109
110std::string GetResourcePath(const std::string& resource_path) {
111#ifdef __APPLE__
112#if TARGET_OS_IOS == 1
113 const std::string kBundlePath = GetBundleResourcePath();
114 if (resource_path.rfind("assets/", 0) == 0) {
115 return kBundlePath + resource_path;
116 }
117 return kBundlePath + "assets/" + resource_path;
118#else
119 return GetBundleResourcePath() + "Contents/Resources/" + resource_path;
120#endif
121#else
122 return resource_path; // On Linux/Windows, resources are relative to
123 // executable
124#endif
125}
126
128 FileDialogOptions options;
129 options.filters.push_back(
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", "*"});
135 }
136 return options;
137}
138
139// Note: FileDialogWrapper implementations are in
140// src/app/platform/file_dialog.mm (platform-specific implementations to avoid
141// duplicate symbols)
142
143} // namespace util
144} // namespace yaze
static std::optional< std::string > ConsumeSaveFileDialogResultForTesting()
Definition file_util.cc:44
static absl::StatusOr< std::filesystem::path > GetConfigDirectory()
Get the user-specific configuration directory for YAZE.
ScopedSaveFileDialogResultForTesting * save_dialog_test_scope
Definition file_util.cc:20
void SaveFile(const std::string &filename, const std::string &contents)
Definition file_util.cc:96
std::string GetFileName(const std::string &filename)
Gets the filename from a full path.
Definition file_util.cc:59
std::string GetResourcePath(const std::string &resource_path)
Definition file_util.cc:110
std::string LoadFileFromConfigDir(const std::string &filename)
Loads a file from the user's config directory.
Definition file_util.cc:78
FileDialogOptions MakeRomFileDialogOptions(bool include_all_files)
Definition file_util.cc:127
std::string GetFileExtension(const std::string &filename)
Gets the file extension from a filename.
Definition file_util.cc:55
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.
Definition file_util.cc:63
std::vector< FileDialogFilter > filters
Definition file_util.h:18