Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
f74f4f32 | 3 | * Copyright (C) 2004-2009 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" | |
902b9f3f | 27 | #include "log.h" |
460b9539 | 28 | #include "sink.h" |
29 | #include "mem.h" | |
30 | #include "vector.h" | |
460b9539 | 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 | 38 | int byte_vasprintf(char **ptrp, |
39 | const char *fmt, | |
40 | va_list ap) { | |
47854c5f | 41 | struct sink *s; |
460b9539 | 42 | struct dynstr d; |
43 | int n; | |
44 | ||
45 | dynstr_init(&d); | |
47854c5f RK |
46 | s = sink_dynstr(&d); |
47 | n = byte_vsinkprintf(s, fmt, ap); | |
48 | xfree(s); | |
49 | if(n >= 0) { | |
460b9539 | 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 | 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 | ||
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 | 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 | ||
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 | 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) | |
2e9ba080 | 106 | disorder_fatal(errno, "error calling byte_vasprintf"); |
460b9539 | 107 | return n; |
108 | } | |
109 | ||
110 | /* | |
111 | Local Variables: | |
112 | c-basic-offset:2 | |
113 | comment-column:40 | |
114 | End: | |
115 | */ |