[geany/geany] e39a45: Stash Settings: Add double key type (#3004)

xiota git-noreply at xxxxx
Wed Nov 17 10:27:08 UTC 2021


Branch:      refs/heads/master
Author:      xiota <xiota at users.noreply.github.com>
Committer:   GitHub <noreply at github.com>
Date:        Wed, 17 Nov 2021 10:27:08 UTC
Commit:      e39a45e672fe23f48850c13e5cce65d07ff9eb0d
             https://github.com/geany/geany/commit/e39a45e672fe23f48850c13e5cce65d07ff9eb0d

Log Message:
-----------
Stash Settings: Add double key type (#3004)

Export the new function to plugins (including utils_get_setting_double()).


Modified Paths:
--------------
    doc/stash-example.c
    src/plugindata.h
    src/stash.c
    src/stash.h
    src/utils.c
    src/utils.h

Modified: doc/stash-example.c
12 lines changed, 8 insertions(+), 4 deletions(-)
===================================================================
@@ -1,19 +1,23 @@
 StashGroup *group;
-gboolean china_enabled;
+gboolean porcelain_enabled;
 gchar *potter_name;
+gint stock;
+gdouble price;
 const gchar filename[] = "/path/data.conf";
 
 /* setup the group */
 group = stash_group_new("cup");
-stash_group_add_boolean(group, &china_enabled, "china", TRUE);
+stash_group_add_boolean(group, &porcelain_enabled, "porcelain", TRUE);
 stash_group_add_string(group, &potter_name, "potter_name", "Miss Clay");
+stash_group_add_integer(group, &stock, "stock", 5);
+stash_group_add_double(group, &price, "price", 1.50);
 
 /* load the settings from a file */
 if (!stash_group_load_from_file(group, filename))
 	g_warning(_("Could not load keyfile %s!"), filename);
 
-/* now use settings china_enabled and potter_name */
-...
+/* now use settings porcelain_enabled, potter_name, stock, and price */
+/* ... */
 
 /* save settings to file */
 if (stash_group_save_to_file(group, filename, G_KEY_FILE_NONE) != 0)


Modified: src/plugindata.h
2 lines changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -58,7 +58,7 @@ G_BEGIN_DECLS
  * @warning You should not test for values below 200 as previously
  * @c GEANY_API_VERSION was defined as an enum value, not a macro.
  */
-#define GEANY_API_VERSION 241
+#define GEANY_API_VERSION 242
 
 /* hack to have a different ABI when built with different GTK major versions
  * because loading plugins linked to a different one leads to crashes.


Modified: src/stash.c
38 lines changed, 36 insertions(+), 2 deletions(-)
===================================================================
@@ -46,8 +46,8 @@
  * property. Macros could be added for common widget properties such as @c GtkExpander:"expanded".
  *
  * @section settings-example Settings Example
- * Here we have some settings for how to make a cup - whether it should be made of china
- * and who's going to make it. (Yes, it's a stupid example).
+ * Here we have some settings for a cup - whether it is made of porcelain, who made it,
+ * how many we have, and how much they cost. (Yes, it's a stupid example).
  * @include stash-example.c
  * @note You might want to handle the warning/error conditions differently from above.
  *
@@ -103,6 +103,7 @@ union Value
 {
 	gboolean bool_val;
 	gint int_val;
+	gdouble double_val;
 	gchar *str_val;
 	gchar **strv_val;
 	gpointer *ptr_val;
@@ -177,6 +178,24 @@ static void handle_boolean_setting(StashGroup *group, StashPref *se,
 }
 
 
+static void handle_double_setting(StashGroup *group, StashPref *se,
+		GKeyFile *config, SettingAction action)
+{
+	gdouble *setting = se->setting;
+
+	switch (action)
+	{
+		case SETTING_READ:
+			*setting = utils_get_setting_double(config, group->name, se->key_name,
+				se->default_value.double_val);
+			break;
+		case SETTING_WRITE:
+			g_key_file_set_double(config, group->name, se->key_name, *setting);
+			break;
+	}
+}
+
+
 static void handle_integer_setting(StashGroup *group, StashPref *se,
 		GKeyFile *config, SettingAction action)
 {
@@ -262,6 +281,8 @@ static void keyfile_action(SettingAction action, StashGroup *group, GKeyFile *ke
 				handle_boolean_setting(group, entry, keyfile, action); break;
 			case G_TYPE_INT:
 				handle_integer_setting(group, entry, keyfile, action); break;
+			case G_TYPE_DOUBLE:
+				handle_double_setting(group, entry, keyfile, action); break;
 			case G_TYPE_STRING:
 				handle_string_setting(group, entry, keyfile, action); break;
 			default:
@@ -485,6 +506,19 @@ void stash_group_add_boolean(StashGroup *group, gboolean *setting,
 }
 
 
+/** Adds double setting.
+ * @param group .
+ * @param setting Address of setting variable.
+ * @param key_name Name for key in a @c GKeyFile.
+ * @param default_value Value to use if the key doesn't exist when loading. */
+GEANY_API_SYMBOL
+void stash_group_add_double(StashGroup *group, gdouble *setting,
+		const gchar *key_name, gdouble default_value)
+{
+	add_pref(group, G_TYPE_DOUBLE, setting, key_name, (union Value) {.double_val = default_value});
+}
+
+
 /** Adds integer setting.
  * @param group .
  * @param setting Address of setting variable.


Modified: src/stash.h
3 lines changed, 3 insertions(+), 0 deletions(-)
===================================================================
@@ -39,6 +39,9 @@ StashGroup *stash_group_new(const gchar *name);
 void stash_group_add_boolean(StashGroup *group, gboolean *setting,
 		const gchar *key_name, gboolean default_value);
 
+void stash_group_add_double(StashGroup *group, gdouble *setting,
+		const gchar *key_name, gdouble default_value);
+
 void stash_group_add_integer(StashGroup *group, gint *setting,
 		const gchar *key_name, gint default_value);
 


Modified: src/utils.c
31 lines changed, 31 insertions(+), 0 deletions(-)
===================================================================
@@ -851,6 +851,37 @@ gboolean utils_get_setting_boolean(GKeyFile *config, const gchar *section, const
 }
 
 
+/**
+ *  Wraps g_key_file_get_double() to add a default value argument.
+ *
+ *  @param config A GKeyFile object.
+ *  @param section The group name to look in for the key.
+ *  @param key The key to find.
+ *  @param default_value The default value which will be returned when @a section or @a key
+ *         don't exist.
+ *
+ *  @return The value associated with @a key as an integer, or the given default value if the value
+ *          could not be retrieved.
+ **/
+GEANY_API_SYMBOL
+gdouble utils_get_setting_double(GKeyFile *config, const gchar *section, const gchar *key,
+							   const gdouble default_value)
+{
+	gdouble tmp;
+	GError *error = NULL;
+
+	g_return_val_if_fail(config, default_value);
+
+	tmp = g_key_file_get_double(config, section, key, &error);
+	if (error)
+	{
+		g_error_free(error);
+		return default_value;
+	}
+	return tmp;
+}
+
+
 /**
  *  Wraps g_key_file_get_string() to add a default value argument.
  *


Modified: src/utils.h
2 lines changed, 2 insertions(+), 0 deletions(-)
===================================================================
@@ -182,6 +182,8 @@ gboolean utils_get_setting_boolean(GKeyFile *config, const gchar *section, const
 
 gint utils_get_setting_integer(GKeyFile *config, const gchar *section, const gchar *key, const gint default_value);
 
+gdouble utils_get_setting_double(GKeyFile *config, const gchar *section, const gchar *key, const gdouble default_value);
+
 gchar *utils_get_setting_string(GKeyFile *config, const gchar *section, const gchar *key, const gchar *default_value);
 
 gboolean utils_spawn_sync(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list