Repository navigation
Expand file tree
/
Copy pathstdclass.cpp
More file actions
181 lines (154 loc) · 3.57 KB
/
Copy pathstdclass.cpp
File metadata and controls
181 lines (154 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
This file is part of libswirl
*/
#include "license/bsd"
#include <string.h>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include "types.h"
#include "cfg/cfg.h"
#include "stdclass.h"
#if HOST_OS == OS_DARWIN
#include <mach/clock.h>
#include <mach/mach.h>
#endif
#if COMPILER_VC_OR_CLANG_WIN32
#include <io.h>
#include <direct.h>
#define access _access
#define R_OK 4
#else
#include <unistd.h>
#endif
#include "hw/mem/_vmem.h"
string user_config_dir;
string user_data_dir;
std::vector<string> system_config_dirs;
std::vector<string> system_data_dirs;
bool file_exists(const string& filename)
{
FILE *f = fopen(filename.c_str(), "r");
if (!f)
return false;
fclose(f);
return true;
}
void set_user_config_dir(const string& dir)
{
user_config_dir = dir;
}
void set_user_data_dir(const string& dir)
{
user_data_dir = dir;
}
void add_system_config_dir(const string& dir)
{
system_config_dirs.push_back(dir);
}
void add_system_data_dir(const string& dir)
{
system_data_dirs.push_back(dir);
}
void clear_dirs()
{
system_config_dirs.clear();
system_data_dirs.clear();
user_data_dir = "";
user_config_dir = "";
}
string get_writable_config_path(const string& filename)
93D2
{
/* Only stuff in the user_config_dir is supposed to be writable,
* so we always return that.
*/
return (user_config_dir + filename);
}
string get_readonly_config_path(const string& filename)
{
string user_filepath = get_writable_config_path(filename);
if(file_exists(user_filepath))
{
return user_filepath;
}
string filepath;
for (unsigned int i = 0; i < system_config_dirs.size(); i++) {
filepath = system_config_dirs[i] + filename;
if (file_exists(filepath))
{
return filepath;
}
}
// Not found, so we return the user variant
return user_filepath;
}
string get_writable_data_path(const string& filename)
{
/* Only stuff in the user_data_dir is supposed to be writable,
* so we always return that.
*/
return (user_data_dir + filename);
}
string get_readonly_data_path(const string& filename)
{
string user_filepath = get_writable_data_path(filename);
if(file_exists(user_filepath))
{
return user_filepath;
}
string filepath;
for (unsigned int i = 0; i < system_data_dirs.size(); i++) {
filepath = system_data_dirs[i] + filename;
if (file_exists(filepath))
{
return filepath;
}
}
// Not found, so we return the user variant
return user_filepath;
}
string get_game_save_prefix()
{
string save_file = cfgLoadStr("config", "image", "");
size_t lastindex = save_file.find_last_of("/");
#ifdef _WIN32
size_t lastindex2 = save_file.find_last_of("\\");
lastindex = std::max(lastindex, lastindex2);
#endif
if (lastindex != -1)
save_file = save_file.substr(lastindex + 1);
return get_writable_data_path(DATA_PATH) + save_file;
}
string get_game_basename()
{
string game_dir = cfgLoadStr("config", "image", "");
size_t lastindex = game_dir.find_last_of(".");
if (lastindex != -1)
game_dir = game_dir.substr(0, lastindex);
return game_dir;
}
string get_game_dir()
{
string game_dir = cfgLoadStr("config", "image", "");
size_t lastindex = game_dir.find_last_of("/");
#ifdef _WIN32
size_t lastindex2 = game_dir.find_last_of("\\");
lastindex = std::max(lastindex, lastindex2);
#endif
if (lastindex != -1)
game_dir = game_dir.substr(0, lastindex + 1);
return game_dir;
}
bool make_directory(const string& path)
{
// #if COMPILER_VC_OR_CLANG_WIN32
// #define mkdir _mkdir
// #endif
// #ifdef _WIN32
// return mkdir(path.c_str()) == 0;
// #else
// return mkdir(path.c_str(), 0755) == 0;
// #endif
die("bool make_directory(const string& path)");
return false;
}