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(platform) + "/" + 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 Platform platform) {
72 std::string filepath = GetConfigDirectory(platform) + "/" + filename;
73 std::ofstream file(filepath);
74 if (file.is_open()) {
75 file << contents;
76 file.close();
77 }
78}
79
80std::string GetConfigDirectory(Platform platform) {
81 std::string config_directory = ".yaze";
82 switch (platform) {
84 config_directory = "~/AppData/Roaming/yaze";
85 break;
88 config_directory = "~/.config/yaze";
89 break;
90 default:
91 break;
92 }
93 return config_directory;
94}
95
96#ifdef _WIN32
97
99 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
100
101 IFileDialog *pfd = NULL;
102 HRESULT hr =
103 CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileDialog,
104 reinterpret_cast<void **>(&pfd));
105 std::string file_path_windows;
106 if (SUCCEEDED(hr)) {
107 // Show the dialog
108 hr = pfd->Show(NULL);
109 if (SUCCEEDED(hr)) {
110 IShellItem *psiResult;
111 hr = pfd->GetResult(&psiResult);
112 if (SUCCEEDED(hr)) {
113 // Get the file path
114 PWSTR pszFilePath;
115 psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
116 char str[128];
117 wcstombs(str, pszFilePath, 128);
118 file_path_windows = str;
119 psiResult->Release();
120 CoTaskMemFree(pszFilePath);
121 }
122 }
123 pfd->Release();
124 }
125
126 CoUninitialize();
127 return file_path_windows;
128}
129
131 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
132
133 IFileDialog *pfd = NULL;
134 HRESULT hr =
135 CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileDialog,
136 reinterpret_cast<void **>(&pfd));
137 std::string folder_path_windows;
138 if (SUCCEEDED(hr)) {
139 // Show the dialog
140 DWORD dwOptions;
141 hr = pfd->GetOptions(&dwOptions);
142 if (SUCCEEDED(hr)) {
143 hr = pfd->SetOptions(dwOptions | FOS_PICKFOLDERS);
144 if (SUCCEEDED(hr)) {
145 hr = pfd->Show(NULL);
146 if (SUCCEEDED(hr)) {
147 IShellItem *psiResult;
148 hr = pfd->GetResult(&psiResult);
149 if (SUCCEEDED(hr)) {
150 // Get the folder path
151 PWSTR pszFolderPath;
152 psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFolderPath);
153 char str[128];
154 wcstombs(str, pszFolderPath, 128);
155 folder_path_windows = str;
156 psiResult->Release();
157 CoTaskMemFree(pszFolderPath);
158 }
159 }
160 }
161 }
162 pfd->Release();
163 }
164
165 CoUninitialize();
166 return folder_path_windows;
167}
168
169std::vector<std::string> FileDialogWrapper::GetSubdirectoriesInFolder(
170 const std::string &folder_path) {
171 std::vector<std::string> subdirectories;
172 WIN32_FIND_DATA findFileData;
173 HANDLE hFind = FindFirstFile((folder_path + "\\*").c_str(), &findFileData);
174 if (hFind != INVALID_HANDLE_VALUE) {
175 do {
176 if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
177 if (strcmp(findFileData.cFileName, ".") != 0 &&
178 strcmp(findFileData.cFileName, "..") != 0) {
179 subdirectories.push_back(findFileData.cFileName);
180 }
181 }
182 } while (FindNextFile(hFind, &findFileData) != 0);
183 FindClose(hFind);
184 }
185 return subdirectories;
186}
187
188std::vector<std::string> FileDialogWrapper::GetFilesInFolder(
189 const std::string &folder_path) {
190 std::vector<std::string> files;
191 WIN32_FIND_DATA findFileData;
192 HANDLE hFind = FindFirstFile((folder_path + "\\*").c_str(), &findFileData);
193 if (hFind != INVALID_HANDLE_VALUE) {
194 do {
195 if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
196 files.push_back(findFileData.cFileName);
197 }
198 } while (FindNextFile(hFind, &findFileData) != 0);
199 FindClose(hFind);
200 }
201 return files;
202}
203
204#elif defined(__linux__)
205
206#include <nfd.h>
207
209 NFD_Init();
210 nfdu8char_t *out_path = NULL;
211 nfdu8filter_item_t filters[1] = {{"Rom File", "sfc,smc"}};
212 nfdopendialogu8args_t args = {0};
213 args.filterList = filters;
214 args.filterCount = 1;
215 nfdresult_t result = NFD_OpenDialogU8_With(&out_path, &args);
216 if (result == NFD_OKAY) {
217 std::string file_path_linux(out_path);
218 NFD_Free(out_path);
219 NFD_Quit();
220 return file_path_linux;
221 } else if (result == NFD_CANCEL) {
222 NFD_Quit();
223 return "";
224 }
225 NFD_Quit();
226 return "Error: NFD_OpenDialog";
227}
228
230 NFD_Init();
231 nfdu8char_t *out_path = NULL;
232 nfdresult_t result = NFD_PickFolderU8(&out_path);
233 if (result == NFD_OKAY) {
234 std::string folder_path_linux(out_path);
235 NFD_Free(out_path);
236 NFD_Quit();
237 return folder_path_linux;
238 } else if (result == NFD_CANCEL) {
239 NFD_Quit();
240 return "";
241 }
242 NFD_Quit();
243 return "Error: NFD_PickFolder";
244}
245
246std::vector<std::string> FileDialogWrapper::GetSubdirectoriesInFolder(
247 const std::string &folder_path) {
248 std::vector<std::string> subdirectories;
249 DIR *dir;
250 struct dirent *ent;
251 if ((dir = opendir(folder_path.c_str())) != NULL) {
252 while ((ent = readdir(dir)) != NULL) {
253 if (ent->d_type == DT_DIR) {
254 if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) {
255 subdirectories.push_back(ent->d_name);
256 }
257 }
258 }
259 closedir(dir);
260 }
261 return subdirectories;
262}
263
264std::vector<std::string> FileDialogWrapper::GetFilesInFolder(
265 const std::string &folder_path) {
266 std::vector<std::string> files;
267 DIR *dir;
268 struct dirent *ent;
269 if ((dir = opendir(folder_path.c_str())) != NULL) {
270 while ((ent = readdir(dir)) != NULL) {
271 if (ent->d_type == DT_REG) {
272 files.push_back(ent->d_name);
273 }
274 }
275 closedir(dir);
276 }
277 return files;
278}
279
280#endif
281
282} // namespace core
283} // 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 GetConfigDirectory(Platform platform)
std::string LoadConfigFile(const std::string &filename)
void SaveFile(const std::string &filename, const std::string &contents, Platform platform)
std::string GetFileName(const std::string &filename)
std::string GetFileExtension(const std::string &filename)
Main namespace for the application.
Definition controller.cc:18