Optimization of ini files load.
[midnight-commander.git] / lib / mcconfig / common.c
blobea708df8980e289ae267c64949ec3d2ce4d38982
1 /*
2 Configure module for the Midnight Commander
4 Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <config.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <errno.h> /* extern int errno */
32 #include "lib/global.h"
33 #include "lib/vfs/vfs.h" /* mc_stat */
34 #include "lib/util.h"
35 #include "lib/mcconfig.h"
37 /*** global variables **************************************************/
39 mc_config_t *mc_main_config;
40 mc_config_t *mc_panels_config;
42 /*** file scope macro definitions **************************************/
44 /*** file scope type declarations **************************************/
46 /*** file scope variables **********************************************/
48 /*** file scope functions **********************************************/
49 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
51 static gboolean
52 mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path, GError ** error)
54 gchar *data, *written_data;
55 gsize len, total_written;
56 gboolean ret;
57 int fd;
58 ssize_t cur_written;
59 vfs_path_t *ini_vpath;
61 ini_vpath = vfs_path_from_str (ini_path);
62 data = g_key_file_to_data (mc_config->handle, &len, NULL);
63 if (!exist_file (ini_path))
65 ret = g_file_set_contents (ini_path, data, len, error);
66 g_free (data);
67 vfs_path_free (ini_vpath);
68 return ret;
70 mc_util_make_backup_if_possible (ini_path, "~");
72 fd = mc_open (ini_vpath, O_WRONLY | O_TRUNC, 0);
73 if (fd == -1)
75 g_propagate_error (error, g_error_new (MC_ERROR, 0, "%s", unix_error_string (errno)));
76 g_free (data);
77 vfs_path_free (ini_vpath);
78 return FALSE;
81 for (written_data = data, total_written = len;
82 (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0;
83 written_data += cur_written, total_written -= cur_written);
84 mc_close (fd);
85 g_free (data);
87 if (cur_written == -1)
89 mc_util_restore_from_backup_if_possible (ini_path, "~");
90 g_propagate_error (error, g_error_new (MC_ERROR, 0, "%s", unix_error_string (errno)));
91 vfs_path_free (ini_vpath);
92 return FALSE;
95 mc_util_unlink_backup_if_possible (ini_path, "~");
96 vfs_path_free (ini_vpath);
97 return TRUE;
100 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
101 /*** public functions **************************************************/
102 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
104 mc_config_t *
105 mc_config_init (const gchar * ini_path, gboolean read_only)
107 mc_config_t *mc_config;
108 struct stat st;
110 mc_config = g_try_malloc0 (sizeof (mc_config_t));
112 if (mc_config == NULL)
113 return NULL;
115 mc_config->handle = g_key_file_new ();
116 if (mc_config->handle == NULL)
118 g_free (mc_config);
119 return NULL;
121 if (ini_path == NULL)
122 return mc_config;
124 if (exist_file (ini_path))
126 vfs_path_t *vpath;
128 vpath = vfs_path_from_str (ini_path);
129 if (mc_stat (vpath, &st) == 0 && st.st_size != 0)
131 GKeyFileFlags flags = G_KEY_FILE_NONE;
133 if (!read_only)
134 flags |= G_KEY_FILE_KEEP_COMMENTS;
136 /* file exists and not empty */
137 g_key_file_load_from_file (mc_config->handle, ini_path, flags, NULL);
139 vfs_path_free (vpath);
142 mc_config->ini_path = g_strdup (ini_path);
143 return mc_config;
146 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
148 void
149 mc_config_deinit (mc_config_t * mc_config)
151 if (mc_config != NULL)
153 g_free (mc_config->ini_path);
154 g_key_file_free (mc_config->handle);
155 g_free (mc_config);
159 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
161 gboolean
162 mc_config_has_param (const mc_config_t * mc_config, const char *group, const gchar * param)
164 if (!mc_config || !group || !param)
165 return FALSE;
167 return g_key_file_has_key (mc_config->handle, group, param, NULL);
170 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
172 gboolean
173 mc_config_has_group (mc_config_t * mc_config, const char *group)
175 if (!mc_config || !group)
176 return FALSE;
178 return g_key_file_has_group (mc_config->handle, group);
181 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
183 gboolean
184 mc_config_del_key (mc_config_t * mc_config, const char *group, const gchar * param)
186 if (!mc_config || !group || !param)
187 return FALSE;
188 #if GLIB_CHECK_VERSION (2, 15, 0)
189 return g_key_file_remove_key (mc_config->handle, group, param, NULL);
190 #else
191 g_key_file_remove_key (mc_config->handle, group, param, NULL);
192 return TRUE;
193 #endif
196 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
198 gboolean
199 mc_config_del_group (mc_config_t * mc_config, const char *group)
201 if (!mc_config || !group)
202 return FALSE;
204 #if GLIB_CHECK_VERSION (2, 15, 0)
205 return g_key_file_remove_group (mc_config->handle, group, NULL);
206 #else
207 g_key_file_remove_group (mc_config->handle, group, NULL);
208 return TRUE;
209 #endif
212 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
214 gboolean
215 mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path, gboolean read_only,
216 gboolean remove_empty)
218 mc_config_t *tmp_config;
219 gchar **groups, **curr_grp;
220 gchar **keys, **curr_key;
221 gchar *value;
223 if (mc_config == NULL)
224 return FALSE;
226 tmp_config = mc_config_init (ini_path, read_only);
227 if (tmp_config == NULL)
228 return FALSE;
230 groups = mc_config_get_groups (tmp_config, NULL);
232 if (groups == NULL)
234 mc_config_deinit (tmp_config);
235 return FALSE;
238 for (curr_grp = groups; *curr_grp != NULL; curr_grp++)
240 keys = mc_config_get_keys (tmp_config, *curr_grp, NULL);
241 for (curr_key = keys; *curr_key != NULL; curr_key++)
243 value = g_key_file_get_value (tmp_config->handle, *curr_grp, *curr_key, NULL);
244 if (value != NULL)
246 if (*value == '\0' && remove_empty)
247 g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
248 else
249 g_key_file_set_value (mc_config->handle, *curr_grp, *curr_key, value);
250 g_free (value);
252 else if (remove_empty)
253 g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
255 g_strfreev (keys);
257 g_strfreev (groups);
258 mc_config_deinit (tmp_config);
259 return TRUE;
262 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
264 gboolean
265 mc_config_save_file (mc_config_t * mc_config, GError ** error)
267 if (mc_config == NULL || mc_config->ini_path == NULL)
269 return FALSE;
271 return mc_config_new_or_override_file (mc_config, mc_config->ini_path, error);
274 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
276 gboolean
277 mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path, GError ** error)
279 if (mc_config == NULL)
281 return FALSE;
283 return mc_config_new_or_override_file (mc_config, ini_path, error);
287 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */