speaker: new comments to add/remove RTP recipients
[disorder] / lib / asprintf.c
1 /*
2 * This file is part of DisOrder
3 * Copyright (C) 2004-2009 Richard Kettlewell
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 /** @file lib/asprintf.c @brief printf() workalikes */
19
20 #include "common.h"
21
22 #include <stdarg.h>
23 #include <stddef.h>
24 #include <errno.h>
25
26 #include "printf.h"
27 #include "sink.h"
28 #include "mem.h"
29 #include "vector.h"
30 #include "log.h"
31
32 /** @brief vasprintf() workalike without encoding errors
33 *
34 * This acts like vasprintf() except that it does not throw an error
35 * if you use a string outside the current locale's encoding rules,
36 * and it uses the memory allocation calls from @ref mem.h.
37 */
38 int byte_vasprintf(char **ptrp,
39 const char *fmt,
40 va_list ap) {
41 struct sink *s;
42 struct dynstr d;
43 int n;
44
45 dynstr_init(&d);
46 s = sink_dynstr(&d);
47 n = byte_vsinkprintf(s, fmt, ap);
48 xfree(s);
49 if(n >= 0) {
50 dynstr_terminate(&d);
51 *ptrp = d.vec;
52 }
53 return n;
54 }
55
56 /** @brief asprintf() workalike without encoding errors
57 *
58 * This acts like asprintf() except that it does not throw an error
59 * if you use a string outside the current locale's encoding rules,
60 * and it uses the memory allocation calls from @ref mem.h.
61 */
62 int byte_asprintf(char **ptrp,
63 const char *fmt,
64 ...) {
65 int n;
66 va_list ap;
67
68 va_start(ap, fmt);
69 n = byte_vasprintf(ptrp, fmt, ap);
70 va_end(ap);
71 return n;
72 }
73
74 /** @brief asprintf() workalike without encoding errors
75 *
76 * This acts like asprintf() except that it does not throw an error if
77 * you use a string outside the current locale's encoding rules; it
78 * uses the memory allocation calls from @ref mem.h; and it terminates
79 * the program on error.
80 */
81 int byte_xasprintf(char **ptrp,
82 const char *fmt,
83 ...) {
84 int n;
85 va_list ap;
86
87 va_start(ap, fmt);
88 n = byte_xvasprintf(ptrp, fmt, ap);
89 va_end(ap);
90 return n;
91 }
92
93 /** @brief vasprintf() workalike without encoding errors
94 *
95 * This acts like vasprintf() except that it does not throw an error
96 * if you use a string outside the current locale's encoding rules; it
97 * uses the memory allocation calls from @ref mem.h; and it terminates
98 * the program on error.
99 */
100 int byte_xvasprintf(char **ptrp,
101 const char *fmt,
102 va_list ap) {
103 int n;
104
105 if((n = byte_vasprintf(ptrp, fmt, ap)) < 0)
106 disorder_fatal(errno, "error calling byte_vasprintf");
107 return n;
108 }
109
110 /*
111 Local Variables:
112 c-basic-offset:2
113 comment-column:40
114 End:
115 */