get.c 6.2 KB

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