[Cogl] [PATCH 3/8] Add a wrapper for 'memmem'

Neil Roberts neil at linux.intel.com
Thu Aug 9 09:08:48 PDT 2012


memmem is a GNU libc extension that works like strstr except that the
size of the needle and the haystack are passed into the function
instead of using null-terminated strings.

This patch adds a wrapper function called 'cogl_util_memmem' so that
we can use this function. There is a configure check and if the
function is not available then a fallback implementation will be used.
Otherwise cogl_util_memmem is just defined to memmem.
---
 cogl/cogl-util.c | 24 ++++++++++++++++++++++++
 cogl/cogl-util.h | 10 ++++++++++
 configure.ac     |  3 +++
 3 files changed, 37 insertions(+)

diff --git a/cogl/cogl-util.c b/cogl/cogl-util.c
index e8ffb04..9ecc34c 100644
--- a/cogl/cogl-util.c
+++ b/cogl/cogl-util.c
@@ -25,6 +25,8 @@
 #include "config.h"
 #endif
 
+#include <string.h>
+
 #include "cogl-util.h"
 #include "cogl-private.h"
 
@@ -252,4 +254,26 @@ _cogl_util_pixel_format_from_masks (unsigned long r_mask,
   return image_format;
 }
 
+#ifndef HAVE_MEMMEM
+
+char *
+_cogl_util_memmem (const void *haystack,
+                   size_t haystack_len,
+                   const void *needle,
+                   size_t needle_len)
+{
+  size_t i;
+
+  if (needle_len > haystack_len)
+    return NULL;
+
+  for (i = 0; i <= haystack_len - needle_len; i++)
+    if (!memcmp ((const char *) haystack + i, needle, needle_len))
+      return (char *) haystack + i;
+
+  return NULL;
+}
+
+#endif /* HAVE_MEMMEM */
+
 #endif /* _COGL_IN_TEST_BITMASK */
diff --git a/cogl/cogl-util.h b/cogl/cogl-util.h
index 67861bc..4fb4227 100644
--- a/cogl/cogl-util.h
+++ b/cogl/cogl-util.h
@@ -268,4 +268,14 @@ _cogl_util_pixel_format_from_masks (unsigned long r_mask,
 #define _Static_assert(EXPRESSION, MESSAGE)
 #endif
 
+#ifdef HAVE_MEMMEM
+#define _cogl_util_memmem memmem
+#else
+char *
+_cogl_util_memmem (const void *haystack,
+                   size_t haystack_len,
+                   const void *needle,
+                   size_t needle_len);
+#endif
+
 #endif /* __COGL_UTIL_H */
diff --git a/configure.ac b/configure.ac
index 685cee1..6764ee7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1094,6 +1094,9 @@ dnl The 'ffs' function is part of C99 so it isn't always
 dnl available. Cogl has a fallback if needed.
 AC_CHECK_FUNCS([ffs])
 
+dnl 'memmem' is a GNU extension but we have a simple fallback
+AC_CHECK_FUNCS([memmem])
+
 dnl ================================================================
 dnl Platform values
 dnl ================================================================
-- 
1.7.11.3.g3c3efa5



More information about the Cogl mailing list