GnuCash  4.12-68-g5dc52459a1+
gnc-gkeyfile-utils.c
Go to the documentation of this file.
1 /*
2  * gnc-gkeyfile-utils.c -- utility functions for working
3  * with GKeyFile data structures from GLib
4  * Copyright (C) 2005 David Hampton <hampton@employees.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, contact:
18  *
19  * Free Software Foundation Voice: +1-617-542-5942
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
21  * Boston, MA 02110-1301, USA gnu@gnu.org
22  */
23 
37 #include <config.h>
38 
39 #include <platform.h>
40 #if PLATFORM(WINDOWS)
41 #include <windows.h>
42 #endif
43 
44 #include <glib.h>
45 #include <glib/gi18n.h>
46 #include <glib/gstdio.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #ifdef HAVE_UNISTD_H
51 # include <unistd.h>
52 #else
53 # ifdef _MSC_VER
54 /* MSVC compatibility code */
55 # include <io.h>
56 # define g_open _open
57 # define close _close
58 # define write _write
59 # define ssize_t int
60 # endif
61 #endif
62 
63 #include "gnc-gkeyfile-utils.h"
64 
65 GKeyFile *
66 gnc_key_file_load_from_file (const gchar *filename,
67  gboolean ignore_error,
68  gboolean return_empty_struct,
69  GError **caller_error)
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 }
98 
99 
100 gboolean
101 gnc_key_file_save_to_file (const gchar *filename,
102  GKeyFile *key_file,
103  GError **error)
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 }
185 
GKeyFile helper routines.
gboolean gnc_key_file_save_to_file(const gchar *filename, GKeyFile *key_file, GError **error)
Write a key/value file from memory to disk.
GKeyFile * gnc_key_file_load_from_file(const gchar *filename, gboolean ignore_error, gboolean return_empty_struct, GError **caller_error)
Open and read a key/value file from disk into memory.