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 <sstream>
6
7#include "core/features.h"
9
10namespace yaze {
11namespace util {
12
13namespace fs = std::filesystem;
14
15std::string GetFileExtension(const std::string& filename) {
16 return fs::path(filename).extension().string();
17}
18
19std::string GetFileName(const std::string& filename) {
20 return fs::path(filename).filename().string();
21}
22
23std::string LoadFile(const std::string& filename) {
24 std::string contents;
25 std::ifstream file(filename);
26 if (file.is_open()) {
27 std::stringstream buffer;
28 buffer << file.rdbuf();
29 contents = buffer.str();
30 file.close();
31 } else {
32 // Throw an exception
33 throw std::runtime_error("Could not open file: " + filename);
34 }
35 return contents;
36}
37
38std::string LoadFileFromConfigDir(const std::string& filename) {
39 auto config_dir = PlatformPaths::GetConfigDirectory();
40 if (!config_dir.ok()) {
41 return ""; // Or handle error appropriately
42 }
43
44 fs::path filepath = *config_dir / filename;
45 std::string contents;
46 std::ifstream file(filepath);
47 if (file.is_open()) {
48 std::stringstream buffer;
49 buffer << file.rdbuf();
50 contents = buffer.str();
51 file.close();
52 }
53 return contents;
54}
55
56void SaveFile(const std::string& filename, const std::string& contents) {
57 auto config_dir = PlatformPaths::GetConfigDirectory();
58 if (!config_dir.ok()) {
59 // Or handle error appropriately
60 return;
61 }
62 fs::path filepath = *config_dir / filename;
63 std::ofstream file(filepath);
64 if (file.is_open()) {
65 file << contents;
66 file.close();
67 }
68}
69
70std::string GetResourcePath(const std::string& resource_path) {
71#ifdef __APPLE__
72#if TARGET_OS_IOS == 1
73 const std::string kBundlePath = GetBundleResourcePath();
74 if (resource_path.rfind("assets/", 0) == 0) {
75 return kBundlePath + resource_path;
76 }
77 return kBundlePath + "assets/" + resource_path;
78#else
79 return GetBundleResourcePath() + "Contents/Resources/" + resource_path;
80#endif
81#else
82 return resource_path; // On Linux/Windows, resources are relative to
83 // executable
84#endif
85}
86
88 FileDialogOptions options;
89 options.filters.push_back({"ROM Files", "sfc,smc"});
90 if (include_all_files) {
91 options.filters.push_back({"All Files", "*"});
92 }
93 return options;
94}
95
96// Note: FileDialogWrapper implementations are in
97// src/app/platform/file_dialog.mm (platform-specific implementations to avoid
98// duplicate symbols)
99
100} // namespace util
101} // namespace yaze
static absl::StatusOr< std::filesystem::path > GetConfigDirectory()
Get the user-specific configuration directory for YAZE.
void SaveFile(const std::string &filename, const std::string &contents)
Definition file_util.cc:56
std::string GetFileName(const std::string &filename)
Gets the filename from a full path.
Definition file_util.cc:19
std::string GetResourcePath(const std::string &resource_path)
Definition file_util.cc:70
std::string LoadFileFromConfigDir(const std::string &filename)
Loads a file from the user's config directory.
Definition file_util.cc:38
FileDialogOptions MakeRomFileDialogOptions(bool include_all_files)
Definition file_util.cc:87
std::string GetFileExtension(const std::string &filename)
Gets the file extension from a filename.
Definition file_util.cc:15
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:23
std::vector< FileDialogFilter > filters
Definition file_util.h:17