Removes dependency on Fl_Preferences::get/set(.., Fl_String, ...)

This commit is contained in:
Matthias Melcher 2023-10-21 18:41:51 +02:00
parent 0c35212467
commit bbf0ea664d
6 changed files with 125 additions and 71 deletions

View File

@ -24,7 +24,7 @@
# include "Fl_Export.H" # include "Fl_Export.H"
# include "fl_attr.h" # include "fl_attr.h"
class Fl_String; //class Fl_String;
/** /**
\brief Fl_Preferences store user settings between application starts. \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, double value, int precision );
char set( const char *entry, const char *value ); char set( const char *entry, const char *value );
char set( const char *entry, const void *value, int size ); 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, int &value, int defaultValue );
char get( const char *entry, float &value, float 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 );
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 maxSize );
char get( const char *entry, void *value, const void *defaultValue, int defaultSize, int *size ); 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 ); int size( const char *entry );

View File

@ -18,6 +18,7 @@
#include "Fl_Group_Type.h" #include "Fl_Group_Type.h"
#include "alignment_panel.h" #include "alignment_panel.h"
#include "shell_command.h" // get and set Fl_String preferences
#include "file.h" #include "file.h"
#include <FL/fl_draw.H> #include <FL/fl_draw.H>
@ -692,7 +693,7 @@ void Fd_Layout_List::read(Fl_Preferences &prefs, Fd_Tool_Store storage) {
Fl_Preferences prefs_list(prefs, "Layouts"); Fl_Preferences prefs_list(prefs, "Layouts");
Fl_String cs; Fl_String cs;
int cp = 0; int cp = 0;
prefs_list.get("current_suite", cs, ""); preferences_get(prefs_list, "current_suite", cs, "");
prefs_list.get("current_preset", cp, 0); prefs_list.get("current_preset", cp, 0);
for (int i = 0; i < prefs_list.groups(); ++i) { for (int i = 0; i < prefs_list.groups(); ++i) {
Fl_Preferences prefs_suite(prefs_list, Fl_Preferences::Name(i)); Fl_Preferences prefs_suite(prefs_list, Fl_Preferences::Name(i));

View File

@ -104,6 +104,41 @@ static Fl_String fltk_config_cmd;
static Fl_Process s_proc; 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 /** \class Fl_Process
Launch an external shell command. Launch an external shell command.
*/ */
@ -532,26 +567,26 @@ bool Fd_Shell_Command::is_active() {
void Fd_Shell_Command::read(Fl_Preferences &prefs) { void Fd_Shell_Command::read(Fl_Preferences &prefs) {
int tmp; int tmp;
prefs.get("name", name, "<unnamed>"); preferences_get(prefs, "name", name, "<unnamed>");
prefs.get("label", label, "<no label>"); preferences_get(prefs, "label", label, "<no label>");
prefs.get("shortcut", tmp, 0); prefs.get("shortcut", tmp, 0);
shortcut = (Fl_Shortcut)tmp; shortcut = (Fl_Shortcut)tmp;
prefs.get("storage", tmp, -1); prefs.get("storage", tmp, -1);
if (tmp != -1) storage = (Fd_Tool_Store)tmp; if (tmp != -1) storage = (Fd_Tool_Store)tmp;
prefs.get("condition", condition, ALWAYS); prefs.get("condition", condition, ALWAYS);
prefs.get("condition_data", condition_data, ""); preferences_get(prefs, "condition_data", condition_data, "");
prefs.get("command", command, ""); preferences_get(prefs, "command", command, "");
prefs.get("flags", flags, 0); prefs.get("flags", flags, 0);
} }
void Fd_Shell_Command::write(Fl_Preferences &prefs, bool save_location) { void Fd_Shell_Command::write(Fl_Preferences &prefs, bool save_location) {
prefs.set("name", name); preferences_set(prefs, "name", name);
prefs.set("label", label); preferences_set(prefs, "label", label);
if (shortcut != 0) prefs.set("shortcut", (int)shortcut); if (shortcut != 0) prefs.set("shortcut", (int)shortcut);
if (save_location) prefs.set("storage", (int)storage); if (save_location) prefs.set("storage", (int)storage);
if (condition != ALWAYS) prefs.set("condition", condition); if (condition != ALWAYS) prefs.set("condition", condition);
if (!condition_data.empty()) prefs.set("condition_data", condition_data); if (!condition_data.empty()) preferences_set(prefs, "condition_data", condition_data);
if (!command.empty()) prefs.set("command", command); if (!command.empty()) preferences_set(prefs, "command", command);
if (flags != 0) prefs.set("flags", flags); 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->name = "Sample Shell Command";
cmd->label = "Sample Shell Command"; cmd->label = "Sample Shell Command";
cmd->shortcut = FL_ALT+'g'; 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_savefl", save_fl, 1);
fluid_prefs.get("shell_writecode", save_code, 1); fluid_prefs.get("shell_writecode", save_code, 1);
fluid_prefs.get("shell_writemsgs", save_strings, 0); fluid_prefs.get("shell_writemsgs", save_strings, 0);

View File

@ -37,6 +37,10 @@
struct Fl_Menu_Item; struct Fl_Menu_Item;
class Fl_Widget; 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); void run_shell_command(const Fl_String &cmd, int flags);

View File

@ -845,32 +845,46 @@ char Fl_Preferences::get( const char *key, char *&text, const char *defaultValue
return ( v != defaultValue ); return ( v != defaultValue );
} }
/** ///**
Reads an entry from the group. A default value must be // Reads an entry from the group. A default value must be
supplied. The return value indicates if the value was available // supplied. The return value indicates if the value was available
(non-zero) or the default was used (0). // (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 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; return 1;
} }
/** ///**
Sets an entry (name/value pair). The return value indicates if there // Sets an entry (name/value pair). The return value indicates if there
was a problem storing the data in memory. However it does not // was a problem storing the data in memory. However it does not
reflect if the value was actually stored in the preference file. // reflect if the value was actually stored in the preference file.
//
\param[in] entry name of entry // \param[in] entry name of entry
\param[in] value set this entry to value (stops at the first nul character). // \param[in] value set this entry to value (stops at the first nul character).
\return 0 if setting the value failed // \return 0 if setting the value failed
*/ // */
char Fl_Preferences::set( const char *entry, const Fl_String &value ) { //char Fl_Preferences::set( const char *entry, const Fl_String &value ) {
return set(entry, value.c_str()); // return set(entry, value.c_str());
} //}
/** /**

View File

@ -221,27 +221,27 @@ TEST(Fl_String, fl_filename_...) {
} }
/* Test additions to Fl_Preferences. */ /* Test additions to Fl_Preferences. */
TEST(Fl_Preferences, Strings) { //TEST(Fl_Preferences, Strings) {
{ // {
Fl_Preferences prefs(Fl_Preferences::USER_L, "fltk.org", "unittests"); // Fl_Preferences prefs(Fl_Preferences::USER_L, "fltk.org", "unittests");
prefs.set("a", Fl_String()); // prefs.set("a", Fl_String());
prefs.set("b", Fl_String("Hello")); // prefs.set("b", Fl_String("Hello"));
prefs.set("c", Fl_String("Hel\\l\nö")); // prefs.set("c", Fl_String("Hel\\l\nö"));
} // }
{ // {
Fl_Preferences prefs(Fl_Preferences::USER_L, "fltk.org", "unittests"); // Fl_Preferences prefs(Fl_Preferences::USER_L, "fltk.org", "unittests");
Fl_String r; // Fl_String r;
prefs.get("a", r, "x"); // prefs.get("a", r, "x");
EXPECT_STREQ(r.c_str(), ""); // EXPECT_STREQ(r.c_str(), "");
prefs.get("b", r, "x"); // prefs.get("b", r, "x");
EXPECT_STREQ(r.c_str(), "Hello"); // EXPECT_STREQ(r.c_str(), "Hello");
prefs.get("c", r, "x"); // prefs.get("c", r, "x");
EXPECT_STREQ(r.c_str(), "Hel\\l\nö"); // EXPECT_STREQ(r.c_str(), "Hel\\l\nö");
prefs.get("d", r, "x"); // prefs.get("d", r, "x");
EXPECT_STREQ(r.c_str(), "x"); // EXPECT_STREQ(r.c_str(), "x");
} // }
return true; // return true;
} //}
TEST(fl_filename, ext) { TEST(fl_filename, ext) {
Fl_String r = fl_filename_ext("test.txt"); Fl_String r = fl_filename_ext("test.txt");