[gcab] Add glib-compat.h, fix compil with glib 2.22



commit 01c484a1dd5236525ad6d49a61f4e9abb5898a7c
Author: Marc-Andrà Lureau <marcandre lureau gmail com>
Date:   Tue Jan 15 23:24:49 2013 +0100

    Add glib-compat.h, fix compil with glib 2.22

 Makefile.am           |    2 +
 libgcab/gcab-priv.h   |    1 +
 libgcab/glib-compat.c |  171 +++++++++++++++++++++++++++++++++++++++++++++++++
 libgcab/glib-compat.h |   77 ++++++++++++++++++++++
 4 files changed, 251 insertions(+), 0 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 4b8da90..f9176fe 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -37,6 +37,8 @@ libgcab_1_0_la_SOURCES =			\
 	libgcab/gcab-priv.h			\
 	libgcab/cabinet.c			\
 	libgcab/cabinet.h			\
+	libgcab/glib-compat.c			\
+	libgcab/glib-compat.h			\
 	$(NULL)
 libgcab_1_0_la_LIBADD = -lz $(GLIB2_LIBS)
 libgcab_1_0_la_LDFLAGS =				\
diff --git a/libgcab/gcab-priv.h b/libgcab/gcab-priv.h
index 5cf8eeb..af2784f 100644
--- a/libgcab/gcab-priv.h
+++ b/libgcab/gcab-priv.h
@@ -2,6 +2,7 @@
 #define GCAB_PRIV_H
 
 #include <glib-object.h>
+#include "glib-compat.h"
 
 #include "cabinet.h"
 #include "gcab-file.h"
diff --git a/libgcab/glib-compat.c b/libgcab/glib-compat.c
new file mode 100644
index 0000000..dd39899
--- /dev/null
+++ b/libgcab/glib-compat.c
@@ -0,0 +1,171 @@
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ */
+
+#include <string.h>
+
+#include "glib-compat.h"
+
+#if !GLIB_CHECK_VERSION(2,26,0)
+static gssize
+scan_for_chars (GDataInputStream *stream,
+                gsize            *checked_out,
+                const char       *stop_chars,
+                gssize            stop_chars_len)
+{
+  GBufferedInputStream *bstream;
+  const char *buffer;
+  gsize start, end, peeked;
+  int i;
+  gsize available, checked;
+  const char *stop_char;
+  const char *stop_end;
+
+  bstream = G_BUFFERED_INPUT_STREAM (stream);
+  stop_end = stop_chars + stop_chars_len;
+
+  checked = *checked_out;
+
+  start = checked;
+  buffer = (const char *)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
+  end = available;
+  peeked = end - start;
+
+  for (i = 0; checked < available && i < peeked; i++)
+    {
+      for (stop_char = stop_chars; stop_char != stop_end; stop_char++)
+        {
+          if (buffer[i] == *stop_char)
+            return (start + i);
+        }
+    }
+
+  checked = end;
+
+  *checked_out = checked;
+  return -1;
+}
+
+char *
+g_data_input_stream_read_upto (GDataInputStream  *stream,
+                               const gchar       *stop_chars,
+                               gssize             stop_chars_len,
+                               gsize             *length,
+                               GCancellable      *cancellable,
+                               GError           **error)
+{
+  GBufferedInputStream *bstream;
+  gsize checked;
+  gssize found_pos;
+  gssize res;
+  char *data_until;
+
+  g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
+
+  if (stop_chars_len < 0)
+    stop_chars_len = strlen (stop_chars);
+
+  bstream = G_BUFFERED_INPUT_STREAM (stream);
+
+  checked = 0;
+
+  while ((found_pos = scan_for_chars (stream, &checked, stop_chars, stop_chars_len)) == -1)
+    {
+      if (g_buffered_input_stream_get_available (bstream) ==
+          g_buffered_input_stream_get_buffer_size (bstream))
+        g_buffered_input_stream_set_buffer_size (bstream,
+                                                 2 * g_buffered_input_stream_get_buffer_size (bstream));
+
+      res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
+      if (res < 0)
+        return NULL;
+      if (res == 0)
+        {
+          /* End of stream */
+          if (g_buffered_input_stream_get_available (bstream) == 0)
+            {
+              if (length)
+                *length = 0;
+              return NULL;
+            }
+          else
+            {
+              found_pos = checked;
+              break;
+            }
+        }
+    }
+
+  data_until = g_malloc (found_pos + 1);
+
+  res = g_input_stream_read (G_INPUT_STREAM (stream),
+                             data_until,
+                             found_pos,
+                             NULL, NULL);
+  if (length)
+    *length = (gsize)found_pos;
+  g_warn_if_fail (res == found_pos);
+  data_until[found_pos] = 0;
+
+  return data_until;
+}
+
+#endif
+
+#if !GLIB_CHECK_VERSION(2,28,0)
+/**
+ * g_slist_free_full: (skip)
+ * @list: a #GSList
+ * @free_func: a #GDestroyNotify
+ *
+ * Convenience method, which frees all the memory used by a #GSList,
+ * and calls the specified destroy function on every element's data
+ *
+ * Since: 2.28
+ **/
+G_GNUC_INTERNAL void
+g_slist_free_full(GSList         *list,
+                  GDestroyNotify free_func)
+{
+    GSList *el;
+
+    if (free_func) {
+        for (el = list; el ; el = g_slist_next(el)) {
+            free_func(el);
+        }
+    }
+
+    g_slist_free(list);
+}
+#endif /* 2.28 */
+
+#if !GLIB_CHECK_VERSION(2,32,0)
+GByteArray *
+g_byte_array_new_take (guint8 *data,
+                       gsize   len)
+{
+  GByteArray *array;
+
+  array = g_byte_array_new ();
+  g_assert (array->data == NULL);
+  g_assert (array->len == 0);
+
+  array->data = data;
+  array->len = len;
+
+  return array;
+}
+#endif /* 2.32 */
diff --git a/libgcab/glib-compat.h b/libgcab/glib-compat.h
new file mode 100644
index 0000000..22354c6
--- /dev/null
+++ b/libgcab/glib-compat.h
@@ -0,0 +1,77 @@
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ */
+#ifndef GLIB_COMPAT_H_
+# define GLIB_COMPAT_H_
+
+#include <glib-object.h>
+#include <gio/gio.h>
+
+#if !GLIB_CHECK_VERSION(2,26,0)
+char *   g_data_input_stream_read_upto (GDataInputStream  *stream,
+                                        const gchar       *stop_chars,
+                                        gssize             stop_chars_len,
+                                        gsize             *length,
+                                        GCancellable      *cancellable,
+                                        GError           **error);
+#endif /* glib 2.26 */
+
+#if !GLIB_CHECK_VERSION(2,28,0)
+#define g_clear_object(object_ptr) \
+  G_STMT_START {                                                             \
+    /* Only one access, please */                                            \
+    gpointer *_p = (gpointer) (object_ptr);                                  \
+    gpointer _o;                                                             \
+                                                                             \
+    do                                                                       \
+      _o = g_atomic_pointer_get (_p);                                        \
+    while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (_p, _o, NULL));\
+                                                                             \
+    if (_o)                                                                  \
+      g_object_unref (_o);                                                   \
+  } G_STMT_END
+
+void
+g_slist_free_full(GSList         *list,
+                  GDestroyNotify free_func);
+
+#endif /* glib 2.28 */
+
+#if !GLIB_CHECK_VERSION(2,32,0)
+GByteArray* g_byte_array_new_take (guint8 *data, gsize len);
+#endif /* glib 2.32 */
+
+#ifndef g_clear_pointer
+#define g_clear_pointer(pp, destroy) \
+  G_STMT_START {                                                               \
+    G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer));                       \
+    /* Only one access, please */                                              \
+    gpointer *_pp = (gpointer *) (pp);                                         \
+    gpointer _p;                                                               \
+    /* This assignment is needed to avoid a gcc warning */                     \
+    GDestroyNotify _destroy = (GDestroyNotify) (destroy);                      \
+                                                                               \
+    (void) (0 ? (gpointer) *(pp) : 0);                                         \
+    do                                                                         \
+      _p = g_atomic_pointer_get (_pp);                                         \
+    while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (_pp, _p, NULL)); \
+                                                                               \
+    if (_p)                                                                    \
+      _destroy (_p);                                                           \
+  } G_STMT_END
+#endif /* g_clear_pointer */
+
+#endif /* !GLIB_COMPAT_H_ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]