glibcompat.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. GLIB - Library of useful routines for C programming
  3. Copyright (C) 2009-2015
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009, 2013.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file glibcompat.c
  20. * \brief Source: compatibility with older versions of glib
  21. *
  22. * Following code was copied from glib to GNU Midnight Commander to
  23. * provide compatibility with older versions of glib.
  24. */
  25. #include <config.h>
  26. #include <string.h>
  27. #include "global.h"
  28. #include "glibcompat.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  31. /*** file scope type declarations ****************************************************************/
  32. /*** file scope variables ************************************************************************/
  33. /*** file scope functions ************************************************************************/
  34. /*** public functions ****************************************************************************/
  35. /* --------------------------------------------------------------------------------------------- */
  36. #if ! GLIB_CHECK_VERSION (2, 16, 0)
  37. /**
  38. * g_strcmp0:
  39. * @str1: (allow-none): a C string or %NULL
  40. * @str2: (allow-none): another C string or %NULL
  41. *
  42. * Compares @str1 and @str2 like strcmp(). Handles %NULL
  43. * gracefully by sorting it before non-%NULL strings.
  44. * Comparing two %NULL pointers returns 0.
  45. *
  46. * Returns: an integer less than, equal to, or greater than zero, if @str1 is <, == or > than @str2.
  47. *
  48. * Since: 2.16
  49. */
  50. int
  51. g_strcmp0 (const char *str1, const char *str2)
  52. {
  53. if (!str1)
  54. return -(str1 != str2);
  55. if (!str2)
  56. return str1 != str2;
  57. return strcmp (str1, str2);
  58. }
  59. #endif /* ! GLIB_CHECK_VERSION (2, 16, 0) */
  60. /* --------------------------------------------------------------------------------------------- */
  61. #if ! GLIB_CHECK_VERSION (2, 28, 0)
  62. /**
  63. * g_slist_free_full:
  64. * @list: a pointer to a #GSList
  65. * @free_func: the function to be called to free each element's data
  66. *
  67. * Convenience method, which frees all the memory used by a #GSList, and
  68. * calls the specified destroy function on every element's data.
  69. *
  70. * Since: 2.28
  71. **/
  72. void
  73. g_slist_free_full (GSList * list, GDestroyNotify free_func)
  74. {
  75. g_slist_foreach (list, (GFunc) free_func, NULL);
  76. g_slist_free (list);
  77. }
  78. /* --------------------------------------------------------------------------------------------- */
  79. /**
  80. * g_list_free_full:
  81. * @list: a pointer to a #GList
  82. * @free_func: the function to be called to free each element's data
  83. *
  84. * Convenience method, which frees all the memory used by a #GList, and
  85. * calls the specified destroy function on every element's data.
  86. *
  87. * Since: 2.28
  88. */
  89. void
  90. g_list_free_full (GList * list, GDestroyNotify free_func)
  91. {
  92. g_list_foreach (list, (GFunc) free_func, NULL);
  93. g_list_free (list);
  94. }
  95. #endif /* ! GLIB_CHECK_VERSION (2, 28, 0) */
  96. /* --------------------------------------------------------------------------------------------- */
  97. #if ! GLIB_CHECK_VERSION (2, 22, 0)
  98. /**
  99. * Creates a new GError with the given domain and code, and a message formatted with format.
  100. * @param domain error domain
  101. * @param code error code
  102. * @param format printf()-style format for error message
  103. * @param args va_list of parameters for the message format
  104. * @returns a new GError
  105. */
  106. GError *
  107. g_error_new_valist (GQuark domain, gint code, const gchar * format, va_list args)
  108. {
  109. char *message;
  110. GError *ret_value;
  111. message = g_strdup_vprintf (format, args);
  112. ret_value = g_error_new_literal (domain, code, message);
  113. g_free (message);
  114. return ret_value;
  115. }
  116. #endif /* ! GLIB_CHECK_VERSION (2, 22, 0) */
  117. /* --------------------------------------------------------------------------------------------- */