comparison mp_strings.c @ 32883:fa04e96e6177

Add mp_strings.c with mp_asprintf function.
author cboesch
date Sat, 26 Feb 2011 11:55:02 +0000
parents
children ca6dcdb31fa1
comparison
equal deleted inserted replaced
32882:bd3ea059fe2d 32883:fa04e96e6177
1 /*
2 * Strings utilities
3 *
4 * This file is part of MPlayer.
5 *
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include "mp_strings.h"
25
26 char *mp_asprintf(const char *fmt, ...)
27 {
28 char *p = NULL;
29 va_list va, va_bak;
30 int len;
31
32 va_start(va, fmt);
33 va_copy(va_bak, va);
34
35 len = vsnprintf(NULL, 0, fmt, va);
36 if (len < 0)
37 goto end;
38
39 p = malloc(len + 1);
40 if (!p)
41 goto end;
42
43 len = vsnprintf(p, len + 1, fmt, va_bak);
44 if (len < 0)
45 free(p), p = NULL;
46
47 end:
48 va_end(va);
49 return p;
50 }