get.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Configure module for the Midnight Commander
  2. Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
  3. 2007, 2009 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. */
  16. #include <config.h>
  17. #include "lib/global.h"
  18. #include "lib/strutil.h"
  19. #include "lib/mcconfig.h"
  20. /*** global variables **************************************************/
  21. extern int utf8_display;
  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 (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 (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. const char *_system_codepage = str_detect_termencoding();
  73. if (!mc_config || !group || !param)
  74. return def ? g_strdup (def) : NULL;
  75. if (! mc_config_has_param(mc_config, group, param))
  76. {
  77. mc_config_set_string (mc_config, group, param, def ? def : "");
  78. return def ? g_strdup (def) : NULL;
  79. }
  80. ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
  81. if (!ret)
  82. ret = def ? g_strdup (def) : NULL;
  83. if (str_isutf8 (_system_codepage))
  84. return ret;
  85. conv = g_iconv_open (_system_codepage, "UTF-8");
  86. if (conv == INVALID_CONV)
  87. return ret;
  88. buffer = g_string_new ("");
  89. if (str_convert (conv, ret, buffer) == ESTR_FAILURE)
  90. {
  91. g_string_free(buffer, TRUE);
  92. str_close_conv (conv);
  93. return ret;
  94. }
  95. str_close_conv (conv);
  96. g_free(ret);
  97. return g_string_free(buffer, FALSE);
  98. }
  99. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  100. gchar *
  101. mc_config_get_string_raw (mc_config_t * mc_config, const gchar * group,
  102. const gchar * param, const gchar * def)
  103. {
  104. gchar *ret;
  105. if (!mc_config || !group || !param)
  106. return def ? g_strdup (def) : NULL;
  107. if (! mc_config_has_param(mc_config, group, param))
  108. {
  109. mc_config_set_string (mc_config, group, param, def ? def : "");
  110. return def ? g_strdup (def) : NULL;
  111. }
  112. ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
  113. if (!ret)
  114. ret = def ? g_strdup (def) : NULL;
  115. return ret;
  116. }
  117. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  118. gboolean
  119. mc_config_get_bool (mc_config_t * mc_config, const gchar * group,
  120. const gchar * param, gboolean def)
  121. {
  122. if (!mc_config || !group || !param)
  123. return def;
  124. if (! mc_config_has_param(mc_config, group, param))
  125. {
  126. mc_config_set_bool (mc_config, group, param, def);
  127. return def;
  128. }
  129. return g_key_file_get_boolean (mc_config->handle, group, param, NULL);
  130. }
  131. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  132. int
  133. mc_config_get_int (mc_config_t * mc_config, const gchar * group,
  134. const gchar * param, int def)
  135. {
  136. if (!mc_config || !group || !param)
  137. return def;
  138. if (! mc_config_has_param(mc_config, group, param))
  139. {
  140. mc_config_set_int (mc_config, group, param, def);
  141. return def;
  142. }
  143. return g_key_file_get_integer (mc_config->handle, group, param, NULL);
  144. }
  145. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  146. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  147. gchar **
  148. mc_config_get_string_list (mc_config_t * mc_config, const gchar * group,
  149. const gchar * param, gsize * length)
  150. {
  151. if (!mc_config || !group || !param)
  152. return NULL;
  153. return g_key_file_get_string_list (mc_config->handle, group, param,
  154. length, NULL);
  155. }
  156. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  157. gboolean *
  158. mc_config_get_bool_list (mc_config_t * mc_config, const gchar * group,
  159. const gchar * param, gsize * length)
  160. {
  161. if (!mc_config || !group || !param)
  162. return NULL;
  163. return g_key_file_get_boolean_list (mc_config->handle, group, param,
  164. length, NULL);
  165. }
  166. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  167. int *
  168. mc_config_get_int_list (mc_config_t * mc_config, const gchar * group,
  169. const gchar * param, gsize * length)
  170. {
  171. if (!mc_config || !group || !param)
  172. return NULL;
  173. return g_key_file_get_integer_list (mc_config->handle, group, param,
  174. length, NULL);
  175. }
  176. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */