Merge branch '4561_tar_segfault'
[midnight-commander.git] / lib / mcconfig / set.c
blobded0bb856ec1ec3f4a675a9142b43f5dd2bdde0c
1 /*
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/>.
23 #include <config.h>
25 #include "lib/global.h"
26 #include "lib/strutil.h"
28 #include "lib/mcconfig.h"
30 /*** global variables ****************************************************************************/
32 /*** file scope macro definitions ****************************************************************/
34 /*** file scope type declarations ****************************************************************/
36 /*** forward declarations (file scope functions) *************************************************/
38 /*** file scope variables ************************************************************************/
40 /* --------------------------------------------------------------------------------------------- */
41 /*** file scope functions ************************************************************************/
42 /* --------------------------------------------------------------------------------------------- */
44 static gchar *
45 mc_config_normalize_before_save (const gchar *value)
47 GIConv conv;
48 GString *buffer;
49 gboolean ok;
51 if (mc_global.utf8_display)
52 return g_strdup (value);
54 conv = str_crt_conv_to ("UTF-8");
55 if (conv == INVALID_CONV)
56 return g_strdup (value);
58 buffer = g_string_new ("");
60 ok = (str_convert (conv, value, buffer) != ESTR_FAILURE);
61 str_close_conv (conv);
63 if (!ok)
65 g_string_free (buffer, TRUE);
66 return g_strdup (value);
69 return g_string_free (buffer, FALSE);
72 /* --------------------------------------------------------------------------------------------- */
73 /*** public functions ****************************************************************************/
74 /* --------------------------------------------------------------------------------------------- */
76 void
77 mc_config_set_string_raw (mc_config_t *mc_config, const gchar *group,
78 const gchar *param, const gchar *value)
80 if (mc_config != NULL && group != NULL && param != NULL && value != NULL)
81 g_key_file_set_string (mc_config->handle, group, param, value);
84 /* --------------------------------------------------------------------------------------------- */
86 void
87 mc_config_set_string_raw_value (mc_config_t *mc_config, const gchar *group,
88 const gchar *param, const gchar *value)
90 if (mc_config != NULL && group != NULL && param != NULL && value != NULL)
91 g_key_file_set_value (mc_config->handle, group, param, value);
94 /* --------------------------------------------------------------------------------------------- */
96 void
97 mc_config_set_string (mc_config_t *mc_config, const gchar *group,
98 const gchar *param, const gchar *value)
100 if (mc_config != NULL && group != NULL && param != NULL && value != NULL)
102 gchar *buffer;
104 buffer = mc_config_normalize_before_save (value);
105 g_key_file_set_string (mc_config->handle, group, param, buffer);
106 g_free (buffer);
110 /* --------------------------------------------------------------------------------------------- */
112 void
113 mc_config_set_bool (mc_config_t *mc_config, const gchar *group, const gchar *param, gboolean value)
115 if (mc_config != NULL && group != NULL && param != NULL)
116 g_key_file_set_boolean (mc_config->handle, group, param, value);
119 /* --------------------------------------------------------------------------------------------- */
121 void
122 mc_config_set_int (mc_config_t *mc_config, const gchar *group, const gchar *param, int value)
124 if (mc_config != NULL && group != NULL && param != NULL)
125 g_key_file_set_integer (mc_config->handle, group, param, value);
128 /* --------------------------------------------------------------------------------------------- */
130 void
131 mc_config_set_string_list (mc_config_t *mc_config, const gchar *group,
132 const gchar *param, const gchar *const value[], gsize length)
134 if (mc_config != NULL && group != NULL && param != NULL && value != NULL && length != 0)
135 g_key_file_set_string_list (mc_config->handle, group, param, value, length);
138 /* --------------------------------------------------------------------------------------------- */
140 void
141 mc_config_set_bool_list (mc_config_t *mc_config, const gchar *group,
142 const gchar *param, gboolean value[], gsize length)
144 if (mc_config != NULL && group != NULL && param != NULL && value != NULL && length != 0)
145 g_key_file_set_boolean_list (mc_config->handle, group, param, value, length);
148 /* --------------------------------------------------------------------------------------------- */
150 void
151 mc_config_set_int_list (mc_config_t *mc_config, const gchar *group,
152 const gchar *param, int value[], gsize length)
154 if (mc_config != NULL && group != NULL && param != NULL && value != NULL && length != 0)
155 g_key_file_set_integer_list (mc_config->handle, group, param, value, length);
158 /* --------------------------------------------------------------------------------------------- */