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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
* nl_string.c
*
*/
#include <cstring>
//FIXME:: pstring should be locale free
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include "pfmtlog.h"
#include "palloc.h"
namespace plib {
plog_dispatch_intf::~plog_dispatch_intf()
{
}
pfmt::pfmt(const pstring &fmt)
: m_str(m_str_buf), m_allocated(0), m_arg(0)
{
std::size_t l = fmt.blen() + 1;
if (l>sizeof(m_str_buf))
{
m_allocated = 2 * l;
m_str = palloc_array<char>(2 * l);
}
memcpy(m_str, fmt.c_str(), l);
}
pfmt::pfmt(const char *fmt)
: m_str(m_str_buf), m_allocated(0), m_arg(0)
{
std::size_t l = strlen(fmt) + 1;
if (l>sizeof(m_str_buf))
{
m_allocated = 2 * l;
m_str = palloc_array<char>(2 * l);
}
memcpy(m_str, fmt, l);
}
pfmt::~pfmt()
{
if (m_allocated > 0)
pfree_array(m_str);
}
void pfmt::format_element(const char *f, const char *l, const char *fmt_spec, ...)
{
va_list ap;
va_start(ap, fmt_spec);
char fmt[30] = "%";
char search[10] = "";
char buf[2048];
m_arg++;
std::size_t sl = static_cast<std::size_t>(sprintf(search, "{%d:", m_arg));
char *p = strstr(m_str, search);
if (p == nullptr)
{
sl = static_cast<std::size_t>(sprintf(search, "{%d}", m_arg));
p = strstr(m_str, search);
if (p == nullptr)
{
sl = 2;
p = strstr(m_str, "{}");
}
if (p==nullptr)
{
sl=1;
p = strstr(m_str, "{");
if (p != nullptr)
{
char *p1 = strstr(p, "}");
if (p1 != nullptr)
{
sl = static_cast<std::size_t>(p1 - p + 1);
strncat(fmt, p+1, static_cast<std::size_t>(p1 - p - 2));
}
else
strcat(fmt, f);
}
else
strcat(fmt, f);
}
}
else
{
char *p1 = strstr(p, "}");
if (p1 != nullptr)
{
sl = static_cast<std::size_t>(p1 - p + 1);
if (m_arg>=10)
strncat(fmt, p+4, static_cast<std::size_t>(p1 - p - 4));
else
strncat(fmt, p+3, static_cast<std::size_t>(p1 - p - 3));
}
else
strcat(fmt, f);
}
strcat(fmt, l);
char *pend = fmt + strlen(fmt) - 1;
if (strchr("fge", *fmt_spec) != nullptr)
{
if (strchr("fge", *pend) == nullptr)
strcat(fmt, fmt_spec);
}
else if (strchr("duxo", *fmt_spec) != nullptr)
{
if (strchr("duxo", *pend) == nullptr)
strcat(fmt, fmt_spec);
}
else
strcat(fmt, fmt_spec);
std::size_t nl = static_cast<std::size_t>(vsprintf(buf, fmt, ap));
if (p != nullptr)
{
// check room
std::size_t new_size = static_cast<std::size_t>(p - m_str) + nl + strlen(p) + 1 - sl;
if (new_size > m_allocated)
{
std::size_t old_alloc = std::max(m_allocated, sizeof(m_str_buf));
if (m_allocated < old_alloc)
m_allocated = old_alloc;
while (new_size > m_allocated)
m_allocated *= 2;
char *np = palloc_array<char>(m_allocated);
memcpy(np, m_str, old_alloc);
p = np + (p - m_str);
if (m_str != m_str_buf)
pfree_array(m_str);
m_str = np;
}
// Make room
memmove(p+nl, p+sl, strlen(p) + 1 - sl);
memcpy(p, buf, nl);
}
va_end(ap);
}
}
|