GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 <[email protected]>
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 [email protected]
22  */
23 
37 #include "config.h"
38 
39 #include <glib.h>
40 #include <glib/gi18n.h>
41 #include <glib/gstdio.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #else
48 # ifdef _MSC_VER
49 /* MSVC compatibility code */
50 # include <io.h>
51 # define g_open _open
52 # define close _close
53 # define write _write
54 # define ssize_t int
55 # endif
56 #endif
57 
58 #include "gnc-gkeyfile-utils.h"
59 #include "qof.h"
60 
61 /* This static indicates the debugging module that this .o belongs to. */
62 static QofLogModule log_module = G_LOG_DOMAIN;
63 
64 GKeyFile *
65 gnc_key_file_load_from_file (const gchar *filename,
66  gboolean ignore_error,
67  gboolean return_empty_struct,
68  GError **caller_error)
69 {
70  GKeyFile *key_file;
71  GError *error = NULL;
72 
73  g_return_val_if_fail(filename != NULL, NULL);
74 
75  if (!g_file_test(filename, G_FILE_TEST_EXISTS))
76  return NULL;
77 
78  key_file = g_key_file_new();
79  if (!key_file)
80  return NULL;
81 
82  if (g_key_file_load_from_file(key_file, filename, G_KEY_FILE_NONE, &error))
83  return key_file;
84 
85  /* An error occurred */
86  if (!return_empty_struct)
87  {
88  g_key_file_free(key_file);
89  key_file = NULL;
90  }
91 
92  if (!ignore_error)
93  g_warning("Unable to read file %s: %s\n", filename, error->message);
94  g_propagate_error(caller_error, error);
95  return key_file;
96 }
97 
98 
99 gboolean
100 gnc_key_file_save_to_file (const gchar *filename,
101  GKeyFile *key_file,
102  GError **error)
103 {
104  gchar *contents;
105  gint fd;
106  extern int errno;
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  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  "Cannot open file %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 
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define DEBUG(format, args...)
Definition: qoflog.h:255
GKeyFile helper routines.
gboolean gnc_key_file_save_to_file(const gchar *filename, GKeyFile *key_file, GError **error)
GKeyFile * gnc_key_file_load_from_file(const gchar *filename, gboolean ignore_error, gboolean return_empty_struct, GError **caller_error)
const gchar * QofLogModule
Definition: qofid.h:89