get.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. Configure module for the Midnight Commander
  3. Copyright (C) 1994-2017
  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. /*** public functions **************************************************/
  27. gchar **
  28. mc_config_get_groups (const mc_config_t * mc_config, gsize * len)
  29. {
  30. gchar **ret = NULL;
  31. if (mc_config != NULL)
  32. ret = g_key_file_get_groups (mc_config->handle, len);
  33. if (ret == NULL)
  34. {
  35. ret = g_try_malloc0 (sizeof (gchar **));
  36. if (len != NULL)
  37. *len = 0;
  38. }
  39. return ret;
  40. }
  41. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. gchar **
  43. mc_config_get_keys (const mc_config_t * mc_config, const gchar * group, gsize * len)
  44. {
  45. gchar **ret = NULL;
  46. if (mc_config != NULL && group != NULL)
  47. ret = g_key_file_get_keys (mc_config->handle, group, len, NULL);
  48. if (ret == NULL)
  49. {
  50. ret = g_try_malloc0 (sizeof (gchar **));
  51. if (len != NULL)
  52. *len = 0;
  53. }
  54. return ret;
  55. }
  56. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  57. gchar *
  58. mc_config_get_string (mc_config_t * mc_config, const gchar * group,
  59. const gchar * param, const gchar * def)
  60. {
  61. GIConv conv;
  62. GString *buffer;
  63. gchar *ret;
  64. estr_t conv_res;
  65. if (!mc_config || !group || !param)
  66. return g_strdup (def);
  67. if (!mc_config_has_param (mc_config, group, param))
  68. {
  69. if (def != NULL)
  70. mc_config_set_string (mc_config, group, param, def);
  71. return g_strdup (def);
  72. }
  73. ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
  74. if (ret == NULL)
  75. ret = g_strdup (def);
  76. if (mc_global.utf8_display)
  77. return ret;
  78. conv = str_crt_conv_from ("UTF-8");
  79. if (conv == INVALID_CONV)
  80. return ret;
  81. buffer = g_string_new ("");
  82. conv_res = str_convert (conv, ret, buffer);
  83. str_close_conv (conv);
  84. if (conv_res == ESTR_FAILURE)
  85. {
  86. g_string_free (buffer, TRUE);
  87. return ret;
  88. }
  89. g_free (ret);
  90. return g_string_free (buffer, FALSE);
  91. }
  92. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  93. gchar *
  94. mc_config_get_string_raw (const mc_config_t * mc_config, const gchar * group,
  95. const gchar * param, const gchar * def)
  96. {
  97. gchar *ret;
  98. if (!mc_config || !group || !param)
  99. return g_strdup (def);
  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. ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
  107. return ret != NULL ? ret : g_strdup (def);
  108. }
  109. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  110. gboolean
  111. mc_config_get_bool (mc_config_t * mc_config, const gchar * group, const gchar * param, gboolean def)
  112. {
  113. if (!mc_config || !group || !param)
  114. return def;
  115. if (!mc_config_has_param (mc_config, group, param))
  116. {
  117. mc_config_set_bool (mc_config, group, param, def);
  118. return def;
  119. }
  120. return g_key_file_get_boolean (mc_config->handle, group, param, NULL);
  121. }
  122. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  123. int
  124. mc_config_get_int (mc_config_t * mc_config, const gchar * group, const gchar * param, int def)
  125. {
  126. if (!mc_config || !group || !param)
  127. return def;
  128. if (!mc_config_has_param (mc_config, group, param))
  129. {
  130. mc_config_set_int (mc_config, group, param, def);
  131. return def;
  132. }
  133. return g_key_file_get_integer (mc_config->handle, group, param, NULL);
  134. }
  135. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  136. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  137. gchar **
  138. mc_config_get_string_list (mc_config_t * mc_config, const gchar * group,
  139. const gchar * param, gsize * length)
  140. {
  141. if (!mc_config || !group || !param)
  142. return NULL;
  143. return g_key_file_get_string_list (mc_config->handle, group, param, length, NULL);
  144. }
  145. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  146. gboolean *
  147. mc_config_get_bool_list (mc_config_t * mc_config, const gchar * group,
  148. const gchar * param, gsize * length)
  149. {
  150. if (!mc_config || !group || !param)
  151. return NULL;
  152. return g_key_file_get_boolean_list (mc_config->handle, group, param, length, NULL);
  153. }
  154. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  155. int *
  156. mc_config_get_int_list (mc_config_t * mc_config, const gchar * group,
  157. const gchar * param, gsize * length)
  158. {
  159. if (!mc_config || !group || !param)
  160. return NULL;
  161. return g_key_file_get_integer_list (mc_config->handle, group, param, length, NULL);
  162. }
  163. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */