This file provides routines that help make it easier to use the GKeyFile functions from within Gnucash.
Files | |
| file | gnc-gkeyfile-utils.c |
| GKeyFile helper routines. | |
| file | gnc-gkeyfile-utils.h |
| GKeyFile helper routines. | |
Functions | |
| gdouble | g_key_file_get_double (GKeyFile *key_file, const gchar *group_name, const gchar *key, GError **error) |
| void | g_key_file_set_double (GKeyFile *key_file, const gchar *group_name, const gchar *key, gdouble value) |
| gdouble * | g_key_file_get_double_list (GKeyFile *key_file, const gchar *group_name, const gchar *key, gsize *length, GError **error) |
| void | g_key_file_set_double_list (GKeyFile *key_file, const gchar *group_name, const gchar *key, gdouble list[], gsize length) |
| 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) |
| 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 266 of file gnc-gkeyfile-utils.c.
00270 { 00271 GKeyFile *key_file; 00272 GError *error = NULL; 00273 00274 g_return_val_if_fail(filename != NULL, NULL); 00275 00276 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) 00277 return NULL; 00278 00279 key_file = g_key_file_new(); 00280 if (!key_file) 00281 return NULL; 00282 00283 if (g_key_file_load_from_file(key_file, filename, G_KEY_FILE_NONE, &error)) 00284 return key_file; 00285 00286 /* An error occurred */ 00287 if (!return_empty_struct) { 00288 g_key_file_free(key_file); 00289 key_file = NULL; 00290 } 00291 00292 if (!ignore_error) 00293 g_warning("Unable to read file %s: %s\n", filename, error->message); 00294 g_propagate_error(caller_error, error); 00295 return key_file; 00296 }
| 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 300 of file gnc-gkeyfile-utils.c.
00303 { 00304 gchar *contents; 00305 gint fd; 00306 extern int errno; 00307 gint length; 00308 ssize_t written; 00309 gboolean success = TRUE; 00310 00311 g_return_val_if_fail(filename != NULL, FALSE); 00312 g_return_val_if_fail(key_file != NULL, FALSE); 00313 if (error) 00314 g_return_val_if_fail(*error == NULL, FALSE); 00315 00316 contents = g_key_file_to_data(key_file, NULL, NULL); 00317 length = strlen(contents); 00318 fd = g_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); 00319 if (fd == -1) { 00320 if (error) { 00321 *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno), 00322 "Cannot open file %s: %s", filename, 00323 strerror(errno)); 00324 } else { 00325 g_critical("Cannot open file %s: %s\n", filename, strerror(errno)); 00326 } 00327 g_free(contents); 00328 return FALSE; 00329 } 00330 00331 written = write(fd, contents, length); 00332 if (written == -1 ) { 00333 success = FALSE; 00334 if (error) { 00335 *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno), 00336 "Cannot write to file %s: %s", filename, 00337 strerror(errno)); 00338 } else { 00339 g_critical("Cannot write to file %s: %s\n", filename, strerror(errno)); 00340 } 00341 close(fd); 00342 } else if (written != length) { 00343 success = FALSE; 00344 if (error) { 00345 *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno), 00346 "File %s truncated (provided %d, written %d)", 00347 filename, length, (int)written); 00348 } else { 00349 g_critical("File %s truncated (provided %d, written %d)", 00350 filename, length, (int)written); 00351 } 00352 /* Ignore any error */ 00353 close(fd); 00354 } else if (close(fd) == -1) { 00355 if (error) { 00356 *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno), 00357 "Close failed for file %s: %s", filename, 00358 strerror(errno)); 00359 } else { 00360 g_warning("Close failed for file %s: %s", filename, strerror(errno)); 00361 } 00362 } 00363 g_free(contents); 00364 return success; 00365 }
1.5.2