aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Krjuchkov <denis@crazydev.net>2013-01-11 13:51:39 +0600
committerDenis Krjuchkov <denis@crazydev.net>2013-01-11 13:51:39 +0600
commita98aa666203cad87913303d2f8b9ca07640518c3 (patch)
tree7fc9aa4231dbe86e227cd50d84f16d937898c28b
parent631a26899610e11a6c6456c2a5750dace68958f7 (diff)
downloadmpd-a98aa666203cad87913303d2f8b9ca07640518c3.tar.gz
mpd-a98aa666203cad87913303d2f8b9ca07640518c3.tar.xz
mpd-a98aa666203cad87913303d2f8b9ca07640518c3.zip
string_util.c: provide fallback strndup() implementation
This patch also adds extern "C" { } wrapper around string_util.h to allow its usage in C++ code
-rw-r--r--configure.ac2
-rw-r--r--src/decoder/OpusReader.hxx1
-rw-r--r--src/string_util.c21
-rw-r--r--src/string_util.h26
4 files changed, 50 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac
index 1cd27ffe5..5515eb18e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -135,6 +135,8 @@ AC_SEARCH_LIBS([gethostbyname], [nsl])
AC_CHECK_FUNCS(pipe2 accept4 eventfd)
+AC_CHECK_FUNCS(strndup)
+
AC_SEARCH_LIBS([exp], [m],,
[AC_MSG_ERROR([exp() not found])])
diff --git a/src/decoder/OpusReader.hxx b/src/decoder/OpusReader.hxx
index 2cfc14118..1fd07b55c 100644
--- a/src/decoder/OpusReader.hxx
+++ b/src/decoder/OpusReader.hxx
@@ -21,6 +21,7 @@
#define MPD_OPUS_READER_HXX
#include "check.h"
+#include "string_util.h"
#include <stdint.h>
#include <string.h>
diff --git a/src/string_util.c b/src/string_util.c
index 6e5429076..b76b257ba 100644
--- a/src/string_util.c
+++ b/src/string_util.c
@@ -20,6 +20,8 @@
#include "config.h"
#include "string_util.h"
+#include <stdlib.h> /* for malloc() */
+#include <string.h> /* for strnlen() */
#include <glib.h>
#include <assert.h>
@@ -45,3 +47,22 @@ string_array_contains(const char *const* haystack, const char *needle)
return false;
}
+
+#if !defined(HAVE_STRNDUP)
+
+char *
+strndup(const char *str, size_t n)
+{
+ assert(str != NULL);
+
+ size_t len = strnlen(str, n);
+ char* ret = (char *) malloc(len + 1);
+ if (ret == NULL)
+ return NULL;
+
+ memcpy(ret, str, len);
+ ret[len] = '\0';
+ return ret;
+}
+
+#endif
diff --git a/src/string_util.h b/src/string_util.h
index c1d316f0c..374fd0f91 100644
--- a/src/string_util.h
+++ b/src/string_util.h
@@ -23,6 +23,11 @@
#include "gcc.h"
#include <stdbool.h>
+#include <stdlib.h> /* for size_t */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
/**
* Remove the "const" attribute from a string pointer. This is a
@@ -78,4 +83,25 @@ strchug_fast(char *p)
bool
string_array_contains(const char *const* haystack, const char *needle);
+#if !defined(HAVE_STRNDUP)
+
+/**
+ * Duplicates the string to a newly allocated buffer
+ * copying at most n characters.
+ *
+ * @param str a string to duplicate
+ * @param n maximal number of characters to copy
+ * @return a pointer to the duplicated string,
+ * or NULL if memory allocation failed.
+ */
+gcc_malloc
+char *
+strndup(const char *str, size_t n);
+
+#endif
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
#endif