get.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /*** file scope macro definitions **************************************/
  22. /*** file scope type declarations **************************************/
  23. /*** file scope variables **********************************************/
  24. /*** file scope functions **********************************************/
  25. /*** public functions **************************************************/
  26. gchar **
  27. mc_config_get_groups (const mc_config_t * mc_config, gsize * len)
  28. {
  29. gchar **ret;
  30. if (!mc_config)
  31. {
  32. ret = g_try_malloc0 (sizeof (gchar **));
  33. if (len != NULL)
  34. *len = 0;
  35. return ret;
  36. }
  37. ret = g_key_file_get_groups (mc_config->handle, len);
  38. if (ret == NULL)
  39. {
  40. ret = g_try_malloc0 (sizeof (gchar **));
  41. }
  42. return ret;
  43. }
  44. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  45. gchar **
  46. mc_config_get_keys (const mc_config_t * mc_config, const gchar * group, gsize * len)
  47. {
  48. gchar **ret;
  49. if (!mc_config || !group)
  50. {
  51. ret = g_try_malloc0 (sizeof (gchar **));
  52. if (len != NULL)
  53. *len = 0;
  54. return ret;
  55. }
  56. ret = g_key_file_get_keys (mc_config->handle, group, len, NULL);
  57. if (ret == NULL)
  58. {
  59. ret = g_try_malloc0 (sizeof (gchar **));
  60. }
  61. return ret;
  62. }
  63. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  64. gchar *
  65. mc_config_get_string (mc_config_t * mc_config, const gchar * group,
  66. const gchar * param, const gchar * def)
  67. {
  68. GIConv conv;
  69. GString *buffer;
  70. gchar *ret;
  71. estr_t conv_res;
  72. if (!mc_config || !group || !param)
  73. return g_strdup (def);
  74. if (!mc_config_has_param (mc_config, group, param))
  75. {
  76. mc_config_set_string (mc_config, group, param, def ? def : "");
  77. return g_strdup (def);
  78. }
  79. ret = g_key_file_get_string (mc_config->handle, group, param, NULL);
  80. if (ret == NULL)
  81. ret = g_strdup (def);
  82. if (mc_global.utf8_display)
  83. return ret;
  84. conv = str_crt_conv_from ("UTF-8");
  85. if (conv == INVALID_CONV)
  86. return ret;
  87. buffer = g_string_new ("");
  88. conv_res = str_convert (conv, ret, buffer);
  89. str_close_conv (conv);
  90. if (conv_res == ESTR_FAILURE)
  91. {
  92. g_string_free (buffer, TRUE);
  93. return ret;
  94. }
  95. g_free (ret);
  96. return g_string_free (buffer, FALSE);
  97. }
  98. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  99. gchar *
  100. mc_config_get_string_raw (const mc_config_t * mc_config, const gchar * group,
  101. const gchar * param, const gchar * def)
  102. {
  103. gchar *ret;
  104. if (!mc_config || !group || !param)
  105. return g_strdup (def);
  106. if (!mc_config_has_param (mc_config, group, param))
  107. {
  108. mc_config_set_string (mc_config, group, param, def ? 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 || !group || !param)
  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 || !group || !param)
  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. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  142. gchar **
  143. mc_config_get_string_list (mc_config_t * mc_config, const gchar * group,
  144. const gchar * param, gsize * length)
  145. {
  146. if (!mc_config || !group || !param)
  147. return NULL;
  148. return g_key_file_get_string_list (mc_config->handle, group, param, length, NULL);
  149. }
  150. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  151. gboolean *
  152. mc_config_get_bool_list (mc_config_t * mc_config, const gchar * group,
  153. const gchar * param, gsize * length)
  154. {
  155. if (!mc_config || !group || !param)
  156. return NULL;
  157. return g_key_file_get_boolean_list (mc_config->handle, group, param, length, NULL);
  158. }
  159. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  160. int *
  161. mc_config_get_int_list (mc_config_t * mc_config, const gchar * group,
  162. const gchar * param, gsize * length)
  163. {
  164. if (!mc_config || !group || !param)
  165. return NULL;
  166. return g_key_file_get_integer_list (mc_config->handle, group, param, length, NULL);
  167. }
  168. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */