From bbf0ea664dbc9d0b6faa29e3b9752fec6a094bba Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Sat, 21 Oct 2023 18:41:51 +0200 Subject: [PATCH] Removes dependency on Fl_Preferences::get/set(.., Fl_String, ...) --- FL/Fl_Preferences.H | 6 +-- fluid/Fd_Snap_Action.cxx | 3 +- fluid/shell_command.cxx | 53 ++++++++++++++++++++---- fluid/shell_command.h | 4 ++ src/Fl_Preferences.cxx | 88 +++++++++++++++++++++++----------------- test/unittest_core.cxx | 42 +++++++++---------- 6 files changed, 125 insertions(+), 71 deletions(-) diff --git a/FL/Fl_Preferences.H b/FL/Fl_Preferences.H index 6aee47878..adf976706 100644 --- a/FL/Fl_Preferences.H +++ b/FL/Fl_Preferences.H @@ -24,7 +24,7 @@ # include "Fl_Export.H" # include "fl_attr.h" -class Fl_String; +//class Fl_String; /** \brief Fl_Preferences store user settings between application starts. @@ -241,7 +241,7 @@ public: char set( const char *entry, double value, int precision ); char set( const char *entry, const char *value ); char set( const char *entry, const void *value, int size ); - char set( const char *entry, const Fl_String &value ); +// char set( const char *entry, const Fl_String &value ); char get( const char *entry, int &value, int defaultValue ); char get( const char *entry, float &value, float defaultValue ); @@ -251,7 +251,7 @@ public: char get( const char *entry, void *&value, const void *defaultValue, int defaultSize ); char get( const char *entry, void *value, const void *defaultValue, int defaultSize, int maxSize ); char get( const char *entry, void *value, const void *defaultValue, int defaultSize, int *size ); - char get( const char *entry, Fl_String &value, const Fl_String &defaultValue ); +// char get( const char *entry, Fl_String &value, const Fl_String &defaultValue ); int size( const char *entry ); diff --git a/fluid/Fd_Snap_Action.cxx b/fluid/Fd_Snap_Action.cxx index 7c5b81e4d..56d1486e2 100644 --- a/fluid/Fd_Snap_Action.cxx +++ b/fluid/Fd_Snap_Action.cxx @@ -18,6 +18,7 @@ #include "Fl_Group_Type.h" #include "alignment_panel.h" +#include "shell_command.h" // get and set Fl_String preferences #include "file.h" #include @@ -692,7 +693,7 @@ void Fd_Layout_List::read(Fl_Preferences &prefs, Fd_Tool_Store storage) { Fl_Preferences prefs_list(prefs, "Layouts"); Fl_String cs; int cp = 0; - prefs_list.get("current_suite", cs, ""); + preferences_get(prefs_list, "current_suite", cs, ""); prefs_list.get("current_preset", cp, 0); for (int i = 0; i < prefs_list.groups(); ++i) { Fl_Preferences prefs_suite(prefs_list, Fl_Preferences::Name(i)); diff --git a/fluid/shell_command.cxx b/fluid/shell_command.cxx index 4eb32bcc1..118cf00d3 100644 --- a/fluid/shell_command.cxx +++ b/fluid/shell_command.cxx @@ -104,6 +104,41 @@ static Fl_String fltk_config_cmd; static Fl_Process s_proc; + +/** + Reads an entry from the group. A default value must be + supplied. The return value indicates if the value was available + (non-zero) or the default was used (0). + + \param[in] prefs preference group + \param[in] key name of entry + \param[out] value returned from preferences or default value if none was set + \param[in] defaultValue default value to be used if no preference was set + \return 0 if the default value was used + */ +char preferences_get(Fl_Preferences &prefs, const char *key, Fl_String &value, const Fl_String &defaultValue) { + char *v = NULL; + char ret = prefs.get(key, v, defaultValue.c_str()); + value = v; + ::free(v); + return ret; +} + +/** + Sets an entry (name/value pair). The return value indicates if there + was a problem storing the data in memory. However it does not + reflect if the value was actually stored in the preference file. + + \param[in] prefs preference group + \param[in] entry name of entry + \param[in] value set this entry to value (stops at the first nul character). + \return 0 if setting the value failed + */ +char preferences_set(Fl_Preferences &prefs, const char *key, const Fl_String &value) { + return prefs.set(key, value.c_str()); +} + + /** \class Fl_Process Launch an external shell command. */ @@ -532,26 +567,26 @@ bool Fd_Shell_Command::is_active() { void Fd_Shell_Command::read(Fl_Preferences &prefs) { int tmp; - prefs.get("name", name, ""); - prefs.get("label", label, ""); + preferences_get(prefs, "name", name, ""); + preferences_get(prefs, "label", label, ""); prefs.get("shortcut", tmp, 0); shortcut = (Fl_Shortcut)tmp; prefs.get("storage", tmp, -1); if (tmp != -1) storage = (Fd_Tool_Store)tmp; prefs.get("condition", condition, ALWAYS); - prefs.get("condition_data", condition_data, ""); - prefs.get("command", command, ""); + preferences_get(prefs, "condition_data", condition_data, ""); + preferences_get(prefs, "command", command, ""); prefs.get("flags", flags, 0); } void Fd_Shell_Command::write(Fl_Preferences &prefs, bool save_location) { - prefs.set("name", name); - prefs.set("label", label); + preferences_set(prefs, "name", name); + preferences_set(prefs, "label", label); if (shortcut != 0) prefs.set("shortcut", (int)shortcut); if (save_location) prefs.set("storage", (int)storage); if (condition != ALWAYS) prefs.set("condition", condition); - if (!condition_data.empty()) prefs.set("condition_data", condition_data); - if (!command.empty()) prefs.set("command", command); + if (!condition_data.empty()) preferences_set(prefs, "condition_data", condition_data); + if (!command.empty()) preferences_set(prefs, "command", command); if (flags != 0) prefs.set("flags", flags); } @@ -667,7 +702,7 @@ void Fd_Shell_Command_List::read(Fl_Preferences &prefs, Fd_Tool_Store storage) { cmd->name = "Sample Shell Command"; cmd->label = "Sample Shell Command"; cmd->shortcut = FL_ALT+'g'; - fluid_prefs.get("shell_command", cmd->command, "echo \"Sample Shell Command\""); + preferences_get(fluid_prefs, "shell_command", cmd->command, "echo \"Sample Shell Command\""); fluid_prefs.get("shell_savefl", save_fl, 1); fluid_prefs.get("shell_writecode", save_code, 1); fluid_prefs.get("shell_writemsgs", save_strings, 0); diff --git a/fluid/shell_command.h b/fluid/shell_command.h index ea39375f2..e2e553d7b 100644 --- a/fluid/shell_command.h +++ b/fluid/shell_command.h @@ -37,6 +37,10 @@ struct Fl_Menu_Item; class Fl_Widget; +class Fl_Preferences; + +char preferences_get(Fl_Preferences &prefs, const char *key, Fl_String &value, const Fl_String &defaultValue); +char preferences_set(Fl_Preferences &prefs, const char *key, const Fl_String &value); void run_shell_command(const Fl_String &cmd, int flags); diff --git a/src/Fl_Preferences.cxx b/src/Fl_Preferences.cxx index 69a47089e..cca5e05cf 100644 --- a/src/Fl_Preferences.cxx +++ b/src/Fl_Preferences.cxx @@ -845,32 +845,46 @@ char Fl_Preferences::get( const char *key, char *&text, const char *defaultValue return ( v != defaultValue ); } -/** - Reads an entry from the group. A default value must be - supplied. The return value indicates if the value was available - (non-zero) or the default was used (0). +///** +// Reads an entry from the group. A default value must be +// supplied. The return value indicates if the value was available +// (non-zero) or the default was used (0). +// +// \param[in] key name of entry +// \param[out] value returned from preferences or default value if none was set +// \param[in] defaultValue default value to be used if no preference was set +// \return 0 if the default value was used +// */ +//char Fl_Preferences::get( const char *key, Fl_String &value, const Fl_String &defaultValue ) { +// const char *v = node->get( key ); +// if (v) { +// if ( strchr( v, '\\' ) ) { +// char *text = decodeText( v ); +// value = text; +// ::free(text); +// } else { +// value = v; +// } +// return 1; +// } else { +// value = defaultValue; +// return 0; +// } +//} + +///** +// Sets an entry (name/value pair). The return value indicates if there +// was a problem storing the data in memory. However it does not +// reflect if the value was actually stored in the preference file. +// +// \param[in] entry name of entry +// \param[in] value set this entry to value (stops at the first nul character). +// \return 0 if setting the value failed +// */ +//char Fl_Preferences::set( const char *entry, const Fl_String &value ) { +// return set(entry, value.c_str()); +//} - \param[in] key name of entry - \param[out] value returned from preferences or default value if none was set - \param[in] defaultValue default value to be used if no preference was set - \return 0 if the default value was used - */ -char Fl_Preferences::get( const char *key, Fl_String &value, const Fl_String &defaultValue ) { - const char *v = node->get( key ); - if (v) { - if ( strchr( v, '\\' ) ) { - char *text = decodeText( v ); - value = text; - ::free(text); - } else { - value = v; - } - return 1; - } else { - value = defaultValue; - return 0; - } -} /** Sets an entry (name/value pair). The return value indicates if there @@ -1042,18 +1056,18 @@ char Fl_Preferences::set( const char *key, const void *data, int dsize ) { return 1; } -/** - Sets an entry (name/value pair). The return value indicates if there - was a problem storing the data in memory. However it does not - reflect if the value was actually stored in the preference file. - - \param[in] entry name of entry - \param[in] value set this entry to value (stops at the first nul character). - \return 0 if setting the value failed - */ -char Fl_Preferences::set( const char *entry, const Fl_String &value ) { - return set(entry, value.c_str()); -} +///** +// Sets an entry (name/value pair). The return value indicates if there +// was a problem storing the data in memory. However it does not +// reflect if the value was actually stored in the preference file. +// +// \param[in] entry name of entry +// \param[in] value set this entry to value (stops at the first nul character). +// \return 0 if setting the value failed +// */ +//char Fl_Preferences::set( const char *entry, const Fl_String &value ) { +// return set(entry, value.c_str()); +//} /** diff --git a/test/unittest_core.cxx b/test/unittest_core.cxx index ad60d172c..e1e8e6e85 100644 --- a/test/unittest_core.cxx +++ b/test/unittest_core.cxx @@ -221,27 +221,27 @@ TEST(Fl_String, fl_filename_...) { } /* Test additions to Fl_Preferences. */ -TEST(Fl_Preferences, Strings) { - { - Fl_Preferences prefs(Fl_Preferences::USER_L, "fltk.org", "unittests"); - prefs.set("a", Fl_String()); - prefs.set("b", Fl_String("Hello")); - prefs.set("c", Fl_String("Hel\\l\nö")); - } - { - Fl_Preferences prefs(Fl_Preferences::USER_L, "fltk.org", "unittests"); - Fl_String r; - prefs.get("a", r, "x"); - EXPECT_STREQ(r.c_str(), ""); - prefs.get("b", r, "x"); - EXPECT_STREQ(r.c_str(), "Hello"); - prefs.get("c", r, "x"); - EXPECT_STREQ(r.c_str(), "Hel\\l\nö"); - prefs.get("d", r, "x"); - EXPECT_STREQ(r.c_str(), "x"); - } - return true; -} +//TEST(Fl_Preferences, Strings) { +// { +// Fl_Preferences prefs(Fl_Preferences::USER_L, "fltk.org", "unittests"); +// prefs.set("a", Fl_String()); +// prefs.set("b", Fl_String("Hello")); +// prefs.set("c", Fl_String("Hel\\l\nö")); +// } +// { +// Fl_Preferences prefs(Fl_Preferences::USER_L, "fltk.org", "unittests"); +// Fl_String r; +// prefs.get("a", r, "x"); +// EXPECT_STREQ(r.c_str(), ""); +// prefs.get("b", r, "x"); +// EXPECT_STREQ(r.c_str(), "Hello"); +// prefs.get("c", r, "x"); +// EXPECT_STREQ(r.c_str(), "Hel\\l\nö"); +// prefs.get("d", r, "x"); +// EXPECT_STREQ(r.c_str(), "x"); +// } +// return true; +//} TEST(fl_filename, ext) { Fl_String r = fl_filename_ext("test.txt");