From d760983687bfa5f201dbde6c247f3c1f01806c39 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 | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ mp_strings.h | 26 ++++++++++++++++++++++++++ 3 files changed, 77 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..4558101 --- /dev/null +++ b/mp_strings.c @@ -0,0 +1,50 @@ +/* + * 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" + +char *mp_asprintf(const char *fmt, ...) +{ + char *p = NULL; + 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; + + p = malloc(len + 1); + if (!p) + goto end; + + len = vsnprintf(p, len + 1, fmt, va_bak); + if (len < 0) + free(p), p = NULL; + +end: + va_end(va); + return p; +} diff --git a/mp_strings.h b/mp_strings.h new file mode 100644 index 0000000..8ad101f --- /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 + +char *mp_asprintf(const char *fmt, ...); + +#endif /* !MPLAYER_MP_STRINGS_H */ -- 1.7.4.1