diff liboctave/system/oct-env.cc @ 28854:491433ba8612

Move history file location to inside XDG_CONFIG_HOME or APPDATA (bug #57463). * oct-env.cc, oct-envh (get_user_data_directory, do_get_user_data_directory): New functions. * oct-hist.cc (history_system::default_file): Store history at platform dependent location by default. (Fhistory_file): Document new default location for history file. * cmd-hist.cc (gnu_history::do_write): Create directory if it doesn't exist. * NEWS: Announce changed history file location.
author Markus Mützel <markus.muetzel@gmx.de>
date Tue, 12 May 2020 16:57:31 +0200
parents bd51beb6205e
children 7854d5752dd2
line wrap: on
line diff
--- a/liboctave/system/oct-env.cc	Thu Oct 01 23:26:28 2020 +0200
+++ b/liboctave/system/oct-env.cc	Tue May 12 16:57:31 2020 +0200
@@ -165,6 +165,13 @@
     }
 
     std::string
+    env::get_user_data_directory ()
+    {
+      return (instance_ok ())
+        ? instance->do_get_user_data_directory () : "";
+    }
+
+    std::string
     env::get_program_name (void)
     {
       return (instance_ok ())
@@ -248,9 +255,9 @@
       if (SHGetFolderPathW (nullptr, CSIDL_APPDATA | CSIDL_FLAG_DONT_VERIFY,
                             nullptr, SHGFP_TYPE_CURRENT, path) == S_OK)
         {
-          char *local_app_data = u8_from_wchar (path);
-          cfg_dir = local_app_data;
-          free (local_app_data);
+          char *app_data = u8_from_wchar (path);
+          cfg_dir = app_data;
+          free (app_data);
         }
 #else
       cfg_dir = do_getenv ("XDG_CONFIG_HOME");
@@ -263,6 +270,32 @@
       return cfg_dir;
     }
 
+  std::string
+  env::do_get_user_data_directory (void) const
+  {
+      std::string data_dir;
+
+#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && defined (OCTAVE_USE_WINDOWS_API)
+      wchar_t path[MAX_PATH+1];
+      if (SHGetFolderPathW (nullptr, CSIDL_APPDATA | CSIDL_FLAG_DONT_VERIFY,
+                            nullptr, SHGFP_TYPE_CURRENT, path) == S_OK)
+        {
+          char *app_data = u8_from_wchar (path);
+          data_dir = app_data;
+          free (app_data);
+        }
+#else
+      data_dir = do_getenv ("XDG_DATA_HOME");
+
+      if (data_dir.empty ())
+        data_dir = do_get_home_directory () + sys::file_ops::dir_sep_str ()
+             + ".local" + sys::file_ops::dir_sep_str () + "share";
+#endif
+
+      return data_dir;
+  }
+
+
     // FIXME: this leaves no way to distinguish between a
     // variable that is not set and one that is set to the empty string.
     // Is this a problem?