Optimization of mcview_hexedit_save_changes() function.
[kaloumi3.git] / lib / mcconfig / common.c
blobdd0cb4ecbc8d0250691a314bb7ed72f525e978fa
1 /* Configure module for the Midnight Commander
2 Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007, 2009 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <config.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <errno.h> /* extern int errno */
28 #include "lib/global.h"
29 #include "lib/vfs/mc-vfs/vfs.h" /* mc_stat */
30 #include "lib/mcconfig.h"
32 /*** global variables **************************************************/
34 mc_config_t *mc_main_config;
35 mc_config_t *mc_panels_config;
37 /*** file scope macro definitions **************************************/
39 /*** file scope type declarations **************************************/
41 /*** file scope variables **********************************************/
43 /*** file scope functions **********************************************/
44 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
46 static gboolean
47 mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path,
48 GError **error)
50 gchar *data, *written_data;
51 gsize len, total_written;
52 gboolean ret;
53 int fd;
54 ssize_t cur_written;
56 data = g_key_file_to_data (mc_config->handle, &len, NULL);
57 if (!exist_file (ini_path)) {
58 ret = g_file_set_contents (ini_path, data, len, error);
59 g_free (data);
60 return ret;
62 mc_util_make_backup_if_possible (ini_path, "~");
64 fd = mc_open (ini_path, O_WRONLY | O_TRUNC | O_SYNC, 0);
65 if (fd == -1) {
66 g_propagate_error (error, g_error_new (mc_main_error_quark() ,0, "%s", unix_error_string (errno)));
67 g_free(data);
68 return FALSE;
71 for (written_data = data, total_written = len;
72 (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0;
73 written_data += cur_written, total_written -= cur_written);
74 mc_close (fd);
75 g_free (data);
77 if (cur_written == -1) {
78 mc_util_restore_from_backup_if_possible (ini_path, "~");
79 g_propagate_error (error, g_error_new (mc_main_error_quark() ,0, "%s", unix_error_string (errno)));
80 return FALSE;
83 mc_util_unlink_backup_if_possible (ini_path, "~");
84 return TRUE;
87 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
88 /*** public functions **************************************************/
89 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
91 mc_config_t *
92 mc_config_init (const gchar * ini_path)
94 mc_config_t *mc_config;
95 struct stat st;
97 mc_config = g_try_malloc0 (sizeof (mc_config_t));
99 if (mc_config == NULL)
100 return NULL;
102 mc_config->handle = g_key_file_new ();
103 if (mc_config->handle == NULL) {
104 g_free (mc_config);
105 return NULL;
107 if (!ini_path || !exist_file (ini_path)) {
108 return mc_config;
111 if (!mc_stat (ini_path, &st) && st.st_size) {
112 /* file present and not empty */
113 g_key_file_load_from_file (mc_config->handle, ini_path, G_KEY_FILE_KEEP_COMMENTS, NULL);
116 mc_config->ini_path = g_strdup (ini_path);
117 return mc_config;
120 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
122 void
123 mc_config_deinit (mc_config_t * mc_config)
125 if (!mc_config)
126 return;
128 g_free (mc_config->ini_path);
129 g_key_file_free (mc_config->handle);
130 g_free (mc_config);
134 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
136 gboolean
137 mc_config_has_param (mc_config_t * mc_config, const char *group, const gchar * param)
139 if (!mc_config || !group || !param)
140 return FALSE;
142 return g_key_file_has_key (mc_config->handle, group, param, NULL);
145 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
147 gboolean
148 mc_config_has_group (mc_config_t * mc_config, const char *group)
150 if (!mc_config || !group)
151 return FALSE;
153 return g_key_file_has_group (mc_config->handle, group);
156 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
159 gboolean
160 mc_config_del_param (mc_config_t * mc_config, const char *group, const gchar * param)
162 if (!mc_config || !group || !param)
163 return FALSE;
164 #if GLIB_CHECK_VERSION (2, 15, 0)
165 return g_key_file_remove_key (mc_config->handle, group, param, NULL);
166 #else
167 g_key_file_remove_key (mc_config->handle, group, param, NULL);
168 return TRUE;
169 #endif
172 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
174 gboolean
175 mc_config_del_group (mc_config_t * mc_config, const char *group)
177 if (!mc_config || !group)
178 return FALSE;
180 #if GLIB_CHECK_VERSION (2, 15, 0)
181 return g_key_file_remove_group (mc_config->handle, group, NULL);
182 #else
183 g_key_file_remove_group (mc_config->handle, group, NULL);
184 return TRUE;
185 #endif
188 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
190 gboolean
191 mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path)
193 mc_config_t *tmp_config;
194 gchar **groups, **curr_grp;
195 gchar **keys, **curr_key;
196 gchar *value;
198 if (mc_config == NULL) {
199 return FALSE;
202 tmp_config = mc_config_init (ini_path);
203 if (tmp_config == NULL)
204 return FALSE;
206 groups = mc_config_get_groups (tmp_config, NULL);
208 if (groups == NULL)
209 return FALSE;
211 for (curr_grp = groups; *curr_grp != NULL; curr_grp++) {
212 keys = mc_config_get_keys (tmp_config, *curr_grp, NULL);
213 for (curr_key = keys; *curr_key != NULL; curr_key++) {
214 value = g_key_file_get_value (tmp_config->handle, *curr_grp, *curr_key, NULL);
215 if (value == NULL)
216 continue;
218 g_key_file_set_value (mc_config->handle, *curr_grp, *curr_key, value);
219 g_free (value);
221 g_strfreev (keys);
223 g_strfreev (groups);
224 mc_config_deinit (tmp_config);
225 return TRUE;
228 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
230 gboolean
231 mc_config_save_file (mc_config_t * mc_config, GError **error)
233 if (mc_config == NULL || mc_config->ini_path == NULL) {
234 return FALSE;
236 return mc_config_new_or_override_file (mc_config, mc_config->ini_path, error);
239 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
241 gboolean
242 mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path, GError **error)
245 if (mc_config == NULL) {
246 return FALSE;
248 return mc_config_new_or_override_file (mc_config, ini_path, error);
252 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */