From 93a5ca3b094f910121c7580e937c1cf4bc1c360d Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Tue, 31 Oct 2023 20:09:47 +0800 Subject: [PATCH] uri supports file: mysql: and postgres: --- gnucash/gnucash-commands.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/gnucash/gnucash-commands.cpp b/gnucash/gnucash-commands.cpp index a0c3b4cf1f2..b29ad1976cf 100644 --- a/gnucash/gnucash-commands.cpp +++ b/gnucash/gnucash-commands.cpp @@ -635,6 +635,29 @@ run_python_cli (int argc, char **argv, scripting_args* args) } #endif +static const std::vector valid_schemes = { "file", "mysql", "postgres" }; + +static bool +is_valid_uri (const bo_str& uri) +{ + if (!uri) + return false; + + if (boost::filesystem::is_regular_file (*uri)) + return true; + + auto scheme = g_uri_parse_scheme (uri->c_str()); + + if (!scheme) + return false; + + auto rv = std::any_of (valid_schemes.begin(), valid_schemes.end(), + [&scheme](const char* str) { return !g_strcmp0(str, scheme); }); + + g_free (scheme); + return rv; +} + int Gnucash::run_scripting (std::vector newArgv, const bo_str& file_to_load, @@ -670,7 +693,7 @@ Gnucash::run_scripting (std::vector newArgv, gnc_prefs_init (); gnc_ui_util_init(); - if (file_to_load && boost::filesystem::is_regular_file (*file_to_load)) + if (is_valid_uri (file_to_load)) [[maybe_unused]] auto session = load_file (*file_to_load, open_readwrite); scripting_args args { script, interactive };