|
GnuCash 2.4.99
|
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) |
This file provides routines that help make it easier to use the GKeyFile functions from within Gnucash.
| 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.
| file | The name of the file to load. This should be a fully qualified path. |
| ignore_error | If true this function will ignore any problems reading the an existing file from disk. |
| return_empty_struct | If 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. |
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.
| file | The name of the file to write. This should be a fully qualified path. |
| key_file | The data to be written. |
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;
}
1.7.4