get.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Configure module for the Midnight Commander
  3. Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
  4. 2007, 2009, 2011
  5. The Free Software Foundation, Inc.
  6. This file is part of the Midnight Commander.
  7. The Midnight Commander is free software: you can redistribute it
  8. and/or modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation, either version 3 of the License,
  10. or (at your option) any later version.
  11. The Midnight Commander is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. 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, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config.h>
  19. #include "lib/global.h"
  20. #include "lib/strutil.h"
  21. #include "lib/mcconfig.h"
  22. /*** global variables **************************************************/
  23. /*** file scope macro definitions **************************************/
  24. /*** file scope type declarations **************************************/
  25. /*** file scope variables **********************************************/
  26. /*** file scope functions **********************************************/
  27. /*** public functions **************************************************/
  28. gchar **
  29. mc_config_get_groups (const mc_config_t * mc_config, gsize * len)
  30. {
  31. gchar **ret;
  32. if (!mc_config)
  33. {
  34. ret = g_try_malloc0 (sizeof (gchar **));
  35. if (len != NULL)
  36. *len = 0;
  37. return ret;
  38. }
  39. ret = g_key_file_get_groups (mc_config->handle, len);
  40. if (ret == NULL)
  41. {
  42. ret = g_try_malloc0 (sizeof (gchar **));
  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;
  51. if (!mc_config || !group)
  52. {
  53. ret = g_try_malloc0 (sizeof (gchar **));
  54. if (len != NULL)
  55. *len = 0;
  56. return ret;
  57. }
  58. ret = g_key_file_get_keys (mc_config->handle, group, len, NULL);
  59. if (ret == NULL)
  60. {
  61. ret = g_try_malloc0 (sizeof (gchar **));
  62. }
  63. return ret;
  64. }
  65. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  66. gchar *
  67. mc_config_get_string (mc_config_t * mc_config, const gchar * group,
  68. const gchar * param, const gchar * def)
  69. {
  70. GIConv conv;
  71. GString *buffer;
  72. gchar *ret;
  73. estr_t conv_res;
  74. if (!mc_config || !group || !param)
  75. return g_strdup (def);
  76. if (!mc_config_has_param (mc_config, group, param))
  77. {
  78. mc_config_set_string (mc_config, group, param, def ? 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. mc_config_set_string (mc_config, group, param, def ? def : "");
  111. return g_strdup (def);
  112. }
  113. ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
  114. return ret != NULL ? ret : g_strdup (def);
  115. }
  116. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  117. gboolean
  118. mc_config_get_bool (mc_config_t * mc_config, const gchar * group, const gchar * param, gboolean def)
  119. {
  120. if (!mc_config || !group || !param)
  121. return def;
  122. if (!mc_config_has_param (mc_config, group, param))
  123. {
  124. mc_config_set_bool (mc_config, group, param, def);
  125. return def;
  126. }
  127. return g_key_file_get_boolean (mc_config->handle, group, param, NULL);
  128. }
  129. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  130. int
  131. mc_config_get_int (mc_config_t * mc_config, const gchar * group, const gchar * param, int def)
  132. {
  133. if (!mc_config || !group || !param)
  134. return def;
  135. if (!mc_config_has_param (mc_config, group, param))
  136. {
  137. mc_config_set_int (mc_config, group, param, def);
  138. return def;
  139. }
  140. return g_key_file_get_integer (mc_config->handle, group, param, NULL);
  141. }
  142. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  143. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  144. gchar **
  145. mc_config_get_string_list (mc_config_t * mc_config, const gchar * group,
  146. const gchar * param, gsize * length)
  147. {
  148. if (!mc_config || !group || !param)
  149. return NULL;
  150. return g_key_file_get_string_list (mc_config->handle, group, param, length, NULL);
  151. }
  152. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  153. gboolean *
  154. mc_config_get_bool_list (mc_config_t * mc_config, const gchar * group,
  155. const gchar * param, gsize * length)
  156. {
  157. if (!mc_config || !group || !param)
  158. return NULL;
  159. return g_key_file_get_boolean_list (mc_config->handle, group, param, length, NULL);
  160. }
  161. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  162. int *
  163. mc_config_get_int_list (mc_config_t * mc_config, const gchar * group,
  164. const gchar * param, gsize * length)
  165. {
  166. if (!mc_config || !group || !param)
  167. return NULL;
  168. return g_key_file_get_integer_list (mc_config->handle, group, param, length, NULL);
  169. }
  170. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */