yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
file_dialog.cc
Go to the documentation of this file.
1#include "file_dialog.h"
2
3#ifdef _WIN32
4// Include Windows-specific headers
5#include <shobjidl.h>
6#include <windows.h>
7#else // Linux and MacOS
8#include <dirent.h>
9#include <sys/stat.h>
10#endif
11
12#include <fstream>
13#include <sstream>
14
15namespace yaze {
16namespace core {
17
18std::string GetFileExtension(const std::string &filename) {
19 size_t dot = filename.find_last_of(".");
20 if (dot == std::string::npos) {
21 return "";
22 }
23 return filename.substr(dot + 1);
24}
25
26std::string GetFileName(const std::string &filename) {
27 size_t slash = filename.find_last_of("/");
28 if (slash == std::string::npos) {
29 return filename;
30 }
31 return filename.substr(slash + 1);
32}
33
34std::string LoadFile(const std::string &filename) {
35 std::string contents;
36 std::ifstream file(filename);
37 if (file.is_open()) {
38 std::stringstream buffer;
39 buffer << file.rdbuf();
40 contents = buffer.str();
41 file.close();
42 } else {
43 // Throw an exception
44 throw std::runtime_error("Could not open file: " + filename);
45 }
46 return contents;
47}
48
49std::string LoadConfigFile(const std::string &filename) {
50 std::string contents;
51 Platform platform;
52#if defined(_WIN32)
53 platform = Platform::kWindows;
54#elif defined(__APPLE__)
55 platform = Platform::kMacOS;
56#else
57 platform = Platform::kLinux;
58#endif
59 std::string filepath = GetConfigDirectory() + "/" + filename;
60 std::ifstream file(filepath);
61 if (file.is_open()) {
62 std::stringstream buffer;
63 buffer << file.rdbuf();
64 contents = buffer.str();
65 file.close();
66 }
67 return contents;
68}
69
70void SaveFile(const std::string &filename, const std::string &contents) {
71 std::string filepath = GetConfigDirectory() + "/" + filename;
72 std::ofstream file(filepath);
73 if (file.is_open()) {
74 file << contents;
75 file.close();
76 }
77}
78
79std::string GetConfigDirectory() {
80 std::string config_directory = ".yaze";
81 Platform platform;
82#if defined(__APPLE__) && defined(__MACH__)
83#if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1
84 platform = Platform::kiOS;
85#elif TARGET_OS_MAC == 1
86 platform = Platform::kMacOS;
87#endif
88#elif defined(_WIN32)
89 platform = Platform::kWindows;
90#elif defined(__linux__)
91 platform = Platform::kLinux;
92#else
93 platform = Platform::kUnknown;
94#endif
95 switch (platform) {
97 config_directory = "~/AppData/Roaming/yaze";
98 break;
100 case Platform::kLinux:
101 config_directory = "~/.config/yaze";
102 break;
103 default:
104 break;
105 }
106 return config_directory;
107}
108
109#ifdef _WIN32
110
112 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
113
114 IFileDialog *pfd = NULL;
115 HRESULT hr =
116 CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileDialog,
117 reinterpret_cast<void **>(&pfd));
118 std::string file_path_windows;
119 if (SUCCEEDED(hr)) {
120 // Show the dialog
121 hr = pfd->Show(NULL);
122 if (SUCCEEDED(hr)) {
123 IShellItem *psiResult;
124 hr = pfd->GetResult(&psiResult);
125 if (SUCCEEDED(hr)) {
126 // Get the file path
127 PWSTR pszFilePath;
128 psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
129 char str[128];
130 wcstombs(str, pszFilePath, 128);
131 file_path_windows = str;
132 psiResult->Release();
133 CoTaskMemFree(pszFilePath);
134 }
135 }
136 pfd->Release();
137 }
138
139 CoUninitialize();
140 return file_path_windows;
141}
142
144 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
145
146 IFileDialog *pfd = NULL;
147 HRESULT hr =
148 CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileDialog,
149 reinterpret_cast<void **>(&pfd));
150 std::string folder_path_windows;
151 if (SUCCEEDED(hr)) {
152 // Show the dialog
153 DWORD dwOptions;
154 hr = pfd->GetOptions(&dwOptions);
155 if (SUCCEEDED(hr)) {
156 hr = pfd->SetOptions(dwOptions | FOS_PICKFOLDERS);
157 if (SUCCEEDED(hr)) {
158 hr = pfd->Show(NULL);
159 if (SUCCEEDED(hr)) {
160 IShellItem *psiResult;
161 hr = pfd->GetResult(&psiResult);
162 if (SUCCEEDED(hr)) {
163 // Get the folder path
164 PWSTR pszFolderPath;
165 psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFolderPath);
166 char str[128];
167 wcstombs(str, pszFolderPath, 128);
168 folder_path_windows = str;
169 psiResult->Release();
170 CoTaskMemFree(pszFolderPath);
171 }
172 }
173 }
174 }
175 pfd->Release();
176 }
177
178 CoUninitialize();
179 return folder_path_windows;
180}
181
182std::vector<std::string> FileDialogWrapper::GetSubdirectoriesInFolder(
183 const std::string &folder_path) {
184 std::vector<std::string> subdirectories;
185 WIN32_FIND_DATA findFileData;
186 HANDLE hFind = FindFirstFile((folder_path + "\\*").c_str(), &findFileData);
187 if (hFind != INVALID_HANDLE_VALUE) {
188 do {
189 if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
190 if (strcmp(findFileData.cFileName, ".") != 0 &&
191 strcmp(findFileData.cFileName, "..") != 0) {
192 subdirectories.push_back(findFileData.cFileName);
193 }
194 }
195 } while (FindNextFile(hFind, &findFileData) != 0);
196 FindClose(hFind);
197 }
198 return subdirectories;
199}
200
201std::vector<std::string> FileDialogWrapper::GetFilesInFolder(
202 const std::string &folder_path) {
203 std::vector<std::string> files;
204 WIN32_FIND_DATA findFileData;
205 HANDLE hFind = FindFirstFile((folder_path + "\\*").c_str(), &findFileData);
206 if (hFind != INVALID_HANDLE_VALUE) {
207 do {
208 if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
209 files.push_back(findFileData.cFileName);
210 }
211 } while (FindNextFile(hFind, &findFileData) != 0);
212 FindClose(hFind);
213 }
214 return files;
215}
216
217#elif defined(__linux__)
218
219#include <nfd.h>
220
222 NFD_Init();
223 nfdu8char_t *out_path = NULL;
224 nfdu8filter_item_t filters[1] = {{"Rom File", "sfc,smc"}};
225 nfdopendialogu8args_t args = {0};
226 args.filterList = filters;
227 args.filterCount = 1;
228 nfdresult_t result = NFD_OpenDialogU8_With(&out_path, &args);
229 if (result == NFD_OKAY) {
230 std::string file_path_linux(out_path);
231 NFD_Free(out_path);
232 NFD_Quit();
233 return file_path_linux;
234 } else if (result == NFD_CANCEL) {
235 NFD_Quit();
236 return "";
237 }
238 NFD_Quit();
239 return "Error: NFD_OpenDialog";
240}
241
243 NFD_Init();
244 nfdu8char_t *out_path = NULL;
245 nfdresult_t result = NFD_PickFolderU8(&out_path);
246 if (result == NFD_OKAY) {
247 std::string folder_path_linux(out_path);
248 NFD_Free(out_path);
249 NFD_Quit();
250 return folder_path_linux;
251 } else if (result == NFD_CANCEL) {
252 NFD_Quit();
253 return "";
254 }
255 NFD_Quit();
256 return "Error: NFD_PickFolder";
257}
258
259std::vector<std::string> FileDialogWrapper::GetSubdirectoriesInFolder(
260 const std::string &folder_path) {
261 std::vector<std::string> subdirectories;
262 DIR *dir;
263 struct dirent *ent;
264 if ((dir = opendir(folder_path.c_str())) != NULL) {
265 while ((ent = readdir(dir)) != NULL) {
266 if (ent->d_type == DT_DIR) {
267 if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) {
268 subdirectories.push_back(ent->d_name);
269 }
270 }
271 }
272 closedir(dir);
273 }
274 return subdirectories;
275}
276
277std::vector<std::string> FileDialogWrapper::GetFilesInFolder(
278 const std::string &folder_path) {
279 std::vector<std::string> files;
280 DIR *dir;
281 struct dirent *ent;
282 if ((dir = opendir(folder_path.c_str())) != NULL) {
283 while ((ent = readdir(dir)) != NULL) {
284 if (ent->d_type == DT_REG) {
285 files.push_back(ent->d_name);
286 }
287 }
288 closedir(dir);
289 }
290 return files;
291}
292
293#endif
294
295} // namespace core
296} // namespace yaze
static std::vector< std::string > GetFilesInFolder(const std::string &folder_path)
static std::string ShowOpenFileDialog()
ShowOpenFileDialog opens a file dialog and returns the selected filepath.
static std::string ShowOpenFolderDialog()
ShowOpenFolderDialog opens a file dialog and returns the selected folder path.
static std::vector< std::string > GetSubdirectoriesInFolder(const std::string &folder_path)
std::string LoadFile(const std::string &filename)
std::string LoadConfigFile(const std::string &filename)
std::string GetFileName(const std::string &filename)
std::string GetFileExtension(const std::string &filename)
void SaveFile(const std::string &filename, const std::string &contents)
std::string GetConfigDirectory()
Main namespace for the application.
Definition controller.cc:18