root/lib/mcconfig/get.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mc_config_get_groups
  2. mc_config_get_keys
  3. mc_config_get_string
  4. mc_config_get_string_raw
  5. mc_config_get_bool
  6. mc_config_get_int
  7. mc_config_get_string_list
  8. mc_config_get_bool_list
  9. mc_config_get_int_list

   1 /*
   2    Configure module for the Midnight Commander
   3 
   4    Copyright (C) 1994-2019
   5    Free Software Foundation, Inc.
   6 
   7    This file is part of the Midnight Commander.
   8 
   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.
  13 
  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.
  18 
  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/>.
  21  */
  22 
  23 #include <config.h>
  24 
  25 #include "lib/global.h"
  26 #include "lib/strutil.h"
  27 
  28 #include "lib/mcconfig.h"
  29 
  30 /*** global variables ****************************************************************************/
  31 
  32 /*** file scope macro definitions ****************************************************************/
  33 
  34 /*** file scope type declarations ****************************************************************/
  35 
  36 /*** file scope variables ************************************************************************/
  37 
  38 /*** file scope functions ************************************************************************/
  39 
  40 /* --------------------------------------------------------------------------------------------- */
  41 /*** file scope functions ************************************************************************/
  42 /* --------------------------------------------------------------------------------------------- */
  43 
  44 /* --------------------------------------------------------------------------------------------- */
  45 /*** public functions ****************************************************************************/
  46 /* --------------------------------------------------------------------------------------------- */
  47 
  48 gchar **
  49 mc_config_get_groups (const mc_config_t * mc_config, gsize * len)
     /* [previous][next][first][last][top][bottom][index][help]  */
  50 {
  51     gchar **ret = NULL;
  52 
  53     if (mc_config != NULL)
  54         ret = g_key_file_get_groups (mc_config->handle, len);
  55 
  56     if (ret == NULL)
  57     {
  58         ret = g_try_malloc0 (sizeof (gchar **));
  59         if (len != NULL)
  60             *len = 0;
  61     }
  62 
  63     return ret;
  64 }
  65 
  66 /* --------------------------------------------------------------------------------------------- */
  67 
  68 gchar **
  69 mc_config_get_keys (const mc_config_t * mc_config, const gchar * group, gsize * len)
     /* [previous][next][first][last][top][bottom][index][help]  */
  70 {
  71     gchar **ret = NULL;
  72 
  73     if (mc_config != NULL && group != NULL)
  74         ret = g_key_file_get_keys (mc_config->handle, group, len, NULL);
  75 
  76     if (ret == NULL)
  77     {
  78         ret = g_try_malloc0 (sizeof (gchar **));
  79         if (len != NULL)
  80             *len = 0;
  81     }
  82 
  83     return ret;
  84 }
  85 
  86 /* --------------------------------------------------------------------------------------------- */
  87 
  88 gchar *
  89 mc_config_get_string (mc_config_t * mc_config, const gchar * group,
     /* [previous][next][first][last][top][bottom][index][help]  */
  90                       const gchar * param, const gchar * def)
  91 {
  92     GIConv conv;
  93     GString *buffer;
  94     gchar *ret;
  95     estr_t conv_res;
  96 
  97     if (mc_config == NULL || group == NULL || param == NULL)
  98         return g_strdup (def);
  99 
 100     if (!mc_config_has_param (mc_config, group, param))
 101     {
 102         if (def != NULL)
 103             mc_config_set_string (mc_config, group, param, def);
 104         return g_strdup (def);
 105     }
 106 
 107     ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
 108     if (ret == NULL)
 109         ret = g_strdup (def);
 110 
 111     if (mc_global.utf8_display)
 112         return ret;
 113 
 114     conv = str_crt_conv_from ("UTF-8");
 115     if (conv == INVALID_CONV)
 116         return ret;
 117 
 118     buffer = g_string_new ("");
 119     conv_res = str_convert (conv, ret, buffer);
 120     str_close_conv (conv);
 121 
 122     if (conv_res == ESTR_FAILURE)
 123     {
 124         g_string_free (buffer, TRUE);
 125         return ret;
 126     }
 127 
 128     g_free (ret);
 129 
 130     return g_string_free (buffer, FALSE);
 131 }
 132 
 133 /* --------------------------------------------------------------------------------------------- */
 134 
 135 gchar *
 136 mc_config_get_string_raw (mc_config_t * mc_config, const gchar * group,
     /* [previous][next][first][last][top][bottom][index][help]  */
 137                           const gchar * param, const gchar * def)
 138 {
 139     gchar *ret;
 140 
 141     if (mc_config == NULL || group == NULL || param == NULL)
 142         return g_strdup (def);
 143 
 144     if (!mc_config_has_param (mc_config, group, param))
 145     {
 146         if (def != NULL)
 147             mc_config_set_string (mc_config, group, param, def);
 148         return g_strdup (def);
 149     }
 150 
 151     ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
 152 
 153     return ret != NULL ? ret : g_strdup (def);
 154 }
 155 
 156 /* --------------------------------------------------------------------------------------------- */
 157 
 158 gboolean
 159 mc_config_get_bool (mc_config_t * mc_config, const gchar * group, const gchar * param, gboolean def)
     /* [previous][next][first][last][top][bottom][index][help]  */
 160 {
 161     if (mc_config == NULL || group == NULL || param == NULL)
 162         return def;
 163 
 164     if (!mc_config_has_param (mc_config, group, param))
 165     {
 166         mc_config_set_bool (mc_config, group, param, def);
 167         return def;
 168     }
 169 
 170     return g_key_file_get_boolean (mc_config->handle, group, param, NULL);
 171 }
 172 
 173 /* --------------------------------------------------------------------------------------------- */
 174 
 175 int
 176 mc_config_get_int (mc_config_t * mc_config, const gchar * group, const gchar * param, int def)
     /* [previous][next][first][last][top][bottom][index][help]  */
 177 {
 178     if (mc_config == NULL || group == NULL || param == NULL)
 179         return def;
 180 
 181     if (!mc_config_has_param (mc_config, group, param))
 182     {
 183         mc_config_set_int (mc_config, group, param, def);
 184         return def;
 185     }
 186 
 187     return g_key_file_get_integer (mc_config->handle, group, param, NULL);
 188 }
 189 
 190 /* --------------------------------------------------------------------------------------------- */
 191 
 192 gchar **
 193 mc_config_get_string_list (mc_config_t * mc_config, const gchar * group,
     /* [previous][next][first][last][top][bottom][index][help]  */
 194                            const gchar * param, gsize * length)
 195 {
 196     if (mc_config == NULL || group == NULL || param == NULL)
 197         return NULL;
 198 
 199     return g_key_file_get_string_list (mc_config->handle, group, param, length, NULL);
 200 }
 201 
 202 /* --------------------------------------------------------------------------------------------- */
 203 
 204 gboolean *
 205 mc_config_get_bool_list (mc_config_t * mc_config, const gchar * group,
     /* [previous][next][first][last][top][bottom][index][help]  */
 206                          const gchar * param, gsize * length)
 207 {
 208     if (mc_config == NULL || group == NULL || param == NULL)
 209         return NULL;
 210 
 211     return g_key_file_get_boolean_list (mc_config->handle, group, param, length, NULL);
 212 }
 213 
 214 /* --------------------------------------------------------------------------------------------- */
 215 
 216 int *
 217 mc_config_get_int_list (mc_config_t * mc_config, const gchar * group,
     /* [previous][next][first][last][top][bottom][index][help]  */
 218                         const gchar * param, gsize * length)
 219 {
 220     if (mc_config == NULL || group == NULL || param == NULL)
 221         return NULL;
 222 
 223     return g_key_file_get_integer_list (mc_config->handle, group, param, length, NULL);
 224 }
 225 
 226 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */