get.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. Configure module for the Midnight Commander
  3. Copyright (C) 1994-2021
  4. Free Software Foundation, Inc.
  5. This file is part of the Midnight Commander.
  6. The Midnight Commander is free software: you can redistribute it
  7. and/or modify it under the terms of the GNU General Public License as
  8. published by the Free Software Foundation, either version 3 of the License,
  9. or (at your option) any later version.
  10. The Midnight Commander 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.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <config.h>
  18. #include "lib/global.h"
  19. #include "lib/strutil.h"
  20. #include "lib/mcconfig.h"
  21. /*** global variables ****************************************************************************/
  22. /*** file scope macro definitions ****************************************************************/
  23. /*** file scope type declarations ****************************************************************/
  24. /*** file scope variables ************************************************************************/
  25. /*** file scope functions ************************************************************************/
  26. /* --------------------------------------------------------------------------------------------- */
  27. /*** file scope functions ************************************************************************/
  28. /* --------------------------------------------------------------------------------------------- */
  29. /* --------------------------------------------------------------------------------------------- */
  30. /*** public functions ****************************************************************************/
  31. /* --------------------------------------------------------------------------------------------- */
  32. gchar **
  33. mc_config_get_groups (const mc_config_t * mc_config, gsize * len)
  34. {
  35. gchar **ret = NULL;
  36. if (mc_config != NULL)
  37. ret = g_key_file_get_groups (mc_config->handle, len);
  38. if (ret == NULL)
  39. {
  40. ret = g_try_malloc0 (sizeof (gchar **));
  41. if (len != NULL)
  42. *len = 0;
  43. }
  44. return ret;
  45. }
  46. /* --------------------------------------------------------------------------------------------- */
  47. gchar **
  48. mc_config_get_keys (const mc_config_t * mc_config, const gchar * group, gsize * len)
  49. {
  50. gchar **ret = NULL;
  51. if (mc_config != NULL && group != NULL)
  52. ret = g_key_file_get_keys (mc_config->handle, group, len, NULL);
  53. if (ret == NULL)
  54. {
  55. ret = g_try_malloc0 (sizeof (gchar **));
  56. if (len != NULL)
  57. *len = 0;
  58. }
  59. return ret;
  60. }
  61. /* --------------------------------------------------------------------------------------------- */
  62. gchar *
  63. mc_config_get_string (mc_config_t * mc_config, const gchar * group,
  64. const gchar * param, const gchar * def)
  65. {
  66. GIConv conv;
  67. GString *buffer;
  68. gchar *ret;
  69. estr_t conv_res;
  70. if (mc_config == NULL || group == NULL || param == NULL)
  71. return g_strdup (def);
  72. if (!mc_config_has_param (mc_config, group, param))
  73. {
  74. if (def != NULL)
  75. mc_config_set_string (mc_config, group, param, def);
  76. return g_strdup (def);
  77. }
  78. ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
  79. if (ret == NULL)
  80. ret = g_strdup (def);
  81. if (mc_global.utf8_display)
  82. return ret;
  83. conv = str_crt_conv_from ("UTF-8");
  84. if (conv == INVALID_CONV)
  85. return ret;
  86. buffer = g_string_new ("");
  87. conv_res = str_convert (conv, ret, buffer);
  88. str_close_conv (conv);
  89. if (conv_res == ESTR_FAILURE)
  90. {
  91. g_string_free (buffer, TRUE);
  92. return ret;
  93. }
  94. g_free (ret);
  95. return g_string_free (buffer, FALSE);
  96. }
  97. /* --------------------------------------------------------------------------------------------- */
  98. gchar *
  99. mc_config_get_string_raw (mc_config_t * mc_config, const gchar * group,
  100. const gchar * param, const gchar * def)
  101. {
  102. gchar *ret;
  103. if (mc_config == NULL || group == NULL || param == NULL)
  104. return g_strdup (def);
  105. if (!mc_config_has_param (mc_config, group, param))
  106. {
  107. if (def != NULL)
  108. mc_config_set_string (mc_config, group, param, def);
  109. return g_strdup (def);
  110. }
  111. ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
  112. return ret != NULL ? ret : g_strdup (def);
  113. }
  114. /* --------------------------------------------------------------------------------------------- */
  115. gboolean
  116. mc_config_get_bool (mc_config_t * mc_config, const gchar * group, const gchar * param, gboolean def)
  117. {
  118. if (mc_config == NULL || group == NULL || param == NULL)
  119. return def;
  120. if (!mc_config_has_param (mc_config, group, param))
  121. {
  122. mc_config_set_bool (mc_config, group, param, def);
  123. return def;
  124. }
  125. return g_key_file_get_boolean (mc_config->handle, group, param, NULL);
  126. }
  127. /* --------------------------------------------------------------------------------------------- */
  128. int
  129. mc_config_get_int (mc_config_t * mc_config, const gchar * group, const gchar * param, int def)
  130. {
  131. if (mc_config == NULL || group == NULL || param == NULL)
  132. return def;
  133. if (!mc_config_has_param (mc_config, group, param))
  134. {
  135. mc_config_set_int (mc_config, group, param, def);
  136. return def;
  137. }
  138. return g_key_file_get_integer (mc_config->handle, group, param, NULL);
  139. }
  140. /* --------------------------------------------------------------------------------------------- */
  141. gchar **
  142. mc_config_get_string_list (mc_config_t * mc_config, const gchar * group,
  143. const gchar * param, gsize * length)
  144. {
  145. if (mc_config == NULL || group == NULL || param == NULL)
  146. return NULL;
  147. return g_key_file_get_string_list (mc_config->handle, group, param, length, NULL);
  148. }
  149. /* --------------------------------------------------------------------------------------------- */
  150. gboolean *
  151. mc_config_get_bool_list (mc_config_t * mc_config, const gchar * group,
  152. const gchar * param, gsize * length)
  153. {
  154. if (mc_config == NULL || group == NULL || param == NULL)
  155. return NULL;
  156. return g_key_file_get_boolean_list (mc_config->handle, group, param, length, NULL);
  157. }
  158. /* --------------------------------------------------------------------------------------------- */
  159. int *
  160. mc_config_get_int_list (mc_config_t * mc_config, const gchar * group,
  161. const gchar * param, gsize * length)
  162. {
  163. if (mc_config == NULL || group == NULL || param == NULL)
  164. return NULL;
  165. return g_key_file_get_integer_list (mc_config->handle, group, param, length, NULL);
  166. }
  167. /* --------------------------------------------------------------------------------------------- */