2 Configure module for the Midnight Commander
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software: you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation, either version 3 of the License,
12 or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/types.h>
28 #include <errno.h> /* extern int errno */
30 #include "lib/global.h"
31 #include "lib/vfs/vfs.h" /* mc_stat */
34 #include "lib/mcconfig.h"
36 /*** global variables ****************************************************************************/
38 /*** file scope macro definitions ****************************************************************/
40 /*** file scope type declarations ****************************************************************/
42 /*** forward declarations (file scope functions) *************************************************/
44 /*** file scope variables ************************************************************************/
46 /* --------------------------------------------------------------------------------------------- */
47 /*** file scope functions ************************************************************************/
48 /* --------------------------------------------------------------------------------------------- */
51 mc_config_new_or_override_file (mc_config_t
*mc_config
, const gchar
*ini_path
, GError
**mcerror
)
53 gchar
*data
, *written_data
;
54 gsize len
, total_written
;
58 vfs_path_t
*ini_vpath
;
60 mc_return_val_if_error (mcerror
, FALSE
);
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
, mcerror
);
70 mc_util_make_backup_if_possible (ini_path
, "~");
72 ini_vpath
= vfs_path_from_str (ini_path
);
73 fd
= mc_open (ini_vpath
, O_WRONLY
| O_TRUNC
, 0);
74 vfs_path_free (ini_vpath
, TRUE
);
78 mc_propagate_error (mcerror
, 0, "%s", unix_error_string (errno
));
83 for (written_data
= data
, total_written
= len
;
84 (cur_written
= mc_write (fd
, (const void *) written_data
, total_written
)) > 0;
85 written_data
+= cur_written
, total_written
-= cur_written
)
91 if (cur_written
== -1)
93 mc_util_restore_from_backup_if_possible (ini_path
, "~");
94 mc_propagate_error (mcerror
, 0, "%s", unix_error_string (errno
));
98 mc_util_unlink_backup_if_possible (ini_path
, "~");
102 /* --------------------------------------------------------------------------------------------- */
103 /*** public functions ****************************************************************************/
104 /* --------------------------------------------------------------------------------------------- */
107 mc_config_init (const gchar
*ini_path
, gboolean read_only
)
109 mc_config_t
*mc_config
;
112 mc_config
= g_try_malloc0 (sizeof (mc_config_t
));
113 if (mc_config
== NULL
)
116 mc_config
->handle
= g_key_file_new ();
117 if (mc_config
->handle
== NULL
)
123 if (ini_path
== NULL
)
126 if (exist_file (ini_path
))
130 vpath
= vfs_path_from_str (ini_path
);
131 if (mc_stat (vpath
, &st
) == 0 && st
.st_size
!= 0)
133 GKeyFileFlags flags
= G_KEY_FILE_NONE
;
136 flags
|= G_KEY_FILE_KEEP_COMMENTS
;
138 /* file exists and not empty */
139 g_key_file_load_from_file (mc_config
->handle
, ini_path
, flags
, NULL
);
141 vfs_path_free (vpath
, TRUE
);
144 mc_config
->ini_path
= g_strdup (ini_path
);
148 /* --------------------------------------------------------------------------------------------- */
151 mc_config_deinit (mc_config_t
*mc_config
)
153 if (mc_config
!= NULL
)
155 g_free (mc_config
->ini_path
);
156 g_key_file_free (mc_config
->handle
);
161 /* --------------------------------------------------------------------------------------------- */
164 mc_config_has_param (const mc_config_t
*mc_config
, const char *group
, const gchar
*param
)
169 g_return_val_if_fail (mc_config
!= NULL
, FALSE
);
171 value
= g_key_file_get_value (mc_config
->handle
, group
, param
, NULL
);
178 /* --------------------------------------------------------------------------------------------- */
181 mc_config_has_group (mc_config_t
*mc_config
, const char *group
)
183 if (mc_config
== NULL
|| group
== NULL
)
186 return g_key_file_has_group (mc_config
->handle
, group
);
189 /* --------------------------------------------------------------------------------------------- */
192 mc_config_del_key (mc_config_t
*mc_config
, const char *group
, const gchar
*param
)
194 if (mc_config
== NULL
|| group
== NULL
|| param
== NULL
)
197 return g_key_file_remove_key (mc_config
->handle
, group
, param
, NULL
);
200 /* --------------------------------------------------------------------------------------------- */
203 mc_config_del_group (mc_config_t
*mc_config
, const char *group
)
205 if (mc_config
== NULL
|| group
== NULL
)
208 return g_key_file_remove_group (mc_config
->handle
, group
, NULL
);
211 /* --------------------------------------------------------------------------------------------- */
214 mc_config_read_file (mc_config_t
*mc_config
, const gchar
*ini_path
, gboolean read_only
,
215 gboolean remove_empty
)
217 mc_config_t
*tmp_config
;
218 gchar
**groups
, **curr_grp
;
222 if (mc_config
== NULL
)
225 tmp_config
= mc_config_init (ini_path
, read_only
);
226 if (tmp_config
== NULL
)
229 groups
= mc_config_get_groups (tmp_config
, NULL
);
230 ok
= (*groups
!= NULL
);
232 for (curr_grp
= groups
; *curr_grp
!= NULL
; curr_grp
++)
234 gchar
**keys
, **curr_key
;
236 keys
= mc_config_get_keys (tmp_config
, *curr_grp
, NULL
);
238 for (curr_key
= keys
; *curr_key
!= NULL
; curr_key
++)
240 value
= g_key_file_get_value (tmp_config
->handle
, *curr_grp
, *curr_key
, NULL
);
243 if (*value
== '\0' && remove_empty
)
244 g_key_file_remove_key (mc_config
->handle
, *curr_grp
, *curr_key
, NULL
);
246 g_key_file_set_value (mc_config
->handle
, *curr_grp
, *curr_key
, value
);
249 else if (remove_empty
)
250 g_key_file_remove_key (mc_config
->handle
, *curr_grp
, *curr_key
, NULL
);
256 mc_config_deinit (tmp_config
);
261 /* --------------------------------------------------------------------------------------------- */
264 mc_config_save_file (mc_config_t
*mc_config
, GError
**mcerror
)
266 mc_return_val_if_error (mcerror
, FALSE
);
268 if (mc_config
== NULL
|| mc_config
->ini_path
== NULL
)
271 return mc_config_new_or_override_file (mc_config
, mc_config
->ini_path
, mcerror
);
274 /* --------------------------------------------------------------------------------------------- */
277 mc_config_save_to_file (mc_config_t
*mc_config
, const gchar
*ini_path
, GError
**mcerror
)
279 mc_return_val_if_error (mcerror
, FALSE
);
281 if (mc_config
== NULL
)
284 return mc_config_new_or_override_file (mc_config
, ini_path
, mcerror
);
287 /* --------------------------------------------------------------------------------------------- */