From acaf7480a5f145ba73da6aef9683dbc7a1ee5a1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= Date: Wed, 12 Jan 2011 23:37:11 +0100 Subject: [PATCH 1/3] Add mp_strings.c with mp_asprintf function. --- 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 5e5ef6d..2996392 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..0c8a577 --- /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 +#include +#include +#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 + 1, 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..3990322 --- /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 MPLAYER_MP_STRINGS_H +#define MPLAYER_MP_STRINGS_H + +int mp_asprintf(char **strp, const char *fmt, ...); + +#endif /* !MPLAYER_MP_STRINGS_H */ -- 1.7.4.1