Moved strecape.[ch] into library
[midnight-commander.git] / lib / mcconfig / get.c
blobf7c468e05c8b07820201661f74037ecc72d6a641
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 "lib/global.h"
23 #include "lib/strutil.h"
24 #include "lib/mcconfig.h"
26 /*** global variables **************************************************/
28 extern int utf8_display;
30 /*** file scope macro definitions **************************************/
32 /*** file scope type declarations **************************************/
34 /*** file scope variables **********************************************/
36 /*** file scope functions **********************************************/
38 /*** public functions **************************************************/
40 gchar **
41 mc_config_get_groups (mc_config_t * mc_config, gsize * len)
43 gchar **ret;
45 if (!mc_config)
47 ret = g_try_malloc0 (sizeof (gchar **));
48 if (len != NULL)
49 *len=0;
50 return ret;
52 ret = g_key_file_get_groups (mc_config->handle, len);
53 if (ret == NULL)
55 ret = g_try_malloc0 (sizeof (gchar **));
57 return ret;
60 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
62 gchar **
63 mc_config_get_keys (mc_config_t * mc_config, const gchar * group, gsize * len)
65 gchar **ret;
67 if (!mc_config || !group)
69 ret = g_try_malloc0 (sizeof (gchar **));
70 if (len != NULL)
71 *len=0;
72 return ret;
74 ret = g_key_file_get_keys (mc_config->handle, group, len, NULL);
75 if (ret == NULL)
77 ret = g_try_malloc0 (sizeof (gchar **));
79 return ret;
82 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
84 gchar *
85 mc_config_get_string (mc_config_t * mc_config, const gchar * group,
86 const gchar * param, const gchar * def)
88 GIConv conv;
89 GString *buffer;
90 gchar *ret;
91 const char *_system_codepage = str_detect_termencoding();
93 if (!mc_config || !group || !param)
94 return def ? g_strdup (def) : NULL;
96 if (! mc_config_has_param(mc_config, group, param))
98 mc_config_set_string (mc_config, group, param, def ? def : "");
99 return def ? g_strdup (def) : NULL;
102 ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
104 if (!ret)
105 ret = def ? g_strdup (def) : NULL;
107 if (str_isutf8 (_system_codepage))
108 return ret;
110 conv = g_iconv_open (_system_codepage, "UTF-8");
111 if (conv == INVALID_CONV)
112 return ret;
114 buffer = g_string_new ("");
116 if (str_convert (conv, ret, buffer) == ESTR_FAILURE)
118 g_string_free(buffer, TRUE);
119 str_close_conv (conv);
120 return ret;
122 str_close_conv (conv);
124 g_free(ret);
126 return g_string_free(buffer, FALSE);
129 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
131 gchar *
132 mc_config_get_string_raw (mc_config_t * mc_config, const gchar * group,
133 const gchar * param, const gchar * def)
135 gchar *ret;
137 if (!mc_config || !group || !param)
138 return def ? g_strdup (def) : NULL;
140 if (! mc_config_has_param(mc_config, group, param))
142 mc_config_set_string (mc_config, group, param, def ? def : "");
143 return def ? g_strdup (def) : NULL;
146 ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
148 if (!ret)
149 ret = def ? g_strdup (def) : NULL;
151 return ret;
154 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
156 gboolean
157 mc_config_get_bool (mc_config_t * mc_config, const gchar * group,
158 const gchar * param, gboolean def)
160 if (!mc_config || !group || !param)
161 return def;
163 if (! mc_config_has_param(mc_config, group, param))
165 mc_config_set_bool (mc_config, group, param, def);
166 return def;
169 return g_key_file_get_boolean (mc_config->handle, group, param, NULL);
172 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
175 mc_config_get_int (mc_config_t * mc_config, const gchar * group,
176 const gchar * param, int def)
178 if (!mc_config || !group || !param)
179 return def;
181 if (! mc_config_has_param(mc_config, group, param))
183 mc_config_set_int (mc_config, group, param, def);
184 return def;
187 return g_key_file_get_integer (mc_config->handle, group, param, NULL);
190 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
191 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
193 gchar **
194 mc_config_get_string_list (mc_config_t * mc_config, const gchar * group,
195 const gchar * param, gsize * length)
197 if (!mc_config || !group || !param)
198 return NULL;
200 return g_key_file_get_string_list (mc_config->handle, group, param,
201 length, NULL);
204 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
206 gboolean *
207 mc_config_get_bool_list (mc_config_t * mc_config, const gchar * group,
208 const gchar * param, gsize * length)
210 if (!mc_config || !group || !param)
211 return NULL;
213 return g_key_file_get_boolean_list (mc_config->handle, group, param,
214 length, NULL);
217 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
219 int *
220 mc_config_get_int_list (mc_config_t * mc_config, const gchar * group,
221 const gchar * param, gsize * length)
223 if (!mc_config || !group || !param)
224 return NULL;
226 return g_key_file_get_integer_list (mc_config->handle, group, param,
227 length, NULL);
230 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */