GnuCash 2.4.99
Files | Functions
GKeyfile Utilities
GLib

Files

file  gnc-gkeyfile-utils.c
 

GKeyFile helper routines.


file  gnc-gkeyfile-utils.h
 

GKeyFile helper routines.


Functions

GKeyFile * gnc_key_file_load_from_file (const gchar *filename, gboolean ignore_error, gboolean return_empty_struct, GError **caller_error)
gboolean gnc_key_file_save_to_file (const gchar *filename, GKeyFile *key_file, GError **error)

Detailed Description

This file provides routines that help make it easier to use the GKeyFile functions from within Gnucash.


Function Documentation

GKeyFile * gnc_key_file_load_from_file ( const gchar *  file,
gboolean  ignore_error,
gboolean  return_empty_struct,
GError **  caller_error 
)

Open and read a key/value file from disk into memory.

Parameters:
fileThe name of the file to load. This should be a fully qualified path.
ignore_errorIf true this function will ignore any problems reading the an existing file from disk.
return_empty_structIf TRUE this function will always return a GKeyFile structure. Set to TRUE if performing a read/modify/write on a file that may or may not already exist.
Returns:
A pointer to a GKeyFile data structure, or NULL.

Definition at line 62 of file gnc-gkeyfile-utils.c.

{
    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 (!return_empty_struct)
    {
        g_key_file_free(key_file);
        key_file = NULL;
    }

    if (!ignore_error)
        g_warning("Unable to read file %s: %s\n", filename, error->message);
    g_propagate_error(caller_error, error);
    return key_file;
}
gboolean gnc_key_file_save_to_file ( const gchar *  file,
GKeyFile *  key_file,
GError **  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.

Parameters:
fileThe name of the file to write. This should be a fully qualified path.
key_fileThe data to be written.
Returns:
A TRUE if the data was successfully written to disk. FALSE if there was an error.

Definition at line 97 of file gnc-gkeyfile-utils.c.

{
    gchar *contents;
    gint fd;
    extern int errno;
    gint length;
    ssize_t written;
    gboolean success = TRUE;

    g_return_val_if_fail(filename != NULL, FALSE);
    g_return_val_if_fail(key_file != NULL, FALSE);
    if (error)
        g_return_val_if_fail(*error == NULL, FALSE);

    contents = g_key_file_to_data(key_file, NULL, NULL);
    length = strlen(contents);
    fd = g_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (fd == -1)
    {
        if (error)
        {
            *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
                                 "Cannot open file %s: %s", filename,
                                 strerror(errno));
        }
        else
        {
            g_critical("Cannot open file %s: %s\n", filename, strerror(errno));
        }
        g_free(contents);
        return FALSE;
    }

    written = write(fd, contents, length);
    if (written == -1 )
    {
        success = FALSE;
        if (error)
        {
            *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
                                 "Cannot write to file %s: %s", filename,
                                 strerror(errno));
        }
        else
        {
            g_critical("Cannot write to file %s: %s\n", filename, strerror(errno));
        }
        close(fd);
    }
    else if (written != length)
    {
        success = FALSE;
        if (error)
        {
            *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
                                 "File %s truncated (provided %d, written %d)",
                                 filename, length, (int)written);
        }
        else
        {
            g_critical("File %s truncated (provided %d, written %d)",
                       filename, length, (int)written);
        }
        /* Ignore any error */
        close(fd);
    }
    else if (close(fd) == -1)
    {
        if (error)
        {
            *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
                                 "Close failed for file %s: %s", filename,
                                 strerror(errno));
        }
        else
        {
            g_warning("Close failed for file %s: %s", filename, strerror(errno));
        }
    }
    g_free(contents);
    return success;
}
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines