[Gnucash-changes] Add some helper functions for reading/writing glib key/value data

David Hampton hampton at cvs.gnucash.org
Fri Oct 7 18:13:20 EDT 2005


Log Message:
-----------
Add some helper functions for reading/writing glib key/value data
structures.

Tags:
----
gnucash-gnome2-dev

Modified Files:
--------------
    gnucash/src/core-utils:
        Makefile.am

Added Files:
-----------
    gnucash/src/core-utils:
        gnc-gkeyfile-utils.c
        gnc-gkeyfile-utils.h

Revision Data
-------------
--- /dev/null
+++ src/core-utils/gnc-gkeyfile-utils.c
@@ -0,0 +1,107 @@
+/*
+ * gnc-gkeyfile-utils.c -- utility functions for working
+ *              with GKeyFile data structures from GLib
+ * Copyright (C) 2005 David Hampton <hampton at employees.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652
+ * Boston, MA  02111-1307,  USA       gnu at gnu.org
+ */
+
+/** @addtogroup GLib
+    @{ */
+/** @addtogroup GKeyFile GKeyfile Utilities
+
+    This file provides routines that help make it easier to use the
+    GKeyFile functions from within Gnucash.
+
+    @{ */
+/** @file gnc-gkeyfile-utils.c
+ *  @brief GKeyFile helper routines.
+ *  @author Copyright (C) 2005 David Hampton <hampton at employees.org>
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "gnc-gkeyfile-utils.h"
+
+GKeyFile *
+gnc_key_file_load_from_file (const gchar *filename, gboolean ignore_error)
+{
+  GKeyFile *key_file;
+  GError *error = NULL;
+
+  g_return_val_if_fail(filename != NULL, NULL);
+
+  if (!g_file_test(filename, G_FILE_TEST_EXISTS))
+    return NULL;
+
+  key_file = g_key_file_new();
+  if (!key_file)
+    return NULL;
+
+  if (g_key_file_load_from_file(key_file, filename, G_KEY_FILE_NONE, &error))
+    return key_file;
+
+  /* An error occurred */
+  if (!ignore_error) {
+    g_key_file_free(key_file);
+    key_file = NULL;
+  }
+
+  g_warning("Unable to read file %s: %s\n", filename, error->message);
+  g_error_free(error);
+  return key_file;
+}
+
+
+gboolean
+gnc_key_file_save_to_file (const gchar *filename,
+			   GKeyFile *key_file)
+{
+  gchar *contents;
+  gint fd;
+  extern int errno;
+  gint length;
+  gboolean success = TRUE;
+
+  contents = g_key_file_to_data(key_file, NULL, NULL);
+  length = strlen(contents);
+  if (length) {
+    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+    if (fd != -1) {
+      write(fd, contents, length);
+      close(fd);
+    } else {
+      g_warning("Cannot open file %s: %s\n", filename, strerror(errno));
+      success = FALSE;
+    }
+  } else {
+    unlink(filename);
+  }
+  g_free(contents);
+  return success;
+}
+
+/** @} */
+/** @} */
Index: Makefile.am
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/core-utils/Makefile.am,v
retrieving revision 1.7.4.6
retrieving revision 1.7.4.7
diff -Lsrc/core-utils/Makefile.am -Lsrc/core-utils/Makefile.am -u -r1.7.4.6 -r1.7.4.7
--- src/core-utils/Makefile.am
+++ src/core-utils/Makefile.am
@@ -4,11 +4,13 @@
 libcore_utils_la_SOURCES = \
   gnc-gconf-utils.c \
   gnc-gdate-utils.c \
+  gnc-gkeyfile-utils.c \
   gnc-gobject-utils.c
 
 libcore_utils_la_LDFLAGS = -module
 
 libcore_utils_la_LIBADD = \
+  ${top_builddir}/lib/glib26/libgncglib.la \
   ${GLIB_LIBS} \
   ${GCONF_LIBS} \
   ${GTK_LIBS}
@@ -24,12 +26,14 @@
 noinst_HEADERS = \
   gnc-gconf-utils.h \
   gnc-gdate-utils.h \
+  gnc-gkeyfile-utils.h \
   gnc-gobject-utils.h \
   gw-core-utils.h
 
 EXTRA_DIST = .cvsignore ${gwmod_DATA}
 
 AM_CFLAGS = \
+  -I${top_srcdir}/lib/glib26 \
   ${G_WRAP_COMPILE_ARGS} \
   ${GUILE_INCS} \
   ${GLIB_CFLAGS} \
--- /dev/null
+++ src/core-utils/gnc-gkeyfile-utils.h
@@ -0,0 +1,79 @@
+/*
+ * gnc-gkeyfile-utils.h -- utility functions for working
+ *              with GKeyFile data structures from GLib
+ * Copyright (C) 2005 David Hampton <hampton at employees.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652
+ * Boston, MA  02111-1307,  USA       gnu at gnu.org
+ */
+
+/** @addtogroup GLib
+    @{ */
+/** @addtogroup GKeyFile GKeyfile Utilities
+
+    This file provides routines that help make it easier to use the
+    GKeyFile functions from within Gnucash.
+
+    @{ */
+/** @file gnc-gkeyfile-utils.h
+ *  @brief GKeyFile helper routines.
+ *  @author Copyright (C) 2005 David Hampton <hampton at employees.org>
+ */
+
+#ifndef GNC_GKEYFILE_UTILS_H
+#define GNC_GKEYFILE_UTILS_H
+
+#ifndef HAVE_GLIB26
+#include "gkeyfile.h"
+#endif
+
+
+/** Open and read a key/value file from disk into memory.
+ *
+ *  @param file The name of the file to load.  This should be a fully
+ *  qualified path.
+ *
+ *  @param ignore_error If true this function will ignore any problems
+ *  reading the an existing file from disk and will return a GKeyFile
+ *  structure.  Set to TRUE if performing a read/modify/write on a
+ *  file that may or may not already exist.
+ *
+ *  @return A pointer to a GKeyFile data structure, or NULL if a
+ *  (non-ignored) error occurred.
+ */
+GKeyFile *gnc_key_file_load_from_file (const gchar *file,
+				       gboolean ignore_error);
+
+
+/** Write a key/value file from memory to disk.  If there is no data
+ * to be written, this function will not create a file and will remove
+ * any exiting file.
+ *
+ *  @param file The name of the file to write.  This should be a fully
+ *  qualified path.
+ *
+ *  @param key_file The data to be written.
+ *
+ *  @return A TRUE if the data was successfully written to disk.
+ *  FALSE if there was an error.
+ */
+gboolean gnc_key_file_save_to_file (const gchar *file,
+				    GKeyFile *key_file);
+
+#endif /* GNC_GKEYFILE_UTILS_H */
+/** @} */
+/** @} */


More information about the gnucash-changes mailing list