19 size_t dot = filename.find_last_of(
".");
20 if (dot == std::string::npos) {
23 return filename.substr(dot + 1);
27 size_t slash = filename.find_last_of(
"/");
28 if (slash == std::string::npos) {
31 return filename.substr(slash + 1);
34std::string
LoadFile(
const std::string &filename) {
36 std::ifstream file(filename);
38 std::stringstream buffer;
39 buffer << file.rdbuf();
40 contents = buffer.str();
44 throw std::runtime_error(
"Could not open file: " + filename);
54#elif defined(__APPLE__)
60 std::ifstream file(filepath);
62 std::stringstream buffer;
63 buffer << file.rdbuf();
64 contents = buffer.str();
70void SaveFile(
const std::string &filename,
const std::string &contents) {
72 std::ofstream file(filepath);
80 std::string config_directory =
".yaze";
82#if defined(__APPLE__) && defined(__MACH__)
83#if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1
85#elif TARGET_OS_MAC == 1
90#elif defined(__linux__)
97 config_directory =
"~/AppData/Roaming/yaze";
101 config_directory =
"~/.config/yaze";
106 return config_directory;
112 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
114 IFileDialog *pfd = NULL;
116 CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileDialog,
117 reinterpret_cast<void **
>(&pfd));
118 std::string file_path_windows;
121 hr = pfd->Show(NULL);
123 IShellItem *psiResult;
124 hr = pfd->GetResult(&psiResult);
128 psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
130 wcstombs(str, pszFilePath, 128);
131 file_path_windows = str;
132 psiResult->Release();
133 CoTaskMemFree(pszFilePath);
140 return file_path_windows;
144 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
146 IFileDialog *pfd = NULL;
148 CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileDialog,
149 reinterpret_cast<void **
>(&pfd));
150 std::string folder_path_windows;
154 hr = pfd->GetOptions(&dwOptions);
156 hr = pfd->SetOptions(dwOptions | FOS_PICKFOLDERS);
158 hr = pfd->Show(NULL);
160 IShellItem *psiResult;
161 hr = pfd->GetResult(&psiResult);
165 psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFolderPath);
167 wcstombs(str, pszFolderPath, 128);
168 folder_path_windows = str;
169 psiResult->Release();
170 CoTaskMemFree(pszFolderPath);
179 return folder_path_windows;
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) {
189 if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
190 if (strcmp(findFileData.cFileName,
".") != 0 &&
191 strcmp(findFileData.cFileName,
"..") != 0) {
192 subdirectories.push_back(findFileData.cFileName);
195 }
while (FindNextFile(hFind, &findFileData) != 0);
198 return subdirectories;
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) {
208 if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
209 files.push_back(findFileData.cFileName);
211 }
while (FindNextFile(hFind, &findFileData) != 0);
217#elif defined(__linux__)
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);
233 return file_path_linux;
234 }
else if (result == NFD_CANCEL) {
239 return "Error: NFD_OpenDialog";
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);
250 return folder_path_linux;
251 }
else if (result == NFD_CANCEL) {
256 return "Error: NFD_PickFolder";
260 const std::string &folder_path) {
261 std::vector<std::string> subdirectories;
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);
274 return subdirectories;
278 const std::string &folder_path) {
279 std::vector<std::string> files;
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);
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.