From e2b5cb6cd61bf8c6f56f624cc835233df92288da Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 12 Nov 2014 22:21:36 +0200 Subject: [PATCH] Have get_user_data_dir return a proper location on Android --- source/core/android/mainthread.cpp | 1 + source/core/android/mainthread.h | 3 +++ source/fs/android/dir_location.cpp | 22 ++++++++++++++++++++++ source/fs/unix/dir.cpp | 16 ---------------- source/fs/unix/dir_location.cpp | 25 +++++++++++++++++++++++++ source/fs/windows/dir.cpp | 20 +------------------- source/fs/windows/dir_location.cpp | 28 ++++++++++++++++++++++++++++ 7 files changed, 80 insertions(+), 35 deletions(-) create mode 100644 source/fs/android/dir_location.cpp create mode 100644 source/fs/unix/dir_location.cpp create mode 100644 source/fs/windows/dir_location.cpp diff --git a/source/core/android/mainthread.cpp b/source/core/android/mainthread.cpp index 6a3f865..7219cdf 100644 --- a/source/core/android/mainthread.cpp +++ b/source/core/android/mainthread.cpp @@ -11,6 +11,7 @@ namespace Android { MainThread::MainThread(ANativeActivity *a): asset_manager(a->assetManager), + int_data_path(a->internalDataPath), starting_up(true) { attach_to_activity(a); diff --git a/source/core/android/mainthread.h b/source/core/android/mainthread.h index efd0fe0..4bac952 100644 --- a/source/core/android/mainthread.h +++ b/source/core/android/mainthread.h @@ -3,6 +3,7 @@ #include #include +#include #include "mutex.h" #include "thread.h" @@ -21,6 +22,7 @@ public: private: ANativeActivity *activity; AAssetManager *asset_manager; + FS::Path int_data_path; bool starting_up; Mutex startup_mutex; @@ -33,6 +35,7 @@ public: void resume_startup(); AAssetManager *get_asset_manager() const { return asset_manager; } + const FS::Path &get_internal_data_path() const { return int_data_path; } private: virtual void main(); diff --git a/source/fs/android/dir_location.cpp b/source/fs/android/dir_location.cpp new file mode 100644 index 0000000..f9e0ea5 --- /dev/null +++ b/source/fs/android/dir_location.cpp @@ -0,0 +1,22 @@ +#include +#include +#include "dir.h" + +using namespace std; + +namespace Msp { +namespace FS { + +Path get_home_dir() +{ + return "/sdcard"; +} + +Path get_user_data_dir(const string &) +{ + Android::MainThread *thread = reinterpret_cast(Application::get_data()); + return thread->get_internal_data_path(); +} + +} // namespace FS +} // namespace Msp diff --git a/source/fs/unix/dir.cpp b/source/fs/unix/dir.cpp index 8169a63..0dc950b 100644 --- a/source/fs/unix/dir.cpp +++ b/source/fs/unix/dir.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -33,20 +32,5 @@ void chdir(const Path &path) throw system_error("chdir"); } -Path get_home_dir() -{ - const char *home = getenv("HOME"); - if(home) - return home; - return "."; -} - -Path get_user_data_dir(const string &appname) -{ - if(appname.empty()) - throw invalid_argument("get_user_data_dir"); - return get_home_dir()/("."+appname); -} - } // namespace FS } // namespace Msp diff --git a/source/fs/unix/dir_location.cpp b/source/fs/unix/dir_location.cpp new file mode 100644 index 0000000..e81b8b9 --- /dev/null +++ b/source/fs/unix/dir_location.cpp @@ -0,0 +1,25 @@ +#include +#include "dir.h" + +using namespace std; + +namespace Msp { +namespace FS { + +Path get_home_dir() +{ + const char *home = getenv("HOME"); + if(home) + return home; + return "."; +} + +Path get_user_data_dir(const string &appname) +{ + if(appname.empty()) + throw invalid_argument("get_user_data_dir"); + return get_home_dir()/("."+appname); +} + +} // namespace FS +} // namespace Msp diff --git a/source/fs/windows/dir.cpp b/source/fs/windows/dir.cpp index 151cc1b..3d2bf6e 100644 --- a/source/fs/windows/dir.cpp +++ b/source/fs/windows/dir.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include "dir.h" @@ -57,23 +57,5 @@ void chdir(const Path &path) throw system_error("SetCurrentDirectory"); } -Path get_home_dir() -{ - char home[MAX_PATH]; - if(SHGetFolderPath(0, CSIDL_PERSONAL, 0, 0, home)==S_OK) - return home; - return "."; -} - -Path get_user_data_dir(const string &appname) -{ - if(appname.empty()) - throw invalid_argument("get_user_data_dir"); - char datadir[MAX_PATH]; - if(SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0, 0, datadir)==S_OK) - return Path(datadir)/appname; - return "."; -} - } // namespace FS } // namespace Msp diff --git a/source/fs/windows/dir_location.cpp b/source/fs/windows/dir_location.cpp new file mode 100644 index 0000000..ea542be --- /dev/null +++ b/source/fs/windows/dir_location.cpp @@ -0,0 +1,28 @@ +#include +#include "dir.h" + +using namespace std; + +namespace Msp { +namespace FS { + +Path get_home_dir() +{ + char home[MAX_PATH]; + if(SHGetFolderPath(0, CSIDL_PERSONAL, 0, 0, home)==S_OK) + return home; + return "."; +} + +Path get_user_data_dir(const string &appname) +{ + if(appname.empty()) + throw invalid_argument("get_user_data_dir"); + char datadir[MAX_PATH]; + if(SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0, 0, datadir)==S_OK) + return Path(datadir)/appname; + return "."; +} + +} // namespace FS +} // namespace Msp -- 2.45.2