[PATCH] Add mp_strings.c with mp_asprintf function.

Clément Bœsch ubitux at gmail.com
Wed Jan 12 23:37:11 CET 2011


---
 Makefile     |    1 +
 mp_strings.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 mp_strings.h |   26 ++++++++++++++++++++++++++
 3 files changed, 76 insertions(+), 0 deletions(-)
 create mode 100644 mp_strings.c
 create mode 100644 mp_strings.h

diff --git a/Makefile b/Makefile
index 2de75fb..edacd15 100644
--- a/Makefile
+++ b/Makefile
@@ -301,6 +301,7 @@ SRCS_COMMON = asxparser.c \
               m_option.c \
               m_struct.c \
               mp_msg.c \
+              mp_strings.c \
               mpcommon.c \
               parser-cfg.c \
               path.c \
diff --git a/mp_strings.c b/mp_strings.c
new file mode 100644
index 0000000..8c7c23e
--- /dev/null
+++ b/mp_strings.c
@@ -0,0 +1,49 @@
+/*
+ * Strings utilities
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include "mp_strings.h"
+
+int mp_asprintf(char **strp, const char *fmt, ...)
+{
+    va_list va, va_bak;
+    int len;
+
+    va_start(va, fmt);
+    va_copy(va_bak, va);
+
+    len = vsnprintf(NULL, 0, fmt, va);
+    if (len < 0)
+        goto end;
+
+    *strp = malloc(len + 1);
+    if (!*strp) {
+        len = -1;
+        goto end;
+    }
+
+    len = vsnprintf(*strp, len, fmt, va_bak);
+
+end:
+    va_end(va);
+    return len;
+}
diff --git a/mp_strings.h b/mp_strings.h
new file mode 100644
index 0000000..ef2b768
--- /dev/null
+++ b/mp_strings.h
@@ -0,0 +1,26 @@
+/*
+ * Strings utilities
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MP_STRINGS_H
+# define MP_STRINGS_H
+
+int mp_asprintf(char **strp, const char *fmt, ...);
+
+#endif
-- 
1.7.3.5


--hUH5gZbnpyIv7Mn4--


More information about the MPlayer-dev-eng mailing list