Subversion Repositories HelenOS

Rev

Rev 4184 | Rev 4200 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1271 cejka 1
/*
2071 jermar 2
 * Copyright (c) 2006 Josef Cejka
1271 cejka 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
 
4180 decky 29
/** @addtogroup generic
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
1271 cejka 35
#include <print.h>
36
#include <printf/printf_core.h>
4180 decky 37
#include <string.h>
1271 cejka 38
#include <memstr.h>
39
 
4180 decky 40
typedef struct {
41
	size_t size;    /* Total size of the buffer (in bytes) */
42
	size_t len;     /* Number of already used bytes */
43
	char *dst;      /* Destination */
44
} vsnprintf_data_t;
1271 cejka 45
 
4180 decky 46
/** Write UTF-8 string to given buffer.
47
 *
48
 * Write at most data->size plain characters including trailing zero.
49
 * According to C99, snprintf() has to return number of characters that
50
 * would have been written if enough space had been available. Hence
51
 * the return value is not the number of actually printed characters
52
 * but size of the input string.
53
 *
54
 * @param str  Source UTF-8 string to print.
55
 * @param size Number of plain characters in str.
56
 * @param data Structure describing destination string, counter
57
 *             of used space and total string size.
58
 *
59
 * @return Number of UTF-8 characters to print (not characters actually
60
 *         printed).
61
 *
1271 cejka 62
 */
4180 decky 63
static int vsnprintf_write_utf8(const char *str, size_t size, vsnprintf_data_t *data)
1271 cejka 64
{
4180 decky 65
	size_t left = data->size - data->len;
1271 cejka 66
 
4180 decky 67
	if (left == 0)
68
		return ((int) size);
69
 
70
	if (left == 1) {
71
		/* We have only one free byte left in buffer
72
		 * -> store trailing zero
73
		 */
74
		data->dst[data->size - 1] = 0;
1604 cejka 75
		data->len = data->size;
4180 decky 76
		return ((int) size);
1604 cejka 77
	}
78
 
4180 decky 79
	if (left <= size) {
80
		/* We have not enought space for whole string
81
		 * with the trailing zero => print only a part
82
		 * of string
83
		 */
84
		index_t index = 0;
85
 
86
		while (index < size) {
4199 svoboda 87
			wchar_t uc = utf8_decode(str, &index, size);
88
 
89
			if (!utf8_encode(uc, data->dst, &data->len, data->size - 1))
4180 decky 90
				break;
91
		}
92
 
93
		/* Put trailing zero at end, but not count it
94
		 * into data->len so it could be rewritten next time
95
		 */
96
		data->dst[data->len] = 0;
97
 
98
		return ((int) size);
99
	}
100
 
101
	/* Buffer is big enought to print the whole string */
102
	memcpy((void *)(data->dst + data->len), (void *) str, size);
103
	data->len += size;
104
 
105
	/* Put trailing zero at end, but not count it
106
	 * into data->len so it could be rewritten next time
107
	 */
108
	data->dst[data->len] = 0;
109
 
110
	return ((int) size);
111
}
112
 
113
/** Write UTF-32 string to given buffer.
114
 *
115
 * Write at most data->size plain characters including trailing zero.
116
 * According to C99, snprintf() has to return number of characters that
117
 * would have been written if enough space had been available. Hence
118
 * the return value is not the number of actually printed characters
119
 * but size of the input string.
120
 *
121
 * @param str  Source UTF-32 string to print.
122
 * @param size Number of bytes in str.
123
 * @param data Structure describing destination string, counter
124
 *             of used space and total string size.
125
 *
126
 * @return Number of UTF-8 characters to print (not characters actually
127
 *         printed).
128
 *
129
 */
130
static int vsnprintf_write_utf32(const wchar_t *str, size_t size, vsnprintf_data_t *data)
131
{
132
	index_t index = 0;
133
 
134
	while (index < (size / sizeof(wchar_t))) {
135
		size_t left = data->size - data->len;
136
 
137
		if (left == 0)
138
			return ((int) size);
139
 
140
		if (left == 1) {
141
			/* We have only one free byte left in buffer
142
			 * -> store trailing zero
143
			 */
144
			data->dst[data->size - 1] = 0;
1604 cejka 145
			data->len = data->size;
4180 decky 146
			return ((int) size);
147
		}
148
 
4199 svoboda 149
		if (!utf8_encode(str[index], data->dst, &data->len, data->size - 1))
4180 decky 150
			break;
151
 
152
		index++;
1604 cejka 153
	}
154
 
4180 decky 155
	/* Put trailing zero at end, but not count it
156
	 * into data->len so it could be rewritten next time
157
	 */
158
	data->dst[data->len] = 0;
159
 
160
	return ((int) size);
1271 cejka 161
}
162
 
163
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
164
{
4180 decky 165
	vsnprintf_data_t data = {
166
		size,
167
		0,
168
		str
169
	};
170
	printf_spec_t ps = {
171
		(int(*) (const char *, size_t, void *)) vsnprintf_write_utf8,
172
		(int(*) (const wchar_t *, size_t, void *)) vsnprintf_write_utf32,
173
		&data
174
	};
175
 
1271 cejka 176
	/* Print 0 at end of string - fix the case that nothing will be printed */
177
	if (size > 0)
178
		str[0] = 0;
179
 
180
	/* vsnprintf_write ensures that str will be terminated by zero. */
181
	return printf_core(fmt, &ps, ap);
182
}
1702 cejka 183
 
1888 jermar 184
/** @}
1702 cejka 185
 */