chiark / gitweb /
more doxygen
[disorder] / lib / asprintf.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2004, 2006 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 2 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, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20
21#include <config.h>
22#include "types.h"
23
24#include <stdio.h>
25#include <string.h>
26#include <stdarg.h>
27#include <stddef.h>
28#include <errno.h>
29
30#include "printf.h"
31#include "sink.h"
32#include "mem.h"
33#include "vector.h"
34#include "log.h"
35
d7b6f0d1 36/** @brief vasprintf() workalike without encoding errors
37 *
38 * This acts like vasprintf() except that it does not throw an error
39 * if you use a string outside the current locale's encoding rules,
40 * and it uses the memory allocation calls from @ref mem.h.
41 */
460b9539 42int byte_vasprintf(char **ptrp,
43 const char *fmt,
44 va_list ap) {
45 struct dynstr d;
46 int n;
47
48 dynstr_init(&d);
49 if((n = byte_vsinkprintf(sink_dynstr(&d), fmt, ap)) >= 0) {
50 dynstr_terminate(&d);
51 *ptrp = d.vec;
52 }
53 return n;
54}
55
d7b6f0d1 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 */
460b9539 62int 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
d7b6f0d1 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 */
460b9539 81int 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
d7b6f0d1 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 */
460b9539 100int 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 fatal(errno, "error calling byte_vasprintf");
107 return n;
108}
109
110/*
111Local Variables:
112c-basic-offset:2
113comment-column:40
114End:
115*/