Have get_user_data_dir return a proper location on Android
authorMikko Rasa <tdb@tdb.fi>
Wed, 12 Nov 2014 20:21:36 +0000 (22:21 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 12 Nov 2014 20:21:36 +0000 (22:21 +0200)
source/core/android/mainthread.cpp
source/core/android/mainthread.h
source/fs/android/dir_location.cpp [new file with mode: 0644]
source/fs/unix/dir.cpp
source/fs/unix/dir_location.cpp [new file with mode: 0644]
source/fs/windows/dir.cpp
source/fs/windows/dir_location.cpp [new file with mode: 0644]

index 6a3f865bbc3ccf53d6142a321bf881547c6b1822..7219cdf2fdd9ef1476072f90aac28a1364beb776 100644 (file)
@@ -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);
index efd0fe0e0af22e1c5c833e000b5fd10862f37546..4bac952bc149bd839b1815be03981d6ff130baea 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <android/native_activity.h>
 #include <sigc++/signal.h>
+#include <msp/fs/path.h>
 #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 (file)
index 0000000..f9e0ea5
--- /dev/null
@@ -0,0 +1,22 @@
+#include <msp/core/application.h>
+#include <msp/core/mainthread.h>
+#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<Android::MainThread *>(Application::get_data());
+       return thread->get_internal_data_path();
+}
+
+} // namespace FS
+} // namespace Msp
index 8169a63fffbe9809af24012ed4ae28101ac881e3..0dc950bf00da0b8ff82b5e26e25346be3170be41 100644 (file)
@@ -1,4 +1,3 @@
-#include <cstdlib>
 #include <unistd.h>
 #include <sys/stat.h>
 #include <msp/core/systemerror.h>
@@ -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 (file)
index 0000000..e81b8b9
--- /dev/null
@@ -0,0 +1,25 @@
+#include <cstdlib>
+#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
index 151cc1b0944c4b455655c1f426da09c0ee81f89f..3d2bf6e65a8f5fd461a48d51d595265c1bad2fec 100644 (file)
@@ -1,4 +1,4 @@
-#include <shlobj.h>
+#include <windows.h>
 #include <msp/core/mutex.h>
 #include <msp/core/systemerror.h>
 #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 (file)
index 0000000..ea542be
--- /dev/null
@@ -0,0 +1,28 @@
+#include <shlobj.h>
+#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