summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Melcher <github@matthiasm.com>2023-02-25 22:44:39 +0100
committerGitHub <noreply@github.com>2023-02-25 22:44:39 +0100
commit13cd927ab43c2ac27d4c5491366ae9d28da23710 (patch)
tree74512edfddd03209abb620caa3bc79aaad479341
parent314ad2310e677b0826b21677afbbf2dfac675f37 (diff)
Add Fl_String to Fl_Preferences (#687)
-rw-r--r--FL/Fl_Preferences.H4
-rw-r--r--src/Fl_Preferences.cxx48
-rw-r--r--test/unittest_core.cxx23
3 files changed, 72 insertions, 3 deletions
diff --git a/FL/Fl_Preferences.H b/FL/Fl_Preferences.H
index 2d09fa271..7112ea42b 100644
--- a/FL/Fl_Preferences.H
+++ b/FL/Fl_Preferences.H
@@ -23,6 +23,8 @@
# include <stdio.h>
# include "Fl_Export.H"
+class Fl_String;
+
/**
\brief Fl_Preferences store user settings between application starts.
@@ -234,6 +236,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 get( const char *entry, int &value, int defaultValue );
char get( const char *entry, float &value, float defaultValue );
@@ -243,6 +246,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 );
int size( const char *entry );
diff --git a/src/Fl_Preferences.cxx b/src/Fl_Preferences.cxx
index ddb521102..6e59537de 100644
--- a/src/Fl_Preferences.cxx
+++ b/src/Fl_Preferences.cxx
@@ -19,6 +19,7 @@
#include "Fl_System_Driver.H"
#include <FL/Fl_Preferences.H>
#include <FL/Fl_Plugin.H>
+#include <FL/Fl_String.H>
#include <FL/filename.H>
#include <stdio.h>
@@ -799,6 +800,33 @@ char Fl_Preferences::get( const char *key, char *&text, const char *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).
+
+ \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.
@@ -969,6 +997,20 @@ char Fl_Preferences::set( const char *key, const void *data, int dsize ) {
}
/**
+ 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] key 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());
+}
+
+
+/**
Returns the size of the value part of an entry.
\param[in] key name of entry
@@ -1211,9 +1253,9 @@ int Fl_Preferences::RootNode::read() {
FILE *f = fl_fopen( filename_, "rb" );
if ( !f )
return -1;
- if (fgets( buf, 1024, f )==0) { /* ignore */ }
- if (fgets( buf, 1024, f )==0) { /* ignore */ }
- if (fgets( buf, 1024, f )==0) { /* ignore */ }
+ if (fgets( buf, 1024, f )==0) { /* ignore: "; FLTK preferences file format 1.0" */ }
+ if (fgets( buf, 1024, f )==0) { /* ignore: "; vendor: ..." */ }
+ if (fgets( buf, 1024, f )==0) { /* ignore: "; application: ..." */ }
Node *nd = prefs_->node;
for (;;) {
if ( !fgets( buf, 1024, f ) ) break; // EOF or Error
diff --git a/test/unittest_core.cxx b/test/unittest_core.cxx
index 7e6699956..30def3782 100644
--- a/test/unittest_core.cxx
+++ b/test/unittest_core.cxx
@@ -193,6 +193,29 @@ TEST(Fl_String, Non-Member Functions) {
return true;
}
+/* 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 aspects of the FLTK core library ----------
//