-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sprintf.hpp
70 lines (58 loc) · 2.11 KB
/
Sprintf.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#include <cstdint>
#include <cstddef> //size_t
#include <cstdarg> //variadic args
#include <cstdio> //vsnprintf
#include "Markup.hpp"
// cannot use this class directly
//
// devices like uart, usb cdc can inherit Sprintf (private)
// for printf use
// each device that uses Sprintf can add a function to its
// struct for printf, which will call Sprintf::sprintf
// and returns a buffer which the device can ouptut as needed
//
// see Uart.xpp and UsbCdcAcm.xpp for printf function examples
// (templates could also be used, and a lot of va_ stuff could be
// eliminated, but code size increases, which may only be the
// result of an older version of gcc used by XC32)
//
// each class that inherits Sprintf will get its own buffer
// (default is 256, which should be plenty for normal use)
//
// example to inherit Sprintf-
// struct Uart : private Sprintf<> {... //default size buffer
// struct Uart : private Sprintf<128> {... //buffer size is 128
template<size_t Tbufsiz = 256>
struct Sprintf : private Markup {
protected:
// printf format into buffer, check for markup (ansi colors)
// return buffer
// va_list already setup (expansion of variadic arguments had to be
// expanded previously in a class printf function, so only need va_list)
//=============================================================================
auto
sprintf (char const *fmt, va_list args) -> char*
{
vsnprintf(m_buf, Tbufsiz, fmt, args);
markup(m_buf, Tbufsiz);
return m_buf;
}
// return pointer to buffer, each class that inherits Sprintf gets its own
// buffer which can be used as needed, this gives a way to get the address
//=============================================================================
auto
sprintfbuf () -> char*
{
return m_buf;
}
// return buffer size
//=============================================================================
auto
sprintfbufsiz() -> size_t
{
return Tbufsiz;
}
private:
char m_buf[Tbufsiz];
};