GnuCash  5.6-150-g038405b370+
Files | Functions
GKeyfile Utilities

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

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 *file, gboolean ignore_error, gboolean return_empty_struct, GError **caller_error)
 Open and read a key/value file from disk into memory. More...
 
gboolean gnc_key_file_save_to_file (const gchar *file, GKeyFile *key_file, GError **error)
 Write a key/value file from memory to disk. More...
 

Detailed Description

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

Function Documentation

◆ gnc_key_file_load_from_file()

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 66 of file gnc-gkeyfile-utils.c.

70 {
71  GKeyFile *key_file;
72  GError *error = NULL;
73 
74  g_return_val_if_fail(filename != NULL, NULL);
75 
76  if (!g_file_test(filename, G_FILE_TEST_EXISTS))
77  return NULL;
78 
79  key_file = g_key_file_new();
80  if (!key_file)
81  return NULL;
82 
83  if (g_key_file_load_from_file(key_file, filename, G_KEY_FILE_NONE, &error))
84  return key_file;
85 
86  /* An error occurred */
87  if (!return_empty_struct)
88  {
89  g_key_file_free(key_file);
90  key_file = NULL;
91  }
92 
93  if (!ignore_error)
94  g_warning("Unable to read file %s: %s\n", filename, error->message);
95  g_propagate_error(caller_error, error);
96  return key_file;
97 }

◆ gnc_key_file_save_to_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 101 of file gnc-gkeyfile-utils.c.

104 {
105  gchar *contents;
106  gint fd;
107  gint length;
108  ssize_t written;
109  gboolean success = TRUE;
110 
111  g_return_val_if_fail(filename != NULL, FALSE);
112  g_return_val_if_fail(key_file != NULL, FALSE);
113  if (error)
114  g_return_val_if_fail(*error == NULL, FALSE);
115 
116  contents = g_key_file_to_data(key_file, NULL, NULL);
117  g_debug ("Keyfile data:\n%s", contents);
118  length = strlen(contents);
119  fd = g_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
120  if (fd == -1)
121  {
122  if (error)
123  {
124  *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
125  "%s: %s", filename,
126  strerror(errno));
127  }
128  else
129  {
130  g_critical("Cannot open file %s: %s\n", filename, strerror(errno));
131  }
132  g_free(contents);
133  return FALSE;
134  }
135 
136  written = write(fd, contents, length);
137  if (written == -1 )
138  {
139  success = FALSE;
140  if (error)
141  {
142  *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
143  "Cannot write to file %s: %s", filename,
144  strerror(errno));
145  }
146  else
147  {
148  g_critical("Cannot write to file %s: %s\n", filename, strerror(errno));
149  }
150  close(fd);
151  }
152  else if (written != length)
153  {
154  success = FALSE;
155  if (error)
156  {
157  *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
158  "File %s truncated (provided %d, written %d)",
159  filename, length, (int)written);
160  }
161  else
162  {
163  g_critical("File %s truncated (provided %d, written %d)",
164  filename, length, (int)written);
165  }
166  /* Ignore any error */
167  close(fd);
168  }
169  else if (close(fd) == -1)
170  {
171  if (error)
172  {
173  *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
174  "Close failed for file %s: %s", filename,
175  strerror(errno));
176  }
177  else
178  {
179  g_warning("Close failed for file %s: %s", filename, strerror(errno));
180  }
181  }
182  g_free(contents);
183  return success;
184 }