chiark / gitweb /
Merge new Disobedience manual
[disorder] / lib / asprintf.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2004-2008 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 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
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  */
58 int 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
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  */
77 int 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
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  */
96 int 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     disorder_fatal(errno, "error calling byte_vasprintf");
103   return n;
104 }
105
106 /*
107 Local Variables:
108 c-basic-offset:2
109 comment-column:40
110 End:
111 */