Line data Source code
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 :
24 : /** @addtogroup GLib
25 : @{ */
26 : /** @addtogroup GKeyFile GKeyfile Utilities
27 :
28 : This file provides routines that help make it easier to use the
29 : GKeyFile functions from within Gnucash.
30 :
31 : @{ */
32 : /** @file gnc-gkeyfile-utils.c
33 : * @brief GKeyFile helper routines.
34 : * @author Copyright (C) 2005 David Hampton <hampton@employees.org>
35 : */
36 :
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 1 : 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 1 : GError *error = NULL;
73 :
74 1 : g_return_val_if_fail(filename != NULL, NULL);
75 :
76 1 : if (!g_file_test(filename, G_FILE_TEST_EXISTS))
77 1 : return NULL;
78 :
79 0 : key_file = g_key_file_new();
80 0 : if (!key_file)
81 0 : return NULL;
82 :
83 0 : if (g_key_file_load_from_file(key_file, filename, G_KEY_FILE_NONE, &error))
84 0 : return key_file;
85 :
86 : /* An error occurred */
87 0 : if (!return_empty_struct)
88 : {
89 0 : g_key_file_free(key_file);
90 0 : key_file = NULL;
91 : }
92 :
93 0 : if (!ignore_error)
94 0 : g_warning("Unable to read file %s: %s\n", filename, error->message);
95 0 : g_propagate_error(caller_error, error);
96 0 : return key_file;
97 : }
98 :
99 :
100 : gboolean
101 1 : 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 1 : gboolean success = TRUE;
110 :
111 1 : g_return_val_if_fail(filename != NULL, FALSE);
112 1 : g_return_val_if_fail(key_file != NULL, FALSE);
113 1 : if (error)
114 0 : g_return_val_if_fail(*error == NULL, FALSE);
115 :
116 1 : contents = g_key_file_to_data(key_file, NULL, NULL);
117 1 : g_debug ("Keyfile data:\n%s", contents);
118 1 : length = strlen(contents);
119 1 : fd = g_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
120 1 : if (fd == -1)
121 : {
122 0 : if (error)
123 : {
124 0 : *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
125 : "%s: %s", filename,
126 0 : strerror(errno));
127 : }
128 : else
129 : {
130 0 : g_critical("Cannot open file %s: %s\n", filename, strerror(errno));
131 : }
132 0 : g_free(contents);
133 0 : return FALSE;
134 : }
135 :
136 1 : written = write(fd, contents, length);
137 1 : if (written == -1 )
138 : {
139 0 : success = FALSE;
140 0 : if (error)
141 : {
142 0 : *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
143 : "Cannot write to file %s: %s", filename,
144 0 : strerror(errno));
145 : }
146 : else
147 : {
148 0 : g_critical("Cannot write to file %s: %s\n", filename, strerror(errno));
149 : }
150 0 : close(fd);
151 : }
152 1 : else if (written != length)
153 : {
154 0 : success = FALSE;
155 0 : if (error)
156 : {
157 0 : *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 0 : g_critical("File %s truncated (provided %d, written %d)",
164 : filename, length, (int)written);
165 : }
166 : /* Ignore any error */
167 0 : close(fd);
168 : }
169 1 : else if (close(fd) == -1)
170 : {
171 0 : if (error)
172 : {
173 0 : *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
174 : "Close failed for file %s: %s", filename,
175 0 : strerror(errno));
176 : }
177 : else
178 : {
179 0 : g_warning("Close failed for file %s: %s", filename, strerror(errno));
180 : }
181 : }
182 1 : g_free(contents);
183 1 : return success;
184 : }
185 :
186 : /** @} */
187 : /** @} */
|