|
GnuCash 2.4.99
|
00001 /* 00002 * gnc-gkeyfile-utils.c -- utility functions for working 00003 * with GKeyFile data structures from GLib 00004 * Copyright (C) 2005 David Hampton <hampton@employees.org> 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License as 00008 * published by the Free Software Foundation; either version 2 of 00009 * the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, contact: 00018 * 00019 * Free Software Foundation Voice: +1-617-542-5942 00020 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 00021 * Boston, MA 02110-1301, USA gnu@gnu.org 00022 */ 00023 00037 #include "config.h" 00038 00039 #include <glib.h> 00040 #include <glib/gi18n.h> 00041 #include <glib/gstdio.h> 00042 #include <string.h> 00043 #include <errno.h> 00044 #include <fcntl.h> 00045 #ifdef HAVE_UNISTD_H 00046 # include <unistd.h> 00047 #else 00048 # ifdef _MSC_VER 00049 /* MSVC compatibility code */ 00050 # include <io.h> 00051 # define g_open _open 00052 # define close _close 00053 # define write _write 00054 # define ssize_t int 00055 # endif 00056 #endif 00057 00058 #include "gnc-gkeyfile-utils.h" 00059 00060 00061 GKeyFile * 00062 gnc_key_file_load_from_file (const gchar *filename, 00063 gboolean ignore_error, 00064 gboolean return_empty_struct, 00065 GError **caller_error) 00066 { 00067 GKeyFile *key_file; 00068 GError *error = NULL; 00069 00070 g_return_val_if_fail(filename != NULL, NULL); 00071 00072 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) 00073 return NULL; 00074 00075 key_file = g_key_file_new(); 00076 if (!key_file) 00077 return NULL; 00078 00079 if (g_key_file_load_from_file(key_file, filename, G_KEY_FILE_NONE, &error)) 00080 return key_file; 00081 00082 /* An error occurred */ 00083 if (!return_empty_struct) 00084 { 00085 g_key_file_free(key_file); 00086 key_file = NULL; 00087 } 00088 00089 if (!ignore_error) 00090 g_warning("Unable to read file %s: %s\n", filename, error->message); 00091 g_propagate_error(caller_error, error); 00092 return key_file; 00093 } 00094 00095 00096 gboolean 00097 gnc_key_file_save_to_file (const gchar *filename, 00098 GKeyFile *key_file, 00099 GError **error) 00100 { 00101 gchar *contents; 00102 gint fd; 00103 extern int errno; 00104 gint length; 00105 ssize_t written; 00106 gboolean success = TRUE; 00107 00108 g_return_val_if_fail(filename != NULL, FALSE); 00109 g_return_val_if_fail(key_file != NULL, FALSE); 00110 if (error) 00111 g_return_val_if_fail(*error == NULL, FALSE); 00112 00113 contents = g_key_file_to_data(key_file, NULL, NULL); 00114 length = strlen(contents); 00115 fd = g_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); 00116 if (fd == -1) 00117 { 00118 if (error) 00119 { 00120 *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno), 00121 "Cannot open file %s: %s", filename, 00122 strerror(errno)); 00123 } 00124 else 00125 { 00126 g_critical("Cannot open file %s: %s\n", filename, strerror(errno)); 00127 } 00128 g_free(contents); 00129 return FALSE; 00130 } 00131 00132 written = write(fd, contents, length); 00133 if (written == -1 ) 00134 { 00135 success = FALSE; 00136 if (error) 00137 { 00138 *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno), 00139 "Cannot write to file %s: %s", filename, 00140 strerror(errno)); 00141 } 00142 else 00143 { 00144 g_critical("Cannot write to file %s: %s\n", filename, strerror(errno)); 00145 } 00146 close(fd); 00147 } 00148 else if (written != length) 00149 { 00150 success = FALSE; 00151 if (error) 00152 { 00153 *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno), 00154 "File %s truncated (provided %d, written %d)", 00155 filename, length, (int)written); 00156 } 00157 else 00158 { 00159 g_critical("File %s truncated (provided %d, written %d)", 00160 filename, length, (int)written); 00161 } 00162 /* Ignore any error */ 00163 close(fd); 00164 } 00165 else if (close(fd) == -1) 00166 { 00167 if (error) 00168 { 00169 *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno), 00170 "Close failed for file %s: %s", filename, 00171 strerror(errno)); 00172 } 00173 else 00174 { 00175 g_warning("Close failed for file %s: %s", filename, strerror(errno)); 00176 } 00177 } 00178 g_free(contents); 00179 return success; 00180 } 00181
1.7.4