chiark / gitweb /
Merge mac build fix
[disorder] / lib / asprintf.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
5aff007d 3 * Copyright (C) 2004-2008 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
14ad73b9 18/** @file lib/asprintf.c @brief printf() workalikes */
460b9539 19
05b75f8d 20#include "common.h"
460b9539 21
460b9539 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
d7b6f0d1 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 */
460b9539 38int byte_vasprintf(char **ptrp,
39 const char *fmt,
40 va_list ap) {
41 struct dynstr d;
42 int n;
43
44 dynstr_init(&d);
45 if((n = byte_vsinkprintf(sink_dynstr(&d), fmt, ap)) >= 0) {
46 dynstr_terminate(&d);
47 *ptrp = d.vec;
48 }
49 return n;
50}
51
d7b6f0d1 52/** @brief asprintf() workalike without encoding errors
53 *
54 * This acts like asprintf() except that it does not throw an error
55 * if you use a string outside the current locale's encoding rules,
56 * and it uses the memory allocation calls from @ref mem.h.
57 */
460b9539 58int byte_asprintf(char **ptrp,
59 const char *fmt,
60 ...) {
61 int n;
62 va_list ap;
63
64 va_start(ap, fmt);
65 n = byte_vasprintf(ptrp, fmt, ap);
66 va_end(ap);
67 return n;
68}
69
d7b6f0d1 70/** @brief asprintf() workalike without encoding errors
71 *
72 * This acts like asprintf() except that it does not throw an error if
73 * you use a string outside the current locale's encoding rules; it
74 * uses the memory allocation calls from @ref mem.h; and it terminates
75 * the program on error.
76 */
460b9539 77int byte_xasprintf(char **ptrp,
78 const char *fmt,
79 ...) {
80 int n;
81 va_list ap;
82
83 va_start(ap, fmt);
84 n = byte_xvasprintf(ptrp, fmt, ap);
85 va_end(ap);
86 return n;
87}
88
d7b6f0d1 89/** @brief vasprintf() workalike without encoding errors
90 *
91 * This acts like vasprintf() except that it does not throw an error
92 * if you use a string outside the current locale's encoding rules; it
93 * uses the memory allocation calls from @ref mem.h; and it terminates
94 * the program on error.
95 */
460b9539 96int byte_xvasprintf(char **ptrp,
97 const char *fmt,
98 va_list ap) {
99 int n;
100
101 if((n = byte_vasprintf(ptrp, fmt, ap)) < 0)
102 fatal(errno, "error calling byte_vasprintf");
103 return n;
104}
105
106/*
107Local Variables:
108c-basic-offset:2
109comment-column:40
110End:
111*/