xref: /minix3/external/bsd/kyua-testers/dist/text.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2012 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "text.h"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <assert.h>
32*11be35a1SLionel Sambuc #include <errno.h>
33*11be35a1SLionel Sambuc #include <stdarg.h>
34*11be35a1SLionel Sambuc #include <stdlib.h>
35*11be35a1SLionel Sambuc #include <stdio.h>
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include "error.h"
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc /// Calculates the length of a formatting string with its replacements.
41*11be35a1SLionel Sambuc ///
42*11be35a1SLionel Sambuc /// \param format The formatting string.
43*11be35a1SLionel Sambuc /// \param ap List of arguments to apply to the formatting string.
44*11be35a1SLionel Sambuc ///
45*11be35a1SLionel Sambuc /// \return -1 if there is an error; or the final length otherwise.
46*11be35a1SLionel Sambuc static int
calculate_length(const char * format,va_list ap)47*11be35a1SLionel Sambuc calculate_length(const char* format, va_list ap)
48*11be35a1SLionel Sambuc {
49*11be35a1SLionel Sambuc     va_list ap2;
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc     char buffer[1];
52*11be35a1SLionel Sambuc     va_copy(ap2, ap);
53*11be35a1SLionel Sambuc     const int needed = vsnprintf(buffer, sizeof(buffer), format, ap);
54*11be35a1SLionel Sambuc     va_end(ap2);
55*11be35a1SLionel Sambuc     return needed;
56*11be35a1SLionel Sambuc }
57*11be35a1SLionel Sambuc 
58*11be35a1SLionel Sambuc 
59*11be35a1SLionel Sambuc /// Generates a string from a format string and its replacements.
60*11be35a1SLionel Sambuc ///
61*11be35a1SLionel Sambuc /// \param [out] output Pointer to the dynamically-allocated string with the
62*11be35a1SLionel Sambuc ///     result of the operation.  The caller must release this with free().
63*11be35a1SLionel Sambuc /// \param format The formatting string.
64*11be35a1SLionel Sambuc /// \param ... Arguments to apply to the formatting string.
65*11be35a1SLionel Sambuc ///
66*11be35a1SLionel Sambuc /// \return OK if the string could be formatted; an error otherwise.
67*11be35a1SLionel Sambuc kyua_error_t
kyua_text_printf(char ** output,const char * format,...)68*11be35a1SLionel Sambuc kyua_text_printf(char** output, const char* format, ...)
69*11be35a1SLionel Sambuc {
70*11be35a1SLionel Sambuc     kyua_error_t error;
71*11be35a1SLionel Sambuc     va_list ap;
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc     va_start(ap, format);
74*11be35a1SLionel Sambuc     error = kyua_text_vprintf(output, format, ap);
75*11be35a1SLionel Sambuc     va_end(ap);
76*11be35a1SLionel Sambuc 
77*11be35a1SLionel Sambuc     return error;
78*11be35a1SLionel Sambuc }
79*11be35a1SLionel Sambuc 
80*11be35a1SLionel Sambuc 
81*11be35a1SLionel Sambuc /// Generates a string from a format string and its replacements.
82*11be35a1SLionel Sambuc ///
83*11be35a1SLionel Sambuc /// \param [out] output Pointer to the dynamically-allocated string with the
84*11be35a1SLionel Sambuc ///     result of the operation.  The caller must release this with free().
85*11be35a1SLionel Sambuc /// \param format The formatting string.
86*11be35a1SLionel Sambuc /// \param ap List of to apply to the formatting string.
87*11be35a1SLionel Sambuc ///
88*11be35a1SLionel Sambuc /// \return OK if the string could be formatted; an error otherwise.
89*11be35a1SLionel Sambuc kyua_error_t
kyua_text_vprintf(char ** output,const char * format,va_list ap)90*11be35a1SLionel Sambuc kyua_text_vprintf(char** output, const char* format, va_list ap)
91*11be35a1SLionel Sambuc {
92*11be35a1SLionel Sambuc     va_list ap2;
93*11be35a1SLionel Sambuc 
94*11be35a1SLionel Sambuc     va_copy(ap2, ap);
95*11be35a1SLionel Sambuc     const int length = calculate_length(format, ap2);
96*11be35a1SLionel Sambuc     va_end(ap2);
97*11be35a1SLionel Sambuc     if (length < 0)
98*11be35a1SLionel Sambuc         return kyua_libc_error_new(errno, "Could not calculate length of "
99*11be35a1SLionel Sambuc                                    "string with format '%s'", format);
100*11be35a1SLionel Sambuc 
101*11be35a1SLionel Sambuc     char* buffer = (char*)malloc(length + 1);
102*11be35a1SLionel Sambuc     if (buffer == NULL)
103*11be35a1SLionel Sambuc         return kyua_oom_error_new();
104*11be35a1SLionel Sambuc 
105*11be35a1SLionel Sambuc     va_copy(ap2, ap);
106*11be35a1SLionel Sambuc     const int printed_length = vsnprintf(buffer, length + 1, format, ap2);
107*11be35a1SLionel Sambuc     va_end(ap2);
108*11be35a1SLionel Sambuc     assert(printed_length == length);
109*11be35a1SLionel Sambuc     if (printed_length < 0) {
110*11be35a1SLionel Sambuc         free(buffer);
111*11be35a1SLionel Sambuc         return kyua_libc_error_new(errno, "Could generate string with format "
112*11be35a1SLionel Sambuc                                    "'%s'", format);
113*11be35a1SLionel Sambuc     }
114*11be35a1SLionel Sambuc 
115*11be35a1SLionel Sambuc     *output = buffer;
116*11be35a1SLionel Sambuc     return kyua_error_ok();
117*11be35a1SLionel Sambuc }
118