yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
file_util.cc
Go to the documentation of this file.
1#include "file_util.h"
2
3#if defined(_WIN32)
4#include <windows.h>
5#else
6#include <dirent.h>
7#include <sys/stat.h>
8#endif
9
10#include <fstream>
11#include <sstream>
12
13namespace yaze {
14namespace app {
15namespace core {
16
17std::string GetFileExtension(const std::string &filename) {
18 size_t dot = filename.find_last_of(".");
19 if (dot == std::string::npos) {
20 return "";
21 }
22 return filename.substr(dot + 1);
23}
24
25std::string GetFileName(const std::string &filename) {
26 size_t slash = filename.find_last_of("/");
27 if (slash == std::string::npos) {
28 return filename;
29 }
30 return filename.substr(slash + 1);
31}
32
33std::string LoadFile(const std::string &filename, Platform platform) {
34 std::string contents;
35 std::string filepath = GetConfigDirectory(platform) + "/" + filename;
36 std::ifstream file(filepath);
37 if (file.is_open()) {
38 std::stringstream buffer;
39 buffer << file.rdbuf();
40 contents = buffer.str();
41 file.close();
42 }
43 return contents;
44}
45
46void SaveFile(const std::string &filename, const std::string &contents,
47 Platform platform) {
48 std::string filepath = GetConfigDirectory(platform) + "/" + filename;
49 std::ofstream file(filepath);
50 if (file.is_open()) {
51 file << contents;
52 file.close();
53 }
54}
55
56std::string GetConfigDirectory(Platform platform) {
57 std::string config_directory = ".yaze";
58 switch (platform) {
60 config_directory = "~/AppData/Roaming/yaze";
61 break;
64 config_directory = "~/.config/yaze";
65 break;
66 default:
67 break;
68 }
69 return config_directory;
70}
71
72} // namespace core
73} // namespace app
74} // namespace yaze
std::string GetFileName(const std::string &filename)
Definition file_util.cc:25
std::string GetFileExtension(const std::string &filename)
Definition file_util.cc:17
std::string LoadFile(const std::string &filename, Platform platform)
Definition file_util.cc:33
std::string GetConfigDirectory(Platform platform)
Definition file_util.cc:56
void SaveFile(const std::string &filename, const std::string &contents, Platform platform)
Definition file_util.cc:46
Definition common.cc:22