source: mainline/uspace/lib/c/generic/io/vsnprintf.c@ 5e801dc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5e801dc was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[3214a20]1/*
[df4ed85]2 * Copyright (c) 2006 Josef Cejka
[3214a20]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[fadd381]29/** @addtogroup libc
[b2951e2]30 * @{
31 */
32/** @file
33 */
34
[3214a20]35#include <stdarg.h>
36#include <stdio.h>
[19f857a]37#include <str.h>
[3214a20]38#include <io/printf_core.h>
[f2b8cdc]39#include <errno.h>
[3214a20]40
[f2b8cdc]41typedef struct {
42 size_t size; /* Total size of the buffer (in bytes) */
43 size_t len; /* Number of already used bytes */
44 char *dst; /* Destination */
45} vsnprintf_data_t;
[3214a20]46
47/** Write string to given buffer.
[2e4bd1f]48 *
[f2b8cdc]49 * Write at most data->size plain characters including trailing zero.
50 * According to C99, snprintf() has to return number of characters that
51 * would have been written if enough space had been available. Hence
52 * the return value is not the number of actually printed characters
53 * but size of the input string.
54 *
55 * @param str Source string to print.
56 * @param size Number of plain characters in str.
57 * @param data Structure describing destination string, counter
58 * of used space and total string size.
59 *
60 * @return Number of characters to print (not characters actually
61 * printed).
62 *
[3214a20]63 */
[f2b8cdc]64static int vsnprintf_str_write(const char *str, size_t size, vsnprintf_data_t *data)
[3214a20]65{
[f2b8cdc]66 size_t left = data->size - data->len;
[a35b458]67
[f2b8cdc]68 if (left == 0)
69 return ((int) size);
[a35b458]70
[f2b8cdc]71 if (left == 1) {
[7c3fb9b]72 /*
73 * We have only one free byte left in buffer
[f2b8cdc]74 * -> store trailing zero
[2e4bd1f]75 */
[f2b8cdc]76 data->dst[data->size - 1] = 0;
[f1249e1]77 data->len = data->size;
[f2b8cdc]78 return ((int) size);
[f1249e1]79 }
[a35b458]80
[f2b8cdc]81 if (left <= size) {
[7c3fb9b]82 /*
83 * We do not have enough space for the whole string
[f2b8cdc]84 * with the trailing zero => print only a part
85 * of string
[2e4bd1f]86 */
[2595dab]87 size_t index = 0;
[a35b458]88
[f2b8cdc]89 while (index < size) {
90 wchar_t uc = str_decode(str, &index, size);
[a35b458]91
[f2b8cdc]92 if (chr_encode(uc, data->dst, &data->len, data->size - 1) != EOK)
93 break;
94 }
[a35b458]95
[7c3fb9b]96 /*
97 * Put trailing zero at end, but not count it
[f2b8cdc]98 * into data->len so it could be rewritten next time
99 */
100 data->dst[data->len] = 0;
[a35b458]101
[f2b8cdc]102 return ((int) size);
[f1249e1]103 }
[a35b458]104
[1724745f]105 /* Buffer is big enough to print the whole string */
[f2b8cdc]106 memcpy((void *)(data->dst + data->len), (void *) str, size);
107 data->len += size;
[a35b458]108
[7c3fb9b]109 /*
110 * Put trailing zero at end, but not count it
[f2b8cdc]111 * into data->len so it could be rewritten next time
[2e4bd1f]112 */
[f2b8cdc]113 data->dst[data->len] = 0;
[a35b458]114
[f2b8cdc]115 return ((int) size);
[3214a20]116}
117
[f2b8cdc]118/** Write wide string to given buffer.
119 *
120 * Write at most data->size plain characters including trailing zero.
121 * According to C99, snprintf() has to return number of characters that
122 * would have been written if enough space had been available. Hence
123 * the return value is not the number of actually printed characters
124 * but size of the input string.
125 *
126 * @param str Source wide string to print.
127 * @param size Number of bytes in str.
128 * @param data Structure describing destination string, counter
129 * of used space and total string size.
130 *
131 * @return Number of wide characters to print (not characters actually
132 * printed).
133 *
[80076f6]134 */
[f2b8cdc]135static int vsnprintf_wstr_write(const wchar_t *str, size_t size, vsnprintf_data_t *data)
136{
[2595dab]137 size_t index = 0;
[a35b458]138
[f2b8cdc]139 while (index < (size / sizeof(wchar_t))) {
140 size_t left = data->size - data->len;
[a35b458]141
[f2b8cdc]142 if (left == 0)
143 return ((int) size);
[a35b458]144
[f2b8cdc]145 if (left == 1) {
[7c3fb9b]146 /*
147 * We have only one free byte left in buffer
[f2b8cdc]148 * -> store trailing zero
149 */
150 data->dst[data->size - 1] = 0;
151 data->len = data->size;
152 return ((int) size);
153 }
[a35b458]154
[f2b8cdc]155 if (chr_encode(str[index], data->dst, &data->len, data->size - 1) != EOK)
156 break;
[a35b458]157
[f2b8cdc]158 index++;
159 }
[a35b458]160
[7c3fb9b]161 /*
162 * Put trailing zero at end, but not count it
[f2b8cdc]163 * into data->len so it could be rewritten next time
164 */
165 data->dst[data->len] = 0;
[a35b458]166
[f2b8cdc]167 return ((int) size);
168}
169
[3214a20]170int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
171{
[f2b8cdc]172 vsnprintf_data_t data = {
173 size,
174 0,
175 str
176 };
177 printf_spec_t ps = {
[1433ecda]178 (int (*) (const char *, size_t, void *)) vsnprintf_str_write,
179 (int (*) (const wchar_t *, size_t, void *)) vsnprintf_wstr_write,
[2e4bd1f]180 &data
181 };
[a35b458]182
[f2b8cdc]183 /* Print 0 at end of string - fix the case that nothing will be printed */
[3214a20]184 if (size > 0)
185 str[0] = 0;
[a35b458]186
[3214a20]187 /* vsnprintf_write ensures that str will be terminated by zero. */
188 return printf_core(fmt, &ps, ap);
189}
190
[fadd381]191/** @}
[b2951e2]192 */
Note: See TracBrowser for help on using the repository browser.