mc_config_get_string_raw(): if default parameter is NULL, then no any config entities...
[midnight-commander.git] / lib / mcconfig / get.c
blobef2a60825dfd190d234cc3eeb2cc4c9e3ed92ff2
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 "lib/global.h"
27 #include "lib/strutil.h"
28 #include "lib/mcconfig.h"
30 /*** global variables **************************************************/
32 /*** file scope macro definitions **************************************/
34 /*** file scope type declarations **************************************/
36 /*** file scope variables **********************************************/
38 /*** file scope functions **********************************************/
40 /*** public functions **************************************************/
42 gchar **
43 mc_config_get_groups (const mc_config_t * mc_config, gsize * len)
45 gchar **ret;
47 if (!mc_config)
49 ret = g_try_malloc0 (sizeof (gchar **));
50 if (len != NULL)
51 *len = 0;
52 return ret;
54 ret = g_key_file_get_groups (mc_config->handle, len);
55 if (ret == NULL)
57 ret = g_try_malloc0 (sizeof (gchar **));
59 return ret;
62 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
64 gchar **
65 mc_config_get_keys (const mc_config_t * mc_config, const gchar * group, gsize * len)
67 gchar **ret;
69 if (!mc_config || !group)
71 ret = g_try_malloc0 (sizeof (gchar **));
72 if (len != NULL)
73 *len = 0;
74 return ret;
76 ret = g_key_file_get_keys (mc_config->handle, group, len, NULL);
77 if (ret == NULL)
79 ret = g_try_malloc0 (sizeof (gchar **));
81 return ret;
84 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
86 gchar *
87 mc_config_get_string (mc_config_t * mc_config, const gchar * group,
88 const gchar * param, const gchar * def)
90 GIConv conv;
91 GString *buffer;
92 gchar *ret;
93 estr_t conv_res;
95 if (!mc_config || !group || !param)
96 return g_strdup (def);
98 if (!mc_config_has_param (mc_config, group, param))
100 mc_config_set_string (mc_config, group, param, def ? def : "");
101 return g_strdup (def);
104 ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
105 if (ret == NULL)
106 ret = g_strdup (def);
108 if (mc_global.utf8_display)
109 return ret;
111 conv = str_crt_conv_from ("UTF-8");
112 if (conv == INVALID_CONV)
113 return ret;
115 buffer = g_string_new ("");
116 conv_res = str_convert (conv, ret, buffer);
117 str_close_conv (conv);
119 if (conv_res == ESTR_FAILURE)
121 g_string_free (buffer, TRUE);
122 return ret;
125 g_free (ret);
127 return g_string_free (buffer, FALSE);
130 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
132 gchar *
133 mc_config_get_string_raw (const mc_config_t * mc_config, const gchar * group,
134 const gchar * param, const gchar * def)
136 gchar *ret;
138 if (!mc_config || !group || !param)
139 return g_strdup (def);
141 if (!mc_config_has_param (mc_config, group, param))
143 if (def != NULL)
144 mc_config_set_string (mc_config, group, param, def);
145 return g_strdup (def);
148 ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
150 return ret != NULL ? ret : g_strdup (def);
153 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
155 gboolean
156 mc_config_get_bool (mc_config_t * mc_config, const gchar * group, const gchar * param, gboolean def)
158 if (!mc_config || !group || !param)
159 return def;
161 if (!mc_config_has_param (mc_config, group, param))
163 mc_config_set_bool (mc_config, group, param, def);
164 return def;
167 return g_key_file_get_boolean (mc_config->handle, group, param, NULL);
170 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
173 mc_config_get_int (mc_config_t * mc_config, const gchar * group, const gchar * param, int def)
175 if (!mc_config || !group || !param)
176 return def;
178 if (!mc_config_has_param (mc_config, group, param))
180 mc_config_set_int (mc_config, group, param, def);
181 return def;
184 return g_key_file_get_integer (mc_config->handle, group, param, NULL);
187 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
188 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
190 gchar **
191 mc_config_get_string_list (mc_config_t * mc_config, const gchar * group,
192 const gchar * param, gsize * length)
194 if (!mc_config || !group || !param)
195 return NULL;
197 return g_key_file_get_string_list (mc_config->handle, group, param, length, NULL);
200 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
202 gboolean *
203 mc_config_get_bool_list (mc_config_t * mc_config, const gchar * group,
204 const gchar * param, gsize * length)
206 if (!mc_config || !group || !param)
207 return NULL;
209 return g_key_file_get_boolean_list (mc_config->handle, group, param, length, NULL);
212 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
214 int *
215 mc_config_get_int_list (mc_config_t * mc_config, const gchar * group,
216 const gchar * param, gsize * length)
218 if (!mc_config || !group || !param)
219 return NULL;
221 return g_key_file_get_integer_list (mc_config->handle, group, param, length, NULL);
224 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */