SF.net SVN: geany:[4778] trunk
ntrel at users.sourceforge.net
ntrel at xxxxx
Fri Mar 19 17:38:27 UTC 2010
Revision: 4778
http://geany.svn.sourceforge.net/geany/?rev=4778&view=rev
Author: ntrel
Date: 2010-03-19 17:38:27 +0000 (Fri, 19 Mar 2010)
Log Message:
-----------
Add stash_group_load_from_file() and stash_group_save_to_file().
Modified Paths:
--------------
trunk/ChangeLog
trunk/doc/stash-example.c
trunk/src/stash.c
trunk/src/stash.h
trunk/src/utils.c
Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog 2010-03-19 12:51:08 UTC (rev 4777)
+++ trunk/ChangeLog 2010-03-19 17:38:27 UTC (rev 4778)
@@ -3,6 +3,8 @@
* src/stash.c:
Make adding string and string vector settings initialise the setting
to NULL for safety.
+ * src/utils.c, src/stash.c, src/stash.h, doc/stash-example.c:
+ Add stash_group_load_from_file() and stash_group_save_to_file().
2010-03-18 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
Modified: trunk/doc/stash-example.c
===================================================================
--- trunk/doc/stash-example.c 2010-03-19 12:51:08 UTC (rev 4777)
+++ trunk/doc/stash-example.c 2010-03-19 17:38:27 UTC (rev 4778)
@@ -1,9 +1,7 @@
StashGroup *group;
gboolean china_enabled;
-gchar *potter_name = NULL; /* strings must be initialised */
-GKeyFile *keyfile;
-const gchar filename[] = "/path/to/data.conf";
-gchar *data;
+gchar *potter_name;
+const gchar filename[] = "/path/data.conf";
/* setup the group */
group = stash_group_new("cups");
@@ -11,23 +9,15 @@
stash_group_add_string(group, &potter_name, "potter_name", "Miss Clay");
/* load the settings from a file */
-keyfile = g_key_file_new();
-g_key_file_load_from_file(keyfile, filename, G_KEY_FILE_NONE, NULL);
-stash_group_load_from_key_file(group, keyfile);
-g_key_file_free(keyfile);
+if (!stash_group_load_from_file(group, filename, G_KEY_FILE_NONE))
+ g_warning(_("Could not load keyfile %s!"), filename);
/* now use settings china_enabled and potter_name */
...
/* save settings to file */
-keyfile = g_key_file_new();
-stash_group_save_to_key_file(group, keyfile);
-data = g_key_file_to_data(keyfile, NULL, NULL);
-if (utils_write_file(filename, data) != 0)
- dialogs_show_msgbox(GTK_MESSAGE_ERROR,
- _("Could not save keyfile %s!"), filename);
-g_free(data);
-g_key_file_free(keyfile);
+if (stash_group_save_to_file(group, filename, G_KEY_FILE_NONE) != 0)
+ g_error(_("Could not save keyfile %s!"), filename);
/* free memory */
stash_group_free(group);
Modified: trunk/src/stash.c
===================================================================
--- trunk/src/stash.c 2010-03-19 12:51:08 UTC (rev 4777)
+++ trunk/src/stash.c 2010-03-19 17:38:27 UTC (rev 4778)
@@ -40,7 +40,7 @@
*
* @section String Settings
* String settings and other dynamically allocated settings will be initialized to NULL when
- * added to a StashGroup (so they can safely be freed before reassigning).
+ * added to a StashGroup (so they can safely be reassigned).
*
* @section Widget Support
* Widgets very commonly used in configuration dialogs will be supported with their own function.
@@ -51,6 +51,7 @@
*
* @section Example
* @include stash-example.c
+ * @note You might want to handle the warning/error conditions differently from above.
*/
/* Implementation Note
@@ -257,6 +258,61 @@
}
+/** Reads group settings from a configuration file using @c GKeyFile.
+ * @param group .
+ * @param filename Filename of the file to write, in locale encoding.
+ * @param flags Keyfile options - @c G_KEY_FILE_NONE is the most efficient.
+ * @return @c TRUE if a key file could be loaded.
+ * @see stash_group_load_from_key_file().
+ **/
+gboolean stash_group_load_from_file(StashGroup *group, const gchar *filename,
+ GKeyFileFlags flags)
+{
+ GKeyFile *keyfile;
+ gboolean ret;
+
+ keyfile = g_key_file_new();
+ ret = g_key_file_load_from_file(keyfile, filename, flags, NULL);
+ /* even on failure we load settings to apply defaults */
+ stash_group_load_from_key_file(group, keyfile);
+
+ g_key_file_free(keyfile);
+ return ret;
+}
+
+
+/** Writes group settings to a configuration file using @c GKeyFile.
+ * If the file doesn't exist, it will be created.
+ * If it already exists, it will be overwritten.
+ *
+ * @param group .
+ * @param filename Filename of the file to write, in locale encoding.
+ * @param flags Keyfile options - @c G_KEY_FILE_NONE is the most efficient.
+ * @return 0 if the file was successfully written, otherwise the @c errno of the
+ * failed operation is returned.
+ * @see stash_group_save_to_key_file().
+ **/
+gint stash_group_save_to_file(StashGroup *group, const gchar *filename,
+ GKeyFileFlags flags)
+{
+ GKeyFile *keyfile;
+ gchar *data;
+ gint ret;
+
+ keyfile = g_key_file_new();
+ /* if we need to keep comments or translations, try to load first */
+ if (flags)
+ g_key_file_load_from_file(keyfile, filename, flags, NULL);
+
+ stash_group_save_to_key_file(group, keyfile);
+ data = g_key_file_to_data(keyfile, NULL, NULL);
+ ret = utils_write_file(filename, data);
+ g_free(data);
+ g_key_file_free(keyfile);
+ return ret;
+}
+
+
/** Creates a new group.
* @param name Name used for @c GKeyFile group.
* @return Group. */
Modified: trunk/src/stash.h
===================================================================
--- trunk/src/stash.h 2010-03-19 12:51:08 UTC (rev 4777)
+++ trunk/src/stash.h 2010-03-19 17:38:27 UTC (rev 4778)
@@ -55,6 +55,12 @@
void stash_group_save_to_key_file(StashGroup *group, GKeyFile *keyfile);
+gboolean stash_group_load_from_file(StashGroup *group, const gchar *filename,
+ GKeyFileFlags flags);
+
+gint stash_group_save_to_file(StashGroup *group, const gchar *filename,
+ GKeyFileFlags flags);
+
void stash_group_free(StashGroup *group);
Modified: trunk/src/utils.c
===================================================================
--- trunk/src/utils.c 2010-03-19 12:51:08 UTC (rev 4777)
+++ trunk/src/utils.c 2010-03-19 17:38:27 UTC (rev 4778)
@@ -221,7 +221,7 @@
* @param filename The filename of the file to write, in locale encoding.
* @param text The text to write into the file.
*
- * @return 0 if the directory was successfully created, otherwise the @c errno of the
+ * @return 0 if the file was successfully written, otherwise the @c errno of the
* failed operation is returned.
**/
gint utils_write_file(const gchar *filename, const gchar *text)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
More information about the Commits
mailing list