29 #include <sys/errno.h>
36 #if defined(_DEBUG) && defined(_WINDOWS)
37 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
45 #if defined(ENABLE_SLES)
48 JNIEXPORT
void JNICALL Java_com_zenilib_app_ZenilibActivity_provideAssetManager(JNIEnv*
env, jclass clazz, jobject assetManager) {
49 Zeni::File_Ops::init_AAssetManager(AAssetManager_fromJava(env, assetManager));
58 template class Singleton<File_Ops>;
60 File_Ops * File_Ops::create() {
65 : m_username(
"username"),
70 static bool once =
true;
74 freopen(
"stderr.txt",
"w", stderr);
75 #if defined(_WINDOWS) || defined(NDEBUG)
76 freopen(
"stdout.txt",
"w", stdout);
78 FILE *
const stdout_txt = popen(
"tee stdout.txt",
"w");
80 dup2(fileno(stdout_txt), STDOUT_FILENO);
87 char username[MAX_PATH];
88 DWORD username_len =
sizeof(username);
89 if(!GetUserName(username, &username_len))
90 throw File_Ops_Init_Failure();
92 passwd *
const pws = getpwuid(geteuid());
93 const char *
const username = pws ? pws->pw_name :
"default";
95 m_username = username;
100 char appdata_path[MAX_PATH];
101 if(SHGetFolderPath(0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, 0, SHGFP_TYPE_CURRENT, appdata_path) != S_OK)
102 throw File_Ops_Init_Failure();
104 const char *
const appdata_path = pws ? pws->pw_dir :
"/tmp/";
106 m_appdata_path = appdata_path;
110 String &unique_app_identifier = get_unique_app_identifier();
111 if(unique_app_identifier.empty())
112 unique_app_identifier =
"zenilib";
115 File_Ops::~File_Ops() {
119 return File_Ops::get();
123 void File_Ops::init_AAssetManager(AAssetManager *
const &asset_manager) {
124 m_asset_manager = asset_manager;
127 AAssetManager *
const File_Ops::get_AAssetManager() {
129 throw File_Ops_No_AAssetManager();
130 return m_asset_manager;
134 FILE * File_Ops::get_asset_FILE(
const String &
filename, off_t *
const &start_, off_t *
const &length_) {
135 ZENI_LOGI((
"Loading asset from file '" + filename +
"'.").c_str());
138 AAsset* asset = AAssetManager_open(File_Ops::get_AAssetManager(), filename.
c_str(), AASSET_MODE_UNKNOWN);
146 int fd = AAsset_openFileDescriptor(asset, &start, &length);
153 FILE *
file = fdopen(fd,
"rb");
155 FILE *file = fopen(filename.
c_str(),
"rb");
169 off_t length = ftell(file);
192 FILE *
const file = get_asset_FILE(filename, 0, &length);
195 memory.
resize(length,
'\0');
204 ZENI_LOGE(
"Loading from fd failed, throwing Error");
219 const String & File_Ops::get_username() {
225 return m_appdata_path +
"\\" + get_unique_app_identifier() +
"\\";
227 return m_appdata_path +
"/." + get_unique_app_identifier() +
"/";
231 bool File_Ops::create_directory(
const String &directory_path) {
233 return CreateDirectory(directory_path.
c_str(),
NULL) != 0 ||
234 GetLastError() == ERROR_ALREADY_EXISTS;
236 const int status = mkdir(directory_path.
c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
237 return !status || errno == EEXIST;
241 bool File_Ops::remove_directory(
const String &directory_path) {
243 return RemoveDirectory(directory_path.
c_str()) != 0 ||
244 GetLastError() == ERROR_PATH_NOT_FOUND;
246 return !rmdir(directory_path.
c_str()) || errno == ENOENT;
251 std::ifstream fin(file_path.
c_str());
255 bool File_Ops::delete_file(
const String &file_path) {
257 return DeleteFile(file_path.
c_str()) != 0 ||
258 GetLastError() == ERROR_PATH_NOT_FOUND;
260 return system((
"rm " + file_path).c_str()) == 0;
265 std::ifstream fin(from.
c_str());
269 std::ofstream fout(to.
c_str());
271 for(
char c; fin.get(
c); fout.put(
c));
276 void File_Ops::preinit(
const String &unique_app_identifier_) {
277 String &unique_app_identifier = get_unique_app_identifier();
279 if(!unique_app_identifier.
empty())
282 unique_app_identifier = unique_app_identifier_;
285 String & File_Ops::get_unique_app_identifier() {
286 static String unique_app_identifier;
287 return unique_app_identifier;
291 AAssetManager * File_Ops::m_asset_manager = 0;
const char * c_str() const
void resize(size_t n, char c)
void ZENI_LOGE(const Zeni::String &str)
void ZENI_LOGI(const Zeni::String &str)
typedef DWORD(WINAPI *XInputGetState_t)(DWORD dwUserIndex
File_Ops & get_File_Ops()
Get access to the singleton.