1
/*
2
* \brief Facility to write format string into character buffer
3
* \author Norman Feske
4
* \date 2006-07-17
5
*/
6
7
/*
8
* Copyright (C) 2006-2013 Genode Labs GmbH
9
*
10
* This file is part of the Genode OS framework, which is distributed
11
* under the terms of the GNU General Public License version 2.
12
*/
13
14
#
ifndef _INCLUDE__BASE__SNPRINTF_H_
15
#
define _INCLUDE__BASE__SNPRINTF_H_
16
17
#
include <base/console.h>
18
#
include <base/stdint.h>
19
20
namespace
Genode {
21
22
class
String_console;
23
24
/**
25
* Print format string into character buffer
26
*
27
* \param dst destination buffer
28
* \param dst_len size of `dst` in bytes
29
* \param format format string followed by the list of arguments
30
*
31
* \return number of characters written to destination buffer
32
*/
33
inline
int
snprintf(
char *
dst
,
size_t
dst_size
,
const
char *
format
,
.
.
.
)
34
__attribute__(
(
format(
printf,
3,
4)
)
)
;
35
}
36
37
38
class
Genode::
String_console :
public
Console
39
{
40
private
:
41
42
char *
_dst;
43
size_t _dst_len;
44
size_t _w_offset;
45
46
public
:
47
48
/**
49
* Constructor
50
*
51
* \param dst destination character buffer
52
* \param dst_len size of `dst`
53
*/
54
String_console(
char *
dst
,
size_t
dst_len
)
55
:
_dst(
dst)
,
_dst_len(
dst_len)
,
_w_offset(
0)
56
{
_dst[0]
=
0;
}
57
58
/**
59
* Return number of characters in destination buffer
60
*/
61
size_t
len(
)
{
return
_w_offset
;
}
62
63
64
/***********************
65
** Console interface **
66
***********************/
67
68
void
_out_char(
char
c
)
override
69
{
70
/* ensure to leave space for null-termination */
71
if
(
_w_offset +
2 >
_dst_len)
72
return
;
73
74
_dst[_w_offset++
]
=
c;
75
_dst[_w_offset]
=
0;
76
}
77
}
;
78
79
80
inline
int
Genode::
snprintf(
char *
dst
,
size_t
dst_len
,
const
char *
format
,
.
.
.
)
81
{
82
va_list list;
83
va_start(
list,
format)
;
84
85
String_console
sc(
dst
,
dst_len
)
;
86
sc.
vprintf(
format,
list)
;
87
88
va_end(
list)
;
89
return
sc.
len(
)
;
90
}
91
92
#
endif /* _INCLUDE__BASE__SNPRINTF_H_ */