-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.cpp
187 lines (156 loc) · 4.56 KB
/
formatter.cpp
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
#include "formatter.h"
#include "utils.h"
#include "convert.h"
Formatter::Formatter(unsigned char *data, int offset)
{
int cnt = readInt(data + offset);
if (! cnt) {
commandsCnt = 0;
commands = NULL;
args = NULL;
}
argsCnt = 0;
offset += 4;
commands = new Command[cnt];
commandsCnt = 0;
for (int i = 0; i < cnt; i++) {
int type = data[offset];
offset++;
int size = readInt(data + offset);
offset += 4;
switch (type) {
case 1:
commands[commandsCnt].type = TEXT_COMMAND;
commands[commandsCnt].data.ptr = new std::wstring(
fromUtf8((char*)data + offset, size));
commandsCnt++;
break;
case 2: add_arg<INT_ARG>(data, offset); break;
case 3: add_arg<STRING_ARG>(data, offset); break;
case 4: add_arg<FLOAT_ARG>(data, offset); break;
case 5: add_arg<DOUBLE_ARG>(data, offset); break;
}
offset += size;
}
if (! argsCnt)
args = NULL;
else {
args = new CmdType[argsCnt];
memset(args, 0, sizeof(CmdType) * argsCnt);
for (int i = 0; i < commandsCnt; i++) {
Command &c = commands[i];
if ((c.type == INT_ARG) || (c.type == STRING_ARG) ||
(c.type == FLOAT_ARG) || (c.type == DOUBLE_ARG))
{
int no = *(int*)c.data.raw;
args[no - 1] = c.type;
}
}
}
}
Formatter::~Formatter()
{
for (int i = 0; i < commandsCnt; i++)
if (TEXT_COMMAND == commands[i].type)
delete (std::wstring*)(commands[i].data.ptr);
if (commands)
delete[] commands;
if (args)
delete[] args;
}
std::wstring Formatter::getMessage() const
{
std::wstring s;
for (int i = 0; i < commandsCnt; i++)
if (TEXT_COMMAND == commands[i].type)
s += *(std::wstring*)(commands[i].data.ptr);
return s;
}
class ArgValue
{
public:
virtual ~ArgValue() { };
virtual std::wstring format(Formatter::Command *command) = 0;
};
template <typename T>
class TemplatedArgValue: public ArgValue
{
private:
T value;
public:
TemplatedArgValue(const T &v) { value = v; };
virtual std::wstring format(Formatter::Command *command) {
return toString(value);
};
};
class StrArgValue: public ArgValue
{
private:
std::wstring value;
public:
StrArgValue(const std::wstring &v): value(v) { };
virtual std::wstring format(Formatter::Command *command) {
return value;
};
};
std::wstring Formatter::format(std::vector<ArgValue*> &argValues) const
{
std::wstring s;
int no;
for (int i = 0; i < commandsCnt; i++) {
Command *cmd = &commands[i];
switch (cmd->type) {
case TEXT_COMMAND:
s += *(std::wstring*)(cmd->data.ptr);
break;
case STRING_ARG:
case INT_ARG:
no = *(int*)cmd->data.raw - 1;
if (no < (int)argValues.size())
s += argValues[no]->format(cmd);
break;
default: ;
}
}
return s;
}
std::wstring Formatter::format(va_list ap) const
{
if (! argsCnt)
return getMessage();
std::vector<ArgValue*> argValues;
for (int i = 0; i < argsCnt; i++) {
switch (args[i]) {
case INT_ARG:
argValues.push_back(new TemplatedArgValue<int>
(va_arg(ap, int)));
break;
case STRING_ARG:
argValues.push_back(new StrArgValue(va_arg(ap, wchar_t*)));
break;
case DOUBLE_ARG:
argValues.push_back(new TemplatedArgValue<double>
(va_arg(ap, double)));
break;
case FLOAT_ARG:
argValues.push_back(new TemplatedArgValue<float>
((float)va_arg(ap, double)));
break;
default:
i = argsCnt;
}
}
std::wstring s = format(argValues);
for (auto arg : argValues)
delete arg;
return s;
}
template<Formatter::CmdType T>
void Formatter::add_arg(unsigned char *data, int offset)
{
int argNo = readInt(data + offset);
argsCnt = std::max(argsCnt, argNo);
commands[commandsCnt].type = T;
*(int*)commands[commandsCnt].data.raw = argNo;
commandsCnt++;
}