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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
#include "format.h"
#if 0
#ifdef USE_BOOST_PYTHON
#include "py_eval.h"
#endif
#endif
namespace ledger {
void format_t::parse(const string& fmt)
{
element_t * current = NULL;
char buf[1024];
char * q = buf;
if (elements.size() > 0)
clear_elements();
format_string = fmt;
for (const char * p = fmt.c_str(); *p; p++) {
if (*p != '%' && *p != '\\') {
*q++ = *p;
continue;
}
else if (*p == '\\') {
p++;
switch (*p) {
case 'b': *q++ = '\b'; break;
case 'f': *q++ = '\f'; break;
case 'n': *q++ = '\n'; break;
case 'r': *q++ = '\r'; break;
case 't': *q++ = '\t'; break;
case 'v': *q++ = '\v'; break;
default:
*q++ = *p;
break;
}
continue;
}
else {
assert(*p == '%');
if (*(p + 1) == '%') {
p++; // %% is the same as \%
*q++ = *p;
continue;
}
}
current = new element_t;
elements.push_back(current);
if (q != buf) {
current->kind = element_t::TEXT;
current->chars = new string(buf, q);
q = buf;
current = new element_t;
elements.push_back(current);
}
++p;
if (*p == '-') {
current->align_left = true;
++p;
}
if (*p && std::isdigit(*p)) {
int num = *p++ - '0';
while (*p && std::isdigit(*p)) {
num *= 10;
num += *p++ - '0';
}
current->min_width = num;
}
if (*p == '.') {
++p;
int num = 0;
while (*p && std::isdigit(*p)) {
num *= 10;
num += *p++ - '0';
}
current->max_width = num;
if (current->min_width == -1)
current->min_width = current->max_width;
}
if (current->max_width != -1 && current->min_width != -1 &&
current->max_width < current->min_width)
throw new format_error("Maximum width is less than the minimum width");
switch (*p) {
case '|':
current->kind = element_t::COLUMN;
break;
case '{':
case '(': {
char open = *p;
char close = *p == '{' ? '}' : ')';
++p;
const char * b = p;
int depth = 1;
while (*p) {
if (*p == close && --depth == 0)
break;
else if (*p == open)
++depth;
p++;
}
if (*p != close)
throw new format_error(string("Missing '") + close + "'");
if (open == '{') {
assert(! current->xpath);
current->kind = element_t::XPATH;
current->xpath = new xml::xpath_t(string(b, p));
} else {
assert(! current->format);
current->kind = element_t::GROUP;
current->format = new format_t(string(b, p));
}
break;
}
default:
assert(! current->xpath);
current->kind = element_t::XPATH;
current->xpath = new xml::xpath_t(string(p, p + 1));
break;
}
}
if (q != buf) {
current = new element_t;
elements.push_back(current);
current->kind = element_t::TEXT;
current->chars = new string(buf, q);
}
}
void format_t::compile(xml::node_t * context)
{
for (std::list<element_t *>::iterator i = elements.begin();
i != elements.end();
i++)
switch ((*i)->kind) {
case element_t::XPATH:
assert((*i)->xpath);
(*i)->xpath->compile(context);
break;
case element_t::GROUP:
assert((*i)->format);
(*i)->format->compile(context);
break;
default:
break;
}
}
int format_t::element_formatter_t::operator()
(std::ostream& out_str, element_t * elem, xml::node_t * context,
int column) const
{
if (elem->kind == element_t::COLUMN) {
if (elem->max_width != -1 && elem->max_width < column) {
out_str << '\n';
column = 0;
}
if (elem->min_width != -1 && elem->min_width > column) {
out_str << string(elem->min_width - column, ' ');
column = elem->min_width;
}
return column;
}
std::ostringstream out;
if (elem->align_left)
out << std::left;
else
out << std::right;
if (elem->min_width > 0)
out.width(elem->min_width);
int start_column = column;
if (elem->kind == element_t::XPATH)
elem->xpath->calc(context).strip_annotations()
.write(out, elem->min_width, elem->max_width);
else if (elem->kind == element_t::GROUP)
column = elem->format->format(out, context, column);
else if (elem->kind == element_t::TEXT)
out << *elem->chars;
else
assert(0);
string temp = out.str();
for (string::const_iterator i = temp.begin();
i != temp.end();
i++)
if (*i == '\n' || *i == '\r')
column = 0;
else
column++;
int virtual_width = column - start_column;
if (elem->min_width != -1 && virtual_width < elem->min_width) {
out_str << temp << string(' ', elem->min_width - virtual_width);
}
else if (elem->max_width != -1 && virtual_width > elem->max_width) {
temp.erase(temp.length() - (virtual_width - elem->max_width));
out_str << temp;
}
else {
out_str << temp;
}
return column;
}
int format_t::format(std::ostream& out, xml::node_t * context,
int column, const element_formatter_t& formatter) const
{
for (std::list<element_t *>::const_iterator i = elements.begin();
i != elements.end();
i++)
column = formatter(out, *i, context, column);
return column;
}
} // namespace ledger
#if 0
#ifdef USE_BOOST_PYTHON
using namespace boost::python;
using namespace ledger;
void export_format()
{
class_< format_t > ("Format")
.def(init<string>())
.def("parse", &format_t::parse)
.def("format", &format_t::format)
;
}
#endif // USE_BOOST_PYTHON
#endif
|